shelving 1.297.1 → 1.297.2

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.297.1",
3
+ "version": "1.297.2",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { XMarkIcon } from "@heroicons/react/24/solid";
3
- import { memo, Suspense, startTransition, useLayoutEffect, useRef, ViewTransition } from "react";
3
+ import { memo, Suspense, startTransition, ViewTransition } from "react";
4
4
  import { getButtonClass } from "../button/Button.js";
5
5
  import { getClass, getModuleClass } from "../util/css.js";
6
6
  import "../transition/FadeTransition.css";
@@ -18,12 +18,7 @@ import styles from "./Dialog.module.css";
18
18
  * @see https://shelving.cc/ui/Dialog
19
19
  */
20
20
  export const Dialog = memo(({ children, onClose, ...props }) => {
21
- const ref = useRef(null);
22
- // Open in a layout effect, not a passive effect. React runs layout effects inside the view transition's update, so the new snapshot shows the open dialog.
23
- useLayoutEffect(() => {
24
- ref.current?.showModal();
25
- }, []);
26
- return (_jsx(Suspense, { fallback: null, children: _jsx(ViewTransition, { enter: "fade", exit: "fade", children: _jsxs("dialog", { ref: ref, className: getModuleClass(styles, "dialog"), onClick: _closeOnBackdropClick, onCancel: e => {
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 => {
27
22
  // 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.
28
23
  if (!onClose || !e.cancelable)
29
24
  return;
@@ -31,6 +26,15 @@ export const Dialog = memo(({ children, onClose, ...props }) => {
31
26
  startTransition(() => onClose());
32
27
  }, onClose: onClose, ...props, children: [_jsx("div", { className: getModuleClass(styles, "content"), children: children }), _jsx("div", { className: getModuleClass(styles, "close"), children: _jsx(DialogCloseButton, {}) })] }) }) }));
33
28
  });
29
+ /**
30
+ * Open a `<dialog>` as a modal when React attaches it.
31
+ * - A ref callback, not a layout effect. React holds the layout effects of a view transition until its images are ready, and an image in a closed `<dialog>` is never ready, so a layout effect would never open a dialog with an image in it.
32
+ * - The ref callback still runs inside the view transition's update, so the new snapshot shows the open dialog.
33
+ */
34
+ function _showModal(dialog) {
35
+ if (dialog && !dialog.open)
36
+ dialog.showModal();
37
+ }
34
38
  /** When the user clicks anywhere on a `<dialog>` element (and the click isn't on a link etc), then close the dialog. */
35
39
  function _closeOnBackdropClick({ currentTarget, target }) {
36
40
  // Close the dialog when clicking on the dialog itself (but not its children).
@@ -1,5 +1,5 @@
1
1
  import { XMarkIcon } from "@heroicons/react/24/solid";
2
- import { type MouseEvent, memo, type ReactElement, Suspense, startTransition, useLayoutEffect, useRef, ViewTransition } from "react";
2
+ import { type MouseEvent, memo, type ReactElement, Suspense, startTransition, ViewTransition } from "react";
3
3
  import type { Callback } from "../../util/function.js";
4
4
  import { type ButtonVariants, getButtonClass } from "../button/Button.js";
5
5
  import { getClass, getModuleClass } from "../util/css.js";
@@ -30,20 +30,13 @@ export interface DialogProps extends OptionalChildProps {
30
30
  * @see https://shelving.cc/ui/Dialog
31
31
  */
32
32
  export const Dialog = memo(({ children, onClose, ...props }: DialogProps) => {
33
- const ref = useRef<HTMLDialogElement>(null);
34
-
35
- // Open in a layout effect, not a passive effect. React runs layout effects inside the view transition's update, so the new snapshot shows the open dialog.
36
- useLayoutEffect(() => {
37
- ref.current?.showModal();
38
- }, []);
39
-
40
33
  return (
41
34
  <Suspense fallback={null}>
42
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. */}
43
36
  <ViewTransition enter="fade" exit="fade">
44
37
  {/** biome-ignore lint/a11y/useKeyWithClickEvents: Dialogs also show a close button. */}
45
38
  <dialog
46
- ref={ref}
39
+ ref={_showModal}
47
40
  className={getModuleClass(styles, "dialog")}
48
41
  onClick={_closeOnBackdropClick}
49
42
  onCancel={e => {
@@ -65,6 +58,15 @@ export const Dialog = memo(({ children, onClose, ...props }: DialogProps) => {
65
58
  );
66
59
  });
67
60
 
61
+ /**
62
+ * Open a `<dialog>` as a modal when React attaches it.
63
+ * - A ref callback, not a layout effect. React holds the layout effects of a view transition until its images are ready, and an image in a closed `<dialog>` is never ready, so a layout effect would never open a dialog with an image in it.
64
+ * - The ref callback still runs inside the view transition's update, so the new snapshot shows the open dialog.
65
+ */
66
+ function _showModal(dialog: HTMLDialogElement | null): void {
67
+ if (dialog && !dialog.open) dialog.showModal();
68
+ }
69
+
68
70
  /** When the user clicks anywhere on a `<dialog>` element (and the click isn't on a link etc), then close the dialog. */
69
71
  function _closeOnBackdropClick({ currentTarget, target }: MouseEvent<HTMLDialogElement>): void {
70
72
  // Close the dialog when clicking on the dialog itself (but not its children).