# Dropdowns

**Dropdowns** are toggleable, contextual overlays for displaying actions related to the content.

## Usage

They consist of a dropdown toggle and an overlay containing the actions. The overlay is shown after clicking the dropdown toggle. Dropdown are available with text only or with text and icon.

![Dropdown](<../images/dropdown.png>)

### When to use

- When there is a need for actions, which are related to each other (e.g. Export: PDF, Excel, Word, ...).
- When users need to make a choice among a list of mutually exclusive options.

### Best practices

- Minimal button width is `100px`.
- Menu container should be at least the same width as the button.

## Design

### Elements

![Dropdown - active](<../images/dropdown-active.png>)

> 1. Placeholder text, 2. Arrow, 3. Container, 4. [menu container](<https://element.siemens.io/components/buttons-menus/menu/>), 5. Icon (optional)

### Variants

![Dropdown alternatives - text](<../images/dropdown-text.png>)

Interaction states are identical to the [normal buttons](<https://element.siemens.io/components/buttons-menus/buttons/>).

## Code

> [!WARNING]
>
> **Dropdown menu**
>
> If the overlay should show a contextual menu, please use the [menu component](<https://element.siemens.io/components/buttons-menus/menu/>). It already implements specific keyboard interactions and aria roles that are needed for a *dropdown menu*.

Use a [button](<https://element.siemens.io/components/buttons-menus/buttons/>) with the `dropdown-toggle` class as the trigger and place the content in a `dropdown-menu`. Add a `dropdown-caret` icon to indicate that the button opens an overlay.

The theme provides the dropdown styles. Use the [CDK Overlay module](<https://material.angular.io/cdk/overlay/overview>) to create, position, and dismiss the overlay. The CDK directives can also be used with content that does not use the `dropdown-menu` class.

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { A11yModule } from '@angular/cdk/a11y';
import { CdkConnectedOverlay, CdkOverlayOrigin } from '@angular/cdk/overlay';
import { Component, inject, signal } from '@angular/core';
import { elementDown2 } from '@siemens/element-icons';
import { addIcons, SiIconComponent } from '@siemens/element-ng/icon';
import { LOG_EVENT } from '@siemens/live-preview';

@Component({
  selector: 'app-sample',
  imports: [A11yModule, CdkOverlayOrigin, CdkConnectedOverlay, SiIconComponent],
  templateUrl: './dropdown-with-overlay.html',
  host: { class: 'p-5' }
})
export class SampleComponent {
  protected readonly logEvent = inject(LOG_EVENT);
  protected readonly icons = addIcons({ elementDown2 });
  protected readonly open = signal(false);
}
```

Template:

```html
<button
  #trigger="cdkOverlayOrigin"
  type="button"
  class="btn btn-primary dropdown-toggle"
  cdkOverlayOrigin
  [class.show]="open()"
  [attr.aria-expanded]="open()"
  (click)="open.set(true)"
>
  Overlay trigger / origin
  <si-icon class="dropdown-caret icon" [icon]="icons.elementDown2" />
</button>

<ng-template
  cdkConnectedOverlay
  cdkConnectedOverlayBackdropClass="cdk-overlay-transparent-backdrop"
  [cdkConnectedOverlayOpen]="open()"
  [cdkConnectedOverlayOrigin]="trigger"
  [cdkConnectedOverlayHasBackdrop]="true"
  (backdropClick)="open.set(false)"
  (detach)="open.set(false)"
>
  <div
    class="dropdown-menu position-static d-block"
    [cdkTrapFocus]="true"
    [cdkTrapFocusAutoCapture]="true"
  >
    <button class="dropdown-item focus-inside" type="button" (click)="logEvent('Action')">
      Action
    </button>
    <button class="dropdown-item focus-inside" type="button" (click)="logEvent('Action 2')">
      Action 2
    </button>
    <button class="dropdown-item focus-inside" type="button" (click)="logEvent('Action 3')">
      Action 3
    </button>
  </div>
</ng-template>
```

The overlay is toggled by the `open` property which needs to be updated properly. We added a transparent backdrop to the overlay so that we can listen for `(backdropClick)` which should close the overlay. The listener for `(detach)` is necessary for updating the `open` property after escape was pressed. In a real world scenario, one has to be careful here, because `(detach)` is always called when the overlay is closed.

For having a proper keyboard interaction, we are using the [CDK Focus trap](<https://material.angular.io/cdk/a11y/overview#focustrap>). It ensures that the focus will remain within the overlay while it is open. Enabling `[cdkTrapFocusAutoCapture]="true"` ensures, that the focus will be moved into the overlay on creation and returned to the trigger when the overlay is closed.

### Menu content

Use `dropdown-item` for interactive actions. It supports the `active` and `disabled` states. Use `dropdown-item-text` for non-interactive content and `dropdown-header` to label a group of items. Use `dropdown-divider` to separate related groups.

```
<div class="dropdown-menu show">
  <div class="dropdown-header">Export</div>
  <button type="button" class="dropdown-item active">PDF</button>
  <button type="button" class="dropdown-item">Excel</button>
  <button type="button" class="dropdown-item" disabled>Word</button>
  <div class="dropdown-divider"></div>
  <div class="dropdown-item-text">Exports include the current filters.</div>
</div>
```

For items with icons, place the icon before an `item-title`. A trailing `menu-end-icon` can communicate an additional state or a follow-up action.

```
<button type="button" class="dropdown-item">
  <si-icon class="icon" icon="element-download" />
  <span class="item-title">Download</span>
  <si-icon class="menu-end-icon icon" icon="element-right-2" />
</button>
```

### Long menus

`dropdown-menu` scrolls when its height exceeds the viewport. Add `dropdown-menu-scroller` to apply the recommended bounded height for menus with many items.

```
<div class="dropdown-menu dropdown-menu-scroller show">
  <!-- Dropdown items -->
</div>
```

### Related components

Use [button groups](<https://element.siemens.io/components/buttons-menus/button-group/>) to create split-button patterns. Use the [menu component](<https://element.siemens.io/components/buttons-menus/menu/>) when the content, see [content actions](<https://element.siemens.io/components/buttons-menus/content-actions/>).

### CdkConnectedOverlay

**Kind:** directive

**Selector:** `[cdk-connected-overlay], [connected-overlay], [cdkConnectedOverlay]`

**Exported as:** `cdkConnectedOverlay`

Directive to facilitate declarative creation of an Overlay using a FlexibleConnectedPositionStrategy.

```
directive CdkConnectedOverlay implements OnDestroy, OnChanges
```

#### Input Properties

| Name | Type | Description |
| --- | --- | --- |
| cdkConnectedOverlay | `string \| CdkConnectedOverlayConfig` | Shorthand for setting multiple overlay options at once. |
| cdkConnectedOverlayBackdropClass | `string \| string[]` | The custom class to be set on the backdrop element. |
| cdkConnectedOverlayDisableClose | `boolean` | Whether the overlay can be closed by user interaction. |
| cdkConnectedOverlayDisposeOnNavigation | `boolean` | Whether the overlay should be disposed of when the user goes backwards/forwards in history. |
| cdkConnectedOverlayFlexibleDimensions | `boolean` | Whether the overlay's width and height can be constrained to fit within the viewport. |
| cdkConnectedOverlayGrowAfterOpen | `boolean` | Whether the overlay can grow after the initial open when flexible positioning is turned on. |
| cdkConnectedOverlayHasBackdrop | `boolean` | Whether or not the overlay should attach a backdrop. |
| cdkConnectedOverlayHeight | `string \| number` | The height of the overlay panel. |
| cdkConnectedOverlayLockPosition | `boolean` | Whether or not the overlay should be locked when scrolling. |
| cdkConnectedOverlayMatchWidth | `boolean` | Whether the overlay should match the trigger's width. |
| cdkConnectedOverlayMinHeight | `string \| number` | The min height of the overlay panel. |
| cdkConnectedOverlayMinWidth | `string \| number` | The min width of the overlay panel. |
| cdkConnectedOverlayOffsetX | `number` | The offset in pixels for the overlay connection point on the x-axis |
| cdkConnectedOverlayOffsetY | `number` | The offset in pixels for the overlay connection point on the y-axis |
| cdkConnectedOverlayOpen | `boolean` | Whether the overlay is open. |
| cdkConnectedOverlayOrigin | `CdkOverlayOrigin \| FlexibleConnectedPositionStrategyOrigin` | Origin for the connected overlay. |
| cdkConnectedOverlayPanelClass | `string \| string[]` | The custom class to add to the overlay pane element. |
| cdkConnectedOverlayPositions | `ConnectedPosition[]` | Registered connected position pairs. |
| cdkConnectedOverlayPositionStrategy | `FlexibleConnectedPositionStrategy` | This input overrides the positions input if specified. It lets users pass in arbitrary positioning strategies. |
| cdkConnectedOverlayPush | `boolean` | Whether the overlay can be pushed on-screen if none of the provided positions fit. |
| cdkConnectedOverlayScrollStrategy | `ScrollStrategy` | Strategy to be used when handling scroll events while the overlay is open. |
| cdkConnectedOverlayTransformOriginOn | `string` | CSS selector which to set the transform origin. |
| cdkConnectedOverlayUsePopover | `FlexibleOverlayPopoverLocation \| null` | Whether the connected overlay should be rendered inside a popover element or the overlay container. |
| cdkConnectedOverlayViewportMargin | `ViewportMargin` | Margin between the overlay and the viewport edges. |
| cdkConnectedOverlayWidth | `string \| number` | The width of the overlay panel. |

#### Output Properties

| Name | Type | Description |
| --- | --- | --- |
| attach | `void` | Event emitted when the overlay has been attached. |
| backdropClick | `MouseEvent` | Event emitted when the backdrop is clicked. |
| detach | `void` | Event emitted when the overlay has been detached. |
| overlayKeydown | `KeyboardEvent` | Emits when there are keyboard events that are targeted at the overlay. |
| overlayOutsideClick | `MouseEvent` | Emits when there are mouse outside click events that are targeted at the overlay. |
| positionChange | `ConnectedOverlayPositionChange` | Event emitted when the position has changed. |

#### Attributes and Methods

##### attachOverlay

Attaches the overlay.

```
attachOverlay(): void
```

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### detachOverlay

Detaches the overlay.

```
detachOverlay(): void
```

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### dir

**Modifiers:** readonly

**Type:** `Direction`

The element's layout direction.

##### overlayRef

**Modifiers:** readonly

**Type:** `OverlayRef`

The associated overlay reference.

### CdkOverlayOrigin

**Kind:** directive

**Selector:** `[cdk-overlay-origin], [overlay-origin], [cdkOverlayOrigin]`

**Exported as:** `cdkOverlayOrigin`

Directive applied to an element to make it usable as an origin for an Overlay using a ConnectedPositionStrategy.

```
directive CdkOverlayOrigin
```

#### Attributes and Methods

##### elementRef

**Type:** `ElementRef<any>`

### CdkTrapFocus

**Kind:** directive

**Selector:** `[cdkTrapFocus]`

**Exported as:** `cdkTrapFocus`

Directive for trapping focus within a region.

```
directive CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoCheck
```

#### Input Properties

| Name | Type | Description |
| --- | --- | --- |
| cdkTrapFocus | `boolean` | Whether the focus trap is active. |
| cdkTrapFocusAutoCapture | `boolean` | Whether the directive should automatically move focus into the trapped region upon initialization and return focus to the previous activeElement upon destruction. |

#### Attributes and Methods

##### focusTrap

**Type:** `FocusTrap`

Underlying FocusTrap instance.
