Fragiola

API: components

Dialog

A modal dialog that takes focus, traps it, makes the page inert and returns focus on close, following the APG modal dialog pattern in each framework.

A window over the page: it takes focus, keeps it inside, makes the rest of the page inert and stops it scrolling, and gives focus back when it closes. It follows the WAI-ARIA APG modal dialog pattern. Non-modal floating content is a Popover; a dialog the user must answer, never dismissed from outside, is an AlertDialog.

Edit profile
Gallery

Behaviour

  • Open: a press on the Trigger (Enter and Space, as any button). Focus moves to initialFocus, else the first tabbable element of the popup, else the popup itself.
  • Inside: Tab and Shift+Tab wrap inside the popup; everything outside the portal is inert, and the page does not scroll (its scrollbar gutter is kept, so nothing shifts).
  • Close: Escape (reason escape-key), a press outside the popup (outside-press), a Close part (close-press) or the Trigger again (trigger-press). Focus moves to finalFocus, else the trigger, else the element focused before it opened.
  • Change details: onOpenChange(open, details) is called before the state changes, with the reason, the DOM event and the trigger; details.cancel() keeps the dialog as it is (it keeps working, focus and inertness included). A close of the handle or of the actions has the reason imperative-action and returns focus as none does.
  • Layers: the dialog is a layer of the nested overlay tree. Escape closes the newest layer only; a press inside the dialog closes the overlays opened from it, not the dialog; closing the dialog closes them all (reason none).
  • Nested dialogs: a Dialog whose Root is inside another dialog's Popup is nested: its parts carry data-nested, the parent's data-nested-dialog-open, and the parent Popup's --nested-dialogs counts the open dialogs below it (2 for a child with its own child open, 0 when none), so a recipe can scale or offset it. A nested Backdrop renders nothing unless forceRender. A press on a nested dialog's Viewport outside its Popup closes that dialog only.
  • Detached triggers: Triggers anywhere in the page open the same Root through a handle, each with a payload the content reads (detached triggers). Only the active trigger reports the open state, and focus returns to it. The handle's open(triggerId), openWithPayload(payload) and close() act from code (reason imperative-action), isOpen reads the state; a call with no Root attached does nothing.
  • Animation and lifecycle: the Backdrop, the Viewport and the Popup carry data-starting-style on their first frame and data-ending-style while closing, and unmount when their animations end; onOpenChangeComplete(open) reports the end of each transition. With the Portal's keepMounted, they stay in the page while closed, hidden and data-closed.

Anatomy

partelementdata-primitives-partrole
Rootnoneholds the state
Triggerbuttondialog-triggeropens the dialog
Portalnonemoves the Backdrop, the Viewport and the Popup to the end of body
Backdropdivdialog-backdropcovers the page (optional; a nested dialog's renders nothing unless forceRender)
Viewportdivdialog-viewporta full-page container that positions the Popup (optional, role="presentation")
Popupdivdialog-popupthe dialog
Titleh2dialog-titlenames the dialog
Descriptionpdialog-descriptiondescribes the dialog
Closebuttondialog-closecloses the dialog

Usage

import { Dialog } from "@fragiola/primitives-react/dialog";

<Dialog.Root onOpenChange={(open, { reason }) => console.log(open, reason)}>
    <Dialog.Trigger>Edit profile</Dialog.Trigger>
    <Dialog.Portal>
        <Dialog.Backdrop />
        <Dialog.Viewport>
            <Dialog.Popup>
                <Dialog.Title>Edit profile</Dialog.Title>
                <Dialog.Description>Change how others see you.</Dialog.Description>
                <Dialog.Close>Close</Dialog.Close>
            </Dialog.Popup>
        </Dialog.Viewport>
    </Dialog.Portal>
</Dialog.Root>;

Controlled: open and onOpenChange, with useState. The actions: actionsRef={ref}, then ref.current?.close(). Detached triggers: const handle = Dialog.createHandle<User>(), <Dialog.Trigger handle={handle} payload={user}> anywhere, and the Root's children as a function {({ payload }) => ...}.

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:open, @open-change), and Angular as inputs and outputs ([(open)], (openChangeDetails)).

Root

