@astra-spec/ui 0.0.1

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 (259) hide show
  1. package/LICENSE +29 -0
  2. package/README.md +261 -0
  3. package/TOKENS.md +93 -0
  4. package/blocks.css +7 -0
  5. package/components.css +13 -0
  6. package/dist/blocks/analysis-tree.d.ts +13 -0
  7. package/dist/blocks/analysis-tree.d.ts.map +1 -0
  8. package/dist/blocks/analysis-tree.js +19 -0
  9. package/dist/blocks/analysis-tree.js.map +1 -0
  10. package/dist/blocks/decisions-list.d.ts +17 -0
  11. package/dist/blocks/decisions-list.d.ts.map +1 -0
  12. package/dist/blocks/decisions-list.js +56 -0
  13. package/dist/blocks/decisions-list.js.map +1 -0
  14. package/dist/blocks/findings-list.d.ts +9 -0
  15. package/dist/blocks/findings-list.d.ts.map +1 -0
  16. package/dist/blocks/findings-list.js +35 -0
  17. package/dist/blocks/findings-list.js.map +1 -0
  18. package/dist/blocks/index.d.ts +18 -0
  19. package/dist/blocks/index.d.ts.map +1 -0
  20. package/dist/blocks/index.js +10 -0
  21. package/dist/blocks/index.js.map +1 -0
  22. package/dist/blocks/inputs-list.d.ts +9 -0
  23. package/dist/blocks/inputs-list.d.ts.map +1 -0
  24. package/dist/blocks/inputs-list.js +32 -0
  25. package/dist/blocks/inputs-list.js.map +1 -0
  26. package/dist/blocks/outputs-list.d.ts +18 -0
  27. package/dist/blocks/outputs-list.d.ts.map +1 -0
  28. package/dist/blocks/outputs-list.js +47 -0
  29. package/dist/blocks/outputs-list.js.map +1 -0
  30. package/dist/blocks/papers-list.d.ts +16 -0
  31. package/dist/blocks/papers-list.d.ts.map +1 -0
  32. package/dist/blocks/papers-list.js +20 -0
  33. package/dist/blocks/papers-list.js.map +1 -0
  34. package/dist/blocks/prior-insights-list.d.ts +9 -0
  35. package/dist/blocks/prior-insights-list.d.ts.map +1 -0
  36. package/dist/blocks/prior-insights-list.js +29 -0
  37. package/dist/blocks/prior-insights-list.js.map +1 -0
  38. package/dist/blocks/section.d.ts +36 -0
  39. package/dist/blocks/section.d.ts.map +1 -0
  40. package/dist/blocks/section.js +30 -0
  41. package/dist/blocks/section.js.map +1 -0
  42. package/dist/components/artifact-preview.d.ts +71 -0
  43. package/dist/components/artifact-preview.d.ts.map +1 -0
  44. package/dist/components/artifact-preview.js +170 -0
  45. package/dist/components/artifact-preview.js.map +1 -0
  46. package/dist/components/decision-detail.d.ts +17 -0
  47. package/dist/components/decision-detail.d.ts.map +1 -0
  48. package/dist/components/decision-detail.js +21 -0
  49. package/dist/components/decision-detail.js.map +1 -0
  50. package/dist/components/detail-entry.d.ts +15 -0
  51. package/dist/components/detail-entry.d.ts.map +1 -0
  52. package/dist/components/detail-entry.js +14 -0
  53. package/dist/components/detail-entry.js.map +1 -0
  54. package/dist/components/finding-detail.d.ts +19 -0
  55. package/dist/components/finding-detail.d.ts.map +1 -0
  56. package/dist/components/finding-detail.js +39 -0
  57. package/dist/components/finding-detail.js.map +1 -0
  58. package/dist/components/index.d.ts +28 -0
  59. package/dist/components/index.d.ts.map +1 -0
  60. package/dist/components/index.js +15 -0
  61. package/dist/components/index.js.map +1 -0
  62. package/dist/components/input-detail.d.ts +15 -0
  63. package/dist/components/input-detail.d.ts.map +1 -0
  64. package/dist/components/input-detail.js +21 -0
  65. package/dist/components/input-detail.js.map +1 -0
  66. package/dist/components/insight-detail.d.ts +25 -0
  67. package/dist/components/insight-detail.d.ts.map +1 -0
  68. package/dist/components/insight-detail.js +30 -0
  69. package/dist/components/insight-detail.js.map +1 -0
  70. package/dist/components/insight-trigger.d.ts +20 -0
  71. package/dist/components/insight-trigger.d.ts.map +1 -0
  72. package/dist/components/insight-trigger.js +32 -0
  73. package/dist/components/insight-trigger.js.map +1 -0
  74. package/dist/components/output-detail.d.ts +39 -0
  75. package/dist/components/output-detail.d.ts.map +1 -0
  76. package/dist/components/output-detail.js +124 -0
  77. package/dist/components/output-detail.js.map +1 -0
  78. package/dist/components/output-dialog.d.ts +18 -0
  79. package/dist/components/output-dialog.d.ts.map +1 -0
  80. package/dist/components/output-dialog.js +11 -0
  81. package/dist/components/output-dialog.js.map +1 -0
  82. package/dist/components/paper-detail.d.ts +34 -0
  83. package/dist/components/paper-detail.d.ts.map +1 -0
  84. package/dist/components/paper-detail.js +60 -0
  85. package/dist/components/paper-detail.js.map +1 -0
  86. package/dist/components/record-dialog.d.ts +33 -0
  87. package/dist/components/record-dialog.d.ts.map +1 -0
  88. package/dist/components/record-dialog.js +114 -0
  89. package/dist/components/record-dialog.js.map +1 -0
  90. package/dist/components/relation-items.d.ts +9 -0
  91. package/dist/components/relation-items.d.ts.map +1 -0
  92. package/dist/components/relation-items.js +20 -0
  93. package/dist/components/relation-items.js.map +1 -0
  94. package/dist/components/use-detail-stack.d.ts +31 -0
  95. package/dist/components/use-detail-stack.d.ts.map +1 -0
  96. package/dist/components/use-detail-stack.js +49 -0
  97. package/dist/components/use-detail-stack.js.map +1 -0
  98. package/dist/lib/cn.d.ts +3 -0
  99. package/dist/lib/cn.d.ts.map +1 -0
  100. package/dist/lib/cn.js +6 -0
  101. package/dist/lib/cn.js.map +1 -0
  102. package/dist/lib/labels.d.ts +61 -0
  103. package/dist/lib/labels.d.ts.map +1 -0
  104. package/dist/lib/labels.js +79 -0
  105. package/dist/lib/labels.js.map +1 -0
  106. package/dist/lib/slot.d.ts +11 -0
  107. package/dist/lib/slot.d.ts.map +1 -0
  108. package/dist/lib/slot.js +83 -0
  109. package/dist/lib/slot.js.map +1 -0
  110. package/dist/model/doi.d.ts +2 -0
  111. package/dist/model/doi.d.ts.map +1 -0
  112. package/dist/model/doi.js +5 -0
  113. package/dist/model/doi.js.map +1 -0
  114. package/dist/model/index.d.ts +10 -0
  115. package/dist/model/index.d.ts.map +1 -0
  116. package/dist/model/index.js +7 -0
  117. package/dist/model/index.js.map +1 -0
  118. package/dist/model/locate-record.d.ts +8 -0
  119. package/dist/model/locate-record.d.ts.map +1 -0
  120. package/dist/model/locate-record.js +7 -0
  121. package/dist/model/locate-record.js.map +1 -0
  122. package/dist/model/papers.d.ts +38 -0
  123. package/dist/model/papers.d.ts.map +1 -0
  124. package/dist/model/papers.js +91 -0
  125. package/dist/model/papers.js.map +1 -0
  126. package/dist/model/records.d.ts +10 -0
  127. package/dist/model/records.d.ts.map +1 -0
  128. package/dist/model/records.js +32 -0
  129. package/dist/model/records.js.map +1 -0
  130. package/dist/model/relations.d.ts +45 -0
  131. package/dist/model/relations.d.ts.map +1 -0
  132. package/dist/model/relations.js +98 -0
  133. package/dist/model/relations.js.map +1 -0
  134. package/dist/primitives/badge.d.ts +11 -0
  135. package/dist/primitives/badge.d.ts.map +1 -0
  136. package/dist/primitives/badge.js +7 -0
  137. package/dist/primitives/badge.js.map +1 -0
  138. package/dist/primitives/button.d.ts +18 -0
  139. package/dist/primitives/button.d.ts.map +1 -0
  140. package/dist/primitives/button.js +12 -0
  141. package/dist/primitives/button.js.map +1 -0
  142. package/dist/primitives/detail-layout.d.ts +26 -0
  143. package/dist/primitives/detail-layout.d.ts.map +1 -0
  144. package/dist/primitives/detail-layout.js +21 -0
  145. package/dist/primitives/detail-layout.js.map +1 -0
  146. package/dist/primitives/dialog.d.ts +133 -0
  147. package/dist/primitives/dialog.d.ts.map +1 -0
  148. package/dist/primitives/dialog.js +266 -0
  149. package/dist/primitives/dialog.js.map +1 -0
  150. package/dist/primitives/index.d.ts +25 -0
  151. package/dist/primitives/index.d.ts.map +1 -0
  152. package/dist/primitives/index.js +14 -0
  153. package/dist/primitives/index.js.map +1 -0
  154. package/dist/primitives/kind.d.ts +6 -0
  155. package/dist/primitives/kind.d.ts.map +1 -0
  156. package/dist/primitives/kind.js +14 -0
  157. package/dist/primitives/kind.js.map +1 -0
  158. package/dist/primitives/prose.d.ts +36 -0
  159. package/dist/primitives/prose.d.ts.map +1 -0
  160. package/dist/primitives/prose.js +57 -0
  161. package/dist/primitives/prose.js.map +1 -0
  162. package/dist/primitives/record-list.d.ts +31 -0
  163. package/dist/primitives/record-list.d.ts.map +1 -0
  164. package/dist/primitives/record-list.js +17 -0
  165. package/dist/primitives/record-list.js.map +1 -0
  166. package/dist/primitives/relation-list.d.ts +22 -0
  167. package/dist/primitives/relation-list.d.ts.map +1 -0
  168. package/dist/primitives/relation-list.js +13 -0
  169. package/dist/primitives/relation-list.js.map +1 -0
  170. package/dist/primitives/surface-header.d.ts +31 -0
  171. package/dist/primitives/surface-header.d.ts.map +1 -0
  172. package/dist/primitives/surface-header.js +13 -0
  173. package/dist/primitives/surface-header.js.map +1 -0
  174. package/dist/views/index.d.ts +4 -0
  175. package/dist/views/index.d.ts.map +1 -0
  176. package/dist/views/index.js +3 -0
  177. package/dist/views/index.js.map +1 -0
  178. package/dist/views/inventory.d.ts +46 -0
  179. package/dist/views/inventory.d.ts.map +1 -0
  180. package/dist/views/inventory.js +101 -0
  181. package/dist/views/inventory.js.map +1 -0
  182. package/package.json +127 -0
  183. package/primitives.css +15 -0
  184. package/src/blocks/analysis-tree.tsx +109 -0
  185. package/src/blocks/decisions-list.tsx +113 -0
  186. package/src/blocks/findings-list.tsx +61 -0
  187. package/src/blocks/index.ts +23 -0
  188. package/src/blocks/inputs-list.tsx +51 -0
  189. package/src/blocks/outputs-list.tsx +143 -0
  190. package/src/blocks/papers-list.tsx +65 -0
  191. package/src/blocks/prior-insights-list.tsx +52 -0
  192. package/src/blocks/section.tsx +113 -0
  193. package/src/components/artifact-preview.tsx +314 -0
  194. package/src/components/decision-detail.tsx +93 -0
  195. package/src/components/detail-entry.ts +19 -0
  196. package/src/components/finding-detail.tsx +99 -0
  197. package/src/components/index.ts +44 -0
  198. package/src/components/input-detail.tsx +67 -0
  199. package/src/components/insight-detail.tsx +120 -0
  200. package/src/components/insight-trigger.tsx +102 -0
  201. package/src/components/output-detail.tsx +258 -0
  202. package/src/components/output-dialog.tsx +63 -0
  203. package/src/components/paper-detail.tsx +216 -0
  204. package/src/components/record-dialog.tsx +232 -0
  205. package/src/components/relation-items.ts +31 -0
  206. package/src/components/use-detail-stack.ts +78 -0
  207. package/src/lib/cn.ts +5 -0
  208. package/src/lib/labels.tsx +145 -0
  209. package/src/lib/slot.tsx +87 -0
  210. package/src/model/doi.ts +5 -0
  211. package/src/model/index.ts +32 -0
  212. package/src/model/locate-record.ts +13 -0
  213. package/src/model/papers.ts +146 -0
  214. package/src/model/records.ts +43 -0
  215. package/src/model/relations.ts +134 -0
  216. package/src/primitives/badge.tsx +32 -0
  217. package/src/primitives/button.tsx +58 -0
  218. package/src/primitives/detail-layout.tsx +84 -0
  219. package/src/primitives/dialog.tsx +555 -0
  220. package/src/primitives/index.ts +48 -0
  221. package/src/primitives/kind.ts +19 -0
  222. package/src/primitives/prose.tsx +74 -0
  223. package/src/primitives/record-list.tsx +103 -0
  224. package/src/primitives/relation-list.tsx +95 -0
  225. package/src/primitives/surface-header.tsx +87 -0
  226. package/src/views/index.ts +3 -0
  227. package/src/views/inventory.tsx +224 -0
  228. package/styles/base.css +49 -0
  229. package/styles/blocks/analysis-tree.css +64 -0
  230. package/styles/blocks/decisions-list.css +73 -0
  231. package/styles/blocks/outputs-list.css +349 -0
  232. package/styles/blocks/papers-list.css +98 -0
  233. package/styles/blocks/records.css +72 -0
  234. package/styles/components/artifact-preview.css +242 -0
  235. package/styles/components/decision-detail.css +159 -0
  236. package/styles/components/evidence.css +79 -0
  237. package/styles/components/finding-detail.css +63 -0
  238. package/styles/components/input-detail.css +95 -0
  239. package/styles/components/insight-detail.css +138 -0
  240. package/styles/components/insight-list.css +46 -0
  241. package/styles/components/insight-trigger.css +54 -0
  242. package/styles/components/output-detail.css +732 -0
  243. package/styles/components/paper-detail.css +249 -0
  244. package/styles/components/paper-doi.css +36 -0
  245. package/styles/primitives/badge.css +107 -0
  246. package/styles/primitives/button.css +116 -0
  247. package/styles/primitives/count-heading.css +27 -0
  248. package/styles/primitives/detail-layout.css +225 -0
  249. package/styles/primitives/dialog.css +634 -0
  250. package/styles/primitives/empty-state.css +10 -0
  251. package/styles/primitives/kind.css +41 -0
  252. package/styles/primitives/prose.css +15 -0
  253. package/styles/primitives/record-list.css +425 -0
  254. package/styles/primitives/relation-list.css +184 -0
  255. package/styles/primitives/surface-header.css +183 -0
  256. package/styles/tokens.css +119 -0
  257. package/styles/views/inventory.css +186 -0
  258. package/styles.css +2 -0
  259. package/views.css +3 -0
