import { SvelteComponentTyped } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; export type DropdownItem = { id: Id; text: string; /** Whether the item is disabled */ disabled?: boolean; /** Icon component shown left of the item text */ icon?: any; /** Group label. Items sharing a label render together under a non-selectable header, ungrouped items first and groups in order of first appearance. */ group?: string; }; type $RestProps = SvelteHTMLElements["div"]; type $Props = DropdownItem> = { /** * Set the dropdown items. * @default [] */ items?: ReadonlyArray; /** * Override the display of a dropdown item. * @default (item) => item.text ?? item.id */ itemToString?: (item: Item) => string; /** * Specify the selected item id. */ selectedId?: Item["id"] | undefined; /** * Specify the type of dropdown. * @default "default" */ type?: "default" | "inline"; /** * Specify the direction of the dropdown menu. * @default "bottom" */ direction?: "bottom" | "top"; /** * Specify which edge of the field the menu lines up with. * Only visible when the menu is wider than the field, as with `type="inline"`. * Set to `"end"` to open the menu toward the start side, e.g. for an inline * dropdown near the right edge of its container. * @default "start" */ align?: "start" | "end"; /** * Specify the size of the dropdown field. * Inherits the parent `Form` size when unset. * Also accepts Carbon v11's `"md"` (the 40px default, which overrides a * `Form` size). `"lg"` and `"xl"` are both 48px. */ size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined; /** * Set to `true` to open the dropdown. * @default false */ open?: boolean; /** * Set to `true` to disable the dropdown * @default false */ disabled?: boolean; /** * Specify the label text * @default "" */ labelText?: string; /** * Set to `true` to indicate an invalid state * @default false */ invalid?: boolean; /** * Specify the invalid state text * @default "" */ invalidText?: string; /** * Set to `true` to indicate a warning state * @default false */ warn?: boolean; /** * Specify the warning state text * @default "" */ warnText?: string; /** * Specify the helper text * @default "" */ helperText?: string; /** * Set to `true` to use the read-only variant * @default false */ readonly?: boolean; /** * Specify the assistive text announced to screen readers when read-only. * Exposed because VoiceOver does not announce `aria-readonly`. * @default "Read-only" */ readonlyText?: string; /** * Set to `true` to show a clear button when an item is selected. * Clears `selectedId` and dispatches a `clear` event. * Also enables clearing the selection with Delete/Backspace. * @default false */ clearable?: boolean; /** * Specify the assistive text advertising the keyboard shortcut that clears * the selection. Appended to the field's description whenever there is a * selection to clear. Only used when `clearable` is `true`. * @default "To clear the selection, press Delete or Backspace" */ clearSelectionText?: string; /** * Specify the assistive text announced through the status live region when * the selection is cleared via the keyboard or the clear button. Only used * when `clearable` is `true`. * @default "Selection cleared" */ selectionClearedText?: string; /** * Set to `true` to reopen the dropdown menu after clearing the selection. * This allows users to immediately see all available items after clearing. * Only used when `clearable` is `true`. * @default false */ openOnClear?: boolean; /** * Set to `true` to use the fluid variant. * Inherited from the parent `FluidForm` context, * so it does not need to be set when used inside `FluidForm`. * Cannot be combined with the inline variant. * @default false */ fluid?: boolean; /** * Set to `true` to render condensed menu items in the fluid variant. * Menu items use the default height instead of the taller fluid height. * Only applies when the fluid variant is active. * @default false */ condensed?: boolean; /** * Specify the list box label. */ label?: string | undefined; /** * Set to `true` to visually hide the label text * @default false */ hideLabel?: boolean; /** * Override the chevron icon label based on the open state. * Defaults to "Open menu" when closed and "Close menu" when open. */ translateWithId?: ((id: import("../ListBox/ListBoxMenuIcon.svelte").ListBoxMenuIconTranslationId) => string) | undefined; /** * Override the label of the clear button when a selection is present. * Defaults to "Clear selected item" since a dropdown can only have one selection. */ translateWithIdSelection?: ((id: "clearSelection") => string) | undefined; /** * Enable virtualization for large lists. Virtualization renders only the items currently visible in the viewport, improving performance for large lists. * * By default, virtualization is automatically enabled for lists with more than 100 items. * * Set `virtualize={false}` to explicitly disable virtualization, even for large lists. * * Set `virtualize={true}` to explicitly enable virtualization with default settings. * * Provide an object to customize virtualization behavior: * - `itemHeight` (default: size-based, or 64px for fluid unless `condensed`): Height of each item in pixels. Override when custom slots change row height. Under `wrapOptions`, heights are measured from the rendered options and this serves as the starting estimate for ones not yet measured. * - `containerHeight` (default: 300): The maximum height in pixels of the dropdown container. * - `overscan` (default: 3): The number of extra items to render above and below the viewport for smoother scrolling. Higher values may cause more flickering during very fast scrolling. * - `threshold` (default: 100): The minimum number of items required before virtualization activates. Lists with fewer items will render all items normally without virtualization. * - `maxItems` (default: undefined): The maximum number of items to render. When undefined, all visible items are rendered. * - `optimizeFastScroll` (default: false): Keep the rendered options in view during very fast scrolling, such as a scrollbar drag, instead of showing blank space until new options render. The options then follow the scroll a frame behind, and pause while the main thread is busy. */ virtualize?: undefined | boolean | { itemHeight?: number, containerHeight?: number, overscan?: number, threshold?: number, maxItems?: number, optimizeFastScroll?: boolean }; /** * Specify the message shown in the open menu when `items` is empty, such as * before async options arrive. Screen readers hear it through the status * live region, also when the `empty` slot replaces the visible text. * Set to `""` to show no message. * @default "No options" */ emptyText?: string; /** * Set to `true` to show a loading row after the options while the menu is * open, such as while fetching more items on `scrollend`. Marks the menu * `aria-busy` and hides the empty state; the options stay interactive. * @default false */ loading?: boolean; /** * Specify the text of the loading row * @default "Loading..." */ loadingText?: string; /** * Wait this many milliseconds after `loading` turns `true` before showing * the loading row. Once shown, it stays for at least 500 ms, so fast work * shows nothing and slow work never flashes. The menu sets `aria-busy` and * holds back the empty state as soon as `loading` is `true`. * Set to `0` to show it at once. * @default 0 */ loadingDelay?: number; /** * Set to `true` to let an option's label wrap onto as many lines as it needs * instead of being truncated with an ellipsis. * @default false */ wrapOptions?: boolean; /** * Set to `true` to always render the menu in a portal, or `false` to * keep it in place. Left unset, the menu moves to a portal inside a * Modal, or when a box around it (`overflow` other than `visible`) * would cut it off. */ portalMenu?: boolean | undefined; /** * Set an id for the list box component * @default uniqueId() */ id?: string; /** * Specify a name attribute for native form submission. * Mounts a hidden input whose value mirrors `selectedId` * (empty string when nothing is selected, or when the * selected item is disabled). Omitted from submission * when the dropdown is disabled. */ name?: string | undefined; /** * Specify the id of a form element outside the component to associate the * hidden `name` input with. */ form?: string | undefined; /** * Set to `true` to require a selection. A wrapping `
` blocks * submission while nothing is selected or the selected item is disabled, * and the field shows its invalid state with `requiredInvalidText` * instead of the browser's error bubble. * @default false */ required?: boolean; /** * Specify the invalid state text shown when a required dropdown blocks * submission. Cleared once an item is selected. `invalid` and * `invalidText` take precedence. * @default "Select an item" */ requiredInvalidText?: string; /** * Obtain a reference to the button HTML element. * @default null */ ref?: null | HTMLButtonElement; /** * Obtain a reference to the list HTML element. * @default null */ listRef?: null | HTMLDivElement; decorator?: (this: void) => void; empty?: (this: void) => void; group?: (this: void, ...args: [{ group: string; items: ReadonlyArray; }]) => void; icon?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; /** Content at the end of each item. Replaces the checkmark on the selected item. */ iconEnd?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; /** * @deprecated use the `iconEnd` slot */ iconRight?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; labelChildren?: (this: void) => void; children?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; [key: `data-${string}`]: unknown; }; export type DropdownProps = DropdownItem> = Omit<$RestProps, keyof $Props> & $Props; export default class Dropdown = DropdownItem> extends SvelteComponentTyped< DropdownProps, { clear: CustomEvent; close: CustomEvent<{ trigger: "escape-key" | "outside-click" | "select"; }>; open: CustomEvent<{ trigger: "click" | "keydown" | "programmatic"; }>; scroll: WindowEventMap["scroll"]; /** * Dispatched when the menu is scrolled near the bottom (load-more signal). * Not the browser's native `scrollend` (scroll stopped). */ scrollend: CustomEvent<{ scrollTop: number; scrollHeight: number; clientHeight: number }>; select: CustomEvent<{ selectedId: Item["id"]; selectedItem: Item; }>; }, { default: { item: Item; index: number; selected: boolean; highlighted: boolean; }; decorator: Record; empty: Record; group: { group: string; items: ReadonlyArray; }; icon: { item: Item; index: number; selected: boolean; highlighted: boolean; }; /** Content at the end of each item. Replaces the checkmark on the selected item. */ iconEnd: { item: Item; index: number; selected: boolean; highlighted: boolean; }; /** * @deprecated use the `iconEnd` slot */ iconRight: { item: Item; index: number; selected: boolean; highlighted: boolean; }; labelChildren: Record; } > { /** * Clear the dropdown selection programmatically. * By default, focuses the dropdown after clearing. Set `options.focus` to `false` to prevent focusing. * Set `options.open` to `true` to open the dropdown menu after clearing. * @example * ```svelte * * * ``` */ clear: (options?: { focus?: boolean; open?: boolean; }) => Promise; }