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

Main wrapper component. Renders DropdownStandardComponent by default. When DROPDOWN_STANDARD_COMPONENT_TOKEN is provided, renders the injected component via NgComponentOutlet.

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.

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 cssClass without the class alias, so bind [cssClass] when you use the <smart-dropdown-preset> selector directly; class on <smart-dropdown> reaches it through the wrapper.

Abstract base directive for extending custom dropdown implementations. Besides the inputs and the output below it provides:

  • toggle(): opens or closes the menu
  • close(): closes the menu
  • selectItem(itemId): emits selectedItem with { itemId } and closes the menu

API

Inputs

InputTypeDefaultDescription
itemsInputSignal<IDropdownItem[]>[]Menu items
triggerLabelInputSignal<string | undefined>undefinedTrigger text. Without it the standard renders the projected content and the preset shows Actions
openModelSignal<boolean>falseTwo-way bindable open/closed state
optionsInputSignal<IDropdownOptions | undefined>undefinedVariant and header label
classInputSignal<string>''Classes on the root element (cssClass input, alias class)

Outputs

OutputTypeDescription
selectedItemOutputEmitterRef<{ itemId: string }>Emits when a menu item is selected; closes the menu
openChangeOutputEmitterRef<boolean>The open model's change event, for [(open)]

IDropdownItem

FieldTypeDefaultDescription
idstringrequiredReported as itemId.
labelstringrequiredItem text.
iconstring-Text or glyph before the label. Preset only, in the with-icons and with-header variants.
disabledboolean-Disables the item.
dividerboolean-Makes the entry a separator (standard); the preset's with-dividers variant splits the menu there, others hide it.
hrefstring-Not read by the built-in implementations (no item renders as a link); available to a custom implementation.

IDropdownOptions

FieldTypeDefaultDescription
variantSmartDropdownVariant'simple''simple', 'with-dividers', 'with-icons', 'minimal' or 'with-header' (preset). The standard only checks 'with-header'.
headerLabelstring-Header text, shown by both implementations when variant is 'with-header'.

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.

ExportKindWhat it is
SmartDropdowncomponentRenders the implementation registered as components.dropdown on SmartProvider, SmartDropdownStandard by default.
SmartDropdownPresetcomponentStyled dropdown variation (preset).
SmartDropdownStandardcomponentThe default dropdown rendering.
useDropdownhookThe 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

PropTypeDefaultDescription
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?booleanfalseInitial 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

FieldTypeDefaultDescription
variant?SmartDropdownVariant'simple'The look (simple, with-dividers, with-icons, minimal, with-header).
headerLabel?string—Header text of the with-header variant.

IDropdownItem

FieldTypeDefaultDescription
idstringrequiredReported as itemId.
labelstringrequiredItem 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).
  • 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.

ReturnsTypeDescription
openbooleanThe open state (controlled or internal).
setOpen(value: boolean) => voidSets the state and calls onOpenChange.
toggle() => voidOpens or closes the menu.
close() => voidCloses the menu.
selectItem(itemId: string) => voidReports 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-header class hooks); SmartDropdownPreset keeps aria-haspopup / aria-expanded and adds the menu look with smart:dark: variants.
  • className is 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.