@@ -0,0 +1,58 @@
1
+ import { forwardRef, type ButtonHTMLAttributes } from 'react';
2
+ import { cn } from '../lib/cn.js';
3
+ import { Slot } from '../lib/slot.js';
4
+
5
+ export type ButtonVariant = 'primary' | 'secondary' | 'quiet';
6
+ export type ButtonSize = 'small' | 'medium';
7
+ export type ButtonTone = 'neutral' | 'accent';
8
+
9
+ export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
10
+ variant?: ButtonVariant | undefined;
11
+ size?: ButtonSize | undefined;
12
+ tone?: ButtonTone | undefined;
13
+ /** Render the child element instead of a `<button>`, merging props onto it. */
14
+ asChild?: boolean | undefined;
15
+ }
16
+
17
+ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button({
18
+ className,
19
+ variant = 'secondary',
20
+ size = 'medium',
21
+ tone = 'neutral',
22
+ type,
23
+ asChild = false,
24
+ ...props
25
+ }, ref) {
26
+ const Component = asChild ? Slot : 'button';
27
+ return (
28
+ <Component
29
+ data-slot="button"
30
+ {...props}
31
+ {...(asChild ? (type ? { type } : {}) : { type: type ?? 'button' })}
32
+ ref={ref}
33
+ className={cn('astra-button', className)}
34
+ data-variant={variant}
35
+ data-size={size}
36
+ data-tone={tone}
37
+ />
38
+ );
39
+ });
40
+
41
+ export interface IconButtonProps extends ButtonProps {
42
+ /** Accessible name; icon buttons never rely on visible text. */
43
+ label: string;
44
+ }
45
+
46
+ export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(
47
+ function IconButton({ label, className, ...props }, ref) {
48
+ return (
49
+ <Button
50
+ data-slot="icon-button"
51
+ {...props}
52
+ ref={ref}
53
+ className={cn('astra-icon-button', className)}
54
+ aria-label={label}
55
+ />
56
+ );
57
+ },
58
+ );
@@ -0,0 +1,84 @@
1
+ import { forwardRef, type HTMLAttributes, type ReactNode } from 'react';
2
+ import { cn } from '../lib/cn.js';
3
+
4
+ export type DetailLayoutMode = 'split' | 'single';
5
+
6
+ export interface DetailLayoutProps extends HTMLAttributes<HTMLDivElement> {
7
+ /** `split` places a rail beside the main column; `single` is one scrolling column. */
8
+ layout?: DetailLayoutMode | undefined;
9
+ }
10
+
11
+ /** Two-column (or single-column) frame for a record detail body. */
12
+ export const DetailLayout = forwardRef<HTMLDivElement, DetailLayoutProps>(function DetailLayout({
13
+ layout = 'split',
14
+ className,
15
+ ...props
16
+ }, ref) {
17
+ return (
18
+ <div
19
+ data-slot="detail-layout"
20
+ {...props}
21
+ ref={ref}
22
+ className={cn('astra-detail__layout', className)}
23
+ data-layout={layout}
24
+ />
25
+ );
26
+ });
27
+
28
+ export const DetailMain = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(function DetailMain({ className, ...props }, ref) {
29
+ return <div data-slot="detail-main" {...props} ref={ref} className={cn('astra-detail__main', className)} />;
30
+ });
31
+
32
+ export interface DetailRailProps extends HTMLAttributes<HTMLElement> {
33
+ label: string;
34
+ }
35
+
36
+ export const DetailRail = forwardRef<HTMLElement, DetailRailProps>(function DetailRail({ label, className, ...props }, ref) {
37
+ return <aside data-slot="detail-rail" {...props} ref={ref} className={cn('astra-detail__aside', className)} aria-label={label} />;
38
+ });
39
+
40
+ export interface DetailSectionProps extends HTMLAttributes<HTMLElement> {
41
+ label: ReactNode;
42
+ /** `section` renders the label as a section heading rather than an eyebrow. */
43
+ heading?: 'eyebrow' | 'section' | undefined;
44
+ }
45
+
46
+ /** A labelled block of prose or content inside a detail body. */
47
+ export const DetailSection = forwardRef<HTMLElement, DetailSectionProps>(function DetailSection({
48
+ label,
49
+ heading = 'eyebrow',
50
+ className,
51
+ children,
52
+ ...props
53
+ }, ref) {
54
+ return (
55
+ <section
56
+ data-slot="detail-section"
57
+ {...props}
58
+ ref={ref}
59
+ className={cn('astra-detail__section', className)}
60
+ {...(heading === 'section' ? { 'data-heading': 'section' } : {})}
61
+ >
62
+ <span>{label}</span>
63
+ <div>{children}</div>
64
+ </section>
65
+ );
66
+ });
67
+
68
+ export interface CountHeadingProps extends Omit<HTMLAttributes<HTMLHeadingElement>, 'title'> {
69
+ title: ReactNode;
70
+ count: number;
71
+ }
72
+
73
+ export const CountHeading = forwardRef<HTMLHeadingElement, CountHeadingProps>(function CountHeading({
74
+ title,
75
+ count,
76
+ className,
77
+ ...props
78
+ }, ref) {
79
+ return (
80
+ <h4 data-slot="count-heading" {...props} ref={ref} className={cn('astra-count-heading', className)}>
81
+ {title} <span>{count}</span>
82
+ </h4>
83
+ );
84
+ });
@@ -0,0 +1,555 @@
1
+ import {
2
+ createContext,
3
+ forwardRef,
4
+ useCallback,
5
+ useContext,
6
+ useEffect,
7
+ useId,
8
+ useLayoutEffect,
9
+ useMemo,
10
+ useRef,
11
+ type FocusEvent,
12
+ type HTMLAttributes,
13
+ type MouseEvent,
14
+ type ReactNode,
15
+ type Ref,
16
+ type SyntheticEvent,
17
+ } from 'react';
18
+ import { cn } from '../lib/cn.js';
19
+ import { useLabels } from '../lib/labels.js';
20
+ import { Slot } from '../lib/slot.js';
21
+ import { IconButton } from './button.js';
22
+ import type { SurfaceKind } from './kind.js';
23
+ import { SurfaceHeader, type SurfaceHeadingLevel } from './surface-header.js';
24
+
25
+ export type DialogMode = 'modal' | 'embedded';
26
+ export type DialogLayout = 'single' | 'reader';
27
+
28
+ const useIsomorphicLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect;
29
+
30
+ /* ------------------------------------------------------------------ */
31
+ /* Presentation defaults shared by a subtree (mode, back trail) */
32
+ /* ------------------------------------------------------------------ */
33
+
34
+ interface DialogPresentation {
35
+ mode?: DialogMode | undefined;
36
+ backLabel?: string | undefined;
37
+ backText?: string | undefined;
38
+ }
39
+
40
+ const PresentationContext = createContext<DialogPresentation>({});
41
+
42
+ export interface DialogProviderProps extends DialogPresentation {
43
+ children: ReactNode;
44
+ }
45
+
46
+ /**
47
+ * Sets the default mode (modal or embedded) and back-trail text for every
48
+ * Dialog below it. Nested providers inherit what they do not set; explicit
49
+ * props on a Dialog still win.
50
+ */
51
+ export function DialogProvider({ mode, backLabel, backText, children }: DialogProviderProps) {
52
+ const parent = useContext(PresentationContext);
53
+ const value = useMemo<DialogPresentation>(() => ({
54
+ mode: mode ?? parent.mode,
55
+ backLabel: backLabel ?? parent.backLabel,
56
+ backText: backText ?? parent.backText,
57
+ }), [mode, backLabel, backText, parent]);
58
+ return <PresentationContext.Provider value={value}>{children}</PresentationContext.Provider>;
59
+ }
60
+
61
+ /* ------------------------------------------------------------------ */
62
+ /* Dialog root */
63
+ /* ------------------------------------------------------------------ */
64
+
65
+ export interface DialogContextValue {
66
+ open: boolean;
67
+ mode: DialogMode;
68
+ kind: SurfaceKind | undefined;
69
+ layout: DialogLayout | undefined;
70
+ titleId: string;
71
+ backLabel: string;
72
+ backText: string | undefined;
73
+ /** Dismisses the dialog: runs the native close (modal), which then notifies `onOpenChange(false)`. */
74
+ requestClose: () => void;
75
+ /**
76
+ * What Escape does: the innermost dismissal guard handles it when one is
77
+ * registered (an inner layer exits), otherwise the dialog closes.
78
+ */
79
+ requestDismiss: () => void;
80
+ /** Reports a state change to the host; DialogContent calls it from the native `close` event. */
81
+ notifyOpenChange: (open: boolean) => void;
82
+ /** Registers a dismissal guard with its Escape handler; returns the release function. */
83
+ addGuard: (onDismiss?: () => void) => () => void;
84
+ isGuarded: () => boolean;
85
+ /** DialogContent registers the native close steps here so `requestClose` can run them. */
86
+ registerNativeClose: (close: (() => boolean) | null) => void;
87
+ closeRef: { current: HTMLButtonElement | null };
88
+ }
89
+
90
+ const DialogContext = createContext<DialogContextValue | null>(null);
91
+
92
+ export function useDialog(): DialogContextValue {
93
+ const value = useContext(DialogContext);
94
+ if (!value) throw new Error('Dialog parts must be rendered inside <Dialog>.');
95
+ return value;
96
+ }
97
+
98
+ /** The enclosing Dialog, or null outside one. */
99
+ export function useOptionalDialog(): DialogContextValue | null {
100
+ return useContext(DialogContext);
101
+ }
102
+
103
+ export interface DialogProps {
104
+ /** Mounting a Dialog opens it; pass `false` to keep it mounted but closed. */
105
+ open?: boolean | undefined;
106
+ /** Called with `false` when the user dismisses the dialog (Escape, backdrop, close button). */
107
+ onOpenChange?: ((open: boolean) => void) | undefined;
108
+ mode?: DialogMode | undefined;
109
+ kind?: SurfaceKind | undefined;
110
+ layout?: DialogLayout | undefined;
111
+ backLabel?: string | undefined;
112
+ backText?: string | undefined;
113
+ children: ReactNode;
114
+ }
115
+
116
+ export function Dialog({
117
+ open = true,
118
+ onOpenChange,
119
+ mode,
120
+ kind,
121
+ layout,
122
+ backLabel,
123
+ backText,
124
+ children,
125
+ }: DialogProps) {
126
+ const presentation = useContext(PresentationContext);
127
+ const labels = useLabels();
128
+ const titleId = useId();
129
+ const guards = useRef<(() => void)[]>([]);
130
+ const nativeClose = useRef<(() => boolean) | null>(null);
131
+ const closeRef = useRef<HTMLButtonElement | null>(null);
132
+ const onOpenChangeRef = useRef(onOpenChange);
133
+ useEffect(() => { onOpenChangeRef.current = onOpenChange; });
134
+
135
+ const notifyOpenChange = useCallback((next: boolean) => { onOpenChangeRef.current?.(next); }, []);
136
+ // The native close steps restore focus to the opener and fire `close`,
137
+ // which relays the change; fall back to the callback when there is no
138
+ // open <dialog> (embedded mode, or already closed).
139
+ const requestClose = useCallback(() => {
140
+ if (!nativeClose.current?.()) notifyOpenChange(false);
141
+ }, [notifyOpenChange]);
142
+ const addGuard = useCallback((onDismiss?: () => void) => {
143
+ const guard = { onDismiss };
144
+ const entry = () => guard.onDismiss?.();
145
+ guards.current = [...guards.current, entry];
146
+ return () => { guards.current = guards.current.filter((candidate) => candidate !== entry); };
147
+ }, []);
148
+ const isGuarded = useCallback(() => guards.current.length > 0, []);
149
+ // Decided when Escape arrives, not when a keydown listener saw it earlier:
150
+ // a guard released by that keydown would otherwise let the same Escape
151
+ // close the dialog too.
152
+ const requestDismiss = useCallback(() => {
153
+ const guard = guards.current.at(-1);
154
+ if (guard) guard();
155
+ else requestClose();
156
+ }, [requestClose]);
157
+ const registerNativeClose = useCallback((close: (() => boolean) | null) => { nativeClose.current = close; }, []);
158
+
159
+ const value = useMemo<DialogContextValue>(() => ({
160
+ open,
161
+ mode: mode ?? presentation.mode ?? 'modal',
162
+ kind,
163
+ layout,
164
+ titleId,
165
+ backLabel: backLabel ?? presentation.backLabel ?? labels.backTo,
166
+ backText: backText ?? presentation.backText,
167
+ requestClose,
168
+ requestDismiss,
169
+ notifyOpenChange,
170
+ addGuard,
171
+ isGuarded,
172
+ registerNativeClose,
173
+ closeRef,
174
+ }), [open, mode, presentation, kind, layout, titleId, backLabel, backText, labels.backTo, requestClose, requestDismiss, notifyOpenChange, addGuard, isGuarded, registerNativeClose]);
175
+ return <DialogContext.Provider value={value}>{children}</DialogContext.Provider>;
176
+ }
177
+
178
+ /**
179
+ * Registers a dismissal guard while `active`: backdrop clicks are ignored and
180
+ * Escape calls `onDismiss` (the inner layer exits) instead of closing the
181
+ * dialog. Inside a modal Dialog the inner layer must not listen for Escape
182
+ * itself; the `<dialog>`'s cancel event is the one signal that arrives after
183
+ * any host keydown handling. Safe to call outside a Dialog.
184
+ */
185
+ export function useDialogDismissGuard(active: boolean, onDismiss?: () => void): void {
186
+ const context = useContext(DialogContext);
187
+ const addGuard = context?.addGuard;
188
+ const onDismissRef = useRef(onDismiss);
189
+ useEffect(() => { onDismissRef.current = onDismiss; });
190
+ useEffect(() => {
191
+ if (!active || !addGuard) return undefined;
192
+ return addGuard(() => onDismissRef.current?.());
193
+ }, [active, addGuard]);
194
+ }
195
+
196
+ /* ------------------------------------------------------------------ */
197
+ /* Content: the <dialog> (modal) or <div> (embedded) plus the panel */
198
+ /* ------------------------------------------------------------------ */
199
+
200
+ export interface DialogContentProps extends HTMLAttributes<HTMLElement> {
201
+ /** Class name for the inner panel; the root receives `className`. */
202
+ panelClassName?: string | undefined;
203
+ /** Element focused when the dialog opens; defaults to the close button. */
204
+ initialFocusRef?: { current: HTMLElement | null } | undefined;
205
+ }
206
+
207
+ export const DialogContent = forwardRef<HTMLElement, DialogContentProps>(function DialogContent({
208
+ className,
209
+ panelClassName,
210
+ initialFocusRef,
211
+ children,
212
+ onMouseDown,
213
+ onFocus,
214
+ ...props
215
+ }, ref) {
216
+ const { open, mode, kind, layout, titleId, requestClose, requestDismiss, notifyOpenChange, isGuarded, registerNativeClose, closeRef } = useDialog();
217
+ const dialogRef = useRef<HTMLDialogElement | null>(null);
218
+ // The element that had focus when the dialog opened; focus returns to it on
219
+ // close (browsers do this for showModal() too, but not every environment).
220
+ const openerRef = useRef<HTMLElement | null>(null);
221
+ // Set while this component closes the <dialog> itself (host set `open`
222
+ // to false, or unmount), so the resulting `close` event is not reported
223
+ // back as a user dismissal. React StrictMode's simulated unmount would
224
+ // otherwise close every dialog right after it opened in development.
225
+ const closingSilently = useRef(false);
226
+ // The last element focused inside the dialog; when a render removes it
227
+ // (drill-down/back swaps the body), focus is kept inside the dialog.
228
+ const lastFocused = useRef<Element | null>(null);
229
+ const restoreFocus = useCallback(() => {
230
+ const opener = openerRef.current;
231
+ openerRef.current = null;
232
+ const active = document.activeElement;
233
+ if (opener?.isConnected && (!active || active === document.body || dialogRef.current?.contains(active))) opener.focus();
234
+ }, []);
235
+ const setRefs = useCallback((node: HTMLDialogElement | null) => {
236
+ dialogRef.current = node;
237
+ if (typeof ref === 'function') ref(node);
238
+ else if (ref) ref.current = node;
239
+ }, [ref]);
240
+
241
+ // Let the root's requestClose run the native close steps.
242
+ useIsomorphicLayoutEffect(() => {
243
+ registerNativeClose(() => {
244
+ const dialog = dialogRef.current;
245
+ if (mode !== 'modal' || !dialog?.open) return false;
246
+ dialog.close();
247
+ return true;
248
+ });
249
+ return () => { registerNativeClose(null); };
250
+ }, [mode, registerNativeClose]);
251
+
252
+ // Open/close the native dialog to match `open`.
253
+ useIsomorphicLayoutEffect(() => {
254
+ if (mode !== 'modal') return;
255
+ const dialog = dialogRef.current;
256
+ if (!dialog) return;
257
+ if (open && !dialog.open) {
258
+ openerRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
259
+ dialog.showModal();
260
+ (initialFocusRef?.current ?? closeRef.current)?.focus();
261
+ } else if (!open && dialog.open) {
262
+ closingSilently.current = true;
263
+ dialog.close();
264
+ restoreFocus();
265
+ }
266
+ }, [open, mode, initialFocusRef, closeRef, restoreFocus]);
267
+
268
+ // Closing on unmount keeps the browser's focus restoration: close()
269
+ // returns focus to the element that opened the dialog, removal does not.
270
+ useIsomorphicLayoutEffect(() => () => {
271
+ const dialog = dialogRef.current;
272
+ if (dialog?.open) {
273
+ closingSilently.current = true;
274
+ dialog.close();
275
+ }
276
+ restoreFocus();
277
+ }, [restoreFocus]);
278
+
279
+ // When the body swaps (drill-down/back) and the element that had focus was
280
+ // removed, keep focus inside the dialog rather than letting it fall to
281
+ // <body>. Focus that the user moved elsewhere (a host portal, a click on
282
+ // plain text) is left alone.
283
+ useEffect(() => {
284
+ const dialog = dialogRef.current;
285
+ const last = lastFocused.current;
286
+ if (mode !== 'modal' || !dialog?.open || !last || last.isConnected) return;
287
+ lastFocused.current = null;
288
+ closeRef.current?.focus();
289
+ });
290
+
291
+ const handleCancel = (event: SyntheticEvent<HTMLDialogElement>) => {
292
+ // Escape. Close (or let the guard handle it) explicitly rather than via
293
+ // the browser's default, which is deterministic across browsers
294
+ // (Chromium makes a repeated Escape non-cancelable) and test environments.
295
+ event.preventDefault();
296
+ requestDismiss();
297
+ };
298
+ const handleClose = () => {
299
+ if (closingSilently.current) {
300
+ closingSilently.current = false;
301
+ return;
302
+ }
303
+ restoreFocus();
304
+ if (open) notifyOpenChange(false);
305
+ };
306
+ const handleFocus = (event: FocusEvent<HTMLElement>) => {
307
+ onFocus?.(event);
308
+ lastFocused.current = event.target;
309
+ };
310
+ const handleMouseDown = (event: MouseEvent<HTMLElement>) => {
311
+ onMouseDown?.(event);
312
+ if (event.defaultPrevented) return;
313
+ if (event.target === event.currentTarget && event.button === 0 && !isGuarded()) requestClose();
314
+ };
315
+
316
+ const data = {
317
+ 'data-slot': 'dialog',
318
+ 'data-mode': mode,
319
+ ...(kind ? { 'data-kind': kind } : {}),
320
+ ...(layout ? { 'data-layout': layout } : {}),
321
+ };
322
+ const panel = (
323
+ <section
324
+ data-slot="dialog-panel"
325
+ className={cn('astra-dialog__panel', panelClassName)}
326
+ {...(kind ? { 'data-kind': kind } : {})}
327
+ aria-labelledby={titleId}
328
+ >
329
+ {children}
330
+ </section>
331
+ );
332
+
333
+ if (mode === 'embedded') {
334
+ if (!open) return null;
335
+ return (
336
+ // eslint-disable-next-line jsx-a11y/no-static-element-interactions -- host handler passthrough only
337
+ <div {...data} {...props} ref={ref as Ref<HTMLDivElement>} className={cn('astra-dialog', className)} onMouseDown={onMouseDown} onFocus={onFocus}>
338
+ {panel}
339
+ </div>
340
+ );
341
+ }
342
+ return (
343
+ // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- backdrop click dismisses the modal
344
+ <dialog
345
+ {...data}
346
+ {...props}
347
+ ref={setRefs}
348
+ className={cn('astra-dialog', className)}
349
+ aria-modal="true"
350
+ aria-labelledby={titleId}
351
+ onCancel={handleCancel}
352
+ onClose={handleClose}
353
+ onFocus={handleFocus}
354
+ onMouseDown={handleMouseDown}
355
+ >
356
+ {panel}
357
+ </dialog>
358
+ );
359
+ });
360
+
361
+ /* ------------------------------------------------------------------ */
362
+ /* Header and body parts */
363
+ /* ------------------------------------------------------------------ */
364
+
365
+ export interface DialogHeaderProps {
366
+ /** Short kind label shown in the trail (e.g. "Decision"). */
367
+ kindLabel?: ReactNode | undefined;
368
+ title: ReactNode;
369
+ titleAs?: SurfaceHeadingLevel | undefined;
370
+ identifier?: ReactNode | undefined;
371
+ /** Renders the back control; the trail text comes from Dialog/DialogProvider `backText`. */
372
+ onBack?: (() => void) | undefined;
373
+ /** Extra controls rendered before the close button. */
374
+ actions?: ReactNode | undefined;
375
+ closeLabel?: string | undefined;
376
+ showCloseButton?: boolean | undefined;
377
+ className?: string | undefined;
378
+ }
379
+
380
+ export function DialogHeader({
381
+ kindLabel,
382
+ title,
383
+ titleAs = 'h3',
384
+ identifier,
385
+ onBack,
386
+ actions,
387
+ closeLabel,
388
+ showCloseButton = true,
389
+ className,
390
+ }: DialogHeaderProps) {
391
+ const { kind, titleId, backText } = useDialog();
392
+ return (
393
+ <SurfaceHeader
394
+ data-slot="dialog-header"
395
+ className={cn('astra-dialog__header', className)}
396
+ classNames={{ actions: 'astra-dialog__actions' }}
397
+ kind={kind}
398
+ density="compact"
399
+ leading={onBack ? <DialogBack onClick={onBack} /> : undefined}
400
+ eyebrow={(
401
+ <span data-slot="dialog-trail" className="astra-dialog__trail">
402
+ {onBack && backText ? (
403
+ <>
404
+ <span className="astra-dialog__crumb">{backText}</span>
405
+ <span className="astra-dialog__trail-separator" aria-hidden="true">▸</span>
406
+ </>
407
+ ) : null}
408
+ {kindLabel != null ? <span data-slot="dialog-kind" className="astra-dialog__kind">{kindLabel}</span> : null}
409
+ </span>
410
+ )}
411
+ title={title}
412
+ titleId={titleId}
413
+ titleAs={titleAs}
414
+ identifier={identifier}
415
+ actions={(
416
+ <>
417
+ {actions}
418
+ {showCloseButton ? <DialogClose label={closeLabel} /> : null}
419
+ </>
420
+ )}
421
+ />
422
+ );
423
+ }
424
+
425
+ export interface DialogCloseProps {
426
+ /** Accessible name; defaults to the labels' close text. */
427
+ label?: string | undefined;
428
+ className?: string | undefined;
429
+ }
430
+
431
+ /** The dialog's close control; dismisses through the Dialog root. */
432
+ export function DialogClose({ label, className }: DialogCloseProps) {
433
+ const { requestClose, closeRef } = useDialog();
434
+ const labels = useLabels();
435
+ return (
436
+ <IconButton
437
+ ref={closeRef}
438
+ data-slot="dialog-close"
439
+ className={className}
440
+ label={label ?? labels.close}
441
+ onClick={requestClose}
442
+ title={labels.close}
443
+ >
444
+ ×
445
+ </IconButton>
446
+ );
447
+ }
448
+
449
+ export interface DialogBackProps {
450
+ onClick: () => void;
451
+ className?: string | undefined;
452
+ }
453
+
454
+ export function DialogBack({ onClick, className }: DialogBackProps) {
455
+ const { backLabel } = useDialog();
456
+ const labels = useLabels();
457
+ return (
458
+ <IconButton
459
+ data-slot="dialog-back"
460
+ className={cn('astra-dialog__back', className)}
461
+ label={backLabel}
462
+ onClick={onClick}
463
+ title={labels.back}
464
+ >
465
+ <span aria-hidden="true">←</span>
466
+ </IconButton>
467
+ );
468
+ }
469
+
470
+ export interface DialogActionProps extends HTMLAttributes<HTMLElement> {
471
+ /** Render the child (e.g. an `<a>`) instead of a `<button>`. */
472
+ asChild?: boolean | undefined;
473
+ }
474
+
475
+ /** A text action for the dialog header (e.g. "Open ↗"); pass `asChild` to render a link. */
476
+ export const DialogAction = forwardRef<HTMLElement, DialogActionProps>(function DialogAction({ asChild = false, className, ...props }, ref) {
477
+ const Component = asChild ? Slot : 'button';
478
+ return (
479
+ <Component
480
+ data-slot="dialog-action"
481
+ {...props}
482
+ {...(asChild ? {} : { type: 'button' })}
483
+ ref={ref as never}
484
+ className={cn('astra-dialog__action', className)}
485
+ />
486
+ );
487
+ });
488
+
489
+ export const DialogBody = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(function DialogBody({ className, ...props }, ref) {
490
+ return <div data-slot="dialog-body" {...props} ref={ref} className={cn('astra-dialog__body', className)} />;
491
+ });
492
+
493
+ /* ------------------------------------------------------------------ */
494
+ /* Preset: the record detail shell */
495
+ /* ------------------------------------------------------------------ */
496
+
497
+ export interface DetailDialogProps
498
+ extends Omit<DialogProps, 'children' | 'onOpenChange'>, Omit<HTMLAttributes<HTMLElement>, 'title' | 'children' | 'onClose'> {
499
+ kindLabel?: ReactNode | undefined;
500
+ title: ReactNode;
501
+ titleAs?: SurfaceHeadingLevel | undefined;
502
+ identifier?: ReactNode | undefined;
503
+ onBack?: (() => void) | undefined;
504
+ actions?: ReactNode | undefined;
505
+ closeLabel?: string | undefined;
506
+ onClose: () => void;
507
+ panelClassName?: string | undefined;
508
+ children: ReactNode;
509
+ }
510
+
511
+ /**
512
+ * Dialog + DialogContent + DialogHeader + DialogBody with the record-detail
513
+ * chrome. Every kind dialog and the inventory explorer build on this; hosts
514
+ * that need a different header compose the parts directly. HTML attributes
515
+ * land on the dialog root.
516
+ */
517
+ export function DetailDialog({
518
+ open,
519
+ mode,
520
+ kind,
521
+ layout,
522
+ backLabel,
523
+ backText,
524
+ kindLabel,
525
+ title,
526
+ titleAs,
527
+ identifier,
528
+ onBack,
529
+ actions,
530
+ closeLabel,
531
+ onClose,
532
+ panelClassName,
533
+ children,
534
+ ...content
535
+ }: DetailDialogProps) {
536
+ const onCloseRef = useRef(onClose);
537
+ useEffect(() => { onCloseRef.current = onClose; });
538
+ const onOpenChange = useCallback((next: boolean) => { if (!next) onCloseRef.current(); }, []);
539
+ return (
540
+ <Dialog open={open} mode={mode} kind={kind} layout={layout} backLabel={backLabel} backText={backText} onOpenChange={onOpenChange}>
541
+ <DialogContent {...content} panelClassName={panelClassName}>
542
+ <DialogHeader
543
+ kindLabel={kindLabel}
544
+ title={title}
545
+ titleAs={titleAs}
546
+ identifier={identifier}
547
+ onBack={onBack}
548
+ actions={actions}
549
+ closeLabel={closeLabel}
550
+ />
551
+ <DialogBody>{children}</DialogBody>
552
+ </DialogContent>
553
+ </Dialog>
554
+ );
555
+ }