Fragiola

API: components

AlertDialog

A modal dialog the user must answer, never dismissed by a press outside, focused on its least destructive action (APG alert dialog), in each framework.

A dialog that interrupts the user with a message that needs an answer, usually the confirmation of a destructive action. It takes focus, traps it, makes the rest of the page inert and returns focus when it closes, as a Dialog does, but a press outside never closes it: the user answers (one of its buttons) or presses Escape. It follows the WAI-ARIA APG alert dialog pattern. A dialog the user may dismiss from outside is a Dialog.

AlertDialog has its own entry and is built on Dialog's parts: the same Trigger, Portal, Backdrop, Viewport, Popup, Title, Description and Close, whose part attribute names the alert dialog (alert-dialog-popup). Only its Root is its own.

Discard draft
Gallery

Behaviour

  • Open: a press on the Trigger (Enter and Space, as any button). Focus moves to initialFocus, else the first Close part of the popup (the APG's least destructive action, which you render as the Close: "Cancel"), else the first tabbable element, else the popup.
  • Inside: Tab and Shift+Tab wrap inside the popup; everything outside the portal is inert, and the page does not scroll.
  • No outside dismissal: a press outside the popup (on the backdrop, on the viewport around the popup, or on the page) does nothing: the alert dialog stays open, onOpenChange is not called, and focus stays where it was.
  • Close: Escape (reason escape-key), a Close part (close-press) or the Trigger again (trigger-press). Your other actions ("Discard", "Delete") are your own buttons: they act, then close it through the controlled open, the handle's close() or the actions' close() (reason imperative-action). Focus moves to finalFocus, else the active trigger, else the element focused before it opened.
  • Layers and nesting: as a Dialog's (nested overlays): Escape closes the newest layer only; an AlertDialog inside a Dialog's popup (or a Dialog inside an AlertDialog's) is a nested dialog, with the same data-nested, data-nested-dialog-open and --nested-dialogs.
  • Detached triggers: as a Dialog's, with an AlertDialog handle (AlertDialog.createHandle(), detached triggers).
  • Lifecycle: as a Dialog's: onOpenChange before the change (details.cancel() keeps it as it is), onOpenChangeComplete(open), the actions close() and unmount(), and the Portal's keepMounted.

Anatomy

partelementdata-primitives-partrole
Rootnoneholds the state
Triggerbuttonalert-dialog-triggeropens the alert dialog
Portalnonemoves the Backdrop, the Viewport and the Popup to the end of body
Backdropdivalert-dialog-backdropcovers the page (optional; a nested one renders nothing unless forceRender)
Viewportdivalert-dialog-viewporta full-page container that positions the Popup (optional)
Popupdivalert-dialog-popupthe alert dialog
Titleh2alert-dialog-titlenames it
Descriptionpalert-dialog-descriptionits message
Closebuttonalert-dialog-closecloses it: the least destructive answer

Usage

import {
    AlertDialog,
    type OverlayActions,
} from "@fragiola/primitives-react/alert-dialog";
import { useRef } from "react";

function DiscardDraft({ onDiscard }: { onDiscard: () => void }) {
    const actions = useRef<OverlayActions | null>(null);
    return (
        <AlertDialog.Root actionsRef={actions}>
            <AlertDialog.Trigger>Discard draft</AlertDialog.Trigger>
            <AlertDialog.Portal>
                <AlertDialog.Backdrop />
                <AlertDialog.Popup>
                    <AlertDialog.Title>Discard draft?</AlertDialog.Title>
                    <AlertDialog.Description>
                        Your unsaved changes will be lost.
                    </AlertDialog.Description>
                    <button
                        type="button"
                        onClick={() => {
                            onDiscard();
                            actions.current?.close();
                        }}
                    >
                        Discard
                    </button>
                    <AlertDialog.Close>Cancel</AlertDialog.Close>
                </AlertDialog.Popup>
            </AlertDialog.Portal>
        </AlertDialog.Root>
    );
}

The "Discard" button is yours: it acts, then closes the alert dialog through the actions (reason imperative-action). A controlled open closes it as well.

Props

Every part also takes what the primitive contract gives. 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)). There is no modal and no outside dismissal option: an alert dialog is always modal and never dismissed from outside.

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, imperative-action; never outside-press); called before the change, details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions
actionsRef (react){ current: OverlayActions | null }receives close() (reason imperative-action) and unmount(); Vue exposes them on the Root's template ref, Angular as the root directive's methods
handleAlertDialogHandle<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
handleAlertDialogHandle<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, hidden and data-closed; reopening shows the same elements
options (angular)Element | { container?, keepMounted? }{}the same two options: *alertDialogPortal="{ keepMounted: true }"

Backdrop

proptypedefaultdescription
forceRenderbooleanfalserender the Backdrop of a nested alert dialog too

Viewport

No props of its own.

proptypedefaultdescription
initialFocusan element, a ref, or falsethe first Close, else the 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 has no alertdialog value), aria-expanded, 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="alertdialog", 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)

The APG asks an alert dialog for a name and a description: render a Title and a Description.

Keyboard

keyaction
Enter, Space on the Triggeropens, focus on the first Close
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/alert-dialogAlertDialog, AlertDialogRootProps, AlertDialogTriggerProps, AlertDialogTriggerState, AlertDialogPortalProps, AlertDialogBackdropProps, AlertDialogBackdropState, AlertDialogViewportState, AlertDialogPopupProps, AlertDialogPopupState, AlertDialogHandle, createAlertDialogHandle, OverlayRootProps, OverlayTriggerRootProps, OverlayTriggerProps, OverlayPortalProps, OverlayHandle, OverlayActions, OpenChangeDetails, FocusTarget, ChangeDetails, ChangeReason, RenderProp
@fragiola/primitives-vue/alert-dialogAlertDialog, AlertDialogRootProps, AlertDialogRootEmits, AlertDialogTriggerProps, AlertDialogTriggerState, AlertDialogPortalProps, AlertDialogBackdropProps, AlertDialogBackdropState, AlertDialogViewportState, AlertDialogPopupProps, AlertDialogPopupState, AlertDialogHandle, createAlertDialogHandle, OverlayRootProps, OverlayRootEmits, OverlayRootSlots, OverlayTriggerRootProps, OverlayTriggerProps, OverlayPortalProps, OverlayHandle, OverlayActions, OpenChangeDetails, FocusTarget, ChangeDetails, ChangeReason, RenderSlotProps
@fragiola/primitives-angular/alert-dialogAlertDialogRoot, AlertDialogTrigger, AlertDialogPortal, AlertDialogBackdrop, AlertDialogViewport, AlertDialogPopup, AlertDialogTitle, AlertDialogDescription, AlertDialogClose, AlertDialogHandle, createAlertDialogHandle, OverlayHandle, OverlayActions, OverlayPortalOptions, OpenChangeDetails, OpenChangeEvent, FocusTarget, ChangeDetails, ChangeReason, AlertDialogBackdropState, AlertDialogPopupState, AlertDialogState, AlertDialogTriggerState, AlertDialogViewportState