proptypedefaultdescription
openbooleanthe open state, controlled
defaultOpenbooleanfalsethe initial open state, uncontrolled
onOpenChange(open: boolean, details: OpenChangeDetails) => voidevery change, with its reason (trigger-press, close-press, escape-key, outside-press); called before the change, details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions (not on the first render, nor for an exit a reopening interrupted)
actionsRef (react){ current: OverlayActions | null }receives close() (reason imperative-action) and unmount() (ends an exit now); Vue exposes them on the Root's template ref, Angular as the root directive's methods
handleDialogHandle<Payload>connects Triggers rendered outside the Root (detached triggers)
triggerIdstring | nullthe active trigger's id, controlled together with open
defaultTriggerIdstring | nullnullthe initial active trigger, uncontrolled

Trigger

proptypedefaultdescription
disabledbooleanfalsea disabled trigger opens nothing
handleDialogHandle<Payload>the handle of a Root this Trigger opens from anywhere, outside it
payloadPayloadwhat the Root's content receives while this Trigger is the active one
idstringgeneratedthe Trigger's id: triggerId and the handle's open(id) name it

Portal

proptypedefaultdescription
container (react, vue)Elementdocument.bodywhere the Backdrop, the Viewport and the Popup go
keepMounted (react, vue)booleanfalsekeep the content rendered while closed (before the first opening and after an exit), hidden and data-closed; reopening shows the same elements
options (angular)Element | { container?, keepMounted? }{}the same two options: *dialogPortal="{ keepMounted: true }", or *dialogPortal="element" for a container alone

Backdrop

proptypedefaultdescription
forceRenderbooleanfalserender the Backdrop of a nested dialog too (by default only the outermost dialog's covers the page)

Viewport

No props of its own.

proptypedefaultdescription
initialFocusan element, a ref, or falsethe first tabbable elementfocused on open (false: the popup itself); Vue and Angular also take a getter
finalFocusan element, a ref, or falsethe triggerfocused on close (false: nothing moves)

Title

proptypedefaultdescription
id (angular)stringgeneratedthe id the popup's aria-labelledby points at; React and Vue take it as a plain attribute

Description

proptypedefaultdescription
id (angular)stringgeneratedthe id the popup's aria-describedby points at; React and Vue take it as a plain attribute

Close

No props of its own.

State and part attributes

partattributes
Triggeraria-haspopup="dialog", aria-expanded ("true" on the active trigger while open), aria-controls (the popup, on the active trigger while open), data-popup-open (the active trigger), data-disabled, an id
Backdroparia-hidden="true", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-dialog-open; hidden while kept mounted and closed
Viewportrole="presentation", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-dialog-open; hidden while kept mounted and closed
Popuprole="dialog", aria-modal="true", aria-labelledby, aria-describedby, tabindex="-1", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-dialog-open, the inline --nested-dialogs: <n>; hidden while kept mounted and closed
Title, Descriptionan id (generated unless given)

Keyboard

keyaction
Enter, Space on the Triggeropens
Tab, Shift+Tabthe next or previous tabbable element of the popup, wrapping
Escapecloses (only the topmost layer, when one is open inside)

Exports

entryexports
@fragiola/primitives-react/dialogDialog, DialogRootProps, DialogTriggerProps, DialogTriggerState, DialogPortalProps, DialogBackdropState, DialogPopupProps, DialogPopupState, ChangeDetails, ChangeReason, RenderProp, DialogBackdropProps, DialogHandle, DialogViewportState, FocusTarget, OpenChangeDetails, OverlayActions, OverlayHandle, OverlayPortalProps, OverlayRootProps, OverlayTriggerProps, OverlayTriggerRootProps, createDialogHandle
@fragiola/primitives-vue/dialogDialog, DialogRootProps, DialogRootEmits, DialogTriggerProps, DialogTriggerState, DialogPortalProps, DialogBackdropState, DialogPopupProps, DialogPopupState, ChangeDetails, ChangeReason, RenderSlotProps, DialogBackdropProps, DialogHandle, DialogViewportState, FocusTarget, OpenChangeDetails, OverlayActions, OverlayHandle, OverlayPortalProps, OverlayRootEmits, OverlayRootProps, OverlayRootSlots, OverlayTriggerProps, OverlayTriggerRootProps, createDialogHandle
@fragiola/primitives-angular/dialogDialogRoot, DialogTrigger, DialogPortal, DialogBackdrop, DialogPopup, DialogTitle, DialogDescription, DialogClose, injectDialog, DialogState, DialogTriggerState, DialogBackdropState, DialogPopupState, ChangeDetails, ChangeReason, OpenChangeEvent, DialogHandle, DialogViewport, DialogViewportState, FocusTarget, OpenChangeDetails, OverlayActions, OverlayHandle, OverlayPortalOptions, createDialogHandle, DIALOG_NESTING, DialogComponent, DialogNesting, DialogRootBase, DialogTriggerBase