@karimsa/mdxserve 0.3.0 → 0.5.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.
Files changed (69) hide show
  1. package/README.md +80 -10
  2. package/client/App.tsx +15 -1
  3. package/client/CodeBlock.tsx +24 -0
  4. package/client/DocView.tsx +9 -2
  5. package/client/ListingView.tsx +14 -10
  6. package/client/MarkdownTable.tsx +190 -0
  7. package/client/MdSection.tsx +63 -23
  8. package/client/MdSectionEditor.tsx +208 -2
  9. package/client/Mermaid.tsx +33 -7
  10. package/client/Settings.css +17 -0
  11. package/client/Settings.tsx +132 -0
  12. package/client/Table.tsx +2 -14
  13. package/client/app.css +341 -1
  14. package/client/block-state/README.md +39 -0
  15. package/client/block-state/storage.ts +73 -0
  16. package/client/block-state/useBlockState.ts +120 -0
  17. package/client/builtins/DataTable.tsx +269 -0
  18. package/client/builtins/chart-data.ts +22 -10
  19. package/client/builtins/index.ts +10 -0
  20. package/client/data-table/ColumnMenu.tsx +168 -0
  21. package/client/data-table/ColumnOptions.tsx +141 -0
  22. package/client/data-table/TableCell.tsx +76 -0
  23. package/client/data-table/TableHeaderCell.tsx +94 -0
  24. package/client/data-table/model.ts +194 -0
  25. package/client/data-table/navigation.ts +34 -0
  26. package/client/data-table/table-state.ts +196 -0
  27. package/client/data-table/useColumnMenu.ts +84 -0
  28. package/client/data-table/useTableState.ts +36 -0
  29. package/client/design/base/editor.css +59 -0
  30. package/client/design/base/prose.css +4 -0
  31. package/client/diagrams/Dialog.tsx +577 -0
  32. package/client/diagrams/EditDialog.tsx +67 -0
  33. package/client/diagrams/Preferences.tsx +213 -0
  34. package/client/diagrams/edit-block.ts +93 -0
  35. package/client/diagrams/edit-context.ts +4 -0
  36. package/client/diagrams/session-id.ts +8 -0
  37. package/client/diagrams/source.ts +73 -0
  38. package/client/diagrams/style.css +580 -0
  39. package/client/doc-module-cache.ts +9 -1
  40. package/client/editor/ImageDialog.tsx +69 -0
  41. package/client/editor/LinkDialog.tsx +60 -0
  42. package/client/editor/blocks.ts +295 -0
  43. package/client/editor/component-fields.css +57 -0
  44. package/client/editor/component-fields.ts +128 -0
  45. package/client/editor/image-dialog.css +44 -0
  46. package/client/editor/mdx-block.tsx +138 -0
  47. package/client/editor/slash.ts +166 -0
  48. package/client/entry.tsx +2 -0
  49. package/client/head-metadata.ts +32 -0
  50. package/client/mdx-components-base.ts +2 -2
  51. package/client/router.ts +56 -17
  52. package/client/shell/AppShell.tsx +38 -15
  53. package/client/shell/Sidebar.tsx +20 -2
  54. package/client/shell/StandaloneShell.tsx +19 -6
  55. package/client/shell/TocRail.tsx +4 -3
  56. package/client/shell/TopBar.tsx +17 -5
  57. package/client/standalone-entry.tsx +11 -2
  58. package/client/state.ts +14 -0
  59. package/client/ui/ConfirmDeleteDialog.tsx +48 -91
  60. package/client/ui/ExpandModal.tsx +63 -104
  61. package/client/ui/Modal.tsx +123 -0
  62. package/client/ui/ResizeHandle.tsx +1 -1
  63. package/client/ui/SearchDialog.tsx +81 -88
  64. package/client/ui/modal.css +42 -0
  65. package/dist/cli.js +4644 -2830
  66. package/dist/diagram-worker.js +17 -0
  67. package/dist/registry.json +297 -0
  68. package/package.json +44 -3
  69. package/skills/mdxserve/SKILL.md +56 -1
