|
| 1 | +import { css, html, LitElement } from 'lit'; |
| 2 | +import { customElement, eventOptions, property } from 'lit/decorators.js'; |
| 3 | +import { classMap } from 'lit/directives/class-map.js'; |
| 4 | +import ExpandMore from '../svgs/ExpandMore.js'; |
| 5 | + |
| 6 | +@customElement('discord-string-select-menu') |
| 7 | +export class DiscordStringSelectMenu extends LitElement { |
| 8 | + public static override readonly styles = css` |
| 9 | + .discord-string-select-menu { |
| 10 | + height: 36px; |
| 11 | + min-height: 36px; |
| 12 | + display: flex; |
| 13 | + align-items: center; |
| 14 | + justify-content: space-between; |
| 15 | + box-sizing: border-box; |
| 16 | + cursor: pointer; |
| 17 | + color: color-mix(in oklab, hsl(210 calc(1 * 9.1%) 87.1% / 1) 100%, black 0%); |
| 18 | + border: 1px solid; |
| 19 | + border-radius: 4px; |
| 20 | + background-color: color-mix(in oklab, hsl(225 calc(1 * 6.3%) 12.5% / 1) 100%, black 0%); |
| 21 | + border-color: color-mix(in oklab, hsl(225 calc(1 * 6.3%) 12.5% / 1) 100%, black 0%); |
| 22 | + padding: 8px !important; |
| 23 | + width: 90%; |
| 24 | + max-width: 400px; |
| 25 | + margin-right: 16px; |
| 26 | + transition: border 0.2s ease; |
| 27 | + font-weight: 500; |
| 28 | + } |
| 29 | +
|
| 30 | + .discord-string-select-menu-option-slot { |
| 31 | + overflow-y: scroll; |
| 32 | + overflow-x: hidden; |
| 33 | + color: currentColor; |
| 34 | + border: none; |
| 35 | + border-top-left-radius: 0px; |
| 36 | + border-top-right-radius: 0px; |
| 37 | + border-color: color-mix(in oklab, hsl(225 calc(1 * 6.3%) 12.5% / 1) 100%, black 0%); |
| 38 | + background-color: color-mix(in oklab, hsl(220 calc(1 * 6.5%) 18% / 1) 100%, black 0%); |
| 39 | +
|
| 40 | + cursor: pointer; |
| 41 | + box-sizing: border-box; |
| 42 | + border-radius: 4px; |
| 43 | + gap: 10px; |
| 44 | + display: block; |
| 45 | + max-height: 282px; |
| 46 | + position: absolute; |
| 47 | +
|
| 48 | + width: 90%; |
| 49 | + max-width: 400px; |
| 50 | + z-index: 1002; |
| 51 | + } |
| 52 | +
|
| 53 | + .discord-string-select-menu-option-slot::-webkit-scrollbar { |
| 54 | + width: 5px; |
| 55 | + background-color: transparent; |
| 56 | + } |
| 57 | +
|
| 58 | + .discord-string-select-menu-option-slot::-webkit-scrollbar-track { |
| 59 | + background-color: none; |
| 60 | + } |
| 61 | +
|
| 62 | + .discord-string-select-menu-option-slot::-webkit-scrollbar-thumb { |
| 63 | + border-radius: 10px; |
| 64 | + background-color: rgba(0, 0, 0, 0.3); |
| 65 | + } |
| 66 | +
|
| 67 | + .discord-string-select-menu-label { |
| 68 | + width: 100%; |
| 69 | + } |
| 70 | +
|
| 71 | + .discord-string-select-inside-menu { |
| 72 | + padding: 8px 8px 8px 12px; |
| 73 | + display: flex; |
| 74 | + align-items: center; |
| 75 | + justify-content: space-between; |
| 76 | + } |
| 77 | +
|
| 78 | + .discord-string-select-menu-hidden { |
| 79 | + display: none; |
| 80 | + } |
| 81 | +
|
| 82 | + .discord-string-select-menu-disabled { |
| 83 | + cursor: not-allowed !important; |
| 84 | + opacity: 0.5; |
| 85 | + } |
| 86 | +
|
| 87 | + .discord-string-select-inside-menu:hover { |
| 88 | + border-color: black; |
| 89 | + cursor: pointer; |
| 90 | + } |
| 91 | +
|
| 92 | + .discord-string-select-menu-rotated { |
| 93 | + transform: rotate(-180deg); |
| 94 | + } |
| 95 | +
|
| 96 | + .discord-expand-more-icon { |
| 97 | + margin-left: auto; |
| 98 | + } |
| 99 | + `; |
| 100 | + |
| 101 | + /** |
| 102 | + * Whether to show the `discord-string-select-menu` as disabled. |
| 103 | + */ |
| 104 | + @property({ type: Boolean, attribute: 'disabled' }) |
| 105 | + public accessor disabled: boolean; |
| 106 | + |
| 107 | + /** |
| 108 | + * The placeholder of the select-menu |
| 109 | + */ |
| 110 | + @property({ attribute: 'placeholder' }) |
| 111 | + public accessor placeholder: string = 'Make a selection'; |
| 112 | + |
| 113 | + protected override render() { |
| 114 | + return html` |
| 115 | + <div class="${classMap({ 'discord-string-select-menu': true, 'discord-string-select-menu-disabled': this.disabled })}"> |
| 116 | + <label class="discord-string-select-menu-label"> |
| 117 | + <div class="${classMap({ 'discord-string-select-inside-menu': true, 'discord-string-select-menu-disabled': this.disabled })}"> |
| 118 | + <span>${this.placeholder}</span> ${ExpandMore({ class: 'discord-expand-more-icon' })} |
| 119 | + <span class="discord-string-select-menu-hidden"><input type="checkbox" @click=${this._onClick} /></span> |
| 120 | + </div> |
| 121 | + </label> |
| 122 | + </div> |
| 123 | + <div class="discord-string-select-menu-option-slot discord-string-select-menu-hidden"><slot></slot></div> |
| 124 | + `; |
| 125 | + } |
| 126 | + |
| 127 | + @eventOptions({ once: false, capture: true, passive: true }) |
| 128 | + private _onClick() { |
| 129 | + const expandMoreIcon = this.shadowRoot?.querySelectorAll('svg.discord-expand-more-icon').item(0); |
| 130 | + const optionsMenu = this.shadowRoot?.querySelectorAll('div.discord-string-select-menu-option-slot').item(0); |
| 131 | + const stringSelectMenu = this.shadowRoot?.querySelectorAll('div.discord-string-select-menu').item(0); |
| 132 | + |
| 133 | + if (stringSelectMenu?.className.includes('discord-string-select-menu-disabled')) return; |
| 134 | + |
| 135 | + if (optionsMenu?.className.includes('discord-string-select-menu-hidden')) { |
| 136 | + optionsMenu?.setAttribute('class', 'discord-string-select-menu-option-slot'); |
| 137 | + expandMoreIcon?.setAttribute('class', 'discord-expand-more-icon discord-string-select-menu-rotated'); |
| 138 | + } else { |
| 139 | + optionsMenu?.setAttribute('class', 'discord-string-select-menu-option-slot discord-string-select-menu-hidden'); |
| 140 | + expandMoreIcon?.setAttribute('class', 'discord-expand-more-icon'); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +declare global { |
| 146 | + interface HTMLElementTagNameMap { |
| 147 | + 'discord-string-select-menu': DiscordStringSelectMenu; |
| 148 | + } |
| 149 | +} |
0 commit comments