ui/popover.svelte.ts

Popover state class with trigger, content, and container attachments.

Popover manages visibility, positioning, outside-click and Escape dismissal, and ARIA attributes. The trigger, content, and container attachments wire up the DOM via Svelte's {@attach} directive.

While visible, the trigger carries aria-expanded="true" and aria-controls naming the content (cleared on hide, when the content usually leaves the DOM), and the content — role="dialog" unless it sets its own role — is aria-labelledby the trigger unless it carries its own name. Escape hides the popover while it's visible, returning focus to the trigger when focus was inside the content. The listener is on document, so it runs before window listeners, and it prevents the default — so an enclosing layer that skips handled Escapes, like fuz_ui's AppShell narrow overlay, stays open, and only the popover closes. An Escape another listener already prevented is left alone. With several popovers visible, only the newest handles Escape, so nested ones close innermost first. While a modal (:modal) is open, a popover outside it leaves Escape to the modal; a popover inside the modal still closes first.

For the common button + popover pattern, see PopoverButton.

<script lang="ts"> import {Popover} from '@fuzdev/fuz_app/ui/popover.svelte.ts'; const popover = new Popover(); </script> <div class="relative" {@attach popover.container}> <button type="button" {@attach popover.trigger({position: 'bottom', align: 'center'})}> toggle </button> {#if popover.visible} <div {@attach popover.content({position: 'bottom', align: 'center'})}> <button type="button" onclick={() => popover.hide()}>close</button> </div> {/if} </div>
// programmatic control with callbacks const popover = new Popover({ position: 'right', align: 'start', offset: '8px', disable_outside_click: true, onshow: () => console.log('opened'), onhide: () => console.log('closed'), }); popover.show(); popover.toggle(); popover.hide();
view source

Declarations
#

3 declarations

Popover
#

ui/popover.svelte.ts view source

import {Popover} from '@fuzdev/fuz_app/ui/popover.svelte.js';

Class that manages state and provides actions for popovers.

visible

Whether the popover is currently visible.

type boolean

$state.raw

position

Position of the popover relative to its trigger.

type Position

$state.raw

align

Alignment along the position edge.

type Alignment

$state.raw

offset

Distance from the position.

type string

$state.raw

disable_outside_click

Whether to disable closing when clicking outside.

type boolean

$state.raw

popover_class

Custom class for the popover.

type string

$state.raw

constructor

type new (params?: PopoverParameters | undefined): Popover

params?

optional

update

Updates the popover configuration. Swaps popover_class on the live content element when supplied and re-syncs the outside-click handler if disable_outside_click changed.

type (params: PopoverParameters): void

params

returns void

mutates

  • this

show

Shows the popover, registering the outside-click handler and updating aria-expanded on the trigger. No-ops when already visible.

type (): void

returns void

mutates

  • this

hide

Hides the popover, removing the outside-click handler. No-ops when already hidden.

type (): void

returns void

mutates

  • this

toggle

Toggles the popover visibility, or sets it to the given value.

type (visible?: boolean): void

visible?

desired visibility (defaults to the inverse of current)

type boolean
optional default !this.visible
returns void

mutates

  • this

container

Attachment for the container element.

type Attachment<HTMLElement>

trigger

Attachment factory for the trigger element that shows/hides the popover.

type (params?: PopoverParameters | undefined) => Attachment<HTMLElement>

content

Attachment factory for the popover content element.

type (params?: PopoverContentParameters | undefined) => Attachment<HTMLElement>

PopoverContentParameters
#

PopoverParameters
#

ui/popover.svelte.ts view source

PopoverParameters import type {PopoverParameters} from '@fuzdev/fuz_app/ui/popover.svelte.js';

Parameters for configuring the popover.

position?

Position of the popover relative to its trigger.

type Position

align?

Alignment along the position edge.

type Alignment

offset?

Distance from the position.

type string

disable_outside_click?

Whether to disable closing when clicking outside.

type boolean

popover_class?

Custom class for the popover content.

type string

onshow?

Optional callback when popover is shown.

type (): void

returns void

onhide?

Optional callback when popover is hidden.

type (): void

returns void

Depends on
#

Imported by
#