import type { MenuContent } from "../menu/types.js"; import type { MenuOwner } from "../menu/inner.js"; import type { ButtonComponent } from "../button/types.js"; /** Visual style, the same set the button offers */ export type SplitButtonVariant = "filled" | "tonal" | "outlined" | "elevated"; /** Size, matching the button and icon button scale */ export type SplitButtonSize = "xs" | "s" | "m" | "l" | "xl"; /** Events the component emits */ export type SplitButtonEventType = "click" | "expand" | "collapse" | "change" | "select"; /** What an event handler receives */ export interface SplitButtonEvent { /** The component that emitted it */ splitButton: SplitButtonComponent; /** Whether the trailing button is showing its menu */ expanded: boolean; /** The DOM event behind it, when there was one */ originalEvent: Event | null; /** The chosen item, on a `select` event */ item?: MenuContent; /** The chosen item's `id`, on a `select` event: the `value` of the `` element's `select` */ value?: string | null; } /** Configuration for the split button */ export interface SplitButtonConfig { /** Label of the leading button */ text?: string; /** `"top"` renders the menu next to the trailing button, in its tree (a shadow root included), and shows it in the browser's top layer. */ layer?: "top"; /** Icon of the leading button. */ icon?: string; /** Visual style, shared by both buttons */ variant?: SplitButtonVariant; /** Size of both buttons */ size?: SplitButtonSize; /** Whether both buttons are disabled */ disabled?: boolean; /** * Accessible name of the trailing button. * @default 'More options' */ trailingLabel?: string; /** Accessible name of the leading button, when its label is not enough */ ariaLabel?: string; /** Accessible name of the whole group */ groupLabel?: string; /** What the leading button does */ onClick?: (event: SplitButtonEvent) => void; /** Menu items for the trailing button to open. */ items?: MenuContent[]; /** Called with the chosen item when the component owns the menu */ onSelect?: (event: SplitButtonEvent) => void; /** Additional CSS classes */ class?: string; /** Component prefix for CSS class names */ prefix?: string; /** Component name for CSS class names */ componentName?: string; /** Event handlers */ on?: { [key in SplitButtonEventType]?: (event: SplitButtonEvent) => void; }; } /** The split button's public API */ export interface SplitButtonComponent { /** The element holding both buttons */ element: HTMLElement; /** The leading button's element */ leadingElement: HTMLButtonElement; /** The trailing button's element */ trailingElement: HTMLButtonElement; /** Sets the leading button's label */ setText: (text: string) => SplitButtonComponent; /** The leading button's label */ getText: () => string; /** Sets the leading button's icon */ setIcon: (icon: string) => SplitButtonComponent; /** Replaces the menu's items. */ setItems: (items: MenuContent[]) => SplitButtonComponent; /** The menu's items; empty while the split button has no menu */ getItems: () => MenuContent[]; /** Opens whatever the trailing button opens. */ expand: () => SplitButtonComponent; /** Closes it. */ collapse: () => SplitButtonComponent; /** Whether it is open */ isExpanded: () => boolean; /** Disables both buttons */ disable: () => SplitButtonComponent; /** Enables both buttons */ enable: () => SplitButtonComponent; /** Whether both buttons are disabled */ isDisabled: () => boolean; /** Adds an event listener */ on: (event: SplitButtonEventType, handler: (event: SplitButtonEvent) => void) => SplitButtonComponent; /** Removes an event listener */ off: (event: SplitButtonEventType, handler: (event: SplitButtonEvent) => void) => SplitButtonComponent; /** Takes the component off the page and releases what it holds */ destroy: () => void; } export interface ButtonsFeature { leading: ButtonComponent; trailing: ButtonComponent; leadingElement: HTMLElement; trailingElement: HTMLElement; } export type MenuFeature = MenuOwner; export interface BaseComponent extends ButtonsFeature, MenuFeature { element: HTMLElement; emit?: (event: string, data?: unknown) => void; on?: (event: string, handler: (...args: unknown[]) => void) => unknown; off?: (event: string, handler: (...args: unknown[]) => void) => unknown; getClass?: (name: string) => string; lifecycle?: { destroy?: () => void; }; }