Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | 73x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 330x 73x 330x 330x 256x 188x 188x 288x 288x 246x 30x 246x 288x 576x 246x 890x 890x 246x 37x 251x 251x 246x 41x 36x 338x 417x 417x 39x 417x 2x | import { LitElement, html, unsafeCSS } from "lit" import { customElement, property, query, state } from "lit/decorators.js" import { repeat } from "lit/directives/repeat.js" import { classMap } from "lit/directives/class-map.js" import style from "./style.scss?inline" /** * Option definition consumed by {@link VgDropdown}. */ export interface DropdownOption { /** * Display label shown to the user. */ label: string /** * Value returned when the option is selected. */ value: string /** * Optional helper text displayed beneath the option label. */ description?: string /** * Marks the option as disabled within the dropdown list. */ disabled?: boolean } /** * Detail payload for the {@link VgDropdown} change event. */ export interface DropdownChangeDetail { /** * Selected option value. */ readonly value: string /** * Full option object corresponding to the selected value, when available. */ readonly option?: DropdownOption /** * Original change event from the native `<select>` element. */ readonly originalEvent: Event } let dropdownId = 0 const nextDropdownId = () => `vg-dropdown-${++dropdownId}` /** * Theme-aware dropdown select element supporting helper text, errors, and descriptions per option. * * @tag vg-dropdown * @tagname vg-dropdown * @summary The Dropdown component, used for selecting options from a list. * * @slot prefix - Optional slot rendered before the native select * @slot suffix - Optional slot rendered after the native select (for badges, icons, etc.) * * @csspart select - Allows you to style the select element * @csspart label - Allows you to style the label element * * @fires {DropdownChangeDetail} vg-change - Emitted when the selected option changes * */ @customElement("vg-dropdown") export class VgDropdown extends LitElement { static styles = unsafeCSS(style) /** * Label displayed above the dropdown control. */ @property({ type: String }) public label: string | null = null /** * Optional text rendered below the control for guidance. */ @property({ type: String, attribute: "helper-text" }) public helperText: string | null = null /** * Error message displayed below the control; marks the dropdown as invalid. */ @property({ type: String }) public error: string | null = null /** * Placeholder rendered as the first option when no value is selected. */ @property({ type: String }) public placeholder: string | null = null /** * Currently selected value. */ @property({ type: String }) public value: string | null = null /** * Name attribute forwarded to the native select element. */ @property({ type: String }) public name: string | null = null /** * Disables the dropdown and prevents user interaction. */ @property({ type: Boolean, reflect: true }) public disabled = false /** * Marks the dropdown as required when used in forms. */ @property({ type: Boolean, reflect: true }) public required = false /** * Collection of options rendered by the dropdown. */ @property({ attribute: false }) public options: DropdownOption[] = [] @query("select") private selectElement!: HTMLSelectElement @state() private hasPrefixContent = false @state() private hasSuffixContent = false private readonly dropdownId = nextDropdownId() protected firstUpdated() { this.updateSlotState("prefix") this.updateSlotState("suffix") } protected updated() { if (this.selectElement) { const desired = this.value ?? "" Iif (this.selectElement.value !== desired) { this.selectElement.value = desired } } } /** * @inheritdoc */ protected render() { const classes = { control: true, "has-prefix": this.hasPrefixContent, "has-suffix": this.hasSuffixContent, "has-error": !!this.error, } const describedBy = [ this.helperText ? `${this.dropdownId}-helper` : null, this.error ? `${this.dropdownId}-error` : null, ].filter((value): value is string => value !== null) return html` <label class="field" for=${this.dropdownId}> ${this.label ? html`<span class="field__label">${this.label}</span>` : null} <div class=${classMap(classes)}> <slot name="prefix" @slotchange=${this.onSlotChange}></slot> <select id=${this.dropdownId} name=${this.name ?? ""} ?disabled=${this.disabled} ?required=${this.required} aria-invalid=${this.error ? "true" : "false"} aria-describedby=${describedBy.join(" ")} @change=${this.onChange} > ${this.renderPlaceholder()} ${repeat( this.options, (option) => option.value, (option) => html` <option value=${option.value} ?disabled=${option.disabled} ?selected=${option.value === this.value} title=${option.description ?? ""} > ${option.label} </option> `, )} </select> <slot name="suffix" @slotchange=${this.onSlotChange}></slot> </div> ${this.renderDescriptions()} </label> ` } private renderPlaceholder() { Iif (!this.placeholder) { return null } const isSelected = !this.value return html`<option value="" ?selected=${isSelected} disabled hidden>${this.placeholder}</option>` } private renderDescriptions() { return html` ${this.helperText ? html`<p id=${`${this.dropdownId}-helper`} class="field__helper">${this.helperText}</p>` : null} ${this.error ? html`<p id=${`${this.dropdownId}-error`} class="field__error">${this.error}</p>` : null} ` } private onChange(event: Event) { const target = event.target as HTMLSelectElement this.value = target.value || null const selected = this.options.find((option) => option.value === this.value) this.dispatchEvent(new CustomEvent<DropdownChangeDetail>("vg-change", { detail: { value: this.value ?? "", option: selected, originalEvent: event, }, // bubbles: true, composed: true, })) } private onSlotChange(event: Event) { const slot = event.target as HTMLSlotElement this.updateSlotState(slot.name as "prefix" | "suffix") } private updateSlotState(name: "prefix" | "suffix") { const slot = this.shadowRoot?.querySelector(`slot[name="${name}"]`) as HTMLSlotElement | null const hasContent = (slot?.assignedNodes({ flatten: true }).length ?? 0) > 0 if (name === "prefix" && this.hasPrefixContent !== hasContent) { this.hasPrefixContent = hasContent } if (name === "suffix" && this.hasSuffixContent !== hasContent) { this.hasSuffixContent = hasContent } } } declare global { interface HTMLElementTagNameMap { "vg-dropdown": VgDropdown } } |