r/reactjs • • 17d ago

Porting SwiftUI's spring animation to React for a Windows "Dynamic Island"

Openbar shows Claude Code and Codex sessions in a Dynamic Island. The Mac app is SwiftUI, the Windows one is Tauri + React, and I wanted the island to resize with the same feel on both.

SwiftUI describes a spring by duration and bounce, so I ported that math. Width and height each ride their own spring. A growing side may swing past its target (capped at 12 px), a shrinking side never does.

export function springProgress(spring: Spring, time: number): number {

if (time <= 0) return 0;

const natural = (2 * Math.PI) / spring.duration;

const damping = 1 - spring.bounce;

if (damping >= 1) return 1 - Math.exp(-natural * time) * (1 + natural * time);

const damped = natural * Math.sqrt(1 - damping * damping);

return (

1 -

Math.exp(-damping * natural * time) *

(Math.cos(damped * time) + ((damping * natural) / damped) * Math.sin(damped * time))

);

}

Full file: windows/src/island/motion.ts in https://github.com/MF-DEEV/Openbar (GPL)

Do you write spring math yourself or reach for a library?

0 Upvotes

4 comments sorted by

2

u/Honey-Entire 17d ago

You didn’t port anything from the looks of it. What are you even trying to convey to us?

1

u/CesnaitisSwitlana 15d ago

pretty sure the whole point was just to drop the repo link.

0

u/[deleted] 17d ago

[deleted] — view deleted comment

0

u/Significant-Sea-7238 17d ago

Good catch on the sqrt, but the damping >= 1 line returns before it, so a negative bounce falls back to critical damping instead of NaN. Retargeting is handled in MotionTimeline: when a target moves on the way the side was already going, it adds another spring on top of the one in flight, so velocity carries over. Only a side that turns back starts again from where it is. And yes, it stops once size and centre stay within 0.5 px.