|
| 1 | +// Import Third-party Dependencies |
| 2 | +import { LitElement, html, css, nothing } from "lit"; |
| 3 | + |
| 4 | +// Import Internal Dependencies |
| 5 | +import { EVENTS } from "../../core/events.js"; |
| 6 | + |
| 7 | +export class RootSelector extends LitElement { |
| 8 | + static properties = { |
| 9 | + secureDataSet: { attribute: false }, |
| 10 | + _open: { state: true } |
| 11 | + }; |
| 12 | + |
| 13 | + static styles = css` |
| 14 | + :host { |
| 15 | + position: relative; |
| 16 | + display: inline-block; |
| 17 | + color: #1a1a2e; |
| 18 | + } |
| 19 | +
|
| 20 | + :host-context(body.dark) { |
| 21 | + color: rgb(255 255 255 / 87%); |
| 22 | + } |
| 23 | +
|
| 24 | + .selector-btn { |
| 25 | + display: flex; |
| 26 | + align-items: center; |
| 27 | + gap: 4px; |
| 28 | + background: white; |
| 29 | + border: 1.5px solid rgb(55 34 175 / 20%); |
| 30 | + border-radius: 8px; |
| 31 | + padding: 5px 10px; |
| 32 | + cursor: pointer; |
| 33 | + font-family: mononoki, monospace; |
| 34 | + font-size: 13px; |
| 35 | + color: #1a1a2e; |
| 36 | + transition: background 0.12s, border-color 0.12s; |
| 37 | + white-space: nowrap; |
| 38 | + } |
| 39 | +
|
| 40 | + .selector-btn:hover { |
| 41 | + background: #f0eeff; |
| 42 | + border-color: rgb(55 34 175 / 40%); |
| 43 | + } |
| 44 | +
|
| 45 | + :host-context(body.dark) .selector-btn { |
| 46 | + background: #1e1c2e; |
| 47 | + border-color: rgb(163 148 255 / 25%); |
| 48 | + color: rgb(255 255 255 / 87%); |
| 49 | + } |
| 50 | +
|
| 51 | + :host-context(body.dark) .selector-btn:hover { |
| 52 | + background: #2a2640; |
| 53 | + border-color: rgb(163 148 255 / 45%); |
| 54 | + } |
| 55 | +
|
| 56 | + .pkg-version { |
| 57 | + color: #7c6fff; |
| 58 | + } |
| 59 | +
|
| 60 | + :host-context(body.dark) .pkg-version { |
| 61 | + color: #a394ff; |
| 62 | + } |
| 63 | +
|
| 64 | + .chevron { |
| 65 | + font-size: 9px; |
| 66 | + opacity: 0.5; |
| 67 | + margin-left: 2px; |
| 68 | + } |
| 69 | +
|
| 70 | + .dropdown { |
| 71 | + position: absolute; |
| 72 | + top: calc(100% + 6px); |
| 73 | + left: 0; |
| 74 | + min-width: 100%; |
| 75 | + background: white; |
| 76 | + border: 1px solid rgb(0 0 0 / 10%); |
| 77 | + border-radius: 8px; |
| 78 | + box-shadow: 0 8px 24px rgb(0 0 0 / 10%); |
| 79 | + z-index: 100; |
| 80 | + overflow: hidden; |
| 81 | + padding: 4px 0; |
| 82 | + } |
| 83 | +
|
| 84 | + :host-context(body.dark) .dropdown { |
| 85 | + background: #1e1c2e; |
| 86 | + border-color: rgb(255 255 255 / 10%); |
| 87 | + box-shadow: 0 8px 24px rgb(0 0 0 / 40%); |
| 88 | + } |
| 89 | +
|
| 90 | + .pkg-item { |
| 91 | + display: flex; |
| 92 | + align-items: center; |
| 93 | + gap: 2px; |
| 94 | + padding: 7px 14px; |
| 95 | + font-family: mononoki, monospace; |
| 96 | + font-size: 12px; |
| 97 | + color: #1a1a2e; |
| 98 | + cursor: pointer; |
| 99 | + white-space: nowrap; |
| 100 | + transition: background 0.1s; |
| 101 | + } |
| 102 | +
|
| 103 | + :host-context(body.dark) .pkg-item { |
| 104 | + color: rgb(255 255 255 / 87%); |
| 105 | + } |
| 106 | +
|
| 107 | + .pkg-item:hover { |
| 108 | + background: rgb(124 111 255 / 8%); |
| 109 | + } |
| 110 | +
|
| 111 | + :host-context(body.dark) .pkg-item:hover { |
| 112 | + background: rgb(163 148 255 / 10%); |
| 113 | + } |
| 114 | +
|
| 115 | + .item-version { |
| 116 | + color: #7c6fff; |
| 117 | + } |
| 118 | +
|
| 119 | + :host-context(body.dark) .item-version { |
| 120 | + color: #a394ff; |
| 121 | + } |
| 122 | + `; |
| 123 | + |
| 124 | + connectedCallback() { |
| 125 | + super.connectedCallback(); |
| 126 | + this._handleOutsideClick = (event) => { |
| 127 | + if (!event.composedPath().includes(this)) { |
| 128 | + this._open = false; |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + document.addEventListener("click", this._handleOutsideClick); |
| 133 | + } |
| 134 | + |
| 135 | + disconnectedCallback() { |
| 136 | + super.disconnectedCallback(); |
| 137 | + document.removeEventListener("click", this._handleOutsideClick); |
| 138 | + } |
| 139 | + |
| 140 | + #switchTo(spec) { |
| 141 | + window.dispatchEvent(new CustomEvent(EVENTS.ROOT_SWITCH, { |
| 142 | + detail: { spec } |
| 143 | + })); |
| 144 | + this._open = false; |
| 145 | + } |
| 146 | + |
| 147 | + render() { |
| 148 | + if (!this.secureDataSet) { |
| 149 | + return nothing; |
| 150 | + } |
| 151 | + |
| 152 | + const rootEntry = this.secureDataSet.linker.get(0); |
| 153 | + if (!rootEntry) { |
| 154 | + return nothing; |
| 155 | + } |
| 156 | + |
| 157 | + const others = (window.cachedSpecs ?? []).filter( |
| 158 | + (pkg) => pkg.spec !== window.activePackage |
| 159 | + ); |
| 160 | + |
| 161 | + return html` |
| 162 | + <button |
| 163 | + class="selector-btn" |
| 164 | + @click=${(event) => { |
| 165 | + event.stopPropagation(); |
| 166 | + if (others.length > 0) { |
| 167 | + this._open = !this._open; |
| 168 | + } |
| 169 | + }} |
| 170 | + > |
| 171 | + <span class="pkg-name">${rootEntry.name}</span> |
| 172 | + <span class="pkg-version">@${rootEntry.version}</span> |
| 173 | + ${others.length > 0 |
| 174 | + ? html`<span class="chevron">${this._open ? "▴" : "▾"}</span>` |
| 175 | + : nothing |
| 176 | + } |
| 177 | + </button> |
| 178 | + ${this._open && others.length > 0 ? html` |
| 179 | + <div class="dropdown"> |
| 180 | + ${others.map((pkg) => { |
| 181 | + const atIndex = pkg.spec.lastIndexOf("@"); |
| 182 | + const pkgName = pkg.spec.slice(0, atIndex); |
| 183 | + const pkgVersion = pkg.spec.slice(atIndex); |
| 184 | +
|
| 185 | + return html` |
| 186 | + <div class="pkg-item" @click=${() => this.#switchTo(pkg.spec)}> |
| 187 | + <span class="item-name">${pkgName}</span> |
| 188 | + <span class="item-version">${pkgVersion}</span> |
| 189 | + </div> |
| 190 | + `; |
| 191 | + })} |
| 192 | + </div> |
| 193 | + ` : nothing} |
| 194 | + `; |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +customElements.define("root-selector", RootSelector); |
0 commit comments