@@ -44,6 +44,7 @@ export function TopBar({
44
44
  exportDoc,
45
45
  theme,
46
46
  onToggleTheme,
47
+ onDiagramPreferences,
47
48
  }: {
48
49
  /** Wordmark's link target; undefined renders it as a plain (non-link) span. */
49
50
  homeHref?: string;
@@ -57,13 +58,14 @@ export function TopBar({
57
58
  exportDoc?: { pending: boolean; onExport: (format: ExportFormat) => void };
58
59
  theme: Theme;
59
60
  onToggleTheme: () => void;
61
+ onDiagramPreferences?: () => void;
60
62
  }) {
61
63
  const mac = isApplePlatform();
62
64
 
63
65
  return (
64
66
  <header
65
67
  data-print-hide
66
- className="sticky top-0 z-10 flex h-topbar shrink-0 items-center gap-3 border-b border-border-subtle px-4"
68
+ className="sticky top-0 z-10 flex h-topbar shrink-0 items-center gap-1 border-b border-border-subtle px-2 md:gap-3 md:px-4 [&>button]:max-md:h-11 [&>button]:max-md:w-11 [&>button]:shrink-0"
67
69
  style={{
68
70
  background: "color-mix(in oklab, var(--surface-page) 82%, transparent)",
69
71
  backdropFilter: "var(--blur-chrome)",
@@ -80,7 +82,10 @@ export function TopBar({
80
82
  ) : null}
81
83
  <Wordmark href={homeHref} />
82
84
  {hostLabel ? (
83
- <span className="rounded-sm border border-border-default px-1.5 py-0.5 font-mono text-[length:var(--size-2xs)] text-text-subtle">
85
+ <span
86
+ title={hostLabel}
87
+ className="hidden min-w-0 max-w-48 truncate md:block rounded-sm border border-border-default px-1.5 py-0.5 font-mono text-[length:var(--size-2xs)] text-text-subtle"
88
+ >
84
89
  {hostLabel}
85
90
  </span>
86
91
  ) : null}
@@ -89,11 +94,15 @@ export function TopBar({
89
94
  <button
90
95
  type="button"
91
96
  onClick={search.onOpen}
92
- className="flex h-[30px] w-60 items-center gap-2 rounded-md border border-border-default bg-surface-card px-2.5 text-[13px] text-text-subtle shadow-xs cursor-pointer"
97
+ aria-label="Search docs"
98
+ title="Search docs"
99
+ className="flex h-11 w-11 shrink-0 justify-center md:h-[30px] md:w-60 md:justify-start items-center gap-2 rounded-md border border-border-default bg-surface-card px-2.5 text-[13px] text-text-subtle shadow-xs cursor-pointer"
93
100
  >
94
101
  <Icon name="search" size="sm" />
95
- <span className="flex-1 text-left">Search docs</span>
96
- <Kbd>{mac ? "⌘K" : "Ctrl K"}</Kbd>
102
+ <span className="hidden md:block flex-1 text-left">Search docs</span>
103
+ <span className="hidden md:contents">
104
+ <Kbd>{mac ? "⌘K" : "Ctrl K"}</Kbd>
105
+ </span>
97
106
  </button>
98
107
  ) : null}
99
108
  {exportDoc ? (
@@ -106,6 +115,9 @@ export function TopBar({
106
115
  onChange={(format) => exportDoc.onExport(format as ExportFormat)}
107
116
  />
108
117
  ) : null}
118
+ {onDiagramPreferences && (
119
+ <IconButton icon="settings" label="Settings" size="sm" onClick={onDiagramPreferences} />
120
+ )}
109
121
  <IconButton
110
122
  icon={theme === "dark" ? "sun" : "moon"}
111
123
  label={theme === "dark" ? "Light mode" : "Dark mode"}
@@ -5,6 +5,8 @@ import type { ComponentType } from "react";
5
5
  import { mdxComponentsBase } from "./mdx-components-base";
6
6
  import { docModuleCache } from "./doc-module-cache";
7
7
  import { StandaloneShell } from "./shell/StandaloneShell";
8
+ import { applyHeadMetadata } from "./head-metadata";
9
+ import type { DocumentMetadata } from "../src/docs/metadata";
8
10
 
9
11
  /**
10
12
  * What `mdxserve export` embeds as `#__mdxserve_route` JSON, read back by
@@ -16,6 +18,7 @@ export interface StandaloneMeta {
16
18
  path: string;
17
19
  label: string;
18
20
  mtime?: number;
21
+ metadata?: DocumentMetadata;
19
22
  }
20
23
 
21
24
  /** Parses the `#__mdxserve_route` script tag the same way entry.tsx does. */
@@ -35,12 +38,18 @@ export function readMeta(): StandaloneMeta {
35
38
  * export names and this signature stable, since that generated import is not
36
39
  * type-checked against this file.
37
40
  */
38
- export function mount(Doc: ComponentType, meta: StandaloneMeta): void {
41
+ export function mount(
42
+ Doc: ComponentType,
43
+ meta: StandaloneMeta,
44
+ moduleMetadata?: DocumentMetadata,
45
+ ): void {
39
46
  const rootEl = document.getElementById("root");
40
47
  if (!rootEl) return;
41
48
  // DocView reads the module from the router's cache; seed it with the doc
42
49
  // that was compiled into this file so it renders synchronously.
43
- docModuleCache.set(meta.path, { status: "ok", Component: Doc });
50
+ const metadata = moduleMetadata ?? meta.metadata ?? { openGraph: [] };
51
+ docModuleCache.set(meta.path, { status: "ok", Component: Doc, metadata });
52
+ applyHeadMetadata(meta.label, metadata);
44
53
  createRoot(rootEl).render(
45
54
  <MotionConfig reducedMotion="user">
46
55
  <MDXProvider components={mdxComponentsBase}>
package/client/state.ts CHANGED
@@ -88,3 +88,17 @@ export const docWidthAtom = atomWithStorage<number | null>("mdxserve.doc.width",
88
88
  * back to read mode, discarding whatever it had open (see client/MdSection.tsx).
89
89
  */
90
90
  export const openSectionAtom = atom<string | null>(null);
91
+
92
+ export type ContentLayout = "flexible" | "full-width";
93
+
94
+ /** Content layout preference; saved widths are retained when full width is active. */
95
+ export const contentLayoutAtom = atomWithStorage<ContentLayout>(
96
+ "mdxserve.content.layout",
97
+ "flexible",
98
+ undefined,
99
+ { getOnInit: true },
100
+ );
101
+
102
+ export const tocVisibleAtom = atomWithStorage<boolean>("mdxserve.toc.visible", true, undefined, {
103
+ getOnInit: true,
104
+ });
@@ -1,11 +1,8 @@
1
- import { useEffect } from "react";
2
- import { createPortal } from "react-dom";
3
- import { AnimatePresence, motion } from "framer-motion";
4
1
  import { formatModified, formatSize } from "../format";
5
2
  import Button from "../builtins/Button";
6
3
  import type { ListingEntry } from "../router";
7
4
  import { Icon } from "./Icon";
8
- import { VARIANTS } from "../motion";
5
+ import { Modal } from "./Modal";
9
6
 
10
7
  export function ConfirmDeleteDialog({
11
8
  open,
@@ -21,93 +18,53 @@ export function ConfirmDeleteDialog({
21
18
  onCancel: () => void;
22
19
  onConfirm: () => void;
23
20
  }) {
24
- // Document-level, not panel-level: while a delete is pending both buttons
25
- // are disabled and the browser drops focus to <body>, so a panel onKeyDown
26
- // would stop hearing Escape (e.g. after a failed request leaves the dialog
27
- // open).
28
- useEffect(() => {
29
- if (!open) return;
30
- function onKeyDown(event: KeyboardEvent) {
31
- if (event.key === "Escape" && !pending) {
32
- event.preventDefault();
33
- onCancel();
34
- }
35
- }
36
- document.addEventListener("keydown", onKeyDown);
37
- return () => document.removeEventListener("keydown", onKeyDown);
38
- }, [open, pending, onCancel]);
39
-
40
- // Portal to <body>: this renders inside the route wrapper, whose fadeRise
41
- // transform would otherwise turn `fixed` into "fixed to the column" and let
42
- // the scrim miss the top bar and sidebar.
43
- return createPortal(
44
- <AnimatePresence>
45
- {open ? (
46
- <motion.div
47
- key="scrim"
48
- onClick={pending ? undefined : onCancel}
49
- {...VARIANTS.scrim}
50
- className="fixed inset-0 z-[var(--z-modal)] flex justify-center bg-[var(--scrim)] pt-[10vh] backdrop-blur-sm"
51
- >
52
- <motion.div
53
- onClick={(event) => event.stopPropagation()}
54
- {...VARIANTS.pop}
55
- role="alertdialog"
56
- aria-modal="true"
57
- aria-labelledby="confirm-delete-title"
58
- className="w-[560px] max-w-[92vw] self-start overflow-hidden rounded-xl border border-border-default bg-surface-raised shadow-lg"
21
+ return (
22
+ <Modal
23
+ open={open}
24
+ onClose={onCancel}
25
+ dismissible={!pending}
26
+ placement="top"
27
+ role="alertdialog"
28
+ aria-labelledby="confirm-delete-title"
29
+ className="w-[560px] overflow-hidden"
30
+ >
31
+ <div className="flex items-center gap-2 border-b border-border-subtle px-4 py-3">
32
+ <Icon name="trash-2" size="md" className="text-status-danger-fg" />
33
+ <div className="flex flex-col">
34
+ <span
35
+ id="confirm-delete-title"
36
+ className="font-sans font-semibold text-text-heading text-[length:var(--size-md)]"
59
37
  >
60
- <div className="flex items-center gap-2 border-b border-border-subtle px-4 py-3">
61
- <Icon name="trash-2" size="md" className="text-status-danger-fg" />
62
- <div className="flex flex-col">
63
- <span
64
- id="confirm-delete-title"
65
- className="font-sans font-semibold text-text-heading text-[length:var(--size-md)]"
66
- >
67
- Delete {files.length} {files.length === 1 ? "file" : "files"}?
68
- </span>
69
- <span className="font-sans text-[length:var(--size-sm)] text-text-muted">
70
- They&rsquo;ll be moved to the system Trash — this can be undone from there.
71
- </span>
72
- </div>
73
- </div>
74
- <div className="max-h-72 overflow-y-auto p-2">
75
- {files.map((file) => (
76
- <div key={file.name} className="flex items-center gap-2 rounded-md px-2 py-1.5">
77
- <Icon name="file-text" size="sm" className="shrink-0 text-text-subtle" />
78
- <span className="truncate font-mono text-[length:var(--size-sm)]">
79
- {file.name}
80
- </span>
81
- <span className="ml-auto flex shrink-0 gap-4 pl-4 font-mono text-[length:var(--size-xs)] text-text-subtle tabular-nums">
82
- {typeof file.mtime === "number" ? (
83
- <span>{formatModified(file.mtime)}</span>
84
- ) : null}
85
- {typeof file.size === "number" ? (
86
- <span className="w-16 text-right">{formatSize(file.size)}</span>
87
- ) : null}
88
- </span>
89
- </div>
90
- ))}
91
- </div>
92
- <div className="flex justify-end gap-2 border-t border-border-subtle px-4 py-3">
93
- {/* Start keyboard focus on the safe action. */}
94
- <Button variant="secondary" size="sm" autoFocus disabled={pending} onClick={onCancel}>
95
- Cancel
96
- </Button>
97
- <Button
98
- variant="danger"
99
- size="sm"
100
- icon="trash-2"
101
- disabled={pending}
102
- onClick={onConfirm}
103
- >
104
- {pending ? "Moving…" : "Move to Trash"}
105
- </Button>
106
- </div>
107
- </motion.div>
108
- </motion.div>
109
- ) : null}
110
- </AnimatePresence>,
111
- document.body,
38
+ Delete {files.length} {files.length === 1 ? "file" : "files"}?
39
+ </span>
40
+ <span className="font-sans text-[length:var(--size-sm)] text-text-muted">
41
+ They&rsquo;ll be moved to the system Trash — this can be undone from there.
42
+ </span>
43
+ </div>
44
+ </div>
45
+ <div className="max-h-72 overflow-y-auto p-2">
46
+ {files.map((file) => (
47
+ <div key={file.name} className="flex items-center gap-2 rounded-md px-2 py-1.5">
48
+ <Icon name="file-text" size="sm" className="shrink-0 text-text-subtle" />
49
+ <span className="truncate font-mono text-[length:var(--size-sm)]">{file.name}</span>
50
+ <span className="ml-auto flex shrink-0 gap-4 pl-4 font-mono text-[length:var(--size-xs)] text-text-subtle tabular-nums">
51
+ {typeof file.mtime === "number" ? <span>{formatModified(file.mtime)}</span> : null}
52
+ {typeof file.size === "number" ? (
53
+ <span className="w-16 text-right">{formatSize(file.size)}</span>
54
+ ) : null}
55
+ </span>
56
+ </div>
57
+ ))}
58
+ </div>
59
+ <div className="flex justify-end gap-2 border-t border-border-subtle px-4 py-3">
60
+ {/* Start keyboard focus on the safe action. */}
61
+ <Button variant="secondary" size="sm" autoFocus disabled={pending} onClick={onCancel}>
62
+ Cancel
63
+ </Button>
64
+ <Button variant="danger" size="sm" icon="trash-2" disabled={pending} onClick={onConfirm}>
65
+ {pending ? "Moving…" : "Move to Trash"}
66
+ </Button>
67
+ </div>
68
+ </Modal>
112
69
  );
113
70
  }
@@ -1,14 +1,15 @@
1
1
  import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
2
- import { createPortal } from "react-dom";
3
- import { AnimatePresence, motion, useSpring } from "framer-motion";
2
+ import { Modal } from "./Modal";
3
+ import { useSpring } from "framer-motion";
4
4
  import { Icon } from "./Icon";
5
5
  import { IconButton } from "./IconButton";
6
6
  import { Kbd } from "./Kbd";
7
- import { TRANSITIONS, VARIANTS } from "../motion";
7
+ import { TRANSITIONS } from "../motion";
8
8
 
9
9
  export interface ExpandModalProps {
10
10
  open: boolean;
11
11
  onClose: () => void;
12
+ onExitComplete?: () => void;
12
13
  /** Lucide icon shown before the title. */
13
14
  icon: string;
14
15
  title: string;
@@ -114,13 +115,14 @@ function resizeFromCentre(
114
115
  export function ExpandModal({
115
116
  open,
116
117
  onClose,
118
+ onExitComplete,
117
119
  icon,
118
120
  title,
119
121
  hint,
120
122
  actions,
121
123
  children,
122
124
  }: ExpandModalProps) {
123
- const panelRef = useRef<HTMLDivElement>(null);
125
+ const panelRef = useRef<HTMLDialogElement>(null);
124
126
  // Whether the reader has taken over the size; the springs below hold it.
125
127
  const [custom, setCustom] = useState(false);
126
128
  const [dragging, setDragging] = useState<Edge | null>(null);
@@ -137,35 +139,14 @@ export function ExpandModal({
137
139
  grab: Point;
138
140
  } | null>(null);
139
141
 
140
- // Document-level so Escape works wherever focus landed (a pan surface or
141
- // chart is a plain element and never holds focus itself). A control in the
142
- // header that already consumed the key (the layout menu closing itself)
143
- // marks the event default-prevented, and that Escape must not also close
144
- // the modal.
145
142
  useEffect(() => {
146
143
  if (!open) return;
147
- function onKeyDown(event: KeyboardEvent) {
148
- if (event.key === "Escape" && !event.defaultPrevented) {
149
- event.preventDefault();
150
- onClose();
151
- }
152
- }
153
- document.addEventListener("keydown", onKeyDown);
154
- // Wheel-zooming the content must not also scroll the document behind the scrim.
155
- const previousOverflow = document.body.style.overflow;
156
- document.body.style.overflow = "hidden";
157
144
  return () => {
158
- document.removeEventListener("keydown", onKeyDown);
159
- document.body.style.overflow = previousOverflow;
160
- // A drag interrupted by closing (Escape, scrim click, unmount) never sees
161
- // pointerup, so release its global state here: otherwise text selection
162
- // stays disabled page-wide and the next open treats a hover over a handle
163
- // as an in-progress drag.
164
145
  drag.current = null;
165
146
  setDragging(null);
166
147
  document.body.style.userSelect = "";
167
148
  };
168
- }, [open, onClose]);
149
+ }, [open]);
169
150
 
170
151
  // A custom size lasts for one viewing: reopening starts from the default inset.
171
152
  const resetSize = useCallback(() => {
@@ -174,7 +155,7 @@ export function ExpandModal({
174
155
  }, []);
175
156
 
176
157
  useEffect(() => {
177
- if (!open) resetSize();
158
+ if (open) resetSize();
178
159
  }, [open, resetSize]);
179
160
 
180
161
  // Keep a custom size inside the window if the window shrinks underneath it.
@@ -260,83 +241,61 @@ export function ExpandModal({
260
241
  document.body.style.userSelect = "";
261
242
  }, []);
262
243
 
263
- // The SSR entry renders the same component tree; never touch `document` at
264
- // render time there (the modal is only ever opened client-side anyway).
265
- if (typeof document === "undefined") return null;
266
-
267
- // Portal to <body>: callers sit inside the route wrapper, whose fadeRise
268
- // transform would otherwise turn `fixed` into "fixed to the column" and let
269
- // the scrim miss the top bar and sidebar.
270
- return createPortal(
271
- <AnimatePresence>
272
- {open ? (
273
- <motion.div
274
- key="scrim"
275
- onClick={onClose}
276
- {...VARIANTS.scrim}
277
- className="fixed inset-0 z-[var(--z-modal)] flex items-center justify-center bg-[var(--scrim)] p-4 backdrop-blur-sm sm:p-8"
244
+ return (
245
+ <Modal
246
+ open={open}
247
+ onClose={onClose}
248
+ onExitComplete={onExitComplete}
249
+ panelRef={panelRef}
250
+ aria-label={`Expanded ${title.toLowerCase()}`}
251
+ // A dragged panel takes an explicit size; auto margins keep it centered.
252
+ style={
253
+ custom
254
+ ? { width: widthSpring, height: heightSpring, maxWidth: "none", flex: "none" }
255
+ : undefined
256
+ }
257
+ className="flex h-[calc(100dvh-32px)] w-[min(1600px,calc(100vw-32px))] flex-col overflow-visible sm:h-[calc(100dvh-64px)] sm:w-[min(1600px,calc(100vw-64px))]"
258
+ >
259
+ <div className="flex shrink-0 items-center gap-2 rounded-t-xl border-b border-border-subtle px-3 py-2">
260
+ <Icon name={icon} size="md" className="text-text-muted" />
261
+ <span className="font-sans font-semibold text-text-heading text-[length:var(--size-sm)]">
262
+ {title}
263
+ </span>
264
+ <span className="hidden items-center gap-1.5 font-sans text-[length:var(--size-xs)] text-text-subtle sm:flex">
265
+ {hint ? <>{hint} · </> : null}
266
+ drag an edge to resize · <Kbd>esc</Kbd> to close
267
+ </span>
268
+ <div className="ml-auto flex items-center gap-2">
269
+ {actions}
270
+ <IconButton icon="x" label="Close" size="md" autoFocus onClick={onClose} />
271
+ </div>
272
+ </div>
273
+ <div className="min-h-0 flex-1 overflow-hidden rounded-b-xl">{children}</div>
274
+ {EDGE_HANDLES.map(({ edge, className, bar }) => (
275
+ <div
276
+ key={edge}
277
+ role="separator"
278
+ aria-label={`Resize ${edge} edge`}
279
+ className={"group absolute z-10 " + className}
280
+ onPointerDown={(event) => onHandleDown(edge, event)}
281
+ onPointerMove={onHandleMove}
282
+ onPointerUp={onHandleUp}
283
+ onPointerCancel={onHandleUp}
284
+ onDoubleClick={resetSize}
278
285
  >
279
- <motion.div
280
- ref={panelRef}
281
- onClick={(event) => event.stopPropagation()}
282
- {...VARIANTS.pop}
283
- role="dialog"
284
- aria-modal="true"
285
- aria-label={`Expanded ${title.toLowerCase()}`}
286
- // Once an edge has been dragged the panel takes an explicit size and stays
287
- // centred by the scrim's flex box; until then it fills the padded area
288
- // (capped for very wide windows).
289
- style={
290
- custom
291
- ? { width: widthSpring, height: heightSpring, maxWidth: "none", flex: "none" }
292
- : undefined
293
- }
294
- className="relative flex h-full w-full max-w-[1600px] flex-col rounded-xl border border-border-default bg-surface-raised shadow-lg"
295
- >
296
- <div className="flex shrink-0 items-center gap-2 rounded-t-xl border-b border-border-subtle px-3 py-2">
297
- <Icon name={icon} size="md" className="text-text-muted" />
298
- <span className="font-sans font-semibold text-text-heading text-[length:var(--size-sm)]">
299
- {title}
300
- </span>
301
- <span className="hidden items-center gap-1.5 font-sans text-[length:var(--size-xs)] text-text-subtle sm:flex">
302
- {hint ? <>{hint} · </> : null}
303
- drag an edge to resize · <Kbd>esc</Kbd> to close
304
- </span>
305
- <div className="ml-auto flex items-center gap-2">
306
- {actions}
307
- <IconButton icon="x" label="Close" size="md" autoFocus onClick={onClose} />
308
- </div>
309
- </div>
310
- <div className="min-h-0 flex-1 overflow-hidden rounded-b-xl">{children}</div>
311
- {EDGE_HANDLES.map(({ edge, className, bar }) => (
312
- <div
313
- key={edge}
314
- role="separator"
315
- aria-label={`Resize ${edge} edge`}
316
- className={"group absolute z-10 " + className}
317
- onPointerDown={(event) => onHandleDown(edge, event)}
318
- onPointerMove={onHandleMove}
319
- onPointerUp={onHandleUp}
320
- onPointerCancel={onHandleUp}
321
- onDoubleClick={resetSize}
322
- >
323
- {bar ? (
324
- <span
325
- className={
326
- "absolute rounded-full transition-opacity " +
327
- bar +
328
- (dragging === edge
329
- ? " bg-surface-accent opacity-100"
330
- : " bg-border-default opacity-0 group-hover:opacity-100")
331
- }
332
- />
333
- ) : null}
334
- </div>
335
- ))}
336
- </motion.div>
337
- </motion.div>
338
- ) : null}
339
- </AnimatePresence>,
340
- document.body,
286
+ {bar ? (
287
+ <span
288
+ className={
289
+ "absolute rounded-full transition-opacity " +
290
+ bar +
291
+ (dragging === edge
292
+ ? " bg-surface-accent opacity-100"
293
+ : " bg-border-default opacity-0 group-hover:opacity-100")
294
+ }
295
+ />
296
+ ) : null}
297
+ </div>
298
+ ))}
299
+ </Modal>
341
300
  );
342
301
  }
@@ -0,0 +1,123 @@
1
+ import { useEffect, useRef, type Ref } from "react";
2
+ import { createPortal } from "react-dom";
3
+ import { AnimatePresence, motion, useIsPresent, type HTMLMotionProps } from "framer-motion";
4
+ import { VARIANTS } from "../motion";
5
+
6
+ type ModalProps = Omit<HTMLMotionProps<"dialog">, "onClose" | "onCancel" | "ref" | "open"> & {
7
+ open: boolean;
8
+ onClose?: () => void;
9
+ onExitComplete?: () => void;
10
+ /** Disable Escape and backdrop dismissal while an operation is pending. */
11
+ dismissible?: boolean;
12
+ dismissOnBackdrop?: boolean;
13
+ /** Override backdrop dismissal, for example to protect an unsaved draft. */
14
+ onBackdropClick?: () => void;
15
+ placement?: "top" | "center";
16
+ panelRef?: Ref<HTMLDialogElement>;
17
+ initialFocus?: string;
18
+ };
19
+
20
+ // A parent and its nested modal can unmount in either order.
21
+ const scrollLocks = new WeakMap<Document, { count: number; overflow: string }>();
22
+
23
+ /**
24
+ * Native modality supplies focus containment, focus return and nested top-layer ordering.
25
+ * Toggle `open` to close, or wrap a conditionally mounted owner in AnimatePresence.
26
+ * Exit propagation keeps the native dialog open until its panel has finished fading.
27
+ */
28
+ export function Modal({ open, onExitComplete, ...props }: ModalProps) {
29
+ if (typeof document === "undefined") return null;
30
+ return createPortal(
31
+ <AnimatePresence propagate onExitComplete={onExitComplete}>
32
+ {open && <ModalSurface key="modal" {...props} />}
33
+ </AnimatePresence>,
34
+ document.body,
35
+ );
36
+ }
37
+
38
+ function ModalSurface({
39
+ onClose,
40
+ dismissible = true,
41
+ dismissOnBackdrop = true,
42
+ onBackdropClick,
43
+ placement = "center",
44
+ panelRef,
45
+ initialFocus,
46
+ className = "",
47
+ children,
48
+ onKeyDown,
49
+ ...props
50
+ }: Omit<ModalProps, "open">) {
51
+ const present = useIsPresent();
52
+ const dialogRef = useRef<HTMLDialogElement>(null);
53
+ const backdropPress = useRef(false);
54
+ useEffect(() => {
55
+ const dialog = dialogRef.current!;
56
+ const previousFocus = document.activeElement;
57
+ const scrollLock = scrollLocks.get(document) ?? {
58
+ count: 0,
59
+ overflow: document.body.style.overflow,
60
+ };
61
+ scrollLock.count += 1;
62
+ scrollLocks.set(document, scrollLock);
63
+ dialog.showModal();
64
+ if (initialFocus) dialog.querySelector<HTMLElement>(initialFocus)?.focus();
65
+ document.body.style.overflow = "hidden";
66
+ return () => {
67
+ dialog.close();
68
+ scrollLock.count -= 1;
69
+ if (scrollLock.count === 0) {
70
+ document.body.style.overflow = scrollLock.overflow;
71
+ scrollLocks.delete(document);
72
+ }
73
+ if (previousFocus instanceof HTMLElement && previousFocus.isConnected) previousFocus.focus();
74
+ };
75
+ }, [initialFocus]);
76
+
77
+ function isBackdrop(event: React.MouseEvent<HTMLDialogElement>) {
78
+ const bounds = event.currentTarget.getBoundingClientRect();
79
+ return (
80
+ event.target === event.currentTarget &&
81
+ (event.clientX < bounds.left ||
82
+ event.clientX > bounds.right ||
83
+ event.clientY < bounds.top ||
84
+ event.clientY > bounds.bottom)
85
+ );
86
+ }
87
+
88
+ return (
89
+ <motion.dialog
90
+ {...VARIANTS.pop}
91
+ {...props}
92
+ ref={(dialog) => {
93
+ dialogRef.current = dialog;
94
+ if (typeof panelRef === "function") return panelRef(dialog);
95
+ if (panelRef) panelRef.current = dialog;
96
+ }}
97
+ aria-modal="true"
98
+ data-exiting={!present || undefined}
99
+ className={`modal modal--${placement} ${className}`}
100
+ onCancel={(event) => {
101
+ event.preventDefault();
102
+ event.stopPropagation();
103
+ if (present && dismissible) onClose?.();
104
+ }}
105
+ onKeyDown={(event) => {
106
+ onKeyDown?.(event);
107
+ event.stopPropagation();
108
+ }}
109
+ onPointerDown={(event) => {
110
+ backdropPress.current = isBackdrop(event);
111
+ }}
112
+ onClick={(event) => {
113
+ event.stopPropagation();
114
+ const outside = backdropPress.current && isBackdrop(event);
115
+ backdropPress.current = false;
116
+ if (present && outside && dismissible && dismissOnBackdrop)
117
+ (onBackdropClick ?? onClose)?.();
118
+ }}
119
+ >
120
+ {children}
121
+ </motion.dialog>
122
+ );
123
+ }
@@ -178,7 +178,7 @@ export function ResizeHandle({
178
178
  onPointerCancel={onPointerUp}
179
179
  style={{ width: HANDLE_ZONE, ...zoneStyle }}
180
180
  className={
181
- "absolute inset-y-0 z-10 flex cursor-col-resize " +
181
+ "absolute inset-y-0 z-10 hidden md:flex cursor-col-resize " +
182
182
  (anchored
183
183
  ? "items-center " + (side === "right" ? "justify-center" : "justify-end")
184
184
  : // A column so the sticky pill can slide along the zone's full height.