@astra-spec/ui 0.0.2 → 0.0.4

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 (247) hide show
  1. package/README.md +231 -20
  2. package/TOKENS.md +10 -3
  3. package/components.css +2 -0
  4. package/dist/blocks/decisions-list.d.ts +0 -1
  5. package/dist/blocks/decisions-list.d.ts.map +1 -1
  6. package/dist/blocks/decisions-list.js +1 -5
  7. package/dist/blocks/decisions-list.js.map +1 -1
  8. package/dist/blocks/index.d.ts +3 -3
  9. package/dist/blocks/index.d.ts.map +1 -1
  10. package/dist/blocks/index.js +2 -2
  11. package/dist/blocks/index.js.map +1 -1
  12. package/dist/blocks/inputs-list.js +1 -1
  13. package/dist/blocks/inputs-list.js.map +1 -1
  14. package/dist/blocks/outputs-list.d.ts +2 -2
  15. package/dist/blocks/outputs-list.d.ts.map +1 -1
  16. package/dist/blocks/outputs-list.js +15 -19
  17. package/dist/blocks/outputs-list.js.map +1 -1
  18. package/dist/blocks/section.d.ts +1 -4
  19. package/dist/blocks/section.d.ts.map +1 -1
  20. package/dist/blocks/section.js +1 -12
  21. package/dist/blocks/section.js.map +1 -1
  22. package/dist/components/artifact-preview.d.ts +2 -54
  23. package/dist/components/artifact-preview.d.ts.map +1 -1
  24. package/dist/components/artifact-preview.js +2 -107
  25. package/dist/components/artifact-preview.js.map +1 -1
  26. package/dist/components/decision-detail.d.ts +1 -1
  27. package/dist/components/decision-detail.d.ts.map +1 -1
  28. package/dist/components/decision-detail.js.map +1 -1
  29. package/dist/components/finding-detail.d.ts +2 -2
  30. package/dist/components/finding-detail.d.ts.map +1 -1
  31. package/dist/components/finding-detail.js.map +1 -1
  32. package/dist/components/index.d.ts +11 -13
  33. package/dist/components/index.d.ts.map +1 -1
  34. package/dist/components/index.js +7 -8
  35. package/dist/components/index.js.map +1 -1
  36. package/dist/components/input-detail.d.ts +1 -2
  37. package/dist/components/input-detail.d.ts.map +1 -1
  38. package/dist/components/input-detail.js +0 -3
  39. package/dist/components/input-detail.js.map +1 -1
  40. package/dist/components/insight-detail.d.ts +2 -8
  41. package/dist/components/insight-detail.d.ts.map +1 -1
  42. package/dist/components/insight-detail.js +5 -11
  43. package/dist/components/insight-detail.js.map +1 -1
  44. package/dist/components/insight-trigger.d.ts +1 -1
  45. package/dist/components/insight-trigger.d.ts.map +1 -1
  46. package/dist/components/insight-trigger.js.map +1 -1
  47. package/dist/components/output-detail.d.ts +2 -7
  48. package/dist/components/output-detail.d.ts.map +1 -1
  49. package/dist/components/output-detail.js +3 -30
  50. package/dist/components/output-detail.js.map +1 -1
  51. package/dist/components/output-dialog.d.ts +2 -2
  52. package/dist/components/output-dialog.d.ts.map +1 -1
  53. package/dist/components/output-dialog.js +2 -1
  54. package/dist/components/output-dialog.js.map +1 -1
  55. package/dist/components/paper-detail.d.ts +14 -14
  56. package/dist/components/paper-detail.d.ts.map +1 -1
  57. package/dist/components/paper-detail.js +49 -29
  58. package/dist/components/paper-detail.js.map +1 -1
  59. package/dist/components/paper-pdf-viewer.d.ts +19 -0
  60. package/dist/components/paper-pdf-viewer.d.ts.map +1 -0
  61. package/dist/components/paper-pdf-viewer.js +250 -0
  62. package/dist/components/paper-pdf-viewer.js.map +1 -0
  63. package/dist/components/record-dialog.d.ts +7 -5
  64. package/dist/components/record-dialog.d.ts.map +1 -1
  65. package/dist/components/record-dialog.js +7 -5
  66. package/dist/components/record-dialog.js.map +1 -1
  67. package/dist/components/record-preview.d.ts +61 -0
  68. package/dist/components/record-preview.d.ts.map +1 -0
  69. package/dist/components/record-preview.js +177 -0
  70. package/dist/components/record-preview.js.map +1 -0
  71. package/dist/components/relation-items.d.ts +1 -1
  72. package/dist/components/relation-items.d.ts.map +1 -1
  73. package/dist/components/relation-items.js +10 -3
  74. package/dist/components/relation-items.js.map +1 -1
  75. package/dist/{components/use-detail-stack.d.ts → lib/detail-stack.d.ts} +17 -2
  76. package/dist/lib/detail-stack.d.ts.map +1 -0
  77. package/dist/{components/use-detail-stack.js → lib/detail-stack.js} +15 -2
  78. package/dist/lib/detail-stack.js.map +1 -0
  79. package/dist/lib/index.d.ts +14 -0
  80. package/dist/lib/index.d.ts.map +1 -0
  81. package/dist/lib/index.js +9 -0
  82. package/dist/lib/index.js.map +1 -0
  83. package/dist/lib/labels.d.ts +32 -0
  84. package/dist/lib/labels.d.ts.map +1 -1
  85. package/dist/lib/labels.js +32 -0
  86. package/dist/lib/labels.js.map +1 -1
  87. package/dist/lib/pdf-quote.d.ts +47 -0
  88. package/dist/lib/pdf-quote.d.ts.map +1 -0
  89. package/dist/lib/pdf-quote.js +154 -0
  90. package/dist/lib/pdf-quote.js.map +1 -0
  91. package/dist/lib/pdf-runtime.d.ts +102 -0
  92. package/dist/lib/pdf-runtime.d.ts.map +1 -0
  93. package/dist/lib/pdf-runtime.js +48 -0
  94. package/dist/lib/pdf-runtime.js.map +1 -0
  95. package/dist/lib/preview-data.d.ts +55 -0
  96. package/dist/lib/preview-data.d.ts.map +1 -0
  97. package/dist/lib/preview-data.js +108 -0
  98. package/dist/lib/preview-data.js.map +1 -0
  99. package/dist/lib/prose.d.ts +37 -0
  100. package/dist/lib/prose.d.ts.map +1 -0
  101. package/dist/lib/prose.js +60 -0
  102. package/dist/lib/prose.js.map +1 -0
  103. package/dist/lib/use-isomorphic-layout-effect.d.ts +4 -0
  104. package/dist/lib/use-isomorphic-layout-effect.d.ts.map +1 -0
  105. package/dist/lib/use-isomorphic-layout-effect.js +4 -0
  106. package/dist/lib/use-isomorphic-layout-effect.js.map +1 -0
  107. package/dist/lib/use-output-expanded.d.ts +7 -0
  108. package/dist/lib/use-output-expanded.d.ts.map +1 -0
  109. package/dist/lib/use-output-expanded.js +29 -0
  110. package/dist/lib/use-output-expanded.js.map +1 -0
  111. package/dist/model/index.d.ts +5 -3
  112. package/dist/model/index.d.ts.map +1 -1
  113. package/dist/model/index.js +3 -2
  114. package/dist/model/index.js.map +1 -1
  115. package/dist/{primitives → model}/kind.d.ts +3 -0
  116. package/dist/model/kind.d.ts.map +1 -0
  117. package/dist/model/kind.js +25 -0
  118. package/dist/model/kind.js.map +1 -0
  119. package/dist/model/papers.d.ts +6 -6
  120. package/dist/model/papers.d.ts.map +1 -1
  121. package/dist/model/papers.js +8 -0
  122. package/dist/model/papers.js.map +1 -1
  123. package/dist/model/records.d.ts +5 -1
  124. package/dist/model/records.d.ts.map +1 -1
  125. package/dist/model/records.js +9 -0
  126. package/dist/model/records.js.map +1 -1
  127. package/dist/primitives/badge.d.ts +1 -1
  128. package/dist/primitives/badge.d.ts.map +1 -1
  129. package/dist/primitives/button.js +1 -1
  130. package/dist/primitives/button.js.map +1 -1
  131. package/dist/primitives/dialog.d.ts +1 -1
  132. package/dist/primitives/dialog.d.ts.map +1 -1
  133. package/dist/primitives/dialog.js +3 -3
  134. package/dist/primitives/dialog.js.map +1 -1
  135. package/dist/primitives/index.d.ts +6 -9
  136. package/dist/primitives/index.d.ts.map +1 -1
  137. package/dist/primitives/index.js +3 -5
  138. package/dist/primitives/index.js.map +1 -1
  139. package/dist/primitives/preview-popover.d.ts +30 -0
  140. package/dist/primitives/preview-popover.d.ts.map +1 -0
  141. package/dist/primitives/preview-popover.js +132 -0
  142. package/dist/primitives/preview-popover.js.map +1 -0
  143. package/dist/primitives/prose.d.ts +1 -29
  144. package/dist/primitives/prose.d.ts.map +1 -1
  145. package/dist/primitives/prose.js +2 -50
  146. package/dist/primitives/prose.js.map +1 -1
  147. package/dist/primitives/record-list.d.ts +1 -1
  148. package/dist/primitives/record-list.d.ts.map +1 -1
  149. package/dist/primitives/record-list.js +1 -1
  150. package/dist/primitives/record-list.js.map +1 -1
  151. package/dist/primitives/relation-list.d.ts +1 -1
  152. package/dist/primitives/relation-list.d.ts.map +1 -1
  153. package/dist/primitives/relation-list.js +1 -1
  154. package/dist/primitives/relation-list.js.map +1 -1
  155. package/dist/primitives/slot.d.ts.map +1 -0
  156. package/dist/{lib → primitives}/slot.js +1 -1
  157. package/dist/primitives/slot.js.map +1 -0
  158. package/dist/primitives/surface-header.d.ts +1 -1
  159. package/dist/primitives/surface-header.d.ts.map +1 -1
  160. package/dist/views/inventory.d.ts +7 -5
  161. package/dist/views/inventory.d.ts.map +1 -1
  162. package/dist/views/inventory.js +5 -4
  163. package/dist/views/inventory.js.map +1 -1
  164. package/package.json +12 -1
  165. package/primitives.css +1 -0
  166. package/src/blocks/decisions-list.tsx +1 -6
  167. package/src/blocks/index.ts +2 -3
  168. package/src/blocks/inputs-list.tsx +1 -1
  169. package/src/blocks/outputs-list.tsx +53 -30
  170. package/src/blocks/section.tsx +1 -14
  171. package/src/components/artifact-preview.tsx +6 -161
  172. package/src/components/decision-detail.tsx +2 -1
  173. package/src/components/finding-detail.tsx +3 -2
  174. package/src/components/index.ts +19 -29
  175. package/src/components/input-detail.tsx +2 -5
  176. package/src/components/insight-detail.tsx +20 -35
  177. package/src/components/insight-trigger.tsx +2 -1
  178. package/src/components/output-detail.tsx +17 -38
  179. package/src/components/output-dialog.tsx +4 -3
  180. package/src/components/paper-detail.tsx +121 -66
  181. package/src/components/paper-pdf-viewer.tsx +319 -0
  182. package/src/components/record-dialog.tsx +15 -10
  183. package/src/components/record-preview.tsx +762 -0
  184. package/src/components/relation-items.ts +15 -5
  185. package/src/{components/use-detail-stack.ts → lib/detail-stack.ts} +24 -1
  186. package/src/lib/index.ts +22 -0
  187. package/src/lib/labels.tsx +67 -0
  188. package/src/lib/pdf-quote.ts +189 -0
  189. package/src/lib/pdf-runtime.ts +131 -0
  190. package/src/lib/preview-data.ts +155 -0
  191. package/src/lib/prose.tsx +87 -0
  192. package/src/lib/use-isomorphic-layout-effect.ts +4 -0
  193. package/src/lib/use-output-expanded.ts +27 -0
  194. package/src/model/index.ts +4 -2
  195. package/src/{primitives → model}/kind.ts +14 -0
  196. package/src/model/papers.ts +9 -7
  197. package/src/model/records.ts +12 -0
  198. package/src/primitives/badge.tsx +1 -1
  199. package/src/primitives/button.tsx +1 -1
  200. package/src/primitives/dialog.tsx +3 -5
  201. package/src/primitives/index.ts +9 -9
  202. package/src/primitives/preview-popover.tsx +278 -0
  203. package/src/primitives/prose.tsx +1 -63
  204. package/src/primitives/record-list.tsx +1 -1
  205. package/src/primitives/relation-list.tsx +1 -1
  206. package/src/{lib → primitives}/slot.tsx +1 -1
  207. package/src/primitives/surface-header.tsx +1 -1
  208. package/src/views/inventory.tsx +12 -8
  209. package/styles/blocks/analysis-tree.css +2 -2
  210. package/styles/blocks/decisions-list.css +4 -4
  211. package/styles/blocks/outputs-list.css +246 -19
  212. package/styles/blocks/papers-list.css +2 -2
  213. package/styles/components/artifact-preview.css +6 -6
  214. package/styles/components/decision-detail.css +2 -2
  215. package/styles/components/evidence.css +2 -2
  216. package/styles/components/input-detail.css +4 -4
  217. package/styles/components/insight-detail.css +56 -94
  218. package/styles/components/insight-list.css +3 -3
  219. package/styles/components/insight-trigger.css +1 -4
  220. package/styles/components/output-detail.css +25 -140
  221. package/styles/components/paper-detail.css +76 -95
  222. package/styles/components/paper-doi.css +3 -12
  223. package/styles/components/paper-pdf-viewer.css +120 -0
  224. package/styles/components/record-preview.css +316 -0
  225. package/styles/primitives/badge.css +1 -1
  226. package/styles/primitives/count-heading.css +2 -2
  227. package/styles/primitives/detail-layout.css +5 -5
  228. package/styles/primitives/dialog.css +9 -9
  229. package/styles/primitives/preview-popover.css +76 -0
  230. package/styles/primitives/record-list.css +17 -17
  231. package/styles/primitives/relation-list.css +6 -6
  232. package/styles/primitives/surface-header.css +5 -5
  233. package/styles/tokens.css +16 -3
  234. package/styles/views/inventory.css +3 -3
  235. package/dist/components/detail-entry.d.ts +0 -15
  236. package/dist/components/detail-entry.d.ts.map +0 -1
  237. package/dist/components/detail-entry.js +0 -14
  238. package/dist/components/detail-entry.js.map +0 -1
  239. package/dist/components/use-detail-stack.d.ts.map +0 -1
  240. package/dist/components/use-detail-stack.js.map +0 -1
  241. package/dist/lib/slot.d.ts.map +0 -1
  242. package/dist/lib/slot.js.map +0 -1
  243. package/dist/primitives/kind.d.ts.map +0 -1
  244. package/dist/primitives/kind.js +0 -14
  245. package/dist/primitives/kind.js.map +0 -1
  246. package/src/components/detail-entry.ts +0 -19
  247. /package/dist/{lib → primitives}/slot.d.ts +0 -0
@@ -0,0 +1,87 @@
1
+ /** Authored-prose machinery: the token parser and the built-in KaTeX renderer behind the `Prose` element. */
2
+ import { Fragment, type ReactNode } from 'react';
3
+ import { renderToString } from 'katex';
4
+
5
+ export type ProseField = 'claim' | 'rationale' | 'description' | 'notes' | 'quote';
6
+
7
+ export interface ProseContext {
8
+ /** Which authored field is being rendered, when known. */
9
+ field?: ProseField | undefined;
10
+ }
11
+
12
+ /** Host-owned rendering for authored prose (Markdown, citations, ...); replaces the built-in rendering. */
13
+ export type TextRenderer = (text: string, context: ProseContext) => ReactNode;
14
+
15
+ export type ProseToken =
16
+ | { type: 'text'; value: string }
17
+ | { type: 'inlineCode'; value: string }
18
+ | { type: 'inlineMath'; value: string }
19
+ | { type: 'math'; value: string };
20
+
21
+ /** String macro expansions accepted by the built-in KaTeX renderer. */
22
+ export type ProseMathMacros = Readonly<Record<string, string>>;
23
+
24
+ export interface ProseRenderOptions {
25
+ /** Host-normalized math macros, such as `{ '\\vect': '\\mathbf{#1}' }`. */
26
+ macros?: ProseMathMacros | undefined;
27
+ }
28
+
29
+ /** Balanced tokens: display math first, then `code`, then inline math. */
30
+ const PROSE_TOKEN = /(\$\$[\s\S]+?\$\$|`[^`\n]+`|\$[^$\n]+\$)/g;
31
+
32
+ /** Splits authored prose into text, inline `code`, `$inline$` and `$$display$$` math. */
33
+ export function parseProse(text: string): ProseToken[] {
34
+ const nodes: ProseToken[] = [];
35
+ let last = 0;
36
+ for (const match of text.matchAll(PROSE_TOKEN)) {
37
+ const index = match.index;
38
+ if (index > last) nodes.push({ type: 'text', value: text.slice(last, index) });
39
+ const token = match[0];
40
+ if (token.startsWith('$$')) nodes.push({ type: 'math', value: token.slice(2, -2).trim() });
41
+ else if (token.startsWith('`')) nodes.push({ type: 'inlineCode', value: token.slice(1, -1) });
42
+ else nodes.push({ type: 'inlineMath', value: token.slice(1, -1) });
43
+ last = index + token.length;
44
+ }
45
+ if (last < text.length) nodes.push({ type: 'text', value: text.slice(last) });
46
+ return nodes;
47
+ }
48
+
49
+ function renderMath(
50
+ value: string,
51
+ displayMode: boolean,
52
+ macros?: ProseMathMacros,
53
+ ): string {
54
+ return renderToString(value, {
55
+ displayMode,
56
+ output: 'htmlAndMathml',
57
+ strict: 'ignore',
58
+ throwOnError: false,
59
+ // KaTeX may mutate its macro object for global definitions. Keep a host's
60
+ // shared configuration immutable across independent prose fields.
61
+ ...(macros ? { macros: { ...macros } } : {}),
62
+ });
63
+ }
64
+
65
+ /**
66
+ * The built-in rendering: inline `code`, and `$inline$` / `$$display$$` math
67
+ * typeset with KaTeX (load `katex/dist/katex.css`; the stylesheet bundles
68
+ * import it). Text without those markers is returned as is.
69
+ */
70
+ export function renderProse(
71
+ text: string,
72
+ options: ProseRenderOptions = {},
73
+ ): ReactNode {
74
+ if (!/[`$]/.test(text)) return text;
75
+ return parseProse(text).map((node, index) => {
76
+ switch (node.type) {
77
+ case 'inlineCode':
78
+ return <code key={index}>{node.value}</code>;
79
+ case 'inlineMath':
80
+ return <span key={index} className="astra-prose__inline-math" dangerouslySetInnerHTML={{ __html: renderMath(node.value, false, options.macros) }} />;
81
+ case 'math':
82
+ return <div key={index} className="astra-prose__display-math" dangerouslySetInnerHTML={{ __html: renderMath(node.value, true, options.macros) }} />;
83
+ default:
84
+ return <Fragment key={index}>{node.value}</Fragment>;
85
+ }
86
+ });
87
+ }
@@ -0,0 +1,4 @@
1
+ import { useEffect, useLayoutEffect } from 'react';
2
+
3
+ /** `useLayoutEffect` in the browser; on the server, where layout effects never run, the silent `useEffect`. */
4
+ export const useIsomorphicLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect;
@@ -0,0 +1,27 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import type { ResolvedOutput } from '@astra-spec/sdk';
3
+
4
+ /** Full-screen state for an output, reset whenever the output changes (a controlled host is asked to reset). */
5
+ export function useOutputExpanded(output: ResolvedOutput, controlled?: { expanded?: boolean | undefined; onExpandedChange?: ((next: boolean) => void) | undefined }) {
6
+ const [internal, setInternal] = useState(false);
7
+ const [lastPath, setLastPath] = useState(output.canonicalPath);
8
+ if (lastPath !== output.canonicalPath) {
9
+ setLastPath(output.canonicalPath);
10
+ setInternal(false);
11
+ }
12
+ const isControlled = controlled?.expanded !== undefined;
13
+ const expanded = isControlled ? Boolean(controlled.expanded) : internal;
14
+ const onChange = controlled?.onExpandedChange;
15
+ const seenPath = useRef(output.canonicalPath);
16
+ const controlledExpanded = isControlled && Boolean(controlled.expanded);
17
+ useEffect(() => {
18
+ if (seenPath.current === output.canonicalPath) return;
19
+ seenPath.current = output.canonicalPath;
20
+ if (controlledExpanded) onChange?.(false);
21
+ }, [output.canonicalPath, controlledExpanded, onChange]);
22
+ const setExpanded = useCallback((next: boolean) => {
23
+ if (!isControlled) setInternal(next);
24
+ onChange?.(next);
25
+ }, [isControlled, onChange]);
26
+ return [expanded, setExpanded] as const;
27
+ }
@@ -1,5 +1,7 @@
1
1
  /** Model: pure, React-free derivations over the SDK's resolved analysis. */
2
- export { analysisTitle, countLabel, isInsight, isVisualOutput, recordTitle, selectedOptionLabel } from './records.js';
2
+ export { analysisTitle, countLabel, decisionTagLabel, inputSourceLabel, isInsight, isVisualOutput, recordTitle, selectedOptionLabel } from './records.js';
3
+ export { sectionKind, surfaceGlyph } from './kind.js';
4
+ export type { InventorySectionId, SurfaceKind } from './kind.js';
3
5
  export { locateRecord } from './locate-record.js';
4
6
  export type { LocatedRecord } from './locate-record.js';
5
7
  export {
@@ -21,12 +23,12 @@ export {
21
23
  paperEvidence,
22
24
  paperForDoi,
23
25
  paperMetadataFor,
26
+ primaryLiteratureEvidence,
24
27
  } from './papers.js';
25
28
  export type {
26
29
  InventoryPaper,
27
30
  InventoryPaperMetadata,
28
31
  InventoryPaperMetadataMap,
29
32
  PaperFetchStatus,
30
- PaperFocusEvidence,
31
33
  } from './papers.js';
32
34
  export { doiHref } from './doi.js';
@@ -17,3 +17,17 @@ const SURFACE_GLYPHS: Record<SurfaceKind, string> = {
17
17
  export function surfaceGlyph(kind: SurfaceKind): string {
18
18
  return SURFACE_GLYPHS[kind];
19
19
  }
20
+
21
+ export type InventorySectionId = 'outputs' | 'decisions' | 'inputs' | 'findings' | 'prior_insights' | 'papers';
22
+
23
+ /** The record kind each inventory section lists; drives the outline glyph and colour. */
24
+ export function sectionKind(section: InventorySectionId): SurfaceKind {
25
+ switch (section) {
26
+ case 'outputs': return 'output';
27
+ case 'decisions': return 'decision';
28
+ case 'inputs': return 'input';
29
+ case 'findings': return 'finding';
30
+ case 'prior_insights': return 'prior_insight';
31
+ case 'papers': return 'paper';
32
+ }
33
+ }
@@ -32,13 +32,6 @@ export interface InventoryPaperMetadata {
32
32
 
33
33
  export type InventoryPaperMetadataMap = Readonly<Record<string, InventoryPaperMetadata>>;
34
34
 
35
- export interface PaperFocusEvidence {
36
- /** Changes on every locate request, including a repeat of the same passage, so a host can react to each one. */
37
- key: string;
38
- insight: ResolvedInsight;
39
- evidence: ResolvedEvidence;
40
- }
41
-
42
35
  /** Looks up metadata by DOI, whichever spelling the host used as the key. */
43
36
  export function paperMetadataFor(doi: string, metadata: InventoryPaperMetadataMap): InventoryPaperMetadata | undefined {
44
37
  const direct = metadata[normalizeDoi(doi)] ?? metadata[doi];
@@ -144,3 +137,12 @@ export function findPaper(papers: readonly InventoryPaper[], doi: string): Inven
144
137
  const key = normalizeDoi(doi);
145
138
  return papers.find((paper) => normalizeDoi(paper.doi) === key);
146
139
  }
140
+
141
+ /**
142
+ * The literature evidence an insight is presented against: the first entry
143
+ * with a DOI. A quote without a DOI names no paper to open, so it is skipped;
144
+ * the passage shown and the paper opened always belong to this one entry.
145
+ */
146
+ export function primaryLiteratureEvidence(insight: ResolvedInsight) {
147
+ return insight.evidence.find((evidence) => Boolean(evidence.doi));
148
+ }
@@ -1,6 +1,7 @@
1
1
  import type {
2
2
  ResolvedAnalysisNode,
3
3
  ResolvedDecision,
4
+ ResolvedInput,
4
5
  ResolvedInsight,
5
6
  ResolvedOutput,
6
7
  ResolvedRecord,
@@ -41,3 +42,14 @@ export function isVisualOutput(output: ResolvedOutput): boolean {
41
42
  export function countLabel(count: number, singular: string, plural = `${singular}s`): string {
42
43
  return `${count} ${count === 1 ? singular : plural}`;
43
44
  }
45
+
46
+ /** Where an input comes from, as declared, or a placeholder. */
47
+ export function inputSourceLabel(record: ResolvedInput): string {
48
+ return record.source ?? record.ref ?? record.resolvedFrom ?? 'Source not declared';
49
+ }
50
+
51
+ /** A decision tag's display label: the host's, or the tag itself in sentence case. */
52
+ export function decisionTagLabel(tag: string, labels: Readonly<Record<string, string>>): string {
53
+ return labels[tag]
54
+ ?? tag.replace(/_/g, ' ').replace(/^./, (character: string) => character.toUpperCase());
55
+ }
@@ -1,6 +1,6 @@
1
1
  import { forwardRef, type HTMLAttributes } from 'react';
2
2
  import { cn } from '../lib/cn.js';
3
- import type { SurfaceKind } from './kind.js';
3
+ import type { SurfaceKind } from '../model/kind.js';
4
4
 
5
5
  export type BadgeTone = 'neutral' | 'kind' | 'status' | 'universe';
6
6
  export type BadgeStatus = 'available' | 'current' | 'materialized' | 'ready' | 'error' | 'missing' | 'stale';
@@ -1,6 +1,6 @@
1
1
  import { forwardRef, type ButtonHTMLAttributes } from 'react';
2
2
  import { cn } from '../lib/cn.js';
3
- import { Slot } from '../lib/slot.js';
3
+ import { Slot } from './slot.js';
4
4
 
5
5
  export type ButtonVariant = 'primary' | 'secondary' | 'quiet';
6
6
  export type ButtonSize = 'small' | 'medium';
@@ -5,7 +5,6 @@ import {
5
5
  useContext,
6
6
  useEffect,
7
7
  useId,
8
- useLayoutEffect,
9
8
  useMemo,
10
9
  useRef,
11
10
  type FocusEvent,
@@ -16,17 +15,16 @@ import {
16
15
  type SyntheticEvent,
17
16
  } from 'react';
18
17
  import { cn } from '../lib/cn.js';
18
+ import { useIsomorphicLayoutEffect } from '../lib/use-isomorphic-layout-effect.js';
19
19
  import { useLabels } from '../lib/labels.js';
20
- import { Slot } from '../lib/slot.js';
20
+ import { Slot } from './slot.js';
21
21
  import { IconButton } from './button.js';
22
- import type { SurfaceKind } from './kind.js';
22
+ import type { SurfaceKind } from '../model/kind.js';
23
23
  import { SurfaceHeader, type SurfaceHeadingLevel } from './surface-header.js';
24
24
 
25
25
  export type DialogMode = 'modal' | 'embedded';
26
26
  export type DialogLayout = 'single' | 'reader';
27
27
 
28
- const useIsomorphicLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect;
29
-
30
28
  /* ------------------------------------------------------------------ */
31
29
  /* Presentation defaults shared by a subtree (mode, back trail) */
32
30
  /* ------------------------------------------------------------------ */
@@ -1,13 +1,13 @@
1
1
  /** Presentation primitives: generic UI with no ASTRA model beyond record kinds. */
2
- export { cn } from '../lib/cn.js';
3
- export { Slot } from '../lib/slot.js';
4
- export type { SlotProps } from '../lib/slot.js';
5
- export { LabelsProvider, defaultLabels, mergeLabels, useLabels } from '../lib/labels.js';
6
- export type { AstraLabelOverrides, AstraLabels, LabelsProviderProps } from '../lib/labels.js';
7
- export { surfaceGlyph } from './kind.js';
8
- export type { SurfaceKind } from './kind.js';
2
+ export { Slot } from './slot.js';
3
+ export type { SlotProps } from './slot.js';
9
4
  export { Button, IconButton } from './button.js';
10
5
  export type { ButtonProps, ButtonSize, ButtonTone, ButtonVariant, IconButtonProps } from './button.js';
6
+ export { PreviewPopover } from './preview-popover.js';
7
+ export type {
8
+ PreviewPopoverPortalProps,
9
+ PreviewPopoverProps,
10
+ } from './preview-popover.js';
11
11
  export { Badge } from './badge.js';
12
12
  export type { BadgeProps, BadgeStatus, BadgeTone } from './badge.js';
13
13
  export { SurfaceHeader } from './surface-header.js';
@@ -44,5 +44,5 @@ export { RelationList } from './relation-list.js';
44
44
  export type { RelationItem, RelationListProps } from './relation-list.js';
45
45
  export { CountHeading, DetailLayout, DetailMain, DetailRail, DetailSection } from './detail-layout.js';
46
46
  export type { CountHeadingProps, DetailLayoutMode, DetailLayoutProps, DetailRailProps, DetailSectionProps } from './detail-layout.js';
47
- export { Prose, parseProse, renderProse } from './prose.js';
48
- export type { ProseContext, ProseField, ProseProps, ProseToken, TextRenderer } from './prose.js';
47
+ export { Prose } from './prose.js';
48
+ export type { ProseProps } from './prose.js';
@@ -0,0 +1,278 @@
1
+ /* eslint-disable react-hooks/refs, @typescript-eslint/unbound-method --
2
+ * Floating UI intentionally exposes callback refs and middleware ref objects
3
+ * that are passed through during render; none of these APIs read `.current`.
4
+ */
5
+ import {
6
+ FloatingArrow,
7
+ FloatingFocusManager,
8
+ FloatingNode,
9
+ FloatingPortal,
10
+ FloatingTree,
11
+ arrow,
12
+ autoUpdate,
13
+ flip,
14
+ offset,
15
+ safePolygon,
16
+ shift,
17
+ size,
18
+ useClientPoint,
19
+ useDismiss,
20
+ useFloating,
21
+ useFloatingNodeId,
22
+ useFloatingParentNodeId,
23
+ useFocus,
24
+ useHover,
25
+ useInteractions,
26
+ useRole,
27
+ useTransitionStatus,
28
+ } from '@floating-ui/react';
29
+ import {
30
+ useCallback,
31
+ useId,
32
+ useRef,
33
+ useState,
34
+ type HTMLAttributes,
35
+ type ReactElement,
36
+ type ReactNode,
37
+ } from 'react';
38
+ import { cn } from '../lib/cn.js';
39
+ import { Slot } from './slot.js';
40
+ import { useDialogDismissGuard, useOptionalDialog } from './dialog.js';
41
+ import type { SurfaceKind } from '../model/kind.js';
42
+
43
+ const ARROW_HEIGHT = 7;
44
+ const REFERENCE_GAP = 6;
45
+ const VIEWPORT_PADDING = 8;
46
+
47
+ export interface PreviewPopoverPortalProps
48
+ extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
49
+ [attribute: `data-${string}`]: string | number | boolean | null | undefined;
50
+ 'data-astra-color-scheme'?: 'light' | 'dark' | undefined;
51
+ }
52
+
53
+ export interface PreviewPopoverProps
54
+ extends Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'content'> {
55
+ /** A single focusable element. Non-focusable elements receive `tabIndex=0`. */
56
+ trigger: ReactElement;
57
+ children: ReactNode;
58
+ /** Accessible name for the non-modal preview dialog. */
59
+ label: string;
60
+ kind?: SurfaceKind | undefined;
61
+ disabled?: boolean | undefined;
62
+ open?: boolean | undefined;
63
+ defaultOpen?: boolean | undefined;
64
+ onOpenChange?: ((open: boolean) => void) | undefined;
65
+ openDelay?: number | undefined;
66
+ closeDelay?: number | undefined;
67
+ /** Optional portal mount. The document body is used by default. */
68
+ portalRoot?: HTMLElement | ShadowRoot | null | undefined;
69
+ /** Host scope and scheme attributes belong here because portals do not inherit DOM ancestry. */
70
+ portalProps?: PreviewPopoverPortalProps | undefined;
71
+ }
72
+
73
+ function PreviewPopoverInner({
74
+ trigger,
75
+ children,
76
+ label,
77
+ kind,
78
+ disabled = false,
79
+ open: controlledOpen,
80
+ defaultOpen = false,
81
+ onOpenChange,
82
+ openDelay = 120,
83
+ closeDelay = 60,
84
+ portalRoot,
85
+ portalProps,
86
+ className,
87
+ ...props
88
+ }: PreviewPopoverProps) {
89
+ const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen);
90
+ const open = !disabled && (controlledOpen ?? uncontrolledOpen);
91
+ const nodeId = useFloatingNodeId();
92
+ const contentId = useId();
93
+ const arrowRef = useRef<SVGSVGElement>(null);
94
+ const cursorX = useRef<number | null>(null);
95
+ const [pinnedX, setPinnedX] = useState<number | null>(null);
96
+ const [automaticPortalRoot, setAutomaticPortalRoot] =
97
+ useState<HTMLDialogElement | null | undefined>(undefined);
98
+
99
+ const setOpen = useCallback((next: boolean, event?: Event) => {
100
+ if (next) {
101
+ setPinnedX(event instanceof MouseEvent ? cursorX.current : null);
102
+ }
103
+ if (controlledOpen === undefined) setUncontrolledOpen(next);
104
+ onOpenChange?.(next);
105
+ }, [controlledOpen, onOpenChange]);
106
+
107
+ const { refs, floatingStyles, context, placement } = useFloating({
108
+ nodeId,
109
+ open,
110
+ onOpenChange: setOpen,
111
+ placement: 'top',
112
+ strategy: 'fixed',
113
+ whileElementsMounted: autoUpdate,
114
+ middleware: [
115
+ offset(ARROW_HEIGHT + REFERENCE_GAP),
116
+ flip({ padding: VIEWPORT_PADDING }),
117
+ shift({ padding: VIEWPORT_PADDING }),
118
+ size({
119
+ padding: 12,
120
+ apply({ availableHeight, elements }) {
121
+ elements.floating.style.setProperty(
122
+ '--astra-preview-available-height',
123
+ `${Math.max(180, availableHeight)}px`,
124
+ );
125
+ },
126
+ }),
127
+ arrow({ element: arrowRef, padding: VIEWPORT_PADDING }),
128
+ ],
129
+ });
130
+
131
+ const hover = useHover(context, {
132
+ enabled: !disabled,
133
+ delay: { open: openDelay, close: closeDelay },
134
+ handleClose: safePolygon({ buffer: 1 }),
135
+ move: false,
136
+ });
137
+ const focus = useFocus(context, { enabled: !disabled });
138
+ const point = useClientPoint(context, {
139
+ enabled: !disabled,
140
+ axis: 'x',
141
+ x: pinnedX,
142
+ });
143
+ const dialog = useOptionalDialog();
144
+ const inModalDialog = dialog?.mode === 'modal';
145
+ useDialogDismissGuard(open && !disabled && inModalDialog, () => {
146
+ setOpen(false);
147
+ });
148
+ const dismiss = useDismiss(context, {
149
+ enabled: !disabled,
150
+ escapeKey: !inModalDialog,
151
+ bubbles: { escapeKey: false },
152
+ });
153
+ const role = useRole(context, { enabled: !disabled, role: 'dialog' });
154
+ const { getReferenceProps, getFloatingProps } = useInteractions([
155
+ hover,
156
+ focus,
157
+ point,
158
+ dismiss,
159
+ role,
160
+ ]);
161
+ const { isMounted, status } = useTransitionStatus(context, {
162
+ duration: { open: 120, close: 80 },
163
+ });
164
+ const setFloatingReference = refs.setReference;
165
+ const setReference = useCallback((element: HTMLElement | null) => {
166
+ setFloatingReference(element);
167
+ const nextRoot = element
168
+ ? (element.closest<HTMLDialogElement>('dialog') ?? null)
169
+ : undefined;
170
+ setAutomaticPortalRoot((currentRoot) =>
171
+ currentRoot === nextRoot ? currentRoot : nextRoot,
172
+ );
173
+ }, [setFloatingReference, setAutomaticPortalRoot]);
174
+
175
+ const reference = (
176
+ <Slot
177
+ ref={setReference}
178
+ {...getReferenceProps({
179
+ ...(!disabled
180
+ ? {
181
+ tabIndex: 0,
182
+ 'aria-haspopup': 'dialog',
183
+ 'aria-expanded': open,
184
+ 'aria-controls': open ? contentId : undefined,
185
+ }
186
+ : {}),
187
+ onMouseEnter(event) {
188
+ cursorX.current = event.clientX;
189
+ },
190
+ onMouseMove(event) {
191
+ cursorX.current = event.clientX;
192
+ },
193
+ })}
194
+ >
195
+ {trigger}
196
+ </Slot>
197
+ );
198
+
199
+ const resolvedPortalRoot = portalRoot === undefined
200
+ ? (automaticPortalRoot ?? undefined)
201
+ : portalRoot;
202
+ const portalRootReady = portalRoot !== undefined || automaticPortalRoot !== undefined;
203
+ // A nested, unschemed `.astra-ui` would redeclare the base tokens and mask
204
+ // theme overrides inherited from an explicitly scoped portal mount.
205
+ const inheritsAstraScope = portalProps === undefined &&
206
+ typeof HTMLElement !== 'undefined' &&
207
+ resolvedPortalRoot instanceof HTMLElement &&
208
+ resolvedPortalRoot.closest('.astra-ui') !== null;
209
+ const portalClassName = cn(
210
+ !inheritsAstraScope && 'astra-ui',
211
+ 'astra-preview-popover-portal',
212
+ portalProps?.className,
213
+ );
214
+
215
+ return (
216
+ <>
217
+ {reference}
218
+ <FloatingNode id={nodeId}>
219
+ {isMounted && portalRootReady ? (
220
+ <FloatingPortal root={resolvedPortalRoot} preserveTabOrder>
221
+ <div
222
+ data-slot="preview-popover-portal"
223
+ {...portalProps}
224
+ className={portalClassName}
225
+ >
226
+ <FloatingFocusManager
227
+ context={context}
228
+ modal={false}
229
+ initialFocus={-1}
230
+ >
231
+ <div
232
+ data-slot="preview-popover"
233
+ {...getFloatingProps(props)}
234
+ id={contentId}
235
+ ref={refs.setFloating}
236
+ style={{ ...floatingStyles, ...props.style }}
237
+ className={cn('astra-preview-popover', className)}
238
+ aria-label={label}
239
+ aria-modal="false"
240
+ data-kind={kind}
241
+ data-placement={placement}
242
+ data-status={status}
243
+ >
244
+ <div className="astra-preview-popover__surface">
245
+ <div className="astra-preview-popover__scroll">{children}</div>
246
+ <FloatingArrow
247
+ ref={arrowRef}
248
+ context={context}
249
+ className="astra-preview-popover__arrow"
250
+ height={ARROW_HEIGHT}
251
+ width={ARROW_HEIGHT * 2}
252
+ />
253
+ </div>
254
+ </div>
255
+ </FloatingFocusManager>
256
+ </div>
257
+ </FloatingPortal>
258
+ ) : null}
259
+ </FloatingNode>
260
+ </>
261
+ );
262
+ }
263
+
264
+ /**
265
+ * Cursor-anchored, non-modal preview opened by hover or keyboard focus.
266
+ * The outermost instance creates the Floating UI tree needed by nested previews.
267
+ */
268
+ export function PreviewPopover(props: PreviewPopoverProps) {
269
+ const parentId = useFloatingParentNodeId();
270
+ if (parentId === null) {
271
+ return (
272
+ <FloatingTree>
273
+ <PreviewPopoverInner {...props} />
274
+ </FloatingTree>
275
+ );
276
+ }
277
+ return <PreviewPopoverInner {...props} />;
278
+ }
@@ -1,66 +1,4 @@
1
- import { Fragment, type ReactNode } from 'react';
2
- import { renderToString } from 'katex';
3
-
4
- export type ProseField = 'claim' | 'rationale' | 'description' | 'notes' | 'quote';
5
-
6
- export interface ProseContext {
7
- /** Which authored field is being rendered, when known. */
8
- field?: ProseField | undefined;
9
- }
10
-
11
- /** Host-owned rendering for authored prose (Markdown, citations, ...); replaces the built-in rendering. */
12
- export type TextRenderer = (text: string, context: ProseContext) => ReactNode;
13
-
14
- export type ProseToken =
15
- | { type: 'text'; value: string }
16
- | { type: 'inlineCode'; value: string }
17
- | { type: 'inlineMath'; value: string }
18
- | { type: 'math'; value: string };
19
-
20
- /** Balanced tokens: display math first, then `code`, then inline math. */
21
- const PROSE_TOKEN = /(\$\$[\s\S]+?\$\$|`[^`\n]+`|\$[^$\n]+\$)/g;
22
-
23
- /** Splits authored prose into text, inline `code`, `$inline$` and `$$display$$` math. */
24
- export function parseProse(text: string): ProseToken[] {
25
- const nodes: ProseToken[] = [];
26
- let last = 0;
27
- for (const match of text.matchAll(PROSE_TOKEN)) {
28
- const index = match.index;
29
- if (index > last) nodes.push({ type: 'text', value: text.slice(last, index) });
30
- const token = match[0];
31
- if (token.startsWith('$$')) nodes.push({ type: 'math', value: token.slice(2, -2).trim() });
32
- else if (token.startsWith('`')) nodes.push({ type: 'inlineCode', value: token.slice(1, -1) });
33
- else nodes.push({ type: 'inlineMath', value: token.slice(1, -1) });
34
- last = index + token.length;
35
- }
36
- if (last < text.length) nodes.push({ type: 'text', value: text.slice(last) });
37
- return nodes;
38
- }
39
-
40
- function renderMath(value: string, displayMode: boolean): string {
41
- return renderToString(value, { displayMode, output: 'htmlAndMathml', strict: 'ignore', throwOnError: false });
42
- }
43
-
44
- /**
45
- * The built-in rendering: inline `code`, and `$inline$` / `$$display$$` math
46
- * typeset with KaTeX (load `katex/dist/katex.css`; the stylesheet bundles
47
- * import it). Text without those markers is returned as is.
48
- */
49
- export function renderProse(text: string): ReactNode {
50
- if (!/[`$]/.test(text)) return text;
51
- return parseProse(text).map((node, index) => {
52
- switch (node.type) {
53
- case 'inlineCode':
54
- return <code key={index}>{node.value}</code>;
55
- case 'inlineMath':
56
- return <span key={index} className="astra-prose__inline-math" dangerouslySetInnerHTML={{ __html: renderMath(node.value, false) }} />;
57
- case 'math':
58
- return <div key={index} className="astra-prose__display-math" dangerouslySetInnerHTML={{ __html: renderMath(node.value, true) }} />;
59
- default:
60
- return <Fragment key={index}>{node.value}</Fragment>;
61
- }
62
- });
63
- }
1
+ import { renderProse, type ProseContext, type TextRenderer } from '../lib/prose.js';
64
2
 
65
3
  export interface ProseProps extends ProseContext {
66
4
  text?: string | undefined;
@@ -1,6 +1,6 @@
1
1
  import { forwardRef, type CSSProperties, type HTMLAttributes, type ReactNode } from 'react';
2
2
  import { cn } from '../lib/cn.js';
3
- import { surfaceGlyph, type SurfaceKind } from './kind.js';
3
+ import { surfaceGlyph, type SurfaceKind } from '../model/kind.js';
4
4
 
5
5
  export interface RecordListColumn {
6
6
  label?: string | undefined;
@@ -1,7 +1,7 @@
1
1
  import { forwardRef, type HTMLAttributes, type ReactNode } from 'react';
2
2
  import { cn } from '../lib/cn.js';
3
3
  import { CountHeading } from './detail-layout.js';
4
- import { surfaceGlyph, type SurfaceKind } from './kind.js';
4
+ import { surfaceGlyph, type SurfaceKind } from '../model/kind.js';
5
5
 
6
6
  export interface RelationItem {
7
7
  key: string;
@@ -8,7 +8,7 @@ import {
8
8
  type ReactNode,
9
9
  type Ref,
10
10
  } from 'react';
11
- import { cn } from './cn.js';
11
+ import { cn } from '../lib/cn.js';
12
12
 
13
13
  export interface SlotProps extends HTMLAttributes<HTMLElement> {
14
14
  children?: ReactNode;
@@ -1,6 +1,6 @@
1
1
  import { forwardRef, type HTMLAttributes, type ReactNode } from 'react';
2
2
  import { cn } from '../lib/cn.js';
3
- import type { SurfaceKind } from './kind.js';
3
+ import type { SurfaceKind } from '../model/kind.js';
4
4
 
5
5
  export type SurfaceHeaderDensity = 'compact' | 'regular' | 'inline';
6
6
  export type SurfaceHeadingLevel = 'h1' | 'h2' | 'h3' | 'h4';