shelving 1.307.2 → 1.307.4
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/package.json +1 -1
- package/ui/dialog/Dialog.d.ts +3 -1
- package/ui/dialog/Dialog.js +17 -9
- package/ui/dialog/Dialog.md +3 -1
- package/ui/dialog/Dialog.tsx +20 -10
- package/ui/dialog/Modal.md +1 -0
package/package.json
CHANGED
package/ui/dialog/Dialog.d.ts
CHANGED
|
@@ -15,8 +15,10 @@ export interface DialogProps extends OptionalChildProps {
|
|
|
15
15
|
/**
|
|
16
16
|
* Modal `<dialog>` element that opens on mount and includes a close button.
|
|
17
17
|
*
|
|
18
|
-
* - Opens via `showModal()` when mounted and closes on
|
|
18
|
+
* - Opens via `showModal()` when mounted, and closes on a tap anywhere on it, the close button, or the Escape key.
|
|
19
|
+
* - A tap on a control (button, input, select, textarea, label) or inside a `<Modal>` panel does not close it. A tap on a link or a `<nav>` button always closes it.
|
|
19
20
|
* - The whole dialog fades in and out in one view transition. A `<Modal>` pinned to an edge leaves that layer and slides in its own.
|
|
21
|
+
* - A change inside an open dialog (new content, a Suspense reveal) runs no view transition. The page ignores taps while one runs.
|
|
20
22
|
* - With `onClose`, a close request calls `onClose()` and the dialog stays open until it unmounts, so the view transition can capture it as it leaves.
|
|
21
23
|
* - Children sit in one wrapper in normal block layout, `--dialog-width` wide and centred on the screen. Content taller than the screen scrolls from its top.
|
|
22
24
|
* - Wraps content in `<Suspense>` so lazy children can stream in.
|
package/ui/dialog/Dialog.js
CHANGED
|
@@ -5,11 +5,14 @@ import { getButtonClass } from "../button/Button.js";
|
|
|
5
5
|
import { getClass, getModuleClass } from "../util/css.js";
|
|
6
6
|
import "../transition/FadeTransition.css";
|
|
7
7
|
import styles from "./Dialog.module.css";
|
|
8
|
+
import modalStyles from "./Modal.module.css";
|
|
8
9
|
/**
|
|
9
10
|
* Modal `<dialog>` element that opens on mount and includes a close button.
|
|
10
11
|
*
|
|
11
|
-
* - Opens via `showModal()` when mounted and closes on
|
|
12
|
+
* - Opens via `showModal()` when mounted, and closes on a tap anywhere on it, the close button, or the Escape key.
|
|
13
|
+
* - A tap on a control (button, input, select, textarea, label) or inside a `<Modal>` panel does not close it. A tap on a link or a `<nav>` button always closes it.
|
|
12
14
|
* - The whole dialog fades in and out in one view transition. A `<Modal>` pinned to an edge leaves that layer and slides in its own.
|
|
15
|
+
* - A change inside an open dialog (new content, a Suspense reveal) runs no view transition. The page ignores taps while one runs.
|
|
13
16
|
* - With `onClose`, a close request calls `onClose()` and the dialog stays open until it unmounts, so the view transition can capture it as it leaves.
|
|
14
17
|
* - Children sit in one wrapper in normal block layout, `--dialog-width` wide and centred on the screen. Content taller than the screen scrolls from its top.
|
|
15
18
|
* - Wraps content in `<Suspense>` so lazy children can stream in.
|
|
@@ -18,7 +21,7 @@ import styles from "./Dialog.module.css";
|
|
|
18
21
|
* @see https://shelving.cc/ui/Dialog
|
|
19
22
|
*/
|
|
20
23
|
export const Dialog = memo(({ children, onClose, ...props }) => {
|
|
21
|
-
return (_jsx(Suspense, { fallback: null, children: _jsx(ViewTransition, { enter: "fade", exit: "fade", children: _jsxs("dialog", { ref: _showModal, className: getModuleClass(styles, "dialog"), onClick:
|
|
24
|
+
return (_jsx(Suspense, { fallback: null, children: _jsx(ViewTransition, { default: "none", enter: "fade", exit: "fade", children: _jsxs("dialog", { ref: _showModal, className: getModuleClass(styles, "dialog"), onClick: _closeOnClick, onCancel: e => {
|
|
22
25
|
// Keep the dialog open and let the parent unmount it in a transition. An uncancelable request closes natively and fires `onClose` from the `close` event.
|
|
23
26
|
if (!onClose || !e.cancelable)
|
|
24
27
|
return;
|
|
@@ -35,13 +38,18 @@ function _showModal(dialog) {
|
|
|
35
38
|
if (dialog && !dialog.open)
|
|
36
39
|
dialog.showModal();
|
|
37
40
|
}
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (target instanceof Element
|
|
41
|
+
/** Links and `<nav>` buttons always close the dialog. */
|
|
42
|
+
const CLOSE_SELECTOR = "a:any-link, nav button:enabled";
|
|
43
|
+
/** Taps on controls and inside a `<Modal>` panel (a true modal, e.g. a form) never close the dialog. */
|
|
44
|
+
const KEEP_SELECTOR = `button, input, select, textarea, label, .${getModuleClass(modalStyles, "modal")}`;
|
|
45
|
+
/** When the user taps anywhere on a `<dialog>` element, close it, unless the tap is on a control or inside a `<Modal>`. */
|
|
46
|
+
function _closeOnClick({ currentTarget, target }) {
|
|
47
|
+
if (!(target instanceof Element))
|
|
48
|
+
return;
|
|
49
|
+
// Ignore taps in a nested dialog. React events bubble through the React tree, so they can reach an outer dialog.
|
|
50
|
+
if (target.closest("dialog") !== currentTarget)
|
|
51
|
+
return;
|
|
52
|
+
if (target.closest(CLOSE_SELECTOR) || !target.closest(KEEP_SELECTOR))
|
|
45
53
|
_requestClose(currentTarget);
|
|
46
54
|
}
|
|
47
55
|
/**
|
package/ui/dialog/Dialog.md
CHANGED
|
@@ -4,13 +4,15 @@ A native `<dialog>` element opened in modal mode. It opens via `showModal()` whe
|
|
|
4
4
|
|
|
5
5
|
**Things to know:**
|
|
6
6
|
|
|
7
|
-
- Closes on a
|
|
7
|
+
- Closes on a tap anywhere on it, the Escape key, or the built-in `<DialogCloseButton>` (an X icon, top-right).
|
|
8
|
+
- A tap on a control (`<button>`, `<input>`, `<select>`, `<textarea>`, `<label>`) does not close it. A tap anywhere inside a `<Modal>` panel does not close it either, so a form in a `<Modal>` is safe. A tap on a link or a `<nav>` button always closes it.
|
|
8
9
|
- Children render inside a `<Suspense>` boundary, so lazy content can stream in.
|
|
9
10
|
- Children sit in one wrapper in normal block layout, so several children stack as they would on the page. The wrapper is `--dialog-width` wide (never wider than the screen) and centred on the screen. A centred `<Modal>` fills it. Content taller than the screen starts at the top, and the dialog scrolls.
|
|
10
11
|
- While a dialog is open, the page behind it does not scroll. A scroll inside the dialog never passes on to the page.
|
|
11
12
|
- `Dialog` only dims the page. Its text is white (`--tint-100`) so it reads on the dark overlay. Wrap the content in `<Modal>` to give it a panel with dark text on a light surface.
|
|
12
13
|
- `onClose` fires when the user closes the dialog. It must unmount the `Dialog`: clear the React state that mounts it, or (when pushed via a store) remove it from the list. `Dialog` calls `onClose` inside `startTransition()`, and the dialog stays open until it unmounts.
|
|
13
14
|
- The dialog animates with [view transitions](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API). The whole dialog fades in and out as one layer. A `<Modal>` pinned to an edge takes its own layer and slides.
|
|
15
|
+
- A change inside an open dialog (new content, a Suspense reveal) runs no view transition, so the dialog never ignores a tap while its content changes. The page ignores taps while a view transition runs.
|
|
14
16
|
- A view transition only runs for a React transition update. `DialogsStore` and `onClose` do this for you. To animate a declarative `Dialog` as it opens, set the state that mounts it inside `startTransition()`.
|
|
15
17
|
- A browser without view transitions shows and hides the dialog at once.
|
|
16
18
|
- Pair with `DialogsStore`, `<DialogsContext>`, and `<Dialogs>` to open dialogs imperatively from anywhere in the app.
|
package/ui/dialog/Dialog.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import { getClass, getModuleClass } from "../util/css.js";
|
|
|
6
6
|
import type { ClassProps, OptionalChildProps } from "../util/props.js";
|
|
7
7
|
import "../transition/FadeTransition.css";
|
|
8
8
|
import styles from "./Dialog.module.css";
|
|
9
|
+
import modalStyles from "./Modal.module.css";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Props for `<Dialog>` — optional `children` content and an `onClose` callback.
|
|
@@ -20,8 +21,10 @@ export interface DialogProps extends OptionalChildProps {
|
|
|
20
21
|
/**
|
|
21
22
|
* Modal `<dialog>` element that opens on mount and includes a close button.
|
|
22
23
|
*
|
|
23
|
-
* - Opens via `showModal()` when mounted and closes on
|
|
24
|
+
* - Opens via `showModal()` when mounted, and closes on a tap anywhere on it, the close button, or the Escape key.
|
|
25
|
+
* - A tap on a control (button, input, select, textarea, label) or inside a `<Modal>` panel does not close it. A tap on a link or a `<nav>` button always closes it.
|
|
24
26
|
* - The whole dialog fades in and out in one view transition. A `<Modal>` pinned to an edge leaves that layer and slides in its own.
|
|
27
|
+
* - A change inside an open dialog (new content, a Suspense reveal) runs no view transition. The page ignores taps while one runs.
|
|
25
28
|
* - With `onClose`, a close request calls `onClose()` and the dialog stays open until it unmounts, so the view transition can capture it as it leaves.
|
|
26
29
|
* - Children sit in one wrapper in normal block layout, `--dialog-width` wide and centred on the screen. Content taller than the screen scrolls from its top.
|
|
27
30
|
* - Wraps content in `<Suspense>` so lazy children can stream in.
|
|
@@ -32,13 +35,13 @@ export interface DialogProps extends OptionalChildProps {
|
|
|
32
35
|
export const Dialog = memo(({ children, onClose, ...props }: DialogProps) => {
|
|
33
36
|
return (
|
|
34
37
|
<Suspense fallback={null}>
|
|
35
|
-
{/* The transition must wrap the `<dialog>`: React only animates a `<ViewTransition>` that comes before any DOM element in the inserted or deleted tree. Fade only on enter and exit, so an open dialog stays still while another dialog opens or closes. */}
|
|
36
|
-
<ViewTransition enter="fade" exit="fade">
|
|
38
|
+
{/* The transition must wrap the `<dialog>`: React only animates a `<ViewTransition>` that comes before any DOM element in the inserted or deleted tree. Fade only on enter and exit, so an open dialog stays still while another dialog opens or closes. No update transition: the page ignores taps while a view transition runs, so a change inside an open dialog must not start one. */}
|
|
39
|
+
<ViewTransition default="none" enter="fade" exit="fade">
|
|
37
40
|
{/** biome-ignore lint/a11y/useKeyWithClickEvents: Dialogs also show a close button. */}
|
|
38
41
|
<dialog
|
|
39
42
|
ref={_showModal}
|
|
40
43
|
className={getModuleClass(styles, "dialog")}
|
|
41
|
-
onClick={
|
|
44
|
+
onClick={_closeOnClick}
|
|
42
45
|
onCancel={e => {
|
|
43
46
|
// Keep the dialog open and let the parent unmount it in a transition. An uncancelable request closes natively and fires `onClose` from the `close` event.
|
|
44
47
|
if (!onClose || !e.cancelable) return;
|
|
@@ -67,13 +70,20 @@ function _showModal(dialog: HTMLDialogElement | null): void {
|
|
|
67
70
|
if (dialog && !dialog.open) dialog.showModal();
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
/**
|
|
71
|
-
|
|
72
|
-
// Close the dialog when clicking on the dialog itself (but not its children).
|
|
73
|
-
if (currentTarget === target) _requestClose(currentTarget);
|
|
73
|
+
/** Links and `<nav>` buttons always close the dialog. */
|
|
74
|
+
const CLOSE_SELECTOR = "a:any-link, nav button:enabled";
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
/** Taps on controls and inside a `<Modal>` panel (a true modal, e.g. a form) never close the dialog. */
|
|
77
|
+
const KEEP_SELECTOR = `button, input, select, textarea, label, .${getModuleClass(modalStyles, "modal")}`;
|
|
78
|
+
|
|
79
|
+
/** When the user taps anywhere on a `<dialog>` element, close it, unless the tap is on a control or inside a `<Modal>`. */
|
|
80
|
+
function _closeOnClick({ currentTarget, target }: MouseEvent<HTMLDialogElement>): void {
|
|
81
|
+
if (!(target instanceof Element)) return;
|
|
82
|
+
|
|
83
|
+
// Ignore taps in a nested dialog. React events bubble through the React tree, so they can reach an outer dialog.
|
|
84
|
+
if (target.closest("dialog") !== currentTarget) return;
|
|
85
|
+
|
|
86
|
+
if (target.closest(CLOSE_SELECTOR) || !target.closest(KEEP_SELECTOR)) _requestClose(currentTarget);
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
/**
|
package/ui/dialog/Modal.md
CHANGED
|
@@ -7,6 +7,7 @@ The panel inside a `<Dialog>`. `<Dialog>` dims the page; `Modal` gives the conte
|
|
|
7
7
|
- A native `<dialog>` does both jobs. Shelving splits them: `<Dialog>` is the overlay and `Modal` is the panel. Content placed directly in a `<Dialog>` shows as white text on the dark overlay.
|
|
8
8
|
- `Modal` sets its text back to `--tint-00`, so it reads on its own `--tint-100` surface.
|
|
9
9
|
- `DialogsStore.show()` wraps its content in a `<Dialog>` only. It does not add a `Modal`, so put the `Modal` in the content yourself.
|
|
10
|
+
- A tap inside a `Modal` never closes its `<Dialog>`, except on a link or a `<nav>` button. A tap on the overlay outside it does.
|
|
10
11
|
- `Modal` only styles the box. Lay out its contents with the usual block components.
|
|
11
12
|
- A centred `Modal` fills the width of its `<Dialog>`, so set `--dialog-width` to change it. `--modal-width` sets the width of a `left` or `right` panel, and the maximum width of a `top` or `bottom` panel.
|
|
12
13
|
- The `padding` variant (for example `padding="large"`) sets the top and bottom padding, and the `indent` variant sets the left and right padding, the same as on `<Panel>`.
|