@tonycodes-ui/mortar 0.24.0 → 0.26.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/src/internal.tsx CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  useContext,
8
8
  useEffect,
9
9
  useId,
10
+ useRef,
10
11
  useState,
11
12
  type HTMLAttributes,
12
13
  type RefObject,
@@ -14,6 +15,7 @@ import {
14
15
  cloneElement,
15
16
  isValidElement,
16
17
  } from "react";
18
+ import { createPortal } from "react-dom";
17
19
  import {
18
20
  CloseButton,
19
21
  Dialog,
@@ -373,19 +375,149 @@ export function FieldMessage({
373
375
  ) : null;
374
376
  }
375
377
 
378
+ /**
379
+ * Where an overlay portals. `document` (default) uses Headless UI's body
380
+ * portal. `contained` keeps the overlay inside the nearest container-scoped
381
+ * ThemeProvider's overlay host so an embedded widget does not cover the page.
382
+ */
383
+ export type OverlayPortal = "document" | "contained";
384
+
385
+ /**
386
+ * The overlay host of the nearest container-scoped ThemeProvider.
387
+ * Undefined outside one; `{ host: null }` while the host node is mounting.
388
+ * Default is undefined (like ThemeScopeContext) so document-portal dialogs do
389
+ * not depend on ThemeProvider in the catalogue.
390
+ */
391
+ export const ContainedPortalContext = createContext<
392
+ { host: HTMLElement | null } | undefined
393
+ >(undefined);
394
+
395
+ const FOCUSABLE =
396
+ 'a[href],button:not([disabled]),textarea:not([disabled]),input:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])';
397
+
398
+ /**
399
+ * Dialog root portalled into a container ThemeProvider's overlay host.
400
+ * Bounds, backdrop and focus stay inside the widget; the host page outside it
401
+ * stays usable. Escape closes; Tab cycles within the dialog.
402
+ */
403
+ function ContainedDialogFrame({
404
+ open,
405
+ onClose,
406
+ component,
407
+ labelledBy,
408
+ children,
409
+ }: {
410
+ open: boolean;
411
+ onClose: () => void;
412
+ component?: string;
413
+ labelledBy?: string;
414
+ children: ReactNode;
415
+ }) {
416
+ const portal = useContext(ContainedPortalContext);
417
+ if (!portal)
418
+ throw new Error(
419
+ 'portal="contained" requires a ThemeProvider with scope="container".',
420
+ );
421
+ const dialogRef = useRef<HTMLDivElement>(null);
422
+ const onCloseRef = useRef(onClose);
423
+ onCloseRef.current = onClose;
424
+
425
+ useEffect(() => {
426
+ if (!open) return;
427
+ const root = dialogRef.current;
428
+ if (!root) return;
429
+ const previouslyFocused = document.activeElement as HTMLElement | null;
430
+ const focusables = () =>
431
+ [...root.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(
432
+ (el) => el.offsetParent !== null || el === document.activeElement,
433
+ );
434
+ const initial = focusables()[0] ?? root;
435
+ initial.focus();
436
+
437
+ const onKeyDown = (event: KeyboardEvent) => {
438
+ if (event.key === "Escape") {
439
+ event.preventDefault();
440
+ event.stopPropagation();
441
+ onCloseRef.current();
442
+ return;
443
+ }
444
+ if (event.key !== "Tab") return;
445
+ const items = focusables();
446
+ if (items.length === 0) {
447
+ event.preventDefault();
448
+ root.focus();
449
+ return;
450
+ }
451
+ const first = items[0];
452
+ const last = items[items.length - 1];
453
+ if (event.shiftKey && document.activeElement === first) {
454
+ event.preventDefault();
455
+ last.focus();
456
+ } else if (!event.shiftKey && document.activeElement === last) {
457
+ event.preventDefault();
458
+ first.focus();
459
+ }
460
+ };
461
+ document.addEventListener("keydown", onKeyDown, true);
462
+ return () => {
463
+ document.removeEventListener("keydown", onKeyDown, true);
464
+ previouslyFocused?.focus?.();
465
+ };
466
+ }, [open]);
467
+
468
+ if (!open || !portal.host) return null;
469
+ return createPortal(
470
+ <div
471
+ ref={dialogRef}
472
+ role="dialog"
473
+ aria-modal="true"
474
+ aria-labelledby={labelledBy}
475
+ tabIndex={-1}
476
+ className="m-dialog"
477
+ data-portal="contained"
478
+ {...componentRoot(component ?? "", component !== undefined)}
479
+ >
480
+ <div
481
+ className="m-backdrop"
482
+ aria-hidden="true"
483
+ onClick={() => onCloseRef.current()}
484
+ />
485
+ {children}
486
+ </div>,
487
+ portal.host,
488
+ );
489
+ }
490
+
376
491
  /** Headless dialog root with the shared backdrop. */
377
492
  export function DialogFrame({
378
493
  open,
379
494
  onClose,
380
495
  component,
496
+ portal = "document",
497
+ labelledBy,
381
498
  children,
382
499
  }: {
383
500
  open: boolean;
384
501
  onClose: () => void;
385
502
  /** The catalogue component this dialog is the root of, if any. */
386
503
  component?: string;
504
+ /** document (default) portals to the body; contained stays in the widget host. */
505
+ portal?: OverlayPortal;
506
+ /** Accessible name for contained portals (document mode uses DialogTitle). */
507
+ labelledBy?: string;
387
508
  children: ReactNode;
388
509
  }) {
510
+ if (portal === "contained")
511
+ return (
512
+ <ContainedDialogFrame
513
+ open={open}
514
+ onClose={onClose}
515
+ component={component}
516
+ labelledBy={labelledBy}
517
+ >
518
+ {children}
519
+ </ContainedDialogFrame>
520
+ );
389
521
  return (
390
522
  <Dialog
391
523
  open={open}
@@ -640,8 +772,9 @@ export const ThemeScopeContext = createContext<
640
772
  >(undefined);
641
773
  /**
642
774
  * Re-applies a container-scoped theme to content rendered outside its wrapper
643
- * (dialogs portal to the document body). The wrapper is display: contents, so
644
- * it carries the tokens without adding a box.
775
+ * (document-portal dialogs land on the body). Contained portals render inside
776
+ * the wrapper and do not need this. The wrapper is display: contents, so it
777
+ * carries the tokens without adding a box.
645
778
  */
646
779
  export function ThemeScope({ children }: { children: ReactNode }) {
647
780
  const attributes = useContext(ThemeScopeContext);
package/src/overlays.css CHANGED
@@ -5,6 +5,29 @@
5
5
  one section, ordered base, then variants and states, then media and container
6
6
  queries, except where that would change which equal-specificity rule wins. */
7
7
  @layer components {
8
+ /* == Contained overlay host == */
9
+ /* ThemeProvider scope="container" mounts this for portal="contained". */
10
+ [data-m-component="ThemeProvider"] {
11
+ position: relative;
12
+ }
13
+ .m-overlay-host {
14
+ position: absolute;
15
+ inset: 0;
16
+ z-index: var(--m-z-overlay);
17
+ pointer-events: none;
18
+ }
19
+ .m-overlay-host > * {
20
+ pointer-events: auto;
21
+ }
22
+ .m-dialog[data-portal="contained"],
23
+ .m-dialog[data-portal="contained"] .m-backdrop,
24
+ .m-dialog[data-portal="contained"] .m-dialog-position {
25
+ position: absolute;
26
+ }
27
+ .m-dialog[data-portal="contained"] .m-slide-over {
28
+ position: absolute;
29
+ }
30
+
8
31
  /* == Modal == */
9
32
  .m-dialog {
10
33
  position: fixed;
@@ -39,6 +39,7 @@ import {
39
39
  rovingIndex,
40
40
  useDomId,
41
41
  useField,
42
+ type OverlayPortal,
42
43
  } from "./internal.js";
43
44
  export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
44
45
  /** accent: the brand's accent fill (its accent gradient when it defines one) with on-accent text. */
@@ -761,6 +762,7 @@ export function Modal({
761
762
  actions,
762
763
  size = "md",
763
764
  className,
765
+ portal = "document",
764
766
  }: {
765
767
  open: boolean;
766
768
  onClose: () => void;
@@ -771,24 +773,57 @@ export function Modal({
771
773
  size?: "md" | "lg";
772
774
  /** On the dialog panel. */
773
775
  className?: string;
776
+ /**
777
+ * document (default) portals over the page. contained keeps the overlay
778
+ * inside the nearest ThemeProvider scope="container" host.
779
+ */
780
+ portal?: OverlayPortal;
774
781
  }) {
775
782
  const component = useComponentName("Modal");
783
+ const titleId = useId();
784
+ const panelClass = cx("m-dialog-panel", className);
785
+ const panelSize = size === "md" ? undefined : size;
786
+ const heading = (
787
+ <div className="m-dialog-heading">
788
+ {portal === "contained" ? (
789
+ <h2 id={titleId}>{title}</h2>
790
+ ) : (
791
+ <DialogTitle>{title}</DialogTitle>
792
+ )}
793
+ <Button variant="ghost" onClick={onClose} aria-label="Close dialog">
794
+ Close
795
+ </Button>
796
+ </div>
797
+ );
798
+ const body = (
799
+ <>
800
+ {heading}
801
+ {children}
802
+ {actions && <div className="m-actions">{actions}</div>}
803
+ </>
804
+ );
776
805
  return (
777
- <DialogFrame open={open} onClose={onClose} component={component}>
806
+ <DialogFrame
807
+ open={open}
808
+ onClose={onClose}
809
+ component={component}
810
+ portal={portal}
811
+ labelledBy={portal === "contained" ? titleId : undefined}
812
+ >
778
813
  <div className="m-dialog-position">
779
- <DialogPanel
780
- className={cx("m-dialog-panel", className)}
781
- data-size={size === "md" ? undefined : size}
782
- >
783
- <div className="m-dialog-heading">
784
- <DialogTitle>{title}</DialogTitle>
785
- <Button variant="ghost" onClick={onClose} aria-label="Close dialog">
786
- Close
787
- </Button>
814
+ {portal === "contained" ? (
815
+ <div
816
+ className={panelClass}
817
+ data-size={panelSize}
818
+ onClick={(event) => event.stopPropagation()}
819
+ >
820
+ {body}
788
821
  </div>
789
- {children}
790
- {actions && <div className="m-actions">{actions}</div>}
791
- </DialogPanel>
822
+ ) : (
823
+ <DialogPanel className={panelClass} data-size={panelSize}>
824
+ {body}
825
+ </DialogPanel>
826
+ )}
792
827
  </div>
793
828
  </DialogFrame>
794
829
  );
package/src/workspace.tsx CHANGED
@@ -18,8 +18,8 @@ import {
18
18
  componentRoot,
19
19
  cx,
20
20
  useComponentName,
21
+ type OverlayPortal,
21
22
  } from "./internal.js";
22
-
23
23
  export interface SlideOverProps {
24
24
  open: boolean;
25
25
  onClose: () => void;
@@ -31,6 +31,11 @@ export interface SlideOverProps {
31
31
  size?: "md" | "lg" | "xl";
32
32
  /** Class on the sliding panel. */
33
33
  className?: string;
34
+ /**
35
+ * document (default) portals over the page. contained keeps the overlay
36
+ * inside the nearest ThemeProvider scope="container" host.
37
+ */
38
+ portal?: OverlayPortal;
34
39
  }
35
40
  export function SlideOver({
36
41
  open,
@@ -41,25 +46,59 @@ export function SlideOver({
41
46
  actions,
42
47
  size = "md",
43
48
  className,
49
+ portal = "document",
44
50
  }: SlideOverProps) {
51
+ const titleId = useId();
52
+ const panelClass = cx("m-slide-over", className);
53
+ const panelSize = size === "md" ? undefined : size;
54
+ const header = (
55
+ <header>
56
+ <div>
57
+ {portal === "contained" ? (
58
+ <h2 id={titleId}>{title}</h2>
59
+ ) : (
60
+ <DialogTitle>{title}</DialogTitle>
61
+ )}
62
+ {description &&
63
+ (portal === "contained" ? (
64
+ <p>{description}</p>
65
+ ) : (
66
+ <Description>{description}</Description>
67
+ ))}
68
+ </div>
69
+ <Button variant="ghost" onClick={onClose} aria-label="Close panel">
70
+ <Close />
71
+ </Button>
72
+ </header>
73
+ );
74
+ const body = (
75
+ <>
76
+ {header}
77
+ <div className="m-slide-over-body">{children}</div>
78
+ {actions && <footer className="m-actions">{actions}</footer>}
79
+ </>
80
+ );
45
81
  return (
46
- <DialogFrame open={open} onClose={onClose} component="SlideOver">
47
- <DialogPanel
48
- className={cx("m-slide-over", className)}
49
- data-size={size === "md" ? undefined : size}
50
- >
51
- <header>
52
- <div>
53
- <DialogTitle>{title}</DialogTitle>
54
- {description && <Description>{description}</Description>}
55
- </div>
56
- <Button variant="ghost" onClick={onClose} aria-label="Close panel">
57
- <Close />
58
- </Button>
59
- </header>
60
- <div className="m-slide-over-body">{children}</div>
61
- {actions && <footer className="m-actions">{actions}</footer>}
62
- </DialogPanel>
82
+ <DialogFrame
83
+ open={open}
84
+ onClose={onClose}
85
+ component="SlideOver"
86
+ portal={portal}
87
+ labelledBy={portal === "contained" ? titleId : undefined}
88
+ >
89
+ {portal === "contained" ? (
90
+ <div
91
+ className={panelClass}
92
+ data-size={panelSize}
93
+ onClick={(event) => event.stopPropagation()}
94
+ >
95
+ {body}
96
+ </div>
97
+ ) : (
98
+ <DialogPanel className={panelClass} data-size={panelSize}>
99
+ {body}
100
+ </DialogPanel>
101
+ )}
63
102
  </DialogFrame>
64
103
  );
65
104
  }