Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add count up component

Joshen Lim5ec08fb6751d
diff --git a/www/components/CountUp.tsx b/www/components/CountUp.tsx
new file mode 100644
index 00000000000..9cc71d03feb
--- /dev/null
+++ b/www/components/CountUp.tsx
@@ -0,0 +1,33 @@
+import { useState, useEffect } from 'react'
+
+type Props = {
+ children: any,
+ duration?: number
+}
+
+const easeOutQuad = (t:number) => t * ( 2 - t )
+const frameDuration = 1000 / 60
+
+const CountUp = (props: Props) => {
+ const { children, duration = 2000 } = props
+ const countTo = parseInt( children, 10 )
+ const [ count, setCount ] = useState( 0 )
+
+ useEffect(() => {
+ let frame = 0
+ const totalFrames = Math.round( duration / frameDuration )
+
+ const counter = setInterval(() => {
+ frame++
+ const progress = easeOutQuad( frame / totalFrames )
+ setCount( countTo * progress )
+
+ if ( frame === totalFrames ) clearInterval( counter )
+ }, frameDuration)
+
+ }, [])
+
+ return <span>{Math.floor(count)}</span>
+}
+
+export default CountUp
\ No newline at end of file
diff --git a/www/pages/beta.tsx b/www/pages/beta.tsx
index 80e1765704a..71232904c63 100644
--- a/www/pages/beta.tsx
+++ b/www/pages/beta.tsx
@@ -1,6 +1,7 @@
import Head from 'next/head'
import Container from 'components/Container'
import Layout from 'components/Layout'
+import CountUp from 'components/CountUp'
import { APP_NAME, DESCRIPTION } from 'lib/constants'
@@ -57,7 +58,7 @@ const Introduction = () => (
</div>
<div className="col-span-10">
<p className="text-6xl">
- {stat.value}
+ <CountUp>{stat.value}</CountUp>
{stat.unit && <span className="text-2xl ml-1">{stat.unit}</span>}
</p>
</div>