MCPcopy Create free account
hub / github.com/SyntaxUI/syntaxui / Counter

Function Counter

src/showcase/components/text/text-ticker/Counter.tsx:6–40  ·  view source on GitHub ↗
({
  value,
  direction = 'up',
}: {
  value: number
  direction?: 'up' | 'down'
})

Source from the content-addressed store, hash-verified

4import { useInView, useMotionValue, useSpring } from 'framer-motion'
5
6export default function Counter({
7 value,
8 direction = 'up',
9}: {
10 value: number
11 direction?: 'up' | 'down'
12}) {
13 const ref = useRef<HTMLSpanElement>(null)
14 const motionValue = useMotionValue(direction === 'down' ? value : 0)
15 const springValue = useSpring(motionValue, {
16 damping: 100,
17 stiffness: 100,
18 })
19 const isInView = useInView(ref, { once: true, margin: '-100px' })
20
21 useEffect(() => {
22 if (isInView) {
23 motionValue.set(direction === 'down' ? 0 : value)
24 }
25 }, [motionValue, isInView, value, direction])
26
27 useEffect(
28 () =>
29 springValue.on('change', (latest) => {
30 if (ref.current) {
31 ref.current.textContent = Intl.NumberFormat('en-US').format(
32 latest.toFixed(0),
33 )
34 }
35 }),
36 [springValue],
37 )
38
39 return <span ref={ref} />
40}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected