@uniflowed/ui 0.0.0-alpha.8 → 0.1.0
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/accordion.js +84 -57
- package/alert-dialog.js +284 -0
- package/alert.js +142 -0
- package/avatar.js +280 -0
- package/breadcrumb.js +138 -0
- package/calendar.js +560 -0
- package/carousel.js +410 -0
- package/checkbox.js +215 -31
- package/collapsible.js +72 -48
- package/color-picker.js +172 -0
- package/combobox.js +216 -39
- package/context-menu.js +215 -0
- package/date-field.js +9 -0
- package/date-picker.js +357 -0
- package/date-range-picker.js +120 -0
- package/dialog.js +235 -198
- package/drag-drop.js +125 -0
- package/drawer.js +504 -0
- package/field.js +260 -43
- package/grid-list.js +8 -0
- package/hover-card.js +334 -0
- package/i18n-provider.js +89 -0
- package/index.js +1254 -32
- package/input-otp.js +218 -0
- package/interactions.js +2327 -0
- package/internal/anchor.js +565 -0
- package/internal/collection.js +395 -0
- package/internal/date-grid.js +260 -0
- package/internal/date-range.js +26 -0
- package/internal/disclosure.js +201 -0
- package/internal/focus.js +64 -0
- package/internal/hover-intent.js +259 -0
- package/internal/menu-tree.js +228 -0
- package/internal/merge-props.js +117 -1
- package/internal/roving-focus.js +15 -4
- package/internal/segmented-field.js +316 -0
- package/list-box.js +13 -0
- package/menu.js +553 -361
- package/menubar.js +295 -0
- package/number-field.js +263 -0
- package/package.json +8 -25
- package/pagination.js +34 -22
- package/popover.js +367 -0
- package/progress.js +21 -16
- package/radio-group.js +81 -75
- package/range-calendar.js +78 -0
- package/resizable.js +155 -9
- package/scroll-area.js +283 -0
- package/select.js +83 -37
- package/separator.js +97 -0
- package/sheet.js +189 -0
- package/sidebar.js +320 -0
- package/skeleton.js +163 -0
- package/slider.js +95 -89
- package/switch.js +42 -34
- package/table.js +112 -71
- package/tabs.js +100 -91
- package/tag-group.js +8 -0
- package/time-field.js +8 -0
- package/toast.js +36 -66
- package/toggle-group.js +53 -49
- package/toggle.js +41 -27
- package/tooltip.js +404 -0
- package/tree.js +8 -0
package/dialog.js
CHANGED
|
@@ -31,6 +31,32 @@
|
|
|
31
31
|
// looks. The page behind is hidden by marking it inert rather than by moving
|
|
32
32
|
// the dialog out of it, which gets the same guarantee without the move.
|
|
33
33
|
//
|
|
34
|
+
// # Three things a caller decides, and why they are props rather than four
|
|
35
|
+
// components
|
|
36
|
+
//
|
|
37
|
+
// A modal dialog is one pattern with three places a *different* modal dialog
|
|
38
|
+
// differs, and each of the three fails silently when it is hard-coded:
|
|
39
|
+
//
|
|
40
|
+
// * **`role`** — `dialog` or `alertdialog`. The second tells a screen reader
|
|
41
|
+
// the dialog is interrupting to say something urgent and makes it announce
|
|
42
|
+
// the description immediately, which is the difference between "dialog,
|
|
43
|
+
// Delete this project?" and an alert the reader is expected to answer.
|
|
44
|
+
// * **`dismissOnOutsidePress`** — whether a press beside the dialog closes
|
|
45
|
+
// it. A confirmation that vanishes when the reader clicks slightly beside
|
|
46
|
+
// it, losing what they were about to confirm and saying nothing about which
|
|
47
|
+
// way it went, is the single behaviour an alert dialog exists to prevent.
|
|
48
|
+
// `Escape` keeps closing it either way: a modal a reader cannot leave from
|
|
49
|
+
// the keyboard is a trap, and declining an alert is what `Escape` means.
|
|
50
|
+
// * **`initialFocus`** — where focus lands. "The first thing worth acting on"
|
|
51
|
+
// is right for a dialog and wrong for a confirmation, where the APG puts
|
|
52
|
+
// focus on the *least* destructive action: Cancel, not Delete.
|
|
53
|
+
//
|
|
54
|
+
// They are three props on `Dialog.Body` and not a `variant` flag, because a
|
|
55
|
+
// flag is a name for a bundle and the bundles differ: `alert-dialog.js` sets
|
|
56
|
+
// all three, `sheet.js` sets none of them and adds an edge, `sidebar.js` is
|
|
57
|
+
// most often not modal at all. What each of those components adds is written
|
|
58
|
+
// where it lives; what they share is here, once.
|
|
59
|
+
//
|
|
34
60
|
// # Composition
|
|
35
61
|
//
|
|
36
62
|
// The parts are one namespace — `Dialog.Root`, `Dialog.Body`, `Dialog.Title` —
|
|
@@ -49,12 +75,34 @@ import {
|
|
|
49
75
|
useRef,
|
|
50
76
|
useState,
|
|
51
77
|
} from "@uniflowed/react";
|
|
78
|
+
import { useScrollLock } from "@uniflowed/hooks/browser";
|
|
52
79
|
import { useStableCallback } from "@uniflowed/hooks/lifecycle";
|
|
53
80
|
|
|
54
|
-
import
|
|
55
|
-
|
|
81
|
+
import { useInteractOutside } from "./interactions.js";
|
|
82
|
+
|
|
83
|
+
import type { PartEvent, RenderProp, Rest } from "./internal/merge-props.js";
|
|
84
|
+
import {
|
|
85
|
+
composeHandlers,
|
|
86
|
+
composeRefs,
|
|
87
|
+
withProps,
|
|
88
|
+
withoutComposed,
|
|
89
|
+
} from "./internal/merge-props.js";
|
|
90
|
+
import { focusable } from "./internal/focus.js";
|
|
56
91
|
import { useControlled } from "./internal/controlled-state.js";
|
|
57
92
|
|
|
93
|
+
/**
|
|
94
|
+
* What a screen reader is told the dialog is.
|
|
95
|
+
*
|
|
96
|
+
* A union rather than a string, so `role="alertdailog"` is a type error at the
|
|
97
|
+
* call rather than a dialog announced as a `div` with a name — which is what a
|
|
98
|
+
* misspelt role produces, silently, in markup that looks correct.
|
|
99
|
+
*
|
|
100
|
+
* Two members and not the whole of ARIA: these are the two roles that carry
|
|
101
|
+
* `aria-modal`, and a `Dialog.Body` that is a `region` or a `complementary` is
|
|
102
|
+
* a different component rather than this one with another string.
|
|
103
|
+
*/
|
|
104
|
+
export type DialogRole = "dialog" | "alertdialog";
|
|
105
|
+
|
|
58
106
|
type DialogState = {|
|
|
59
107
|
readonly base: string,
|
|
60
108
|
readonly open: boolean,
|
|
@@ -114,29 +162,36 @@ export component DialogRoot(
|
|
|
114
162
|
return <DialogContext.Provider value={state}>{children}</DialogContext.Provider>;
|
|
115
163
|
}
|
|
116
164
|
|
|
117
|
-
/**
|
|
118
|
-
|
|
165
|
+
/**
|
|
166
|
+
* What opens the dialog, and what focus comes back to when it closes.
|
|
167
|
+
*
|
|
168
|
+
* `render` for a trigger that is not a `<button>` — a card, a table row, an
|
|
169
|
+
* icon in somebody else's `<Pressable>`. The ref goes across with everything
|
|
170
|
+
* else, which is what keeps "focus comes back here" true of whatever the
|
|
171
|
+
* caller rendered.
|
|
172
|
+
*/
|
|
173
|
+
export component DialogTrigger(children: React.Node, render?: RenderProp, ...rest: Rest) {
|
|
119
174
|
const dialog = useDialog("Dialog.Trigger");
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
175
|
+
const props = withProps(withoutComposed(rest, ["onClick", "ref"]), {
|
|
176
|
+
// Only while it is open. An `aria-controls` naming an element that is not
|
|
177
|
+
// in the document is worse than no `aria-controls`: a reader is told
|
|
178
|
+
// there is somewhere to go and there is not.
|
|
179
|
+
"aria-controls": dialog.open ? `${dialog.base}-body` : undefined,
|
|
180
|
+
"aria-expanded": dialog.open ? "true" : "false",
|
|
181
|
+
"aria-haspopup": "dialog",
|
|
182
|
+
children,
|
|
183
|
+
onClick: composeHandlers(rest.onClick, () => dialog.setOpen(true)),
|
|
184
|
+
ref: composeRefs(rest.ref, (element: HTMLElement | null) => {
|
|
185
|
+
// React calls callback refs during commit; focus restoration reads it later.
|
|
186
|
+
// uf-lint-disable-next-line react-compiler/immutability
|
|
187
|
+
dialog.triggerRef.current = element;
|
|
188
|
+
}),
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
if (render != null) {
|
|
192
|
+
return render(props);
|
|
193
|
+
}
|
|
194
|
+
return <button {...props} type="button" />;
|
|
140
195
|
}
|
|
141
196
|
|
|
142
197
|
/**
|
|
@@ -148,12 +203,16 @@ export component DialogTrigger(children: React.Node, ...rest: Rest) {
|
|
|
148
203
|
* their own backdrop or omits one entirely must still get it. That lives on
|
|
149
204
|
* `Dialog.Body`, which is the part that knows where "outside" is.
|
|
150
205
|
*/
|
|
151
|
-
export component DialogOverlay(...rest: Rest) {
|
|
206
|
+
export component DialogOverlay(render?: RenderProp, ...rest: Rest) {
|
|
152
207
|
const dialog = useDialog("Dialog.Overlay");
|
|
153
208
|
if (!dialog.open) {
|
|
154
209
|
return null;
|
|
155
210
|
}
|
|
156
|
-
|
|
211
|
+
const props = withProps(rest, { "aria-hidden": "true", "data-state": "open" });
|
|
212
|
+
if (render != null) {
|
|
213
|
+
return render(props);
|
|
214
|
+
}
|
|
215
|
+
return <div {...props} />;
|
|
157
216
|
}
|
|
158
217
|
|
|
159
218
|
/**
|
|
@@ -163,7 +222,14 @@ export component DialogOverlay(...rest: Rest) {
|
|
|
163
222
|
* which is the half of "modal" that CSS cannot express; `inert` on everything
|
|
164
223
|
* outside is the half the browser enforces.
|
|
165
224
|
*/
|
|
166
|
-
export component DialogBody(
|
|
225
|
+
export component DialogBody(
|
|
226
|
+
children: React.Node,
|
|
227
|
+
dismissOnOutsidePress?: boolean = true,
|
|
228
|
+
initialFocus?: { current: HTMLElement | null },
|
|
229
|
+
role?: DialogRole = "dialog",
|
|
230
|
+
render?: RenderProp,
|
|
231
|
+
...rest: Rest
|
|
232
|
+
) {
|
|
167
233
|
const dialog = useDialog("Dialog.Body");
|
|
168
234
|
const bodyRef = useRef<HTMLElement | null>(null);
|
|
169
235
|
// Stable, so the effect below depends on `open` and on nothing else. Keyed on
|
|
@@ -172,6 +238,19 @@ export component DialogBody(children: React.Node, ...rest: Rest) {
|
|
|
172
238
|
// parent that re-rendered stole focus back from whatever the reader had
|
|
173
239
|
// moved it to inside the dialog.
|
|
174
240
|
const close = useStableCallback(() => dialog.setOpen(false));
|
|
241
|
+
// Asked at the moment of the press rather than named in the dependencies
|
|
242
|
+
// below, because naming it there re-runs the effect when it changes and
|
|
243
|
+
// re-running the effect re-takes focus. A caller who flips this on a
|
|
244
|
+
// breakpoint would otherwise have focus dragged back to the top of the
|
|
245
|
+
// dialog underneath the reader.
|
|
246
|
+
const dismissable = useStableCallback(() => dismissOnOutsidePress);
|
|
247
|
+
|
|
248
|
+
// The page is held still by `@uniflowed/hooks`' reference-counted lock rather
|
|
249
|
+
// than by a second one written here. Two implementations of this in one
|
|
250
|
+
// repository is the duplication "build uf with uf" exists to catch, and the
|
|
251
|
+
// shared one is the one that also pads out the scrollbar's width — a page
|
|
252
|
+
// that jumps sideways when a dialog opens is this component's doing.
|
|
253
|
+
useScrollLock(dialog.open);
|
|
175
254
|
|
|
176
255
|
useEffect(() => {
|
|
177
256
|
const body = bodyRef.current;
|
|
@@ -185,111 +264,114 @@ export component DialogBody(children: React.Node, ...rest: Rest) {
|
|
|
185
264
|
const opener = trigger ?? (document.activeElement as $FlowFixMe);
|
|
186
265
|
|
|
187
266
|
const restorePage = concealOutside(body);
|
|
188
|
-
const releaseScroll = lockScroll(document);
|
|
189
|
-
|
|
190
|
-
const onOutsidePress = (event: Event) => {
|
|
191
|
-
const target: $FlowFixMe = event.target;
|
|
192
|
-
if (target == null || body.contains(target)) {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
// The trigger is outside the dialog and is not "outside" for this
|
|
196
|
-
// purpose: closing here and letting the trigger's own click reopen made
|
|
197
|
-
// a press on the trigger a no-op that flickered.
|
|
198
|
-
if (trigger != null && trigger.contains(target)) {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
close();
|
|
202
|
-
};
|
|
203
|
-
// Capture, so a press is seen even where something below it stops the
|
|
204
|
-
// event — a menu inside the dialog, for instance.
|
|
205
|
-
document.addEventListener("pointerdown", onOutsidePress, true);
|
|
206
267
|
|
|
207
|
-
//
|
|
208
|
-
// nothing focusable
|
|
209
|
-
|
|
268
|
+
// Where the caller said, then the first thing worth acting on, then the
|
|
269
|
+
// dialog itself when it holds nothing focusable — so focus is inside it
|
|
270
|
+
// whichever of the three answers.
|
|
271
|
+
//
|
|
272
|
+
// The named element has to still be *in* this dialog: a ref left over from
|
|
273
|
+
// a previous opening, or one pointing at something the caller renders
|
|
274
|
+
// elsewhere, would move focus out of a dialog that announces the rest of
|
|
275
|
+
// the page is unavailable.
|
|
276
|
+
const named = initialFocus?.current ?? null;
|
|
277
|
+
const target = named != null && body.contains(named) ? named : (focusable(body)[0] ?? body);
|
|
210
278
|
target.focus();
|
|
211
279
|
|
|
212
280
|
return () => {
|
|
213
|
-
document.removeEventListener("pointerdown", onOutsidePress, true);
|
|
214
281
|
// Order matters: the page comes back before focus is restored, because
|
|
215
282
|
// the trigger is one of the elements that was made `inert` and an inert
|
|
216
283
|
// element cannot take focus.
|
|
217
284
|
restorePage();
|
|
218
|
-
releaseScroll();
|
|
219
285
|
opener?.focus?.();
|
|
220
286
|
};
|
|
221
|
-
}, [dialog.open, dialog.triggerRef,
|
|
287
|
+
}, [dialog.open, dialog.triggerRef, initialFocus]);
|
|
288
|
+
|
|
289
|
+
// A confirmation declines to close here, and `Escape` still does. See the
|
|
290
|
+
// module header: there is a difference between a dialog the reader dismissed
|
|
291
|
+
// and one that went away while they were reaching for it.
|
|
292
|
+
useInteractOutside({
|
|
293
|
+
isDisabled: !dialog.open,
|
|
294
|
+
onInteractOutside: () => {
|
|
295
|
+
if (dismissable()) {
|
|
296
|
+
close();
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
refs: [bodyRef, dialog.triggerRef],
|
|
300
|
+
});
|
|
222
301
|
|
|
223
302
|
if (!dialog.open) {
|
|
224
303
|
return null;
|
|
225
304
|
}
|
|
226
305
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
// outer dialog's handler and one Escape closed both.
|
|
250
|
-
event.stopPropagation();
|
|
251
|
-
close();
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
if (event.key !== "Tab") {
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
const body = bodyRef.current;
|
|
258
|
-
if (body == null) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
const stops = focusable(body);
|
|
262
|
-
// An outer dialog must not also run its trap on this key.
|
|
306
|
+
// The caller's props first. A caller `ref` used to replace `bodyRef`, which
|
|
307
|
+
// left it null, made the Tab branch below return early, and turned the focus
|
|
308
|
+
// trap off while the dialog still announced `aria-modal="true"`. A caller
|
|
309
|
+
// `onKeyDown` used to replace this one, and Escape stopped closing it.
|
|
310
|
+
const props = withProps(withoutComposed(rest, ["onKeyDown", "ref"]), {
|
|
311
|
+
// Only ids that are in the document: an `aria-labelledby` naming a
|
|
312
|
+
// missing element makes a screen reader announce nothing at all, so a
|
|
313
|
+
// dialog without a `Dialog.Title` falls through to whatever `aria-label`
|
|
314
|
+
// the caller passed instead.
|
|
315
|
+
"aria-describedby": dialog.described ? `${dialog.base}-description` : undefined,
|
|
316
|
+
"aria-labelledby": dialog.titled ? `${dialog.base}-title` : undefined,
|
|
317
|
+
"aria-modal": "true",
|
|
318
|
+
children,
|
|
319
|
+
id: `${dialog.base}-body`,
|
|
320
|
+
// Key handling closes the mounted dialog and restores focus after events.
|
|
321
|
+
// uf-lint-disable-next-line react-compiler/refs
|
|
322
|
+
onKeyDown: composeHandlers(rest.onKeyDown, (event: PartEvent) => {
|
|
323
|
+
if (event.key === "Escape") {
|
|
324
|
+
event.preventDefault();
|
|
325
|
+
// The dialog behind this one must not also close. Two stacked
|
|
326
|
+
// dialogs nest in the DOM, so without this the event bubbled to the
|
|
327
|
+
// outer dialog's handler and one Escape closed both.
|
|
263
328
|
event.stopPropagation();
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
//
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
{
|
|
291
|
-
|
|
292
|
-
|
|
329
|
+
close();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (event.key !== "Tab") {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const body = bodyRef.current;
|
|
336
|
+
if (body == null) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const stops = focusable(body);
|
|
340
|
+
// An outer dialog must not also run its trap on this key.
|
|
341
|
+
event.stopPropagation();
|
|
342
|
+
if (stops.length === 0) {
|
|
343
|
+
// Nothing to move to, so Tab must not leave either.
|
|
344
|
+
event.preventDefault();
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const first = stops[0];
|
|
348
|
+
const last = stops[stops.length - 1];
|
|
349
|
+
const active = body.ownerDocument?.activeElement;
|
|
350
|
+
// Wrap at the ends. This is the whole of "focus cannot leave"; every
|
|
351
|
+
// other Tab press is the browser's own business.
|
|
352
|
+
if (event.shiftKey && (active === first || active === body)) {
|
|
353
|
+
event.preventDefault();
|
|
354
|
+
last.focus();
|
|
355
|
+
} else if (!event.shiftKey && active === last) {
|
|
356
|
+
event.preventDefault();
|
|
357
|
+
first.focus();
|
|
358
|
+
}
|
|
359
|
+
}),
|
|
360
|
+
// React calls callback refs during commit; focus effects and traps read it later.
|
|
361
|
+
// uf-lint-disable-next-line react-compiler/refs
|
|
362
|
+
ref: composeRefs(rest.ref, (element: HTMLElement | null) => {
|
|
363
|
+
bodyRef.current = element;
|
|
364
|
+
}),
|
|
365
|
+
role,
|
|
366
|
+
// So the dialog can hold focus itself when it contains nothing focusable,
|
|
367
|
+
// and so the trap has somewhere to put focus that is still inside.
|
|
368
|
+
tabIndex: -1,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
if (render != null) {
|
|
372
|
+
return render(props);
|
|
373
|
+
}
|
|
374
|
+
return <div {...props} />;
|
|
293
375
|
}
|
|
294
376
|
|
|
295
377
|
/**
|
|
@@ -299,7 +381,7 @@ export component DialogBody(children: React.Node, ...rest: Rest) {
|
|
|
299
381
|
* rendered — a conditional title that is absent used to leave the dialog
|
|
300
382
|
* pointing at an id nothing had.
|
|
301
383
|
*/
|
|
302
|
-
export component DialogTitle(children: React.Node, ...rest: Rest) {
|
|
384
|
+
export component DialogTitle(children: React.Node, render?: RenderProp, ...rest: Rest) {
|
|
303
385
|
const dialog = useDialog("Dialog.Title");
|
|
304
386
|
const register = dialog.registerTitle;
|
|
305
387
|
useEffect(() => {
|
|
@@ -307,11 +389,15 @@ export component DialogTitle(children: React.Node, ...rest: Rest) {
|
|
|
307
389
|
return () => register(false);
|
|
308
390
|
}, [register]);
|
|
309
391
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
392
|
+
const props = withProps(rest, { children, id: `${dialog.base}-title` });
|
|
393
|
+
// `<h2>` is a default rather than a decision. Which heading level a dialog's
|
|
394
|
+
// name is depends on what is around it — ubugeeei-prod/uf#276 is the same
|
|
395
|
+
// observation about an accordion — and `render` is how a caller says so
|
|
396
|
+
// without losing the id `aria-labelledby` points at.
|
|
397
|
+
if (render != null) {
|
|
398
|
+
return render(props);
|
|
399
|
+
}
|
|
400
|
+
return <h2 {...props} />;
|
|
315
401
|
}
|
|
316
402
|
|
|
317
403
|
/**
|
|
@@ -321,7 +407,7 @@ export component DialogTitle(children: React.Node, ...rest: Rest) {
|
|
|
321
407
|
* the one moment the reader has to decide whether they care — so this is where
|
|
322
408
|
* "this cannot be undone" belongs, not in body text further down.
|
|
323
409
|
*/
|
|
324
|
-
export component DialogDescription(children: React.Node, ...rest: Rest) {
|
|
410
|
+
export component DialogDescription(children: React.Node, render?: RenderProp, ...rest: Rest) {
|
|
325
411
|
const dialog = useDialog("Dialog.Description");
|
|
326
412
|
const register = dialog.registerDescription;
|
|
327
413
|
useEffect(() => {
|
|
@@ -329,11 +415,11 @@ export component DialogDescription(children: React.Node, ...rest: Rest) {
|
|
|
329
415
|
return () => register(false);
|
|
330
416
|
}, [register]);
|
|
331
417
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
418
|
+
const props = withProps(rest, { children, id: `${dialog.base}-description` });
|
|
419
|
+
if (render != null) {
|
|
420
|
+
return render(props);
|
|
421
|
+
}
|
|
422
|
+
return <p {...props} />;
|
|
337
423
|
}
|
|
338
424
|
|
|
339
425
|
/**
|
|
@@ -345,51 +431,35 @@ export component DialogDescription(children: React.Node, ...rest: Rest) {
|
|
|
345
431
|
* styling layer has a name to attach to, and contributes no semantics because
|
|
346
432
|
* it has none to contribute.
|
|
347
433
|
*/
|
|
348
|
-
export component DialogHeader(children: React.Node, ...rest: Rest) {
|
|
349
|
-
|
|
434
|
+
export component DialogHeader(children: React.Node, render?: RenderProp, ...rest: Rest) {
|
|
435
|
+
const props = withProps(rest, { children });
|
|
436
|
+
if (render != null) {
|
|
437
|
+
return render(props);
|
|
438
|
+
}
|
|
439
|
+
return <div {...props} />;
|
|
350
440
|
}
|
|
351
441
|
|
|
352
442
|
/** The bottom of the dialog, where the actions go. See `Dialog.Header`. */
|
|
353
|
-
export component DialogFooter(children: React.Node, ...rest: Rest) {
|
|
354
|
-
|
|
443
|
+
export component DialogFooter(children: React.Node, render?: RenderProp, ...rest: Rest) {
|
|
444
|
+
const props = withProps(rest, { children });
|
|
445
|
+
if (render != null) {
|
|
446
|
+
return render(props);
|
|
447
|
+
}
|
|
448
|
+
return <div {...props} />;
|
|
355
449
|
}
|
|
356
450
|
|
|
357
451
|
/** A button that closes the dialog. */
|
|
358
|
-
export component DialogClose(children: React.Node, ...rest: Rest) {
|
|
452
|
+
export component DialogClose(children: React.Node, render?: RenderProp, ...rest: Rest) {
|
|
359
453
|
const dialog = useDialog("Dialog.Close");
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
{...passed}
|
|
365
|
-
onClick={composeHandlers(rest.onClick, () => dialog.setOpen(false))}
|
|
366
|
-
type="button"
|
|
367
|
-
>
|
|
368
|
-
{children}
|
|
369
|
-
</button>
|
|
370
|
-
);
|
|
371
|
-
}
|
|
454
|
+
const props = withProps(withoutComposed(rest, ["onClick"]), {
|
|
455
|
+
children,
|
|
456
|
+
onClick: composeHandlers(rest.onClick, () => dialog.setOpen(false)),
|
|
457
|
+
});
|
|
372
458
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
* excludes them, and anything inside `[hidden]`, `[inert]` or `aria-hidden` is
|
|
378
|
-
* excluded because a reader cannot reach it.
|
|
379
|
-
*/
|
|
380
|
-
function focusable(root: HTMLElement): Array<HTMLElement> {
|
|
381
|
-
const selector =
|
|
382
|
-
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
383
|
-
return Array.from(root.querySelectorAll(selector)).filter(
|
|
384
|
-
(element: $FlowFixMe) =>
|
|
385
|
-
// All three attributes hide a whole subtree, so all three are checked on
|
|
386
|
-
// the ancestors. Reading `aria-hidden` off the element alone returned a
|
|
387
|
-
// button inside `<div aria-hidden="true">` as a focus stop, and the trap
|
|
388
|
-
// then moved focus to a control no screen reader exposes.
|
|
389
|
-
element.closest("[hidden]") == null &&
|
|
390
|
-
element.closest("[inert]") == null &&
|
|
391
|
-
element.closest('[aria-hidden="true"]') == null,
|
|
392
|
-
);
|
|
459
|
+
if (render != null) {
|
|
460
|
+
return render(props);
|
|
461
|
+
}
|
|
462
|
+
return <button {...props} type="button" />;
|
|
393
463
|
}
|
|
394
464
|
|
|
395
465
|
/**
|
|
@@ -448,36 +518,3 @@ function concealOutside(element: HTMLElement): () => void {
|
|
|
448
518
|
}
|
|
449
519
|
};
|
|
450
520
|
}
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
* How many dialogs are holding the page still, and what it looked like before.
|
|
454
|
-
*
|
|
455
|
-
* A count rather than each dialog saving and restoring, because two dialogs
|
|
456
|
-
* that open and close in any order but the strictest nesting would otherwise
|
|
457
|
-
* hand the page back a value the other one had already replaced.
|
|
458
|
-
*/
|
|
459
|
-
let scrollLocks = 0;
|
|
460
|
-
let overflowBeforeLock: string = "";
|
|
461
|
-
|
|
462
|
-
/** Stop the page behind the dialog from scrolling, and undo exactly that. */
|
|
463
|
-
function lockScroll(document: Document): () => void {
|
|
464
|
-
const body: $FlowFixMe = document.body;
|
|
465
|
-
if (scrollLocks === 0) {
|
|
466
|
-
overflowBeforeLock = body.style.overflow;
|
|
467
|
-
body.style.overflow = "hidden";
|
|
468
|
-
}
|
|
469
|
-
scrollLocks += 1;
|
|
470
|
-
|
|
471
|
-
let released = false;
|
|
472
|
-
return () => {
|
|
473
|
-
if (released) {
|
|
474
|
-
return;
|
|
475
|
-
}
|
|
476
|
-
released = true;
|
|
477
|
-
scrollLocks -= 1;
|
|
478
|
-
if (scrollLocks === 0) {
|
|
479
|
-
body.style.overflow = overflowBeforeLock;
|
|
480
|
-
overflowBeforeLock = "";
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
}
|