shelving 1.307.2 → 1.307.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.307.2",
3
+ "version": "1.307.3",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,8 @@ 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 backdrop clicks, link/nav-button clicks, the close button, or the Escape key.
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.
20
21
  * - 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
22
  * - 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.
@@ -5,10 +5,12 @@ 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 backdrop clicks, link/nav-button clicks, the close button, or the Escape key.
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.
13
15
  * - 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
16
  * - 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.
@@ -18,7 +20,7 @@ import styles from "./Dialog.module.css";
18
20
  * @see https://shelving.cc/ui/Dialog
19
21
  */
20
22
  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: _closeOnBackdropClick, onCancel: e => {
23
+ return (_jsx(Suspense, { fallback: null, children: _jsx(ViewTransition, { enter: "fade", exit: "fade", children: _jsxs("dialog", { ref: _showModal, className: getModuleClass(styles, "dialog"), onClick: _closeOnClick, onCancel: e => {
22
24
  // 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
25
  if (!onClose || !e.cancelable)
24
26
  return;
@@ -35,13 +37,18 @@ function _showModal(dialog) {
35
37
  if (dialog && !dialog.open)
36
38
  dialog.showModal();
37
39
  }
38
- /** When the user clicks anywhere on a `<dialog>` element (and the click isn't on a link etc), then close the dialog. */
39
- function _closeOnBackdropClick({ currentTarget, target }) {
40
- // Close the dialog when clicking on the dialog itself (but not its children).
41
- if (currentTarget === target)
42
- _requestClose(currentTarget);
43
- // Close the dialog when clicking on links or buttons in a `<nav>` element.
44
- if (target instanceof Element && target.closest("a:any-link, nav button:enabled"))
40
+ /** Links and `<nav>` buttons always close the dialog. */
41
+ const CLOSE_SELECTOR = "a:any-link, nav button:enabled";
42
+ /** Taps on controls and inside a `<Modal>` panel (a true modal, e.g. a form) never close the dialog. */
43
+ const KEEP_SELECTOR = `button, input, select, textarea, label, .${getModuleClass(modalStyles, "modal")}`;
44
+ /** When the user taps anywhere on a `<dialog>` element, close it, unless the tap is on a control or inside a `<Modal>`. */
45
+ function _closeOnClick({ currentTarget, target }) {
46
+ if (!(target instanceof Element))
47
+ return;
48
+ // Ignore taps in a nested dialog. React events bubble through the React tree, so they can reach an outer dialog.
49
+ if (target.closest("dialog") !== currentTarget)
50
+ return;
51
+ if (target.closest(CLOSE_SELECTOR) || !target.closest(KEEP_SELECTOR))
45
52
  _requestClose(currentTarget);
46
53
  }
47
54
  /**
@@ -4,7 +4,8 @@ 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 backdrop click, the Escape key, any link or `<nav>` button clicked inside it, or the built-in `<DialogCloseButton>` (an X icon, top-right).
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.
@@ -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,7 +21,8 @@ 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 backdrop clicks, link/nav-button clicks, the close button, or the Escape key.
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.
25
27
  * - 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
28
  * - 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.
@@ -38,7 +40,7 @@ export const Dialog = memo(({ children, onClose, ...props }: DialogProps) => {
38
40
  <dialog
39
41
  ref={_showModal}
40
42
  className={getModuleClass(styles, "dialog")}
41
- onClick={_closeOnBackdropClick}
43
+ onClick={_closeOnClick}
42
44
  onCancel={e => {
43
45
  // 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
46
  if (!onClose || !e.cancelable) return;
@@ -67,13 +69,20 @@ function _showModal(dialog: HTMLDialogElement | null): void {
67
69
  if (dialog && !dialog.open) dialog.showModal();
68
70
  }
69
71
 
70
- /** When the user clicks anywhere on a `<dialog>` element (and the click isn't on a link etc), then close the dialog. */
71
- function _closeOnBackdropClick({ currentTarget, target }: MouseEvent<HTMLDialogElement>): void {
72
- // Close the dialog when clicking on the dialog itself (but not its children).
73
- if (currentTarget === target) _requestClose(currentTarget);
72
+ /** Links and `<nav>` buttons always close the dialog. */
73
+ const CLOSE_SELECTOR = "a:any-link, nav button:enabled";
74
74
 
75
- // Close the dialog when clicking on links or buttons in a `<nav>` element.
76
- if (target instanceof Element && target.closest("a:any-link, nav button:enabled")) _requestClose(currentTarget);
75
+ /** Taps on controls and inside a `<Modal>` panel (a true modal, e.g. a form) never close the dialog. */
76
+ const KEEP_SELECTOR = `button, input, select, textarea, label, .${getModuleClass(modalStyles, "modal")}`;
77
+
78
+ /** When the user taps anywhere on a `<dialog>` element, close it, unless the tap is on a control or inside a `<Modal>`. */
79
+ function _closeOnClick({ currentTarget, target }: MouseEvent<HTMLDialogElement>): void {
80
+ if (!(target instanceof Element)) return;
81
+
82
+ // Ignore taps in a nested dialog. React events bubble through the React tree, so they can reach an outer dialog.
83
+ if (target.closest("dialog") !== currentTarget) return;
84
+
85
+ if (target.closest(CLOSE_SELECTOR) || !target.closest(KEEP_SELECTOR)) _requestClose(currentTarget);
77
86
  }
78
87
 
79
88
  /**
@@ -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>`.