-
Notifications
You must be signed in to change notification settings - Fork 102
feat(effects): split text without motion-plus #383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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, | ||
| }; | ||
| } |
| 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
|
||||||||||||
| shouldAnimate: | |
| splitBy === "words" | |
| ? (segment.isWordLike ?? /\S/.test(segment.segment)) | |
| : /\S/.test(segment.segment), | |
| shouldAnimate: /\S/.test(segment.segment), |
There was a problem hiding this comment.
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
splitBydefaulted based on the active locale (viauseSplitLocaleBy()), 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 newlocaleprop) to preserve prior behavior.