@funstack/ui 0.1.0-alpha.19 → 0.1.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/accordion/accordion-item.mjs +2 -2
- package/dist/accordion/accordion-item.mjs.map +1 -1
- package/dist/accordion/accordion-trigger.d.mts.map +1 -1
- package/dist/accordion/accordion-trigger.mjs +3 -20
- package/dist/accordion/accordion-trigger.mjs.map +1 -1
- package/dist/alert-dialog/alert-dialog-action.d.mts +73 -0
- package/dist/alert-dialog/alert-dialog-action.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-action.mjs +23 -0
- package/dist/alert-dialog/alert-dialog-action.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-cancel.d.mts +31 -0
- package/dist/alert-dialog/alert-dialog-cancel.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-cancel.mjs +50 -0
- package/dist/alert-dialog/alert-dialog-cancel.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-context.mjs +24 -0
- package/dist/alert-dialog/alert-dialog-context.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-description.d.mts +25 -0
- package/dist/alert-dialog/alert-dialog-description.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-description.mjs +20 -0
- package/dist/alert-dialog/alert-dialog-description.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-form.d.mts +27 -0
- package/dist/alert-dialog/alert-dialog-form.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-form.mjs +22 -0
- package/dist/alert-dialog/alert-dialog-form.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-popup.d.mts +42 -0
- package/dist/alert-dialog/alert-dialog-popup.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-popup.mjs +53 -0
- package/dist/alert-dialog/alert-dialog-popup.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-root.d.mts +31 -0
- package/dist/alert-dialog/alert-dialog-root.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-root.mjs +22 -0
- package/dist/alert-dialog/alert-dialog-root.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-title.d.mts +23 -0
- package/dist/alert-dialog/alert-dialog-title.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-title.mjs +18 -0
- package/dist/alert-dialog/alert-dialog-title.mjs.map +1 -0
- package/dist/alert-dialog/alert-dialog-trigger.d.mts +23 -0
- package/dist/alert-dialog/alert-dialog-trigger.d.mts.map +1 -0
- package/dist/alert-dialog/alert-dialog-trigger.mjs +18 -0
- package/dist/alert-dialog/alert-dialog-trigger.mjs.map +1 -0
- package/dist/alert-dialog/index.d.mts +33 -0
- package/dist/alert-dialog/index.d.mts.map +1 -0
- package/dist/alert-dialog/index.mjs +33 -0
- package/dist/alert-dialog/index.mjs.map +1 -0
- package/dist/checkbox-group/checkbox-group-parent.mjs +1 -1
- package/dist/collapsible/collapsible-context.mjs +8 -0
- package/dist/collapsible/collapsible-context.mjs.map +1 -0
- package/dist/collapsible/collapsible-panel.d.mts +53 -0
- package/dist/collapsible/collapsible-panel.d.mts.map +1 -0
- package/dist/collapsible/collapsible-panel.mjs +41 -0
- package/dist/collapsible/collapsible-panel.mjs.map +1 -0
- package/dist/collapsible/collapsible-root.d.mts +108 -0
- package/dist/collapsible/collapsible-root.d.mts.map +1 -0
- package/dist/collapsible/collapsible-root.mjs +147 -0
- package/dist/collapsible/collapsible-root.mjs.map +1 -0
- package/dist/collapsible/collapsible-trigger.d.mts +45 -0
- package/dist/collapsible/collapsible-trigger.d.mts.map +1 -0
- package/dist/collapsible/collapsible-trigger.mjs +62 -0
- package/dist/collapsible/collapsible-trigger.mjs.map +1 -0
- package/dist/collapsible/index.d.mts +21 -0
- package/dist/collapsible/index.d.mts.map +1 -0
- package/dist/collapsible/index.mjs +21 -0
- package/dist/collapsible/index.mjs.map +1 -0
- package/dist/combobox/chip-text.mjs +24 -0
- package/dist/combobox/chip-text.mjs.map +1 -0
- package/dist/combobox/combobox-chips.d.mts +159 -0
- package/dist/combobox/combobox-chips.d.mts.map +1 -0
- package/dist/combobox/combobox-chips.mjs +198 -0
- package/dist/combobox/combobox-chips.mjs.map +1 -0
- package/dist/combobox/combobox-clear.d.mts +5 -2
- package/dist/combobox/combobox-clear.d.mts.map +1 -1
- package/dist/combobox/combobox-clear.mjs +3 -1
- package/dist/combobox/combobox-clear.mjs.map +1 -1
- package/dist/combobox/combobox-context.mjs +4 -5
- package/dist/combobox/combobox-context.mjs.map +1 -1
- package/dist/combobox/combobox-input.d.mts +10 -3
- package/dist/combobox/combobox-input.d.mts.map +1 -1
- package/dist/combobox/combobox-input.mjs +13 -5
- package/dist/combobox/combobox-input.mjs.map +1 -1
- package/dist/combobox/combobox-keyboard.mjs +50 -4
- package/dist/combobox/combobox-keyboard.mjs.map +1 -1
- package/dist/combobox/combobox-list.d.mts +1 -1
- package/dist/combobox/combobox-list.mjs +2 -1
- package/dist/combobox/combobox-list.mjs.map +1 -1
- package/dist/combobox/combobox-option.d.mts +5 -2
- package/dist/combobox/combobox-option.d.mts.map +1 -1
- package/dist/combobox/combobox-option.mjs +4 -3
- package/dist/combobox/combobox-option.mjs.map +1 -1
- package/dist/combobox/combobox-root.d.mts +127 -35
- package/dist/combobox/combobox-root.d.mts.map +1 -1
- package/dist/combobox/combobox-root.mjs +396 -44
- package/dist/combobox/combobox-root.mjs.map +1 -1
- package/dist/combobox/index.d.mts +8 -2
- package/dist/combobox/index.d.mts.map +1 -1
- package/dist/combobox/index.mjs +7 -1
- package/dist/combobox/index.mjs.map +1 -1
- package/dist/combobox/use-combobox-form.mjs +2 -3
- package/dist/combobox/use-combobox-form.mjs.map +1 -1
- package/dist/dialog/dialog-close.d.mts +9 -5
- package/dist/dialog/dialog-close.d.mts.map +1 -1
- package/dist/dialog/dialog-close.mjs +19 -8
- package/dist/dialog/dialog-close.mjs.map +1 -1
- package/dist/dialog/dialog-context.d.mts +2 -0
- package/dist/dialog/dialog-context.mjs +14 -2
- package/dist/dialog/dialog-context.mjs.map +1 -1
- package/dist/dialog/dialog-description.d.mts +2 -1
- package/dist/dialog/dialog-description.d.mts.map +1 -1
- package/dist/dialog/dialog-description.mjs +8 -4
- package/dist/dialog/dialog-description.mjs.map +1 -1
- package/dist/dialog/dialog-form.d.mts +2 -1
- package/dist/dialog/dialog-form.d.mts.map +1 -1
- package/dist/dialog/dialog-form.mjs +11 -6
- package/dist/dialog/dialog-form.mjs.map +1 -1
- package/dist/dialog/dialog-popup.d.mts +2 -1
- package/dist/dialog/dialog-popup.d.mts.map +1 -1
- package/dist/dialog/dialog-popup.mjs +23 -14
- package/dist/dialog/dialog-popup.mjs.map +1 -1
- package/dist/dialog/dialog-root.d.mts +2 -1
- package/dist/dialog/dialog-root.d.mts.map +1 -1
- package/dist/dialog/dialog-root.mjs +15 -5
- package/dist/dialog/dialog-root.mjs.map +1 -1
- package/dist/dialog/dialog-title.d.mts +2 -1
- package/dist/dialog/dialog-title.d.mts.map +1 -1
- package/dist/dialog/dialog-title.mjs +8 -4
- package/dist/dialog/dialog-title.mjs.map +1 -1
- package/dist/dialog/dialog-trigger.d.mts +2 -1
- package/dist/dialog/dialog-trigger.d.mts.map +1 -1
- package/dist/dialog/dialog-trigger.mjs +14 -9
- package/dist/dialog/dialog-trigger.mjs.map +1 -1
- package/dist/docs/components/accordion.md +2 -0
- package/dist/docs/components/alert-dialog.md +546 -0
- package/dist/docs/components/collapsible.md +408 -0
- package/dist/docs/components/combobox.md +288 -26
- package/dist/docs/components/dialog.md +7 -5
- package/dist/docs/components/field.md +180 -3
- package/dist/docs/components/hover-card.md +494 -0
- package/dist/docs/components/multi-select.md +622 -0
- package/dist/docs/components/number-field.md +1 -1
- package/dist/docs/components/popover.md +4 -4
- package/dist/docs/components/select.md +9 -1
- package/dist/docs/components/separator.md +182 -0
- package/dist/docs/components/slider.md +1 -1
- package/dist/docs/components/tooltip.md +1 -1
- package/dist/docs/concepts/experimental.md +3 -3
- package/dist/docs/concepts/server-components.md +1 -1
- package/dist/docs/index.md +7 -2
- package/dist/docs/roadmap.md +8 -0
- package/dist/field/field-control.d.mts +13 -4
- package/dist/field/field-control.d.mts.map +1 -1
- package/dist/field/field-control.mjs +39 -10
- package/dist/field/field-control.mjs.map +1 -1
- package/dist/field/field-label.d.mts +6 -2
- package/dist/field/field-label.d.mts.map +1 -1
- package/dist/field/field-label.mjs +12 -2
- package/dist/field/field-label.mjs.map +1 -1
- package/dist/field/field-root.mjs +9 -1
- package/dist/field/field-root.mjs.map +1 -1
- package/dist/hover-card/hover-card-context.mjs +13 -0
- package/dist/hover-card/hover-card-context.mjs.map +1 -0
- package/dist/hover-card/hover-card-popup.d.mts +102 -0
- package/dist/hover-card/hover-card-popup.d.mts.map +1 -0
- package/dist/hover-card/hover-card-popup.mjs +80 -0
- package/dist/hover-card/hover-card-popup.mjs.map +1 -0
- package/dist/hover-card/hover-card-root.d.mts +94 -0
- package/dist/hover-card/hover-card-root.d.mts.map +1 -0
- package/dist/hover-card/hover-card-root.mjs +74 -0
- package/dist/hover-card/hover-card-root.mjs.map +1 -0
- package/dist/hover-card/hover-card-trigger.d.mts +65 -0
- package/dist/hover-card/hover-card-trigger.d.mts.map +1 -0
- package/dist/hover-card/hover-card-trigger.mjs +109 -0
- package/dist/hover-card/hover-card-trigger.mjs.map +1 -0
- package/dist/hover-card/index.d.mts +24 -0
- package/dist/hover-card/index.d.mts.map +1 -0
- package/dist/hover-card/index.mjs +24 -0
- package/dist/hover-card/index.mjs.map +1 -0
- package/dist/hover-card/use-hover-card-interest.mjs +242 -0
- package/dist/hover-card/use-hover-card-interest.mjs.map +1 -0
- package/dist/index.d.mts +38 -9
- package/dist/index.mjs +6 -1
- package/dist/internal/action/use-multiple-selection.mjs +83 -0
- package/dist/internal/action/use-multiple-selection.mjs.map +1 -0
- package/dist/internal/details-summary.mjs +56 -0
- package/dist/internal/details-summary.mjs.map +1 -0
- package/dist/internal/form/field-context.mjs.map +1 -1
- package/dist/internal/form/hidden-values.mjs +29 -0
- package/dist/internal/form/hidden-values.mjs.map +1 -0
- package/dist/internal/form/validity.mjs +3 -1
- package/dist/internal/form/validity.mjs.map +1 -1
- package/dist/internal/overlay/use-interest-dismiss.mjs +2 -1
- package/dist/internal/overlay/use-interest-dismiss.mjs.map +1 -1
- package/dist/internal/overlay/use-overlay-close.mjs +5 -3
- package/dist/internal/overlay/use-overlay-close.mjs.map +1 -1
- package/dist/internal/overlay/use-overlay-root.mjs +4 -1
- package/dist/internal/overlay/use-overlay-root.mjs.map +1 -1
- package/dist/internal/overlay/use-popover-popup.mjs +20 -7
- package/dist/internal/overlay/use-popover-popup.mjs.map +1 -1
- package/dist/internal/overlay/use-popover-trigger.d.mts +1 -1
- package/dist/internal/typeahead.mjs +52 -0
- package/dist/internal/typeahead.mjs.map +1 -0
- package/dist/{combobox → internal}/use-active-option.mjs +8 -6
- package/dist/internal/use-active-option.mjs.map +1 -0
- package/dist/{combobox → internal}/use-deferred-leave.mjs +20 -12
- package/dist/internal/use-deferred-leave.mjs.map +1 -0
- package/dist/internal/use-name-warning.mjs +57 -1
- package/dist/internal/use-name-warning.mjs.map +1 -1
- package/dist/internal/use-option-registry.mjs +70 -0
- package/dist/internal/use-option-registry.mjs.map +1 -0
- package/dist/menu/use-menu-keyboard.mjs +7 -27
- package/dist/menu/use-menu-keyboard.mjs.map +1 -1
- package/dist/multi-select/index.d.mts +37 -0
- package/dist/multi-select/index.d.mts.map +1 -0
- package/dist/multi-select/index.mjs +37 -0
- package/dist/multi-select/index.mjs.map +1 -0
- package/dist/multi-select/multi-select-context.mjs +19 -0
- package/dist/multi-select/multi-select-context.mjs.map +1 -0
- package/dist/multi-select/multi-select-group.d.mts +120 -0
- package/dist/multi-select/multi-select-group.d.mts.map +1 -0
- package/dist/multi-select/multi-select-group.mjs +111 -0
- package/dist/multi-select/multi-select-group.mjs.map +1 -0
- package/dist/multi-select/multi-select-keyboard.mjs +145 -0
- package/dist/multi-select/multi-select-keyboard.mjs.map +1 -0
- package/dist/multi-select/multi-select-label.d.mts +42 -0
- package/dist/multi-select/multi-select-label.d.mts.map +1 -0
- package/dist/multi-select/multi-select-label.mjs +41 -0
- package/dist/multi-select/multi-select-label.mjs.map +1 -0
- package/dist/multi-select/multi-select-list.d.mts +47 -0
- package/dist/multi-select/multi-select-list.d.mts.map +1 -0
- package/dist/multi-select/multi-select-list.mjs +65 -0
- package/dist/multi-select/multi-select-list.mjs.map +1 -0
- package/dist/multi-select/multi-select-option.d.mts +68 -0
- package/dist/multi-select/multi-select-option.d.mts.map +1 -0
- package/dist/multi-select/multi-select-option.mjs +93 -0
- package/dist/multi-select/multi-select-option.mjs.map +1 -0
- package/dist/multi-select/multi-select-popup.d.mts +58 -0
- package/dist/multi-select/multi-select-popup.d.mts.map +1 -0
- package/dist/multi-select/multi-select-popup.mjs +71 -0
- package/dist/multi-select/multi-select-popup.mjs.map +1 -0
- package/dist/multi-select/multi-select-root.d.mts +125 -0
- package/dist/multi-select/multi-select-root.d.mts.map +1 -0
- package/dist/multi-select/multi-select-root.mjs +257 -0
- package/dist/multi-select/multi-select-root.mjs.map +1 -0
- package/dist/multi-select/multi-select-trigger.d.mts +66 -0
- package/dist/multi-select/multi-select-trigger.d.mts.map +1 -0
- package/dist/multi-select/multi-select-trigger.mjs +169 -0
- package/dist/multi-select/multi-select-trigger.mjs.map +1 -0
- package/dist/multi-select/multi-select-value.d.mts +57 -0
- package/dist/multi-select/multi-select-value.d.mts.map +1 -0
- package/dist/multi-select/multi-select-value.mjs +41 -0
- package/dist/multi-select/multi-select-value.mjs.map +1 -0
- package/dist/number-field/number-field-root.mjs +1 -1
- package/dist/number-field/use-step-button.d.mts.map +1 -1
- package/dist/number-field/use-step-button.mjs +11 -2
- package/dist/number-field/use-step-button.mjs.map +1 -1
- package/dist/popover/popover-close.d.mts +6 -3
- package/dist/popover/popover-close.d.mts.map +1 -1
- package/dist/popover/popover-close.mjs +3 -2
- package/dist/popover/popover-close.mjs.map +1 -1
- package/dist/popover/popover-popup.mjs +1 -1
- package/dist/select/select-root.d.mts +1 -1
- package/dist/select/select-root.d.mts.map +1 -1
- package/dist/select/select-root.mjs +5 -1
- package/dist/select/select-root.mjs.map +1 -1
- package/dist/separator/index.d.mts +3 -0
- package/dist/separator/index.mjs +1 -0
- package/dist/separator/separator.d.mts +58 -0
- package/dist/separator/separator.d.mts.map +1 -0
- package/dist/separator/separator.mjs +41 -0
- package/dist/separator/separator.mjs.map +1 -0
- package/dist/slider/slider-root.mjs +1 -0
- package/dist/slider/slider-root.mjs.map +1 -1
- package/dist/toast/toast-viewport.mjs +1 -1
- package/package.json +6 -1
- package/skills/funstack-ui-knowledge/SKILL.md +14 -9
- package/dist/combobox/use-active-option.mjs.map +0 -1
- package/dist/combobox/use-deferred-leave.mjs.map +0 -1
- package/dist/combobox/use-option-registry.mjs +0 -52
- package/dist/combobox/use-option-registry.mjs.map +0 -1
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
# AlertDialog
|
|
2
|
+
|
|
3
|
+
An alert dialog interrupts the user with a question that needs an answer: "Delete this project?". It is a [Dialog](dialog.md) with three things fixed: the `alertdialog` role, no light dismiss (a stray click on the backdrop does nothing), and the initial focus on `AlertDialog.Cancel`, the answer that destroys nothing. `AlertDialog.Action` is the asynchronous half: it runs your action in its own transition, shows `data-pending` on itself, closes the dialog when the action resolves and leaves it open when it rejects.
|
|
4
|
+
|
|
5
|
+
```tsx title="AlertDialogDeleteProject.tsx"
|
|
6
|
+
"use client";
|
|
7
|
+
import { startTransition, useState } from "react";
|
|
8
|
+
import { AlertDialog } from "@funstack/ui/alert-dialog";
|
|
9
|
+
import { DemoErrorBoundary } from "./DemoErrorBoundary.js";
|
|
10
|
+
import { fakeSave } from "./fake-api.js";
|
|
11
|
+
|
|
12
|
+
// The confirmed work is the Action's `action`: it runs in the Delete button's own transition
|
|
13
|
+
// (data-pending, "Deleting…"), and the dialog closes only when it resolves. With "Make the
|
|
14
|
+
// server fail" checked it rejects: the Error Boundary inside the dialog shows the error and the
|
|
15
|
+
// dialog stays open (Reset brings the buttons back; Escape still leaves). The dialog opens with
|
|
16
|
+
// focus on Cancel, the answer that destroys nothing, and a click on the backdrop does nothing.
|
|
17
|
+
export function AlertDialogDeleteProject() {
|
|
18
|
+
const [deleted, setDeleted] = useState(false);
|
|
19
|
+
const [fail, setFail] = useState(false);
|
|
20
|
+
|
|
21
|
+
if (deleted) {
|
|
22
|
+
return (
|
|
23
|
+
<div className="demo-row">
|
|
24
|
+
<output className="demo-readout">Project "Atlas" deleted.</output>
|
|
25
|
+
{/* The dialog and its Trigger are gone: put focus on the next thing to do. */}
|
|
26
|
+
<button type="button" className="demo-button" autoFocus onClick={() => setDeleted(false)}>
|
|
27
|
+
Restore
|
|
28
|
+
</button>
|
|
29
|
+
</div>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<div className="demo-row">
|
|
35
|
+
<AlertDialog.Root defaultOpen={false}>
|
|
36
|
+
<AlertDialog.Trigger className="demo-button demo-danger">
|
|
37
|
+
Delete project…
|
|
38
|
+
</AlertDialog.Trigger>
|
|
39
|
+
<AlertDialog.Popup className="demo-dialog">
|
|
40
|
+
<AlertDialog.Title>Delete "Atlas"?</AlertDialog.Title>
|
|
41
|
+
<AlertDialog.Description>
|
|
42
|
+
The project and its 12 documents are removed for everyone. This cannot be undone.
|
|
43
|
+
</AlertDialog.Description>
|
|
44
|
+
<DemoErrorBoundary>
|
|
45
|
+
<div className="demo-dialog-actions">
|
|
46
|
+
<AlertDialog.Cancel className="demo-button">Cancel</AlertDialog.Cancel>
|
|
47
|
+
<AlertDialog.Action
|
|
48
|
+
className="demo-button demo-danger"
|
|
49
|
+
action={async () => {
|
|
50
|
+
await fakeSave('"Atlas"', { ms: 1000, fail });
|
|
51
|
+
// After an await: wrap the update yourself (React limitation).
|
|
52
|
+
startTransition(() => setDeleted(true));
|
|
53
|
+
}}
|
|
54
|
+
>
|
|
55
|
+
{({ pending }) => (
|
|
56
|
+
<>
|
|
57
|
+
{pending && <span className="demo-spinner" aria-hidden />}
|
|
58
|
+
{pending ? "Deleting…" : "Delete"}
|
|
59
|
+
</>
|
|
60
|
+
)}
|
|
61
|
+
</AlertDialog.Action>
|
|
62
|
+
</div>
|
|
63
|
+
</DemoErrorBoundary>
|
|
64
|
+
</AlertDialog.Popup>
|
|
65
|
+
</AlertDialog.Root>
|
|
66
|
+
<label className="demo-label">
|
|
67
|
+
<input type="checkbox" checked={fail} onChange={(e) => setFail(e.target.checked)} /> Make
|
|
68
|
+
the server fail
|
|
69
|
+
</label>
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```css title="dialog.css"
|
|
76
|
+
/* Dialog: the Popup is the <dialog> itself. Open state is `:open` / `:modal` on it (exact in
|
|
77
|
+
both modes); the Trigger carries `data-open` / `data-pending` in library mode only. */
|
|
78
|
+
|
|
79
|
+
/* CSS-only scroll lock, keyed on :modal: covers dialogs opened before hydration and nested ones. */
|
|
80
|
+
html:has(.demo-dialog:modal) {
|
|
81
|
+
overflow: hidden;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.demo-dialog {
|
|
85
|
+
inline-size: min(28rem, calc(100vw - 32px));
|
|
86
|
+
padding: 20px 24px;
|
|
87
|
+
border: 1px solid var(--color-border);
|
|
88
|
+
border-radius: 10px;
|
|
89
|
+
background: var(--color-bg);
|
|
90
|
+
color: var(--color-text);
|
|
91
|
+
box-shadow: 0 12px 40px rgb(0 0 0 / 0.3);
|
|
92
|
+
overscroll-behavior: contain;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.demo-dialog::backdrop {
|
|
96
|
+
background: rgb(0 0 0 / 0.45);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.demo-dialog h2 {
|
|
100
|
+
margin: 0 0 8px;
|
|
101
|
+
padding: 0;
|
|
102
|
+
border: 0;
|
|
103
|
+
font-size: 1.2rem;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.demo-dialog p {
|
|
107
|
+
margin: 0 0 12px;
|
|
108
|
+
color: var(--color-text-secondary);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* Enter and exit with CSS, both modes. Enter works everywhere; the exit animates in Chromium
|
|
112
|
+
(with `overlay`) and Safari and is instant in Firefox. */
|
|
113
|
+
.demo-dialog,
|
|
114
|
+
.demo-dialog::backdrop {
|
|
115
|
+
opacity: 0;
|
|
116
|
+
transition:
|
|
117
|
+
opacity 150ms,
|
|
118
|
+
translate 150ms,
|
|
119
|
+
display 150ms allow-discrete,
|
|
120
|
+
overlay 150ms allow-discrete;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
.demo-dialog {
|
|
124
|
+
translate: 0 8px;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
.demo-dialog:open,
|
|
128
|
+
.demo-dialog:open::backdrop {
|
|
129
|
+
opacity: 1;
|
|
130
|
+
translate: 0 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
@starting-style {
|
|
134
|
+
.demo-dialog:open,
|
|
135
|
+
.demo-dialog:open::backdrop {
|
|
136
|
+
opacity: 0;
|
|
137
|
+
}
|
|
138
|
+
.demo-dialog:open {
|
|
139
|
+
translate: 0 8px;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* With `transition`, a view transition animates the library-driven opens and closes (a Close
|
|
144
|
+
part, a Form, a controlled close), Firefox included; dismissals (Escape, a click on the
|
|
145
|
+
backdrop) are the browser's and keep the CSS exit above.
|
|
146
|
+
The library keeps the two apart; no extra rule is needed. */
|
|
147
|
+
|
|
148
|
+
::view-transition-new(.demo-dialog-pop) {
|
|
149
|
+
animation: 180ms ease-out both demo-dialog-pop-in;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
::view-transition-old(.demo-dialog-pop) {
|
|
153
|
+
animation: 140ms ease-in both demo-dialog-pop-out;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
@keyframes demo-dialog-pop-in {
|
|
157
|
+
from {
|
|
158
|
+
opacity: 0;
|
|
159
|
+
scale: 0.96;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
@keyframes demo-dialog-pop-out {
|
|
164
|
+
to {
|
|
165
|
+
opacity: 0;
|
|
166
|
+
scale: 0.96;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
@media (prefers-reduced-motion: reduce) {
|
|
171
|
+
.demo-dialog,
|
|
172
|
+
.demo-dialog::backdrop {
|
|
173
|
+
transition: none;
|
|
174
|
+
}
|
|
175
|
+
::view-transition-group(*),
|
|
176
|
+
::view-transition-old(*),
|
|
177
|
+
::view-transition-new(*) {
|
|
178
|
+
animation: none !important;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.demo-dialog-actions {
|
|
183
|
+
display: flex;
|
|
184
|
+
justify-content: flex-end;
|
|
185
|
+
gap: 8px;
|
|
186
|
+
margin-top: 16px;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
.demo-dialog-form label {
|
|
190
|
+
display: grid;
|
|
191
|
+
gap: 4px;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
.demo-dialog-form input {
|
|
195
|
+
padding: 0.35em 0.6em;
|
|
196
|
+
border: 1px solid var(--color-border);
|
|
197
|
+
border-radius: 6px;
|
|
198
|
+
background: var(--color-bg);
|
|
199
|
+
color: var(--color-text);
|
|
200
|
+
font: inherit;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.demo-dialog-details {
|
|
204
|
+
display: grid;
|
|
205
|
+
grid-template-columns: auto 1fr;
|
|
206
|
+
gap: 4px 16px;
|
|
207
|
+
margin: 0;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
.demo-dialog-details dd {
|
|
211
|
+
margin: 0;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* The Trigger in library mode: optimistic open and pending. */
|
|
215
|
+
.demo-button[data-open] {
|
|
216
|
+
border-color: var(--color-primary);
|
|
217
|
+
color: var(--color-primary);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
.demo-button[data-pending] {
|
|
221
|
+
cursor: progress;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
ul.demo-list {
|
|
225
|
+
margin: 0 0 12px;
|
|
226
|
+
padding: 0;
|
|
227
|
+
list-style: none;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
ul.demo-list .demo-row {
|
|
231
|
+
justify-content: space-between;
|
|
232
|
+
max-inline-size: 20rem;
|
|
233
|
+
padding-block: 4px;
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Anatomy
|
|
238
|
+
|
|
239
|
+
```tsx
|
|
240
|
+
import { AlertDialog } from "@funstack/ui/alert-dialog";
|
|
241
|
+
|
|
242
|
+
<AlertDialog.Root> {/* no DOM: ids, mode, state */}
|
|
243
|
+
<AlertDialog.Trigger>Delete project</AlertDialog.Trigger>
|
|
244
|
+
<AlertDialog.Popup> {/* dialog[role=alertdialog] */}
|
|
245
|
+
<AlertDialog.Title>Delete "Atlas"?</AlertDialog.Title>
|
|
246
|
+
<AlertDialog.Description>This cannot be undone.</AlertDialog.Description>
|
|
247
|
+
<AlertDialog.Cancel>Cancel</AlertDialog.Cancel> {/* focused on open */}
|
|
248
|
+
<AlertDialog.Action action={() => deleteProject(id)}>
|
|
249
|
+
Delete {/* pending, then closes */}
|
|
250
|
+
</AlertDialog.Action>
|
|
251
|
+
</AlertDialog.Popup>
|
|
252
|
+
</AlertDialog.Root>
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
- **Every part is a Dialog part.** Root, Trigger, Popup, Title, Description and Form run the code of the Dialog parts of the same name; Cancel and Action run `Dialog.Close`'s. Everything the [Dialog](dialog.md) page says applies unless this page says otherwise: the two modes, focus return, exit animations, `keepMounted`, `transition`, `render`.
|
|
256
|
+
- **What is not there.** No `modal` prop (always `showModal()`), no `lightDismiss` or `experimental_closedBy`, and `role` is owned. There is no Close part: it is `Cancel`.
|
|
257
|
+
- **A Description is expected.** A screen reader announces an alert dialog by its name and description, so a Popup without an `AlertDialog.Description` (or an `aria-describedby` of yours) is a development warning (logged when its first showing ends), as a Popup without a Title is.
|
|
258
|
+
|
|
259
|
+
## How it works
|
|
260
|
+
|
|
261
|
+
| | AlertDialog |
|
|
262
|
+
| - | - |
|
|
263
|
+
| Role | `role="alertdialog"` on the `<dialog>`; `aria-modal` from `showModal()` |
|
|
264
|
+
| Backdrop click | Does nothing, in every engine and before hydration: a modal `<dialog>` without `closedby` does not light-dismiss |
|
|
265
|
+
| Escape, Android back | Closes the dialog: the browser's close request, a fact reported to `onOpenChange(false, { reason: "dismiss", returnValue: "" })`. It means "cancel"; there is no prop to turn it off |
|
|
266
|
+
| Initial focus | The first enabled `AlertDialog.Cancel`; `initialFocus` on the Popup overrides it; without a Cancel the platform chooses (the first focusable element) |
|
|
267
|
+
| Cancel | `Dialog.Close` under another name: a close request (`reason: "activate"`, `returnValue` its `value`, `""` by default) |
|
|
268
|
+
| Action | `Dialog.Close` under another name, usually with an `action`; with one it renders no `commandfor` / `command` (as every Close part), so a click before hydration does nothing |
|
|
269
|
+
| Modes | Dialog's: JS-free without open-state props on the Root, library mode with any |
|
|
270
|
+
|
|
271
|
+
### Why Escape closes it
|
|
272
|
+
|
|
273
|
+
The WAI-ARIA alert dialog pattern keeps Escape, as every modal dialog does, and keyboard and screen-reader users rely on it. The one mechanism that would block it, `closedby="none"`, is missing in Safari and cannot be emulated there (the browser lets a page cancel a close request once per user activation, then closes anyway). So write the dialog so that a dismissal is a valid answer: Escape means Cancel, and the destructive step happens only in the Action's `action`. A dialog that must come back (an expired session) is controlled: a parent that keeps `open` after `onOpenChange(false)` gets it shown again at once.
|
|
274
|
+
|
|
275
|
+
### Where the confirmed work goes
|
|
276
|
+
|
|
277
|
+
Put it in the Action's `action`, not in `openChangeAction`. The action is where the pending state is on the button the user pressed, where a rejection keeps the dialog open, and where no dismissal can be mistaken for consent. An action that catches its own error resolves, and a resolved action closes the dialog: to keep the dialog open on a failure, let the error reach an Error Boundary inside the Popup (as in the example above), or use the [Form](#forms) with `Form.Root` for field errors.
|
|
278
|
+
|
|
279
|
+
> **A pending confirmation can be dismissed**
|
|
280
|
+
>
|
|
281
|
+
> Dismissing a pending confirmation hides it; it does not abort it. Escape closes the dialog while the action runs, and the action finishes in the background. In library mode a Cancel click meanwhile is a close request that React entangles with the running action: it is held until the action settles, and nothing near the Cancel shows it (the Trigger's `data-pending` is under the backdrop), so the click looks ignored. When the work can take more than a moment, disable Cancel while it runs with your own optimistic state: see [Cancel unavailable while the confirm runs](#cancel-while-pending).
|
|
282
|
+
|
|
283
|
+
## Props
|
|
284
|
+
|
|
285
|
+
### `AlertDialog.Root`
|
|
286
|
+
|
|
287
|
+
| Prop | Type | Default | Description |
|
|
288
|
+
| - | - | - | - |
|
|
289
|
+
| `open` | `boolean` | – | Controlled open state. undefined means uncontrolled. Selects library mode. Pass `onOpenChange` too: Escape closes the dialog without asking (development warns). |
|
|
290
|
+
| `defaultOpen` | `boolean` | – | Initial open state in uncontrolled library mode. `defaultOpen={false}` selects library mode without an action. |
|
|
291
|
+
| `openChangeAction` | `ChangeAction<boolean, AlertDialogOpenChangeDetails>` | – | Requests only: the Trigger (`true`), a Cancel, an Action, a Form submit or settle, a foreign close command (`false`, with `details.returnValue` the part's `value`). Awaited; the dialog opens or closes when it resolves; a controlled parent that keeps `open` vetoes a close. Never called for Escape. Runs in the Root's transition; may be a Server Function; rejections reach the nearest Error Boundary above the Root. Selects library mode. |
|
|
292
|
+
| `onOpenChange` | `UrgentChangeHandler<boolean, AlertDialogOpenChangeDetails>` | – | Facts only: Escape, Android back, an assistive-technology dismiss, a script `close()` / `requestClose()`. Always `false`, with `reason: "dismiss"` and `returnValue` as the browser left it (`""` for Escape). Called synchronously, outside any transition. In controlled mode set state directly here (`onOpenChange={setOpen}`). Selects library mode. |
|
|
293
|
+
| `children` | `ReactNode \| ((state: AlertDialogRootState) => ReactNode)` | – | The parts. The function form receives the optimistic state and must not suspend. |
|
|
294
|
+
|
|
295
|
+
- **State**: `AlertDialogRootState` is `{ open, pending, modal }` (Dialog's; `modal` is always `true`).
|
|
296
|
+
- **Owned**: nothing rendered: the Root has no element.
|
|
297
|
+
|
|
298
|
+
There is no `modal` prop: an alert dialog is always modal.
|
|
299
|
+
|
|
300
|
+
### `AlertDialog.Trigger`
|
|
301
|
+
|
|
302
|
+
`Dialog.Trigger`, unchanged: `<button command="show-modal">` in JS-free mode, a request through the Root with `data-open` and `data-pending` in library mode. Props: `disabled`, `className`, `style`, `children` (functions of `AlertDialogTriggerState`, `{ open, pending, disabled }`), `render`. See [Dialog.Trigger](dialog.md#props).
|
|
303
|
+
|
|
304
|
+
### `AlertDialog.Popup`
|
|
305
|
+
|
|
306
|
+
| Prop | Type | Default | Description |
|
|
307
|
+
| - | - | - | - |
|
|
308
|
+
| `initialFocus` | `RefObject<HTMLElement \| null> \| string` | `the first enabled Cancel` | The element to focus on open: a ref, or a selector resolved inside the dialog. With it, no Cancel is focused. For a long message, point it at the Title with `tabIndex={-1}` so the text is read from the top. Without a Cancel and without `initialFocus`, the platform focuses the first focusable element. |
|
|
309
|
+
| `keepMounted` | `boolean` | `false` | Library mode: keep the children in `<Activity mode="hidden">` while closed instead of unmounting them. Ignored in JS-free mode (always mounted). |
|
|
310
|
+
| `transition` | `ViewTransitionClass` | – | Library mode only. Forwarded to the `<ViewTransition>` around the dialog: animates the open and every library-driven close (Cancel, Action, Form). Escape animates with CSS only. The transition types are Dialog's: `fsui-dialog`, `fsui-dialog-open`, `fsui-dialog-close`. |
|
|
311
|
+
| `className` | `string \| ((state: AlertDialogPopupState) => string \| undefined)` | – | A class name, or a function of the part's state. |
|
|
312
|
+
| `style` | `CSSProperties \| ((state: AlertDialogPopupState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
|
|
313
|
+
| `children` | `ReactNode \| ((state: AlertDialogPopupState) => ReactNode)` | – | Content, or a function of the part's state. |
|
|
314
|
+
| `render` | `ReactElement \| ((props, state: AlertDialogPopupState) => ReactNode)` | – | Replace the default \<dialog> (element form) or take over rendering (function form). |
|
|
315
|
+
|
|
316
|
+
- **State**: `AlertDialogPopupState` is `{ open, modal }`; `open` is committed (`false` in JS-free mode; `:open` is the DOM truth).
|
|
317
|
+
- **Owned**: `role` (`alertdialog`), `id`, `open`, `closedby` (never rendered), `data-modal`, `suppressHydrationWarning`; `aria-labelledby` and `aria-describedby` are merged with yours.
|
|
318
|
+
|
|
319
|
+
`onCancel` and the other `<dialog>` props pass through.
|
|
320
|
+
|
|
321
|
+
### `AlertDialog.Cancel`
|
|
322
|
+
|
|
323
|
+
| Prop | Type | Default | Description |
|
|
324
|
+
| - | - | - | - |
|
|
325
|
+
| `value` | `string` | `""` | Becomes `dialog.returnValue` and `details.returnValue` of `openChangeAction(false)`. The default is what Escape leaves, so both read as "nothing was confirmed". |
|
|
326
|
+
| `disabled` | `boolean` | `false` | Native `disabled`; owned. A disabled Cancel is not the focus target: the next enabled one is. |
|
|
327
|
+
| `action` | `Action<[details: AlertDialogCancelDetails]>` | – | Rarely needed. As on `Dialog.Close`: runs in this button's own transition, then closes the dialog; a rejection leaves it open. |
|
|
328
|
+
| `whilePending` | `"run" \| "ignore"` | `"ignore"` | As on `Dialog.Close`. No effect without `action`. |
|
|
329
|
+
| `className` | `string \| ((state: AlertDialogCancelState) => string \| undefined)` | – | A class name, or a function of the part's state. |
|
|
330
|
+
| `style` | `CSSProperties \| ((state: AlertDialogCancelState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
|
|
331
|
+
| `children` | `ReactNode \| ((state: AlertDialogCancelState) => ReactNode)` | – | Content, or a function of the part's state. |
|
|
332
|
+
| `render` | `ReactElement \| ((props, state: AlertDialogCancelState) => ReactNode)` | – | Replace the default \<button> (element form) or take over rendering (function form). |
|
|
333
|
+
|
|
334
|
+
- **State**: `AlertDialogCancelState` is `{ pending, disabled }`.
|
|
335
|
+
- **Owned**: `Dialog.Close`'s (`type`, `disabled`, `aria-disabled`, `commandfor`, `command`, `value`, `onClick` in library mode or with `action`, `data-pending`, `data-disabled`), and `autoFocus` in the server and hydration render while it is the focus target in JS-free mode (a Cancel mounted on the client into an open dialog carries the attribute but takes no focus).
|
|
336
|
+
|
|
337
|
+
JS-free mode: `<button command="request-close" autofocus>`, in the server HTML too, so the focus is right before hydration. Library mode: no attribute; the Popup writes `autofocus` on the first enabled Cancel right before `showModal()` and removes it after.
|
|
338
|
+
|
|
339
|
+
### `AlertDialog.Action`
|
|
340
|
+
|
|
341
|
+
| Prop | Type | Default | Description |
|
|
342
|
+
| - | - | - | - |
|
|
343
|
+
| `action` | `Action<[details: AlertDialogActionDetails]>` | – | The confirmed work. Runs in this button's own transition (`data-pending` on it), then closes the dialog with `value` as `returnValue`: in library mode through the Root (`openChangeAction(false)` awaited, view transition if set), in JS-free mode with `close(value)`. A rejection reaches the nearest Error Boundary above the button and leaves the dialog open. With `action` the button renders no `commandfor` / `command`, so a click before hydration does nothing instead of closing the dialog as if the work had been done. Without `action` it only closes the dialog (a native `request-close` invoker in JS-free mode). `details.returnValue` is the `value`. |
|
|
344
|
+
| `whilePending` | `"run" \| "ignore"` | `"ignore"` | `"ignore"`: a second click, Enter or Space while the action runs is cancelled, and the button is `aria-disabled="true"` meanwhile. `"run"`: the action runs again; the dialog closes once, after the last run, if the latest resolved. |
|
|
345
|
+
| `value` | `string` | `""` | Becomes `dialog.returnValue` and `details.returnValue`. |
|
|
346
|
+
| `disabled` | `boolean` | `false` | Never runs or closes. Native `disabled`; owned. |
|
|
347
|
+
| `className` | `string \| ((state: AlertDialogActionState) => string \| undefined)` | – | A class name, or a function of the part's state. |
|
|
348
|
+
| `style` | `CSSProperties \| ((state: AlertDialogActionState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
|
|
349
|
+
| `children` | `ReactNode \| ((state: AlertDialogActionState) => ReactNode)` | – | Content, or a function of the state: `{({ pending }) => pending ? "Deleting…" : "Delete"}`. |
|
|
350
|
+
| `render` | `ReactElement \| ((props, state: AlertDialogActionState) => ReactNode)` | – | Replace the default \<button> (element form) or take over rendering (function form). |
|
|
351
|
+
|
|
352
|
+
- **State**: `AlertDialogActionState` is `{ pending, disabled }`.
|
|
353
|
+
- **Owned**: `type` (on a `<button>` only), `disabled`, `aria-disabled`, `commandfor` and `command` (none with `action`), `value`, `onClick` (library mode, or with `action`), `data-pending`, `data-disabled`.
|
|
354
|
+
|
|
355
|
+
`data-pending` spans the action and the close that follows. `AlertDialog.Action type="submit"` is not supported (`type` is owned): inside an `AlertDialog.Form`, confirm with a plain submit button.
|
|
356
|
+
|
|
357
|
+
### `AlertDialog.Form`, `AlertDialog.Title`, `AlertDialog.Description`
|
|
358
|
+
|
|
359
|
+
`Dialog.Form`, `Dialog.Title` and `Dialog.Description`, unchanged: the same props (`action`, `closeOnSettle`, `method` on the Form; `render` to change the heading level on the Title), the same state types (`AlertDialogFormState` is `{ pending }`; Title and Description get the Popup's) and the same owned keys (`action` and `method`; `id`). See [the Dialog props](dialog.md#props).
|
|
360
|
+
|
|
361
|
+
## Data attributes
|
|
362
|
+
|
|
363
|
+
| Hook | On | Meaning |
|
|
364
|
+
| - | - | - |
|
|
365
|
+
| `:open`, `:modal`, `::backdrop` | Popup | As on a Dialog. |
|
|
366
|
+
| `[role="alertdialog"]` | Popup | Always. |
|
|
367
|
+
| `data-modal` | Popup | Always present. |
|
|
368
|
+
| `data-open`, `data-pending` | Trigger | Library mode, as on a Dialog. |
|
|
369
|
+
| `data-pending` | Cancel, Action | Its `action` is in flight. |
|
|
370
|
+
| `aria-disabled` | Cancel, Action | `"true"` while `data-pending` with `whilePending="ignore"` (the default). |
|
|
371
|
+
| `data-disabled` | Trigger, Cancel, Action | The `disabled` prop. |
|
|
372
|
+
|
|
373
|
+
One stylesheet styles a Dialog and an AlertDialog: the hooks and the transition types are the same. To tell them apart in CSS, select `[role="alertdialog"]`.
|
|
374
|
+
|
|
375
|
+
## Keyboard and ARIA
|
|
376
|
+
|
|
377
|
+
| Key | Behaviour | Who |
|
|
378
|
+
| - | - | - |
|
|
379
|
+
| Enter / Space on the Trigger | Opens; focus moves to the Cancel | the browser / Trigger |
|
|
380
|
+
| Tab / Shift+Tab | Cycles through the dialog; the page is inert | the browser |
|
|
381
|
+
| Escape | Closes and returns focus to the opener; `onOpenChange(false, { reason: "dismiss" })` | the browser |
|
|
382
|
+
| Enter / Space on Cancel | Closes | the browser (JS-free) or the part |
|
|
383
|
+
| Enter / Space on Action | Runs the `action`, then closes; ignored while pending by default | the part |
|
|
384
|
+
|
|
385
|
+
- The Popup is `role="alertdialog"` with `aria-modal` from `showModal()`, `aria-labelledby` pointing at the Title and `aria-describedby` at the Description. The Trigger is a plain button (no `aria-expanded`, no `aria-haspopup`).
|
|
386
|
+
- Development warnings, in addition to Dialog's: a Popup without a Description and without an `aria-describedby` of yours that names an element, once, when its first showing ends (a Description behind a Suspense boundary that resolves meanwhile counts).
|
|
387
|
+
|
|
388
|
+
## Transition types
|
|
389
|
+
|
|
390
|
+
Concept: [Transitions & ViewTransition](../concepts/transitions.md#transition-types)
|
|
391
|
+
|
|
392
|
+
Dialog's, not an AlertDialog set: library mode tags an open with `fsui-dialog` plus `fsui-dialog-open`, a Cancel, Action or Form close with `fsui-dialog` plus `fsui-dialog-close`; Escape is a fact and carries none. To tell an alert dialog's open from a dialog's in a transition map, add your own type with `addTransitionType` in `openChangeAction`.
|
|
393
|
+
|
|
394
|
+
## Optimistic and pending
|
|
395
|
+
|
|
396
|
+
Concept: [Pending & optimistic state](../concepts/pending-and-optimistic.md)
|
|
397
|
+
|
|
398
|
+
The Action is a [pure trigger](../concepts/pending-and-optimistic.md#pure-trigger): `data-pending` and `state.pending` from the click until the close commits, `aria-disabled` while it ignores a second click (the default). From the moment its action resolves the close is the Root's: the Trigger shows `data-pending` too. The Cancel stays active while the Action is pending.
|
|
399
|
+
|
|
400
|
+
### Walkthrough (library mode)
|
|
401
|
+
|
|
402
|
+
1. Click Delete: the button gets `data-pending` and `aria-disabled="true"`; your action runs. A second click is ignored.
|
|
403
|
+
2. It resolves: the library asks the Root to close (`openChangeAction(false)`, `reason: "activate"`); when that settles the dialog closes, inside the view transition if the Popup has `transition`, and focus returns to the Trigger. If the action re-rendered something that suspends, the dialog stays open and the button pending until that content has committed: one transition.
|
|
404
|
+
3. It rejects: nothing is dispatched, the dialog stays open, focus stays on the button, the error reaches the nearest Error Boundary above it.
|
|
405
|
+
|
|
406
|
+
### <a id="cancel-while-pending"></a>Cancel unavailable while the confirm runs
|
|
407
|
+
|
|
408
|
+
```tsx
|
|
409
|
+
const [deleting, setDeleting] = useOptimistic(false);
|
|
410
|
+
|
|
411
|
+
<AlertDialog.Cancel disabled={deleting}>Cancel</AlertDialog.Cancel>
|
|
412
|
+
<AlertDialog.Action
|
|
413
|
+
action={async () => {
|
|
414
|
+
setDeleting(true); // optimistic: reverts by itself when the action settles
|
|
415
|
+
await deleteProject(project.id);
|
|
416
|
+
}}
|
|
417
|
+
>
|
|
418
|
+
Delete
|
|
419
|
+
</AlertDialog.Action>
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
Escape still closes the dialog.
|
|
423
|
+
|
|
424
|
+
## Suspense
|
|
425
|
+
|
|
426
|
+
Concept: [Suspense](../concepts/suspense.md)
|
|
427
|
+
|
|
428
|
+
As a Dialog's: content that suspends holds the open, the Trigger is pending, and the dialog opens complete with focus on the Cancel (the target is resolved right before `showModal()`). With a `<Suspense>` inside the Popup whose fallback has no Cancel, the dialog opens on the fallback and focus does not move when the Cancel arrives: keep the buttons outside that boundary.
|
|
429
|
+
|
|
430
|
+
## Browser-owned state
|
|
431
|
+
|
|
432
|
+
Concept: [Browser-owned state](../concepts/browser-owned-state.md)
|
|
433
|
+
|
|
434
|
+
JS-free mode: the browser owns everything. Library mode: the Trigger, Cancel, Action and Form are requests (`openChangeAction`); Escape is the browser's, a fact: the dialog closes at once and `onOpenChange(false, { reason: "dismiss" })` is called. Cancel and Escape both mean "no" and arrive on different channels: pass the same function to both props to hear every "no".
|
|
435
|
+
|
|
436
|
+
## Before hydration
|
|
437
|
+
|
|
438
|
+
Concept: [Server Components](../concepts/server-components.md#before-hydration)
|
|
439
|
+
|
|
440
|
+
JS-free mode: the Trigger (`command="show-modal"`), the role and the wiring, the Cancel (`command="request-close"`, with `autofocus` in the server HTML), Escape, the backdrop that does not dismiss, and an `AlertDialog.Form` with a Server Function all work from a Server Component and before hydration. An Action with `action` needs hydration: before it the button does nothing. Library mode: the content is not in the server HTML.
|
|
441
|
+
|
|
442
|
+
```tsx title="AlertDialogServerConfirm.tsx"
|
|
443
|
+
// No "use client": this file is a Server Component. The AlertDialog.Root has none of `open`,
|
|
444
|
+
// `defaultOpen`, `openChangeAction` or `onOpenChange`, so it runs in JS-free mode: the Trigger is
|
|
445
|
+
// a native `command="show-modal"` invoker, Cancel is a `command="request-close"` button that
|
|
446
|
+
// carries `autofocus` in the server HTML, and Delete submits the AlertDialog.Form. The browser
|
|
447
|
+
// opens, focuses Cancel, closes on Escape (not on a backdrop click) and returns focus, before
|
|
448
|
+
// hydration and with JavaScript off.
|
|
449
|
+
//
|
|
450
|
+
// This site is static, so the form has no Server Function and closes the dialog
|
|
451
|
+
// (`method="dialog"`). In an app with a server, pass yours, `action={deleteProject}`, and keep
|
|
452
|
+
// everything else: the form then posts before hydration and runs the action after it.
|
|
453
|
+
import { AlertDialog } from "@funstack/ui/alert-dialog";
|
|
454
|
+
|
|
455
|
+
export function AlertDialogServerConfirm() {
|
|
456
|
+
return (
|
|
457
|
+
<AlertDialog.Root>
|
|
458
|
+
<AlertDialog.Trigger className="demo-button demo-danger">Delete project…</AlertDialog.Trigger>
|
|
459
|
+
<AlertDialog.Popup className="demo-dialog">
|
|
460
|
+
<AlertDialog.Title>Delete "Atlas"?</AlertDialog.Title>
|
|
461
|
+
<AlertDialog.Description>This cannot be undone.</AlertDialog.Description>
|
|
462
|
+
<AlertDialog.Form className="demo-dialog-actions">
|
|
463
|
+
<input type="hidden" name="id" value="atlas" />
|
|
464
|
+
<AlertDialog.Cancel className="demo-button">Cancel</AlertDialog.Cancel>
|
|
465
|
+
<button type="submit" className="demo-button demo-danger" value="delete">
|
|
466
|
+
Delete
|
|
467
|
+
</button>
|
|
468
|
+
</AlertDialog.Form>
|
|
469
|
+
</AlertDialog.Popup>
|
|
470
|
+
</AlertDialog.Root>
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
`dialog.css`: the same file as above.
|
|
476
|
+
|
|
477
|
+
This confirmation is a Server Component. The site is static, so its form only closes the dialog; with a server, pass a Server Function and nothing else changes:
|
|
478
|
+
|
|
479
|
+
```tsx
|
|
480
|
+
// A Server Component; deleteProject is a Server Function ("use server").
|
|
481
|
+
<AlertDialog.Root>
|
|
482
|
+
<AlertDialog.Trigger>Delete project</AlertDialog.Trigger>
|
|
483
|
+
<AlertDialog.Popup>
|
|
484
|
+
<AlertDialog.Title>Delete "{project.name}"?</AlertDialog.Title>
|
|
485
|
+
<AlertDialog.Description>This cannot be undone.</AlertDialog.Description>
|
|
486
|
+
<AlertDialog.Form action={deleteProject}>
|
|
487
|
+
<input type="hidden" name="id" value={project.id} />
|
|
488
|
+
<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
|
|
489
|
+
<button type="submit">Delete</button>
|
|
490
|
+
</AlertDialog.Form>
|
|
491
|
+
</AlertDialog.Popup>
|
|
492
|
+
</AlertDialog.Root>
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
| | \`AlertDialog.Form action\` + submit button | \`AlertDialog.Action action\` |
|
|
496
|
+
| - | - | - |
|
|
497
|
+
| Before hydration | the form posts to the Server Function; the page is rendered again | the button does nothing |
|
|
498
|
+
| After hydration | React runs the action; the dialog closes when its transition settles | the action runs in the button's transition, then the close |
|
|
499
|
+
| Pending | `useFormStatus()` in the submit button, `AlertDialogFormState.pending` | `data-pending` / `state.pending` on the Action |
|
|
500
|
+
| Rejection | Error Boundary; the dialog stays open | Error Boundary; the dialog stays open |
|
|
501
|
+
| Arguments | `FormData` (hidden inputs) | a closure |
|
|
502
|
+
|
|
503
|
+
## Forms
|
|
504
|
+
|
|
505
|
+
`AlertDialog.Form` is `Dialog.Form`: with a function `action` it closes the dialog once the action's transition settles without throwing; rendered as a `Form.Root` it also waits for a result without field errors, so a confirm that needs inline errors next to live buttons is a Form (see [Dialog: Forms](dialog.md#forms)). Confirm with a plain `<button type="submit">` inside it, not an `AlertDialog.Action`: the form is React's own channel, and an Action inside it would be a second owner of the same click.
|
|
506
|
+
|
|
507
|
+
## Recipes
|
|
508
|
+
|
|
509
|
+
### Controlled, opened from a menu
|
|
510
|
+
|
|
511
|
+
```tsx
|
|
512
|
+
const [confirming, setConfirming] = useState(false);
|
|
513
|
+
|
|
514
|
+
<Menu.Item action={() => setConfirming(true)}>Delete…</Menu.Item>
|
|
515
|
+
|
|
516
|
+
<AlertDialog.Root open={confirming} openChangeAction={setConfirming} onOpenChange={setConfirming}>
|
|
517
|
+
<AlertDialog.Popup>
|
|
518
|
+
<AlertDialog.Title>Delete 3 files?</AlertDialog.Title>
|
|
519
|
+
<AlertDialog.Description>They are moved to the trash.</AlertDialog.Description>
|
|
520
|
+
<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
|
|
521
|
+
<AlertDialog.Action action={moveToTrash}>Move to trash</AlertDialog.Action>
|
|
522
|
+
</AlertDialog.Popup>
|
|
523
|
+
</AlertDialog.Root>
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
### Confirm before closing an editor
|
|
527
|
+
|
|
528
|
+
The nested confirm of the [Dialog recipes](dialog.md#recipes), with the inner Root written as an `AlertDialog.Root`: "Keep editing" is its Cancel (focused on open), "Discard" its Action. Keep the inner Root JS-free so it opens synchronously in the click; no action awaits the user's answer.
|
|
529
|
+
|
|
530
|
+
### One place for every outcome
|
|
531
|
+
|
|
532
|
+
```tsx
|
|
533
|
+
<AlertDialog.Root
|
|
534
|
+
open={open}
|
|
535
|
+
openChangeAction={async (next, { returnValue }) => {
|
|
536
|
+
if (!next && returnValue === "discard") await discardDraft();
|
|
537
|
+
startTransition(() => setOpen(next));
|
|
538
|
+
}}
|
|
539
|
+
onOpenChange={setOpen}
|
|
540
|
+
>
|
|
541
|
+
…
|
|
542
|
+
<AlertDialog.Cancel>Keep editing</AlertDialog.Cancel>
|
|
543
|
+
<AlertDialog.Action value="discard">Discard</AlertDialog.Action>
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
Here the pending state shows on the Trigger, not on the Action, and a rejection goes to the boundary above the Root, which removes the dialog. Prefer the Action's `action`.
|