Controllers DismissController
Dismiss controller
Lit reactive controller that closes transient surfaces on an outside pointerdown or an Escape key press, with an opt-out list for elements that should count as inside.
Live playground
Open the menu, then press Escape or click anywhere outside it. The trigger is always in ignoreElements; the safe zone is opt-in.
Safe zone —
in ignoreElements, clicks here keep the menu open
Interactive options
Usage example
import {DismissController} from '@nathanmosher/design-system/controllers';
class DsMenu extends LitElement {
private readonly dismiss = new DismissController(this, {
ignoreElements: () => [this.renderRoot.querySelector('.safe-zone')],
onOutsideClick: () => this.close(),
onEscape: () => this.close(),
});
open() {
this.dismiss.activate();
}
}
Options reference
| Option | Type | Default | Description |
|---|---|---|---|
| active | boolean | false | Whether the controller starts listening immediately. |
| onDismiss | (event) => void | undefined | Fires for both outside pointerdown and Escape. |
| onOutsideClick | (event: PointerEvent) => void | undefined | Fires only for a pointerdown outside the host. |
| onEscape | (event: KeyboardEvent) => void | undefined | Fires only for the Escape key. |
| ignoreElements | () => Element[] | undefined | Extra elements treated as inside the host. |
Members reference
| Member | Type | Description |
|---|---|---|
| active | boolean | Reads or sets whether the listeners are attached. |
| activate() | void | Starts listening for outside clicks and Escape. |
| deactivate() | void | Stops listening and detaches the document listeners. |