Skip to content

Commit 290a521

Browse files
committed
🐛 fix: fade inline code in stream order with surrounding text
1 parent 479ddac commit 290a521

3 files changed

Lines changed: 133 additions & 6 deletions

File tree

‎src/rehypeStreamAnimated.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type StreamAnimatedRuntime,
88
} from './rehypeStreamAnimated';
99
import { readStreamTailData, STREAM_TAIL_TAG, type StreamTailData } from './StreamTail';
10+
import { STREAM_INLINE_CLASS } from './styles';
1011

1112
const makeTree = (text: string): Root => ({
1213
children: [
@@ -220,4 +221,89 @@ describe('rehypeStreamAnimated', () => {
220221
expect(first.birth).toBe(100);
221222
expect(second.birth).toBe(200);
222223
});
224+
it('fades inline code in order with the text around it', () => {
225+
const now = performance.now();
226+
const code: Element = {
227+
children: [{ type: 'text', value: 'cd' }],
228+
properties: {},
229+
tagName: 'code',
230+
type: 'element',
231+
};
232+
const tree: Root = {
233+
children: [
234+
{
235+
children: [{ type: 'text', value: 'ab' }, code, { type: 'text', value: 'ef' }],
236+
properties: {},
237+
tagName: 'p',
238+
type: 'element',
239+
},
240+
],
241+
type: 'root',
242+
};
243+
rehypeStreamAnimated({ runtime: makeRuntime(spaced(now, 6)) })(tree);
244+
245+
const paragraph = tree.children[0] as Element;
246+
const codeTail = readStreamTailData(code.children[0] as Element)!;
247+
const afterTail = readStreamTailData(paragraph.children[2] as Element)!;
248+
expect(spansOf(codeTail).map((span) => span.key)).toEqual([2, 3]);
249+
expect(spansOf(afterTail).map((span) => span.key)).toEqual([4, 5]);
250+
expect(code.properties.className).toContain(STREAM_INLINE_CLASS);
251+
expect(code.properties.style).toMatch(/animation-delay:/);
252+
});
253+
254+
it('freezes the inline code fade delay across runs and drops it once faded', () => {
255+
const now = performance.now();
256+
const runtime = makeRuntime([now + 40, now + 60]);
257+
const makeCodeTree = () => {
258+
const code: Element = {
259+
children: [{ type: 'text', value: 'ab' }],
260+
properties: {},
261+
tagName: 'code',
262+
type: 'element',
263+
};
264+
const tree: Root = {
265+
children: [{ children: [code], properties: {}, tagName: 'p', type: 'element' }],
266+
type: 'root',
267+
};
268+
return { code, tree };
269+
};
270+
271+
const first = makeCodeTree();
272+
rehypeStreamAnimated({ fadeDuration: 100, runtime })(first.tree);
273+
expect(first.code.properties.style).toMatch(/animation-delay:/);
274+
const second = makeCodeTree();
275+
rehypeStreamAnimated({ fadeDuration: 100, runtime })(second.tree);
276+
expect(second.code.properties.style).toBe(first.code.properties.style);
277+
278+
runtime.births[0] = now - 500;
279+
runtime.births[1] = now - 480;
280+
const faded = makeCodeTree();
281+
rehypeStreamAnimated({ fadeDuration: 100, runtime })(faded.tree);
282+
expect(faded.code.properties.className ?? []).not.toContain(STREAM_INLINE_CLASS);
283+
expect(faded.code.properties.style).toBeUndefined();
284+
});
285+
286+
it('leaves code blocks inside pre untouched', () => {
287+
const now = performance.now();
288+
const code: Element = {
289+
children: [{ type: 'text', value: 'x' }],
290+
properties: {},
291+
tagName: 'code',
292+
type: 'element',
293+
};
294+
const tree: Root = {
295+
children: [
296+
{
297+
children: [{ children: [code], properties: {}, tagName: 'pre', type: 'element' }],
298+
properties: {},
299+
tagName: 'li',
300+
type: 'element',
301+
},
302+
],
303+
type: 'root',
304+
};
305+
rehypeStreamAnimated({ runtime: makeRuntime(spaced(now, 1)) })(tree);
306+
expect(code.children).toEqual([{ type: 'text', value: 'x' }]);
307+
expect(code.properties.className).toBeUndefined();
308+
});
223309
});

‎src/rehypeStreamAnimated.ts‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { visit } from 'unist-util-visit';
44

55
import { getNow } from './internal';
66
import { createStreamTailNode, type StreamTailData } from './StreamTail';
7+
import { STREAM_INLINE_CLASS } from './styles';
78

89
export interface StreamBirthPacing {
910
capMs: number;
@@ -16,6 +17,12 @@ export interface StreamAnimatedRuntime {
1617
* `pacing` (or supplied up front by legacy callers).
1718
*/
1819
births: number[];
20+
/**
21+
* Inline code fades as one box starting at its first char's birth. The
22+
* delay is frozen at first render, keyed by that char's index, because a
23+
* changed animation-delay on a running animation jumps it.
24+
*/
25+
inlineFades?: Map<number, { birth: number; delay: number }>;
1926
pacing?: StreamBirthPacing;
2027
/**
2128
* Per char, indexed like `births`: `undefined` = not rendered yet,
@@ -73,6 +80,12 @@ const frameOf = (birth: number) => Math.floor(birth / STREAM_FRAME_MS);
7380
const BLOCK_TAGS = new Set(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'td', 'th']);
7481
const SKIP_TAGS = new Set(['pre', 'code', 'svg']);
7582

83+
const classListOf = (node: Element): string[] => {
84+
const className = node.properties.className;
85+
if (Array.isArray(className)) return className.map(String);
86+
return className ? [String(className)] : [];
87+
};
88+
7689
function hasClass(node: Element, cls: string): boolean {
7790
const cn: unknown = node.properties?.className;
7891
if (Array.isArray(cn)) return cn.some((c) => String(c).includes(cls));
@@ -105,11 +118,7 @@ export const rehypeStreamAnimated = (options: StreamAnimatedOptions = {}) => {
105118
if (block) {
106119
for (const node of tree.children) {
107120
if (node.type !== 'element') continue;
108-
const className = node.properties.className;
109-
node.properties.className = [
110-
...(Array.isArray(className) ? className : className ? [String(className)] : []),
111-
'stream-block',
112-
];
121+
node.properties.className = [...classListOf(node), 'stream-block'];
113122
}
114123
return;
115124
}
@@ -200,13 +209,42 @@ export const rehypeStreamAnimated = (options: StreamAnimatedOptions = {}) => {
200209
return createStreamTailNode({ items, text });
201210
};
202211

212+
const inlineFadeDelay = (index: number): number | null => {
213+
const birth = resolvedRuntime?.births[index] ?? now;
214+
if (isSkipped(index) || now - birth >= fadeDuration) return null;
215+
if (!resolvedRuntime) return birth - now;
216+
const fades = (resolvedRuntime.inlineFades ??= new Map());
217+
const frozen = fades.get(index);
218+
if (frozen?.birth === birth) return frozen.delay;
219+
const delay = birth - now;
220+
fades.set(index, { birth, delay });
221+
return delay;
222+
};
223+
224+
const wrapInlineCode = (node: Element) => {
225+
const startIndex = globalCharIndex;
226+
wrapText(node);
227+
if (globalCharIndex === startIndex) return;
228+
const delay = inlineFadeDelay(startIndex);
229+
if (delay === null) {
230+
resolvedRuntime?.inlineFades?.delete(startIndex);
231+
return;
232+
}
233+
node.properties.className = [...classListOf(node), STREAM_INLINE_CLASS];
234+
const style = node.properties.style;
235+
const fadeStyle = `animation-delay:${delay}ms`;
236+
node.properties.style = style ? `${String(style)};${fadeStyle}` : fadeStyle;
237+
};
238+
203239
const wrapText = (node: Element) => {
204240
const newChildren: ElementContent[] = [];
205241
for (const child of node.children) {
206242
if (child.type === 'text') {
207243
newChildren.push(wrapTextValue(child.value));
208244
} else if (child.type === 'element') {
209-
if (!shouldSkip(child)) {
245+
if (child.tagName === 'code') {
246+
wrapInlineCode(child);
247+
} else if (!shouldSkip(child)) {
210248
wrapText(child);
211249
}
212250
newChildren.push(child);

‎src/styles.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ export const STREAM_FADE_DURATION = 180;
44

55
export const STREAMDOWN_ANIMATED_CLASS = 'streamdown-animated';
66

7+
export const STREAM_INLINE_CLASS = 'stream-inline';
8+
79
const css = `
810
@keyframes streamdown-fade-in {
911
from { opacity: 0; }
1012
to { opacity: 1; }
1113
}
1214
.${STREAMDOWN_ANIMATED_CLASS} .stream-char,
15+
.${STREAMDOWN_ANIMATED_CLASS} .${STREAM_INLINE_CLASS},
1316
.${STREAMDOWN_ANIMATED_CLASS} .stream-block {
1417
opacity: 0;
1518
animation-name: streamdown-fade-in;

0 commit comments

Comments
 (0)