Select
Presents a selection of choices to the user, activated by a button.
Props
Features
- 🎹 Keyboard navigation
- 🍃 Multi-selection mode
- 🧠 Smart focus management
- 💬 Highlight items by typing them out (typeahead)
Usage
<script lang="ts"> import { Select } from "melt/builders";
const select = new Select();</script>
<div {...select.root}> Select content</div>
<script lang="ts"> import { Select } from "melt/components";</script>
<Select> {#snippet children(select)} <div {...select.root}> Select content </div> {/snippet}</Select>
API Reference
Constructor Props
The props that are passed when calling
new Select()
new Select()
export type SelectProps<T extends string, Multiple extends boolean = false> = PopoverProps & { /** * If `true`, multiple options can be selected at the same time. * * @default false */ multiple?: MaybeGetter<Multiple | undefined>;
/** * The value for the Select. * * When passing a getter, it will be used as source of truth, * meaning that the value only changes when the getter returns a new value. * * Otherwise, if passing a static value, it'll serve as the default value. * * * @default false */ value?: MaybeMultiple<T, Multiple>; /** * Called when the value is supposed to change. */ onValueChange?: OnMultipleChange<T, Multiple>;
/** * How many time (in ms) the typeahead string is held before it is cleared * @default 500 */ typeaheadTimeout?: MaybeGetter<number | undefined>;};
export type SelectProps<T extends string, Multiple extends boolean = false> = PopoverProps & { /** * If `true`, multiple options can be selected at the same time. * * @default false */ multiple?: MaybeGetter<Multiple | undefined>;
/** * The value for the Select. * * When passing a getter, it will be used as source of truth, * meaning that the value only changes when the getter returns a new value. * * Otherwise, if passing a static value, it'll serve as the default value. * * * @default false */ value?: MaybeMultiple<T, Multiple>; /** * Called when the value is supposed to change. */ onValueChange?: OnMultipleChange<T, Multiple>;
/** * How many time (in ms) the typeahead string is held before it is cleared * @default 500 */ typeaheadTimeout?: MaybeGetter<number | undefined>;};
Methods
The methods returned from
new Select()
new Select()
-
getOptionId
(value: T) => string(value: T) => string -
getOption
(value: T) => {readonly "data-melt-select-option": ""readonly "data-value": DataReturn<T>readonly "aria-hidden": true | undefinedreadonly "aria-selected": booleanreadonly "data-highlighted": booleanreadonly role: "option"readonly onmouseover: () => voidreadonly onclick: () => void}(value: T) => {readonly "data-melt-select-option": ""readonly "data-value": DataReturn<T>readonly "aria-hidden": true | undefinedreadonly "aria-selected": booleanreadonly "data-highlighted": booleanreadonly role: "option"readonly onmouseover: () => voidreadonly onclick: () => void}
Properties
The properties returned from
new Select()
new Select()
-
multiple
Multiple extends null | undefined? Multiple | undefined: Multiple | Exclude<Multiple, null | undefined>Multiple extends null | undefined? Multiple | undefined: Multiple | Exclude<Multiple, null | undefined> -
highlighted
T | nullT | null -
typeaheadTimeout
numbernumber -
typeahead
(letter: string) => { value: T; current: boolean } | undefined(letter: string) => { value: T; current: boolean } | undefined -
value
SelectionStateValue<T, Multiple>SelectionStateValue<T, Multiple> -
trigger
{readonly onfocusout: () => Promise<void>readonly "data-melt-popover-trigger": ""readonly id: stringreadonly popovertarget: stringreadonly onclick: (e: Event) => void} & {"data-melt-select-trigger": stringrole: string"aria-expanded": boolean"aria-controls": string"aria-owns": stringonkeydown: (e: KeyboardEvent) => void}{readonly onfocusout: () => Promise<void>readonly "data-melt-popover-trigger": ""readonly id: stringreadonly popovertarget: stringreadonly onclick: (e: Event) => void} & {"data-melt-select-trigger": stringrole: string"aria-expanded": boolean"aria-controls": string"aria-owns": stringonkeydown: (e: KeyboardEvent) => void} -
content
{readonly onfocusout: () => Promise<void>readonly "data-melt-popover-content": ""readonly id: stringreadonly popover: "manual"readonly ontoggle: (e: ToggleEvent & { currentTarget: EventTarget & HTMLElement },) => voidreadonly tabindex: -1readonly inert: booleanreadonly "data-open": "" | undefined} & {readonly "data-melt-select-content": ""readonly role: "listbox"readonly "aria-expanded": booleanreadonly "aria-activedescendant": string | undefinedreadonly onkeydown: (e: KeyboardEvent) => void}{readonly onfocusout: () => Promise<void>readonly "data-melt-popover-content": ""readonly id: stringreadonly popover: "manual"readonly ontoggle: (e: ToggleEvent & { currentTarget: EventTarget & HTMLElement },) => voidreadonly tabindex: -1readonly inert: booleanreadonly "data-open": "" | undefined} & {readonly "data-melt-select-content": ""readonly role: "listbox"readonly "aria-expanded": booleanreadonly "aria-activedescendant": string | undefinedreadonly onkeydown: (e: KeyboardEvent) => void}