@behavioral-agentic-design-system/core 0.8.4 → 0.9.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.
@@ -2,12 +2,11 @@
2
2
 
3
3
  import { useEffect, useState } from "react";
4
4
  import { Loader2, FileText, GitCommit, ChevronRight, ChevronLeft, Check, AlertCircle, Palette } from "lucide-react";
5
- import ReactMarkdown, { type Components } from "react-markdown";
6
- import remarkGfm from "remark-gfm";
7
5
  import { Button } from "@behavioral-agentic-design-system/core/components/ui/button";
8
6
  import { Input } from "@behavioral-agentic-design-system/core/components/ui/input";
9
7
  import { Textarea } from "@behavioral-agentic-design-system/core/components/ui/textarea";
10
8
  import { ScrollArea } from "@behavioral-agentic-design-system/core/components/ui/scroll-area";
9
+ import { MarkdownPreview } from "@behavioral-agentic-design-system/core/components/markdown-preview";
11
10
 
12
11
  type LayerEntry = { slug: string; label: string; files: string[] };
13
12
  type SaveStatus = "idle" | "saving" | "success" | "error";
@@ -23,95 +22,8 @@ function isMarkdown(path: string | null): boolean {
23
22
  return !!path && path.toLowerCase().endsWith(".md");
24
23
  }
25
24
 
26
- // BADS-styled wrapper around react-markdown so the rendered preview reads
27
- // like the rest of the system (token-aligned colors, AAA text contrast,
28
- // 14px floor on body type via the global type scale).
29
- const MARKDOWN_COMPONENTS: Components = {
30
- h1: ({ children, ...props }: any) => (
31
- <h1 className="mt-6 mb-3 text-2xl font-semibold tracking-tight first:mt-0" {...props}>{children}</h1>
32
- ),
33
- h2: ({ children, ...props }: any) => (
34
- <h2 className="mt-6 mb-3 text-xl font-semibold tracking-tight first:mt-0" {...props}>{children}</h2>
35
- ),
36
- h3: ({ children, ...props }: any) => (
37
- <h3 className="mt-5 mb-2 text-lg font-semibold tracking-tight" {...props}>{children}</h3>
38
- ),
39
- h4: ({ children, ...props }: any) => (
40
- <h4 className="mt-4 mb-2 text-base font-semibold" {...props}>{children}</h4>
41
- ),
42
- h5: ({ children, ...props }: any) => (
43
- <h5 className="mt-3 mb-1 text-sm font-semibold uppercase tracking-wide text-muted-foreground" {...props}>{children}</h5>
44
- ),
45
- h6: ({ children, ...props }: any) => (
46
- <h6 className="mt-3 mb-1 text-sm font-semibold uppercase tracking-wide text-muted-foreground" {...props}>{children}</h6>
47
- ),
48
- p: ({ children, ...props }: any) => (
49
- <p className="mb-3 leading-relaxed" {...props}>{children}</p>
50
- ),
51
- a: ({ children, href, ...props }: any) => (
52
- <a href={href} target="_blank" rel="noreferrer" className="text-primary underline underline-offset-2 hover:text-primary/80" {...props}>{children}</a>
53
- ),
54
- ul: ({ children, ...props }: any) => (
55
- <ul className="mb-3 list-disc space-y-1 pl-6" {...props}>{children}</ul>
56
- ),
57
- ol: ({ children, ...props }: any) => (
58
- <ol className="mb-3 list-decimal space-y-1 pl-6" {...props}>{children}</ol>
59
- ),
60
- li: ({ children, ...props }: any) => (
61
- <li className="leading-relaxed" {...props}>{children}</li>
62
- ),
63
- code: ({ children, className, ...props }: any) => {
64
- const inline = !/language-/.test(className ?? "");
65
- if (inline) {
66
- return (
67
- <code className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs text-foreground" {...props}>{children}</code>
68
- );
69
- }
70
- return (
71
- <code className={`font-mono text-xs ${className ?? ""}`} {...props}>{children}</code>
72
- );
73
- },
74
- pre: ({ children, ...props }: any) => (
75
- <pre className="mb-3 overflow-x-auto rounded-md border bg-muted/50 p-4 text-xs leading-relaxed" {...props}>{children}</pre>
76
- ),
77
- blockquote: ({ children, ...props }: any) => (
78
- <blockquote className="mb-3 border-l-4 border-border pl-4 italic text-muted-foreground" {...props}>{children}</blockquote>
79
- ),
80
- hr: () => <hr className="my-6 border-border" />,
81
- table: ({ children, ...props }: any) => (
82
- <div className="mb-3 overflow-x-auto">
83
- <table className="w-full border-collapse text-xs" {...props}>{children}</table>
84
- </div>
85
- ),
86
- thead: ({ children, ...props }: any) => (
87
- <thead className="border-b bg-muted/40 text-left" {...props}>{children}</thead>
88
- ),
89
- th: ({ children, ...props }: any) => (
90
- <th className="border border-border px-3 py-2 font-medium" {...props}>{children}</th>
91
- ),
92
- td: ({ children, ...props }: any) => (
93
- <td className="border border-border px-3 py-2 align-top" {...props}>{children}</td>
94
- ),
95
- img: ({ src, alt, ...props }: any) => (
96
- <img src={typeof src === "string" ? src : undefined} alt={alt ?? ""} className="my-3 max-w-full rounded-md border" {...props} />
97
- ),
98
- strong: ({ children, ...props }: any) => (
99
- <strong className="font-semibold text-foreground" {...props}>{children}</strong>
100
- ),
101
- em: ({ children, ...props }: any) => (
102
- <em className="italic" {...props}>{children}</em>
103
- ),
104
- };
105
-
106
- function MarkdownPreview({ source }: { source: string }) {
107
- return (
108
- <div className="max-w-3xl text-sm text-foreground/90">
109
- <ReactMarkdown remarkPlugins={[remarkGfm]} components={MARKDOWN_COMPONENTS}>
110
- {source}
111
- </ReactMarkdown>
112
- </div>
113
- );
114
- }
25
+ // Markdown preview now lives in components/markdown-preview.tsx so the docs
26
+ // tab can reuse the same BADS-styled treatment.
115
27
 
116
28
  // fileType is set synchronously when activePath changes, but `content` only
117
29
  // updates after the fetch resolves. That tick of stale content briefly
@@ -0,0 +1,226 @@
1
+ "use client";
2
+
3
+ import { useMemo } from "react";
4
+ import ReactMarkdown, { type Components } from "react-markdown";
5
+ import remarkGfm from "remark-gfm";
6
+
7
+ // A link is "external" (open in a new tab) when it has a scheme or is a pure
8
+ // in-page anchor. Everything else is a repo-relative path — e.g. a doc-to-doc
9
+ // link like `CAPABILITIES.md` or `prompts/setup.md` — which must NOT resolve
10
+ // against the (often private) source repo or 404 against the deployed origin.
11
+ const EXTERNAL_HREF = /^(https?:|mailto:|tel:|#|\/\/)/i;
12
+
13
+ // BADS-styled wrapper around react-markdown so rendered preview reads like
14
+ // the rest of the system — token-aligned colors, AAA text contrast, the 14px
15
+ // floor on body type via the global type scale. Extracted from layers-tab so
16
+ // the docs tab can reuse the same treatment without duplicating the mapping.
17
+ //
18
+ // Tokens used directly: text-foreground, text-muted-foreground, bg-muted,
19
+ // border-border, text-primary, font-mono. No hardcoded values.
20
+ export const MARKDOWN_COMPONENTS: Components = {
21
+ h1: ({ children, ...props }: any) => (
22
+ <h1 className="mt-6 mb-3 text-2xl font-semibold tracking-tight first:mt-0" {...props}>
23
+ {children}
24
+ </h1>
25
+ ),
26
+ h2: ({ children, ...props }: any) => (
27
+ <h2 className="mt-6 mb-3 text-xl font-semibold tracking-tight first:mt-0" {...props}>
28
+ {children}
29
+ </h2>
30
+ ),
31
+ h3: ({ children, ...props }: any) => (
32
+ <h3 className="mt-5 mb-2 text-lg font-semibold tracking-tight" {...props}>
33
+ {children}
34
+ </h3>
35
+ ),
36
+ h4: ({ children, ...props }: any) => (
37
+ <h4 className="mt-4 mb-2 text-base font-semibold" {...props}>
38
+ {children}
39
+ </h4>
40
+ ),
41
+ h5: ({ children, ...props }: any) => (
42
+ <h5
43
+ className="mt-3 mb-1 text-sm font-semibold uppercase tracking-wide text-muted-foreground"
44
+ {...props}
45
+ >
46
+ {children}
47
+ </h5>
48
+ ),
49
+ h6: ({ children, ...props }: any) => (
50
+ <h6
51
+ className="mt-3 mb-1 text-sm font-semibold uppercase tracking-wide text-muted-foreground"
52
+ {...props}
53
+ >
54
+ {children}
55
+ </h6>
56
+ ),
57
+ p: ({ children, ...props }: any) => (
58
+ <p className="mb-3 leading-relaxed" {...props}>
59
+ {children}
60
+ </p>
61
+ ),
62
+ a: ({ children, href, ...props }: any) => (
63
+ <a
64
+ href={href}
65
+ target="_blank"
66
+ rel="noreferrer"
67
+ className="text-primary underline underline-offset-2 hover:text-primary/80"
68
+ {...props}
69
+ >
70
+ {children}
71
+ </a>
72
+ ),
73
+ ul: ({ children, ...props }: any) => (
74
+ <ul className="mb-3 list-disc space-y-1 pl-6" {...props}>
75
+ {children}
76
+ </ul>
77
+ ),
78
+ ol: ({ children, ...props }: any) => (
79
+ <ol className="mb-3 list-decimal space-y-1 pl-6" {...props}>
80
+ {children}
81
+ </ol>
82
+ ),
83
+ li: ({ children, ...props }: any) => (
84
+ <li className="leading-relaxed" {...props}>
85
+ {children}
86
+ </li>
87
+ ),
88
+ code: ({ children, className, ...props }: any) => {
89
+ const inline = !/language-/.test(className ?? "");
90
+ if (inline) {
91
+ return (
92
+ <code
93
+ className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs text-foreground"
94
+ {...props}
95
+ >
96
+ {children}
97
+ </code>
98
+ );
99
+ }
100
+ return (
101
+ <code className={`font-mono text-xs ${className ?? ""}`} {...props}>
102
+ {children}
103
+ </code>
104
+ );
105
+ },
106
+ pre: ({ children, ...props }: any) => (
107
+ <pre
108
+ className="mb-3 overflow-x-auto rounded-md border bg-muted/50 p-4 text-xs leading-relaxed"
109
+ {...props}
110
+ >
111
+ {children}
112
+ </pre>
113
+ ),
114
+ blockquote: ({ children, ...props }: any) => (
115
+ <blockquote
116
+ className="mb-3 border-l-4 border-border pl-4 italic text-muted-foreground"
117
+ {...props}
118
+ >
119
+ {children}
120
+ </blockquote>
121
+ ),
122
+ hr: () => <hr className="my-6 border-border" />,
123
+ table: ({ children, ...props }: any) => (
124
+ <div className="mb-3 overflow-x-auto">
125
+ <table className="w-full border-collapse text-xs" {...props}>
126
+ {children}
127
+ </table>
128
+ </div>
129
+ ),
130
+ thead: ({ children, ...props }: any) => (
131
+ <thead className="border-b bg-muted/40 text-left" {...props}>
132
+ {children}
133
+ </thead>
134
+ ),
135
+ th: ({ children, ...props }: any) => (
136
+ <th className="border border-border px-3 py-2 font-medium" {...props}>
137
+ {children}
138
+ </th>
139
+ ),
140
+ td: ({ children, ...props }: any) => (
141
+ <td className="border border-border px-3 py-2 align-top" {...props}>
142
+ {children}
143
+ </td>
144
+ ),
145
+ img: ({ src, alt, ...props }: any) => (
146
+ <img
147
+ src={typeof src === "string" ? src : undefined}
148
+ alt={alt ?? ""}
149
+ className="my-3 max-w-full rounded-md border"
150
+ {...props}
151
+ />
152
+ ),
153
+ strong: ({ children, ...props }: any) => (
154
+ <strong className="font-semibold text-foreground" {...props}>
155
+ {children}
156
+ </strong>
157
+ ),
158
+ em: ({ children, ...props }: any) => (
159
+ <em className="italic" {...props}>
160
+ {children}
161
+ </em>
162
+ ),
163
+ };
164
+
165
+ export function MarkdownPreview({
166
+ source,
167
+ maxWidth = "max-w-3xl",
168
+ onDocLink,
169
+ }: {
170
+ source: string;
171
+ maxWidth?: string;
172
+ // When provided, repo-relative links (doc-to-doc references like
173
+ // `CAPABILITIES.md`) are intercepted and handed to this callback instead of
174
+ // navigating the browser — so they resolve to an in-app doc rather than the
175
+ // private source repo or a 404. External links still open in a new tab.
176
+ onDocLink?: (href: string) => void;
177
+ }) {
178
+ const components = useMemo<Components>(() => {
179
+ if (!onDocLink) return MARKDOWN_COMPONENTS;
180
+ return {
181
+ ...MARKDOWN_COMPONENTS,
182
+ a: ({ children, href, ...props }: any) => {
183
+ const external = typeof href !== "string" || EXTERNAL_HREF.test(href);
184
+ if (external) {
185
+ return (
186
+ <a
187
+ href={href}
188
+ target="_blank"
189
+ rel="noreferrer"
190
+ className="text-primary underline underline-offset-2 hover:text-primary/80"
191
+ {...props}
192
+ >
193
+ {children}
194
+ </a>
195
+ );
196
+ }
197
+ return (
198
+ <a
199
+ href={href}
200
+ onClick={(e) => {
201
+ // Never let a repo-relative href resolve in the browser — it would
202
+ // hit the private repo or 404. Route it to the in-app doc instead.
203
+ e.preventDefault();
204
+ onDocLink(href);
205
+ }}
206
+ className="cursor-pointer text-primary underline underline-offset-2 hover:text-primary/80"
207
+ {...props}
208
+ >
209
+ {children}
210
+ </a>
211
+ );
212
+ },
213
+ };
214
+ }, [onDocLink]);
215
+
216
+ return (
217
+ <div
218
+ data-slot="markdown-preview"
219
+ className={`${maxWidth} text-sm text-foreground/90`}
220
+ >
221
+ <ReactMarkdown remarkPlugins={[remarkGfm]} components={components}>
222
+ {source}
223
+ </ReactMarkdown>
224
+ </div>
225
+ );
226
+ }
@@ -0,0 +1,164 @@
1
+ "use client";
2
+
3
+ /**
4
+ * Chart entrance reveal — staggered, tab-gated draw-in for the data viz.
5
+ *
6
+ * Separate from the container cascade (reveal.tsx). Containers (cards/sections)
7
+ * just appear; the *charts* inside them draw in. This coordinates that draw:
8
+ *
9
+ * - Nothing animates until the host tab is actually active. The Cards tab is a
10
+ * sticky-mounted iframe that can preload while hidden; without this gate the
11
+ * recharts entrance would run during preload and be finished before you ever
12
+ * click in. We read the parent's TabsContent `data-state` via
13
+ * `window.frameElement` (same-origin) and only release once it's "active".
14
+ * - Charts cascade as a group, top-to-bottom. When a batch becomes eligible
15
+ * (tab active + in view) each chart gets an increasing `animationBegin`
16
+ * offset so they sweep in one after another, nice and slow.
17
+ * - Below-the-fold charts wait until scrolled into view, then release in their
18
+ * own fresh batch — so the radar animates when you reach it, not on click.
19
+ * - Reveal-once: a chart releases a single time; it won't replay on scroll-up.
20
+ * - Reduced motion: charts render immediately with animation off.
21
+ *
22
+ * A chart consumes this via `useChartReveal()`: put the returned `ref` on a
23
+ * wrapper around the ChartContainer, gate the animated series on `play`, and
24
+ * pass `begin` (+ any per-series offset) to recharts `animationBegin`.
25
+ */
26
+
27
+ import * as React from "react";
28
+ import { prefersReducedMotion } from "../../hooks/use-reduced-motion";
29
+
30
+ // Gap between charts cascading in within one batch, and the cap so a long batch
31
+ // doesn't trail forever. Slow on purpose — Sir asked for "nice and slow".
32
+ const CHART_STAGGER_MS = 360;
33
+ const MAX_CHART_STEPS = 6;
34
+ const BATCH_QUIET_MS = 160;
35
+
36
+ // Shared, slow entrance duration every chart uses, plus the within-chart series
37
+ // stagger the multi-series radar applies on top of its batch offset.
38
+ export const CHART_ANIM_DURATION_MS = 1200;
39
+ export const RADAR_SERIES_STAGGER_MS = 280;
40
+
41
+ type ChartItem = { el: HTMLElement; release: (beginMs: number) => void };
42
+
43
+ class ChartRevealController {
44
+ private observer: IntersectionObserver | null = null;
45
+ private items = new Map<Element, ChartItem>();
46
+ private inView = new Set<Element>();
47
+ private released = new Set<Element>();
48
+ private tabActive = false;
49
+ private tabWatched = false;
50
+ private batchIndex = 0;
51
+ private lastReleaseAt = 0;
52
+
53
+ private ensureTabWatcher() {
54
+ if (this.tabWatched) return;
55
+ this.tabWatched = true;
56
+
57
+ let frameEl: Element | null = null;
58
+ try {
59
+ frameEl = window.frameElement;
60
+ } catch {
61
+ frameEl = null;
62
+ }
63
+ // Not inside the tab iframe (e.g. /preview/cards visited directly): treat
64
+ // the tab as already active so charts still draw in.
65
+ if (!frameEl) {
66
+ this.setTabActive(true);
67
+ return;
68
+ }
69
+ const panel = frameEl.closest('[data-slot="tabs-content"]');
70
+ if (!panel) {
71
+ this.setTabActive(true);
72
+ return;
73
+ }
74
+ const sync = () => this.setTabActive(panel.getAttribute("data-state") === "active");
75
+ sync();
76
+ new MutationObserver(sync).observe(panel, {
77
+ attributes: true,
78
+ attributeFilter: ["data-state"],
79
+ });
80
+ }
81
+
82
+ private ensureObserver() {
83
+ if (this.observer || typeof IntersectionObserver === "undefined") return;
84
+ this.observer = new IntersectionObserver(
85
+ (entries) => {
86
+ for (const e of entries) {
87
+ if (e.isIntersecting) this.inView.add(e.target);
88
+ else this.inView.delete(e.target);
89
+ }
90
+ this.releaseEligible();
91
+ },
92
+ { threshold: 0.25 },
93
+ );
94
+ }
95
+
96
+ private setTabActive(v: boolean) {
97
+ const was = this.tabActive;
98
+ this.tabActive = v;
99
+ if (v && !was) this.releaseEligible();
100
+ }
101
+
102
+ private releaseEligible() {
103
+ if (!this.tabActive || !this.observer) return;
104
+ const now = typeof performance !== "undefined" ? performance.now() : 0;
105
+ const ready = [...this.inView]
106
+ .filter((el) => this.items.has(el) && !this.released.has(el))
107
+ .sort((a, b) => a.getBoundingClientRect().top - b.getBoundingClientRect().top);
108
+ if (ready.length === 0) return;
109
+
110
+ if (now - this.lastReleaseAt > BATCH_QUIET_MS) this.batchIndex = 0;
111
+ for (const el of ready) {
112
+ const item = this.items.get(el);
113
+ if (!item) continue;
114
+ const begin = Math.min(this.batchIndex, MAX_CHART_STEPS) * CHART_STAGGER_MS;
115
+ this.batchIndex += 1;
116
+ this.released.add(el);
117
+ this.observer.unobserve(el);
118
+ item.release(begin);
119
+ }
120
+ this.lastReleaseAt = now;
121
+ }
122
+
123
+ register(item: ChartItem) {
124
+ this.ensureObserver();
125
+ this.ensureTabWatcher();
126
+ this.items.set(item.el, item);
127
+ this.observer?.observe(item.el);
128
+ }
129
+
130
+ unregister(el: Element) {
131
+ this.observer?.unobserve(el);
132
+ this.items.delete(el);
133
+ this.inView.delete(el);
134
+ this.released.delete(el);
135
+ }
136
+ }
137
+
138
+ const controller = new ChartRevealController();
139
+
140
+ export function useChartReveal() {
141
+ const ref = React.useRef<HTMLDivElement>(null);
142
+ const [state, setState] = React.useState<{ play: boolean; begin: number; reduced: boolean }>({
143
+ play: false,
144
+ begin: 0,
145
+ reduced: false,
146
+ });
147
+
148
+ React.useEffect(() => {
149
+ const el = ref.current;
150
+ if (!el) return;
151
+ if (prefersReducedMotion()) {
152
+ setState({ play: true, begin: 0, reduced: true });
153
+ return;
154
+ }
155
+ const item: ChartItem = {
156
+ el,
157
+ release: (begin) => setState({ play: true, begin, reduced: false }),
158
+ };
159
+ controller.register(item);
160
+ return () => controller.unregister(el);
161
+ }, []);
162
+
163
+ return { ref, play: state.play, begin: state.begin, reduced: state.reduced };
164
+ }
@@ -0,0 +1,156 @@
1
+ "use client";
2
+
3
+ /**
4
+ * Scroll-driven, staggered cascade reveal — BADS motion pattern.
5
+ *
6
+ * Wrap any element in `<Reveal>` and, IF it starts below the fold, it begins
7
+ * hidden (faded + nudged down) and animates into place the moment it scrolls
8
+ * into the viewport, cascading with its neighbours. Built on the motion tokens
9
+ * (`--duration-slow` + `--ease-out`) so the timing/curve match the system.
10
+ *
11
+ * Behaviour:
12
+ * 1. No entrance for what's already on screen — cards visible when the tab
13
+ * opens just appear; we only animate cards you actually scroll down to.
14
+ * This avoids the "everything cascades on load" jolt.
15
+ * 2. Staggered cascade for below-the-fold cards — one shared
16
+ * IntersectionObserver assigns each entering card a stagger delay by its
17
+ * top-to-bottom order within the batch, so a group reveals as a wave.
18
+ * 3. Reveal once, on scroll-down — a card animates the first time it enters
19
+ * the viewport and then stays put; scrolling back up never replays it.
20
+ *
21
+ * Accessibility: under `prefers-reduced-motion: reduce` cards render visible
22
+ * immediately and never hide/observe (the global base layer also collapses
23
+ * the transition to ~0ms, so this is belt-and-suspenders).
24
+ */
25
+
26
+ import * as React from "react";
27
+ import { prefersReducedMotion } from "../../hooks/use-reduced-motion";
28
+
29
+ // Delay between cascading siblings, capped so a long scroll-in group doesn't
30
+ // drag. translateY distance is the only non-token literal — kept small so the
31
+ // motion reads as a settle, not a slide.
32
+ const STAGGER_STEP_MS = 70;
33
+ const MAX_STAGGER_STEPS = 6;
34
+ // A gap longer than this (no new card entering) starts a fresh cascade, so the
35
+ // stagger restarts from 0 for the next group you scroll to instead of carrying
36
+ // an ever-growing delay down the page.
37
+ const BATCH_QUIET_MS = 140;
38
+ const HIDDEN_TRANSFORM = "translateY(16px)";
39
+
40
+ const FADE =
41
+ "opacity var(--duration-slow) var(--ease-out), transform var(--duration-slow) var(--ease-out)";
42
+
43
+ type RevealItem = {
44
+ el: HTMLElement;
45
+ reveal: (delayMs: number) => void;
46
+ };
47
+
48
+ const useIsoLayoutEffect =
49
+ typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
50
+
51
+ /**
52
+ * One controller per document (the iframe gets its own module instance). Owns
53
+ * the shared observer and the rolling stagger counter for below-the-fold cards.
54
+ */
55
+ class CascadeController {
56
+ private observer: IntersectionObserver | null = null;
57
+ private items = new Map<Element, RevealItem>();
58
+ private batchIndex = 0;
59
+ private lastEntryAt = 0;
60
+
61
+ private ensureObserver() {
62
+ if (this.observer || typeof IntersectionObserver === "undefined") return;
63
+ this.observer = new IntersectionObserver((entries) => this.onIntersect(entries), {
64
+ threshold: 0.12,
65
+ // Fire slightly before a card is fully in view so the reveal lands as it
66
+ // arrives rather than after it's already sitting there.
67
+ rootMargin: "0px 0px -8% 0px",
68
+ });
69
+ }
70
+
71
+ private onIntersect(entries: IntersectionObserverEntry[]) {
72
+ const now = typeof performance !== "undefined" ? performance.now() : 0;
73
+
74
+ // Reveal-once: only cards entering the viewport reveal, sorted top-to-bottom
75
+ // so the wave reads in order regardless of callback ordering. We do NOT hide
76
+ // on exit — once a card has animated in it stays put, so scrolling back up
77
+ // never replays it. Each revealed card is unobserved so it can't fire again;
78
+ // a full replay only comes from retrigger() (tab re-activation).
79
+ const entering = entries
80
+ .filter((e) => e.isIntersecting)
81
+ .sort((a, b) => a.boundingClientRect.top - b.boundingClientRect.top);
82
+ if (entering.length === 0) return;
83
+
84
+ if (now - this.lastEntryAt > BATCH_QUIET_MS) this.batchIndex = 0;
85
+ for (const entry of entering) {
86
+ const item = this.items.get(entry.target);
87
+ if (!item) continue;
88
+ const delay = Math.min(this.batchIndex, MAX_STAGGER_STEPS) * STAGGER_STEP_MS;
89
+ this.batchIndex += 1;
90
+ item.reveal(delay);
91
+ this.observer?.unobserve(entry.target);
92
+ }
93
+ this.lastEntryAt = now;
94
+ }
95
+
96
+ register(item: RevealItem) {
97
+ this.ensureObserver();
98
+ this.items.set(item.el, item);
99
+ this.observer?.observe(item.el);
100
+ }
101
+
102
+ unregister(el: Element) {
103
+ this.observer?.unobserve(el);
104
+ this.items.delete(el);
105
+ }
106
+ }
107
+
108
+ const controller = new CascadeController();
109
+
110
+ export function Reveal({
111
+ children,
112
+ className,
113
+ }: {
114
+ children: React.ReactNode;
115
+ className?: string;
116
+ }) {
117
+ const ref = React.useRef<HTMLDivElement>(null);
118
+
119
+ // Decide before paint so a below-the-fold card never flashes visible first.
120
+ // Cards already on screen are left untouched (no entrance); only cards that
121
+ // start below the fold get hidden and animated in when scrolled to.
122
+ useIsoLayoutEffect(() => {
123
+ const el = ref.current;
124
+ if (!el) return;
125
+ if (prefersReducedMotion()) return;
126
+
127
+ const vh = window.innerHeight || document.documentElement.clientHeight || 0;
128
+ const rect = el.getBoundingClientRect();
129
+ const inViewNow = rect.top < vh && rect.bottom > 0;
130
+ if (inViewNow) return; // above the fold → just appear
131
+
132
+ // Hide without a transition so the hide itself doesn't animate, then arm the
133
+ // fade for the scroll-in reveal.
134
+ el.style.transition = "none";
135
+ el.style.opacity = "0";
136
+ el.style.transform = HIDDEN_TRANSFORM;
137
+ void el.offsetHeight; // commit the hidden state before re-enabling transition
138
+
139
+ controller.register({
140
+ el,
141
+ reveal: (delayMs) => {
142
+ el.style.transition = FADE;
143
+ el.style.transitionDelay = `${delayMs}ms`;
144
+ el.style.opacity = "1";
145
+ el.style.transform = "translateY(0)";
146
+ },
147
+ });
148
+ return () => controller.unregister(el);
149
+ }, []);
150
+
151
+ return (
152
+ <div ref={ref} className={className} style={{ willChange: "opacity, transform" }}>
153
+ {children}
154
+ </div>
155
+ );
156
+ }