Skip to content

components/floating/usePopover

import { … } from 'zidane-ui'ui/src/components/floating/usePopover.ts

Functions#

returnFocusFunction#

Send focus back to target after a surface closes, but only when the surface held it (or nobody does): a pick that moved focus on purpose (into the editor, a rename field) keeps it.

TypeScript
function returnFocus(
  target: HTMLElement | null | undefined,
  surface: HTMLElement | null | undefined,
): void
Parameters
NameTypeDescription
targetHTMLElement | null | undefined

The element to focus, normally the trigger. Ignored when it is null or disconnected.

surfaceHTMLElement | null | undefined

The closing surface. Focus moves only when it held focus, or when nothing did.

ui/src/components/floating/usePopover.ts:98

useDismissFunction#

Outside press and Esc for an open surface. Listens in the capture phase so Esc can be stopped before App's global handler. The handlers are read fresh on each event, so callers may pass new closures every render without re-subscribing.

TypeScript
function useDismiss(
  open: boolean,
  inside: readonly (RefObject<HTMLElement | null> | undefined)[],
  handlers: DismissHandlers,
): void
Parameters
NameTypeDescription
openboolean

Whether the surface is open. Listeners are attached only while it is.

insidereadonly (RefObject<HTMLElement | null> | undefined)[]

Refs of the elements that count as inside the surface, usually the trigger and the surface itself.

handlersDismissHandlers

The callbacks to run, see DismissHandlers.

ui/src/components/floating/usePopover.ts:53

usePopoverFunction#

Owns the open state of a surface that floats off a trigger. An outside press or Esc closes it and calls onDismiss; Esc also returns focus to the trigger without reaching the app's global Esc handler.

TypeScript
function usePopover(
  options?: { initialOpen?: boolean; onDismiss?: () => void },
): PopoverState
Parameters
NameTypeDefaultDescription
options?{ initialOpen?: boolean; onDismiss?: () => void }{}

Options: onDismiss, called for every close the content did not ask for itself, and initialOpen, whether it starts open (default false).

PopoverState

The state, setters and the refs to attach to the trigger and the surface.

ui/src/components/floating/usePopover.ts:113

Interfaces#

DismissHandlersInterface#

Callbacks of useDismiss.

TypeScript
interface DismissHandlers
DismissHandlers properties
NameTypeDescription
onEscape() => void

Esc, already kept from App's global handler.

onKeyDown?(event: KeyboardEvent) => void

Any other key while open (capture phase, before the focused element).

onOutsidePress() => void

A press outside every inside element.

ui/src/components/floating/usePopover.ts:33

PopoverStateInterface#

State and refs returned by usePopover.

TypeScript
interface PopoverState
PopoverState properties
NameTypeDescription
close() => void

Close because the content finished (a pick): does not fire onDismiss.

openboolean

Whether the surface is open.

setOpen(open: boolean) => void

Opens or closes the surface. Closing this way fires onDismiss.

surfaceRefRefObject<HTMLElement | null>

Ref for the floating surface. Presses inside it do not count as outside presses.

toggle() => void

Opens the surface when closed, closes it when open. Closing fires onDismiss.

triggerRefRefObject<HTMLElement | null>

Ref for the trigger element. Presses on it do not count as outside presses, and focus returns to it on Esc.

ui/src/components/floating/usePopover.ts:14