Skip to content

Commit 10bad8b

Browse files
committed
refactor(tui): simplify fade-in animation using useTimeline hook
Replace manual animation frame handling with useTimeline hook from @opentui/solid for cleaner and more maintainable fade-in animation logic. This also removes the need for child element resolution.
1 parent 97bba7b commit 10bad8b

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed
Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @jsxImportSource @opentui/solid */
2-
import { createSignal, onMount, onCleanup, JSX, children as resolveChildren } from "solid-js"
2+
import { createSignal, onMount, JSX } from "solid-js"
3+
import { useTimeline } from "@opentui/solid"
34

45
interface FadeInProps {
56
children: JSX.Element
@@ -11,37 +12,23 @@ export function FadeIn(props: FadeInProps) {
1112
const [opacity, setOpacity] = createSignal(0)
1213
const delay = props.delay ?? 0
1314
const duration = props.duration ?? 800
14-
const resolved = resolveChildren(() => props.children)
1515

16-
onMount(() => {
17-
const startTime = Date.now() + delay
18-
19-
const animate = () => {
20-
const now = Date.now()
21-
if (now < startTime) {
22-
requestAnimationFrame(animate)
23-
return
24-
}
25-
26-
const progress = Math.min(1, (now - startTime) / duration)
27-
setOpacity(progress)
16+
const timeline = useTimeline({ autoplay: true })
17+
const target = { opacity: 0 }
2818

29-
if (progress < 1) {
30-
requestAnimationFrame(animate)
31-
}
32-
}
33-
34-
const animationFrame = requestAnimationFrame(animate)
35-
onCleanup(() => cancelAnimationFrame(animationFrame))
19+
onMount(() => {
20+
timeline.add(
21+
target,
22+
{
23+
duration,
24+
opacity: 1,
25+
ease: "outQuad",
26+
onUpdate: () => setOpacity(target.opacity),
27+
},
28+
delay
29+
)
3630
})
3731

38-
// Apply opacity directly to child element instead of wrapping
39-
const child = resolved()
40-
if (child && typeof child === 'object' && 'type' in child) {
41-
// @ts-expect-error - opacity is a valid box property in OpenTUI
42-
return <box opacity={opacity()} flexGrow={1}>{child}</box>
43-
}
44-
4532
// @ts-expect-error - opacity is a valid box property in OpenTUI
4633
return <box opacity={opacity()} flexGrow={1}>{props.children}</box>
4734
}

0 commit comments

Comments
 (0)