Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"jose": "^6.1.2",
"monaco-editor": "^0.55.1",
"motion": "^12.6.3",
"motion-plus": "^1.5.1",
"next": "15.5.10",
"next-intl": "^4.6.1",
"react": "^19.2.0",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 28 additions & 55 deletions src/app/components/HeadingReveal/HeadingReveal.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use client";

import { animate, stagger, anticipate } from "motion";
import { splitText } from "motion-plus";
import { useEffect, useRef } from "react";
import classNames from "classnames";
import { useSplitLocaleBy } from "@/i18n/hooks";
import { useTokenRevealAnimation } from "@/hooks/useTokenRevealAnimation";
import { type TextSplitBy } from "@/lib/text/segmentText";

export default function HeadingReveal({
text,
Expand All @@ -14,6 +12,7 @@ export default function HeadingReveal({
cursorColor = "#00FFFF",
baseDelay = 0,
splitBy,
locale,
speed = 0.25,
}: {
text: string;
Expand All @@ -22,65 +21,39 @@ export default function HeadingReveal({
color?: string;
cursorColor?: string;
baseDelay?: number;
splitBy?: "words" | "chars";
splitBy?: TextSplitBy;
locale?: string | string[];
speed?: number;
}) {
const containerRef = useRef<HTMLDivElement>(null);
const localeSegmentation = useSplitLocaleBy();
splitBy ??= localeSegmentation;
const resolvedSplitBy = splitBy ?? "words";

useEffect(() => {
document.fonts.ready.then(() => {
if (!containerRef.current) return;
const { containerRef, tokens, getTokenRef } = useTokenRevealAnimation({
text,
splitBy: resolvedSplitBy,
locale,
Comment on lines +28 to +33

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the default splitting behavior: previously splitBy defaulted based on the active locale (via useSplitLocaleBy()), but now it always defaults to "words". If this component is used with zh-Hant/zh-Hans content, that can regress the intended character-by-character reveal. Consider restoring the locale-based default (or deriving it from the new locale prop) to preserve prior behavior.

Copilot uses AI. Check for mistakes.
color,
cursorColor,
baseDelay,
speed,
});

// Hide the container until the fonts are loaded
containerRef.current.style.visibility = "visible";
const HeadingTag = headingLevel;

const { words, chars } = splitText(
containerRef.current.querySelector(headingLevel)!
);

// Animate the words in the h1
animate(
splitBy === "words" ? words : chars,
{
backgroundColor: [
"rgba(255,255,255,0)",
"rgba(255,255,255,0)",
cursorColor,
cursorColor,
cursorColor,
"rgba(255,255,255,0)",
],
color: [
"rgba(255,255,255,0)",
"rgba(255,255,255,0)",
cursorColor,
cursorColor,
cursorColor,
color,
],
},
{
ease: anticipate,
duration: speed,
delay: stagger(speed / 2, { startDelay: baseDelay }),
}
);
});
}, []);
const headingContent = tokens.map((token, index) => (
<span
key={`${index}-${token.text}`}
aria-hidden="true"
ref={getTokenRef(index)}
>
{token.text}
</span>
));

return (
<div ref={containerRef}>
{headingLevel === "h1" && (
<h1 className={classNames("h1", className)}>{text}</h1>
)}
{headingLevel === "h2" && (
<h2 className={classNames("h2", className)}>{text}</h2>
)}
{headingLevel === "h3" && (
<h3 className={classNames("h3", className)}>{text}</h3>
)}
<HeadingTag className={classNames(headingLevel, className)} aria-label={text}>
{headingContent}
</HeadingTag>
</div>
);
}
106 changes: 106 additions & 0 deletions src/hooks/useTokenRevealAnimation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { animate, anticipate, stagger } from "motion";
import { useEffect, useMemo, useRef, type RefObject } from "react";
import {
segmentText,
type SegmentedTextToken,
type TextSplitBy,
} from "@/lib/text/segmentText";

type UseTokenRevealAnimationProps = {
text: string;
splitBy: TextSplitBy;
locale?: string | string[];
color: string;
cursorColor: string;
baseDelay: number;
speed: number;
};

type UseTokenRevealAnimationResult = {
containerRef: RefObject<HTMLDivElement | null>;
tokens: SegmentedTextToken[];
getTokenRef: (index: number) => (element: HTMLSpanElement | null) => void;
};

export function useTokenRevealAnimation({
text,
splitBy,
locale,
color,
cursorColor,
baseDelay,
speed,
}: UseTokenRevealAnimationProps): UseTokenRevealAnimationResult {
const containerRef = useRef<HTMLDivElement>(null);
const tokenRefs = useRef<(HTMLSpanElement | null)[]>([]);
const tokens = useMemo(
() => segmentText(text, splitBy, locale),
[text, splitBy, locale]
);

useEffect(() => {
const container = containerRef.current;
if (!container) return;

let cancelled = false;
let controls: ReturnType<typeof animate> | undefined;
const fontsReady =
"fonts" in document ? document.fonts.ready : Promise.resolve();

fontsReady.then(() => {
if (cancelled || !containerRef.current) return;

containerRef.current.style.visibility = "visible";

const targets = tokens
.map((token, index) =>
token.shouldAnimate ? tokenRefs.current[index] : null
)
.filter((element): element is HTMLSpanElement => Boolean(element));

if (targets.length === 0) return;

controls = animate(
targets,
{
backgroundColor: [
"rgba(255,255,255,0)",
"rgba(255,255,255,0)",
cursorColor,
cursorColor,
cursorColor,
"rgba(255,255,255,0)",
],
color: [
"rgba(255,255,255,0)",
"rgba(255,255,255,0)",
cursorColor,
cursorColor,
cursorColor,
color,
],
},
{
ease: anticipate,
duration: speed,
delay: stagger(speed / 2, { startDelay: baseDelay }),
}
);
});

return () => {
cancelled = true;
controls?.stop();
};
}, [tokens, baseDelay, speed, cursorColor, color]);

const getTokenRef = (index: number) => (element: HTMLSpanElement | null) => {
tokenRefs.current[index] = element;
};

return {
containerRef,
tokens,
getTokenRef,
};
}
55 changes: 55 additions & 0 deletions src/lib/text/segmentText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export type TextSplitBy = "words" | "chars";

export type SegmentedTextToken = {
text: string;
shouldAnimate: boolean;
};

function segmentWithIntl(
text: string,
splitBy: TextSplitBy,
locale?: string | string[]
): SegmentedTextToken[] {
const segmenter = new Intl.Segmenter(locale, {
granularity: splitBy === "words" ? "word" : "grapheme",
});

return Array.from(segmenter.segment(text)).map((segment) => ({
text: segment.segment,
shouldAnimate:
splitBy === "words"
? (segment.isWordLike ?? /\S/.test(segment.segment))
: /\S/.test(segment.segment),
Comment on lines +19 to +22

Copilot AI Mar 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Intl.Segmenter path, shouldAnimate uses segment.isWordLike ?? /\S/.test(...). When isWordLike is false (e.g., punctuation tokens like ","), this will mark them as not animating, which can leave punctuation visible while the rest of the text is revealed. To keep behavior consistent with the fallback (and with typical reveal animations), consider basing shouldAnimate on non-whitespace for word splitting as well, or otherwise ensure punctuation is included in the animated targets.

Suggested change
shouldAnimate:
splitBy === "words"
? (segment.isWordLike ?? /\S/.test(segment.segment))
: /\S/.test(segment.segment),
shouldAnimate: /\S/.test(segment.segment),

Copilot uses AI. Check for mistakes.
}));
}

function segmentWithFallback(
text: string,
splitBy: TextSplitBy
): SegmentedTextToken[] {
if (splitBy === "words") {
return text.split(/(\s+)/).map((part) => ({
text: part,
shouldAnimate: /\S/.test(part),
}));
}

return Array.from(text).map((part) => ({
text: part,
shouldAnimate: /\S/.test(part),
}));
}

export function segmentText(
text: string,
splitBy: TextSplitBy,
locale?: string | string[]
): SegmentedTextToken[] {
const hasSegmenter = typeof Intl !== "undefined" && "Segmenter" in Intl;

if (hasSegmenter) {
return segmentWithIntl(text, splitBy, locale);
}

return segmentWithFallback(text, splitBy);
}