Dropdown
The <smart-dropdown> component provides a flexible dropdown menu wrapper with an InjectionToken-based extension mechanism. It renders a default DropdownStandardComponent which can be replaced via DROPDOWN_STANDARD_COMPONENT_TOKEN.
Usage
<smart-dropdown
triggerLabel="Options"
[items]="items"
[options]="options"
(selectedItem)="onSelect($event)"
/>
@if (selectedId(); as id) {
<p>Selected: {{ id }}</p>
}
The preview renders the code above in an application that registers every preset (provideSmartPresets(), see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Components
DropdownComponent (<smart-dropdown>)
Main wrapper component. Renders DropdownStandardComponent by default. When DROPDOWN_STANDARD_COMPONENT_TOKEN is provided, renders the injected component via NgComponentOutlet.
DropdownStandardComponent (<smart-dropdown-standard>)
Default concrete implementation, unstyled (class hooks smart-dropdown-trigger, smart-dropdown-header). Renders a trigger button showing triggerLabel, or the projected content without it, and while open a <ul role="menu">: a header item with options.headerLabel when options.variant is 'with-header', then one menuitem button per item (disabled respected) and a separator for each divider item.
DropdownPresetComponent (<smart-dropdown-preset>)
Fully styled variation that extends DropdownBaseComponent and is a drop-in replacement for DropdownStandardComponent. Register it for DROPDOWN_STANDARD_COMPONENT_TOKEN to restyle every <smart-dropdown> (or every preset at once with provideSmartPresets()), or use the <smart-dropdown-preset> selector directly. Renders a trigger button (with a rotating chevron) and a menu, dispatched through options.variant (default 'simple'): simple, with-dividers (items split into divider-separated groups at each divider item; the other variants leave divider items out), with-icons (renders each item.icon string), with-header (a header block from options.headerLabel, items shown with icons), and minimal (borderless ghost trigger). The trigger shows triggerLabel, or Actions without it: the preset does not render projected content. All classes are smart:-prefixed Tailwind with explicit dark: variants. Open/close is driven by the inherited open signal + a (click) toggle and @if — Preline's JS plugin is NOT used, but its visual classes and ARIA (aria-haspopup="menu", aria-expanded) are preserved. The class recipes are internal (not exported from @smartsoft001/angular).
The preset declares
cssClasswithout theclassalias, so bind[cssClass]when you use the<smart-dropdown-preset>selector directly;classon<smart-dropdown>reaches it through the wrapper.
DropdownBaseComponent (abstract)
Abstract base directive for extending custom dropdown implementations. Besides the inputs and the output below it provides:
toggle(): opens or closes the menuclose(): closes the menuselectItem(itemId): emitsselectedItemwith{ itemId }and closes the menu
API
Inputs
| Input | Type | Default | Description |
|---|---|---|---|
items | InputSignal<IDropdownItem[]> | [] | Menu items |
triggerLabel | InputSignal<string | undefined> | undefined | Trigger text. Without it the standard renders the projected content and the preset shows Actions |
open | ModelSignal<boolean> | false | Two-way bindable open/closed state |
options | InputSignal<IDropdownOptions | undefined> | undefined | Variant and header label |
class | InputSignal<string> | '' | Classes on the root element (cssClass input, alias class) |
Outputs
| Output | Type | Description |
|---|---|---|
selectedItem | OutputEmitterRef<{ itemId: string }> | Emits when a menu item is selected; closes the menu |
openChange | OutputEmitterRef<boolean> | The open model's change event, for [(open)] |
IDropdownItem
| Field | Type | Default | Description |
|---|---|---|---|
id | string | required | Reported as itemId. |
label | string | required | Item text. |
icon | string | - | Text or glyph before the label. Preset only, in the with-icons and with-header variants. |
disabled | boolean | - | Disables the item. |
divider | boolean | - | Makes the entry a separator (standard); the preset's with-dividers variant splits the menu there, others hide it. |
href | string | - | Not read by the built-in implementations (no item renders as a link); available to a custom implementation. |
IDropdownOptions
| Field | Type | Default | Description |
|---|---|---|---|
variant | SmartDropdownVariant | 'simple' | 'simple', 'with-dividers', 'with-icons', 'minimal' or 'with-header' (preset). The standard only checks 'with-header'. |
headerLabel | string | - | Header text, shown by both implementations when variant is 'with-header'. |
DROPDOWN_STANDARD_COMPONENT_TOKEN
InjectionToken that allows replacing the default DropdownStandardComponent with a custom implementation. Provide a Type<DropdownBaseComponent> to override. The wrapper passes its inputs and the projected content on, and re-emits the implementation's selectedItem and open changes.
Extending the base class
import {
ChangeDetectionStrategy,
Component,
signal,
ViewEncapsulation,
} from '@angular/core';
import {
DROPDOWN_STANDARD_COMPONENT_TOKEN,
DropdownBaseComponent,
DropdownComponent,
IDropdownItem,
} from '@smartsoft001/angular';
@Component({
selector: 'docs-custom-dropdown',
template: `
<div class="docs-dropdown" [class]="cssClass()">
<button
type="button"
class="docs-dropdown__trigger"
[attr.aria-expanded]="open()"
(click)="toggle()"
>
{{ triggerLabel() }}
</button>
@if (open()) {
<ul class="docs-dropdown__menu" role="menu">
@for (item of items(); track item.id) {
@if (item.divider) {
<li class="docs-dropdown__divider" role="separator"></li>
} @else {
<li role="none">
<button
type="button"
role="menuitem"
class="docs-dropdown__item"
[disabled]="item.disabled"
(click)="selectItem(item.id)"
>
{{ item.label }}
</button>
</li>
}
}
</ul>
}
</div>
`,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CustomDropdownComponent extends DropdownBaseComponent {
// `cssClass` comes from the base (alias `class`): the wrapper hands the
// consumer's class to it under that name.
// toggle(), selectItem() and close() come from DropdownBaseComponent:
// selectItem() emits selectedItem and closes the menu for you.
}
@Component({
selector: 'docs-dropdown-custom-example',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [DropdownComponent],
// The token swaps the standard dropdown for the custom one everywhere below
// this component, so consumers keep writing `<smart-dropdown>`.
providers: [
{
provide: DROPDOWN_STANDARD_COMPONENT_TOKEN,
useValue: CustomDropdownComponent,
},
],
// The wrapper re-emits the implementation's `selectedItem`, so the handler
// stays on `<smart-dropdown>`.
template: `
<smart-dropdown
[items]="items"
triggerLabel="Actions"
(selectedItem)="selectedId.set($event.itemId)"
/>
`,
})
export class DropdownCustomExampleComponent {
selectedId = signal<string | null>(null);
items: IDropdownItem[] = [
{ id: 'newsletter', label: 'Newsletter' },
{ id: 'sep', label: '', divider: true },
{ id: 'downloads', label: 'Downloads' },
];
}
Source
The component lives in packages/shared/angular/src/lib/components/dropdown and is documented for Claude Code by the angular-components-dropdown skill.
SmartDropdown renders a trigger button and, while open, a menu (role="menu") of items. Choosing an item reports onSelectedItem({ itemId }) and closes the menu. The open state is controlled (open + onOpenChange) or kept inside (defaultOpen). The trigger shows triggerLabel; without it the standard trigger renders children (the preset shows Actions instead and ignores children).
Usage
import { useState } from 'react';
import {
IDropdownItem,
IDropdownOptions,
SmartDropdown,
} from '@smartsoft001/react';
const options: IDropdownOptions = {
variant: 'with-header',
headerLabel: 'Signed in as tom@example.com',
};
const items: IDropdownItem[] = [
{ id: 'settings', label: 'Account settings' },
{ id: 'support', label: 'Support' },
{ id: 'license', label: 'License', disabled: true },
{ id: 'divider', label: '', divider: true },
{ id: 'sign-out', label: 'Sign out' },
];
export function DropdownUsageExample() {
const [selectedId, setSelectedId] = useState<string | null>(null);
return (
<>
<SmartDropdown
triggerLabel="Options"
items={items}
options={options}
onSelectedItem={({ itemId }) => setSelectedId(itemId)}
/>
{selectedId && <p>Selected: {selectedId}</p>}
</>
);
}
The preview renders the code above in an application that registers every preset (SMART_PRESET_COMPONENTS on SmartProvider, see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Exports
All from @smartsoft001/react.
| Export | Kind | What it is |
|---|---|---|
SmartDropdown | component | Renders the implementation registered as components.dropdown on SmartProvider, SmartDropdownStandard by default. |
SmartDropdownPreset | component | Styled dropdown variation (preset). |
SmartDropdownStandard | component | The default dropdown rendering. |
useDropdown | hook | The behaviour every dropdown variant shares: the open state, controlled through open / onOpenChange or kept internally from defaultOpen; toggle(), close(), and selectItem(id), which reports onSelectedItem and closes the menu. |
The preset's class helpers (getDropdownTriggerClasses, getDropdownMenuClasses, getDropdownChevronClasses, DROPDOWN_CONTAINER, DROPDOWN_GROUP, DROPDOWN_ITEM, DROPDOWN_ICON, DROPDOWN_HEADER, DROPDOWN_HEADER_TEXT) are exported too, for a custom implementation that wants the preset look.
Props and Types
SmartDropdownProps
| Prop | Type | Default | Description |
|---|---|---|---|
items? | IDropdownItem[] | [] | The menu items. |
triggerLabel? | string | — | Trigger text; without it the standard trigger renders children. |
open? | boolean | — | Whether the menu is shown. Leave it undefined for an uncontrolled dropdown that starts from defaultOpen. |
defaultOpen? | boolean | false | Initial open of an uncontrolled dropdown. |
onOpenChange? | (open: boolean) => void | — | Called when the menu opens or closes. |
options? | IDropdownOptions | — | Variant and header label. |
className? | string | — | Classes on the root element. |
onSelectedItem? | (value: { itemId: string }) => void | — | An item was selected; the menu closes afterwards. |
children? | ReactNode | — | Trigger content of the standard dropdown when triggerLabel is not set. |
IDropdownOptions
| Field | Type | Default | Description |
|---|---|---|---|
variant? | SmartDropdownVariant | 'simple' | The look (simple, with-dividers, with-icons, minimal, with-header). |
headerLabel? | string | — | Header text of the with-header variant. |
IDropdownItem
| Field | Type | Default | Description |
|---|---|---|---|
id | string | required | Reported as itemId. |
label | string | required | Item text. |
icon? | string | — | Text or glyph shown before the label by the preset's with-icons / with-header variants. |
href? | string | — | Declared; no variant renders it as a link (navigate in onSelectedItem). |
disabled? | boolean | — | Disables the item. |
divider? | boolean | — | Makes the entry a separator (the preset's with-dividers variant splits the menu into sections there; other preset variants hide it). |
Related types
SmartDropdownVariant:'simple' \| 'with-dividers' \| 'with-icons' \| 'minimal' \| 'with-header'
Replacing the Implementation
SmartDropdown renders the component registered under the 'dropdown' key of SmartProvider's components, and SmartDropdownStandard when nothing is registered there. Every SmartDropdown below the provider then renders the registered component, which receives the same props.
SmartDropdownPreset is the styled (preset) implementation: register it under the 'dropdown' key of SmartProvider's components, render it directly in place of SmartDropdown, or spread SMART_PRESET_COMPONENTS on the provider to register every preset at once (see the react-provider skill). Pass components as a stable object (a module constant or a memoised value), or the context changes on every render.
The useDropdown hook
The behaviour every dropdown variant shares: the open state, controlled through open / onOpenChange or kept internally from defaultOpen; toggle(), close(), and selectItem(id), which reports onSelectedItem and closes the menu.
| Returns | Type | Description |
|---|---|---|
open | boolean | The open state (controlled or internal). |
setOpen | (value: boolean) => void | Sets the state and calls onOpenChange. |
toggle | () => void | Opens or closes the menu. |
close | () => void | Closes the menu. |
selectItem | (itemId: string) => void | Reports onSelectedItem({ itemId }) and closes the menu. |
import {
cn,
IDropdownItem,
SmartDropdown,
SmartDropdownProps,
SmartProvider,
useDropdown,
} from '@smartsoft001/react';
export function CustomDropdown(props: SmartDropdownProps) {
const { items = [], triggerLabel, className } = props;
// useDropdown keeps the shared behaviour: the open state, toggle() and
// selectItem(), which reports onSelectedItem and closes the menu.
const { open, toggle, selectItem } = useDropdown(props);
return (
<div className={cn('docs-dropdown', className)}>
<button
type="button"
className="docs-dropdown__trigger"
aria-expanded={open}
onClick={toggle}
>
{triggerLabel}
</button>
{open && (
<ul className="docs-dropdown__menu" role="menu">
{items.map((item) =>
item.divider ? (
<li
key={item.id}
className="docs-dropdown__divider"
role="separator"
/>
) : (
<li key={item.id} role="none">
<button
type="button"
role="menuitem"
className="docs-dropdown__item"
disabled={item.disabled}
onClick={() => selectItem(item.id)}
>
{item.label}
</button>
</li>
),
)}
</ul>
)}
</div>
);
}
// A module constant: a new object on every render would change the context.
const components = { dropdown: CustomDropdown };
const items: IDropdownItem[] = [
{ id: 'newsletter', label: 'Newsletter' },
{ id: 'sep', label: '', divider: true },
{ id: 'downloads', label: 'Downloads' },
];
export function DropdownCustomExample() {
// Every SmartDropdown below the provider renders CustomDropdown.
return (
<SmartProvider components={components}>
<SmartDropdown items={items} triggerLabel="Actions" />
</SmartProvider>
);
}
Styling
- The standard dropdown is unstyled (
smart-dropdown-trigger,smart-dropdown-headerclass hooks);SmartDropdownPresetkeepsaria-haspopup/aria-expandedand adds the menu look withsmart:dark:variants. classNameis appended to the root element.
Source
The component lives in packages/shared/react/src/lib/components/dropdown and is documented for Claude Code by the react-components-dropdown skill.