Skip to content

Commit

Permalink
improve npm download count animation
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart committed Dec 31, 2024
1 parent b59555c commit 6228002
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 64 deletions.
25 changes: 16 additions & 9 deletions app/components/OpenSourceStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import { FaDownload } from 'react-icons/fa'
import convexImageWhite from '~/images/convex-white.svg'
import convexImageDark from '~/images/convex-dark.svg'

const counterIntervalMs = 500

const StableCounter = ({ value }: { value?: number }) => {
const StableCounter = ({
value,
intervalMs,
}: {
value?: number
intervalMs?: number
}) => {
const dummyString = Number(
Array(value?.toString().length ?? 1)
.fill('8')
Expand All @@ -23,14 +27,19 @@ const StableCounter = ({ value }: { value?: number }) => {
<span className="opacity-0">{dummyString}</span>
<span className="absolute -top-0.5 left-0">
<NumberFlow
// Defer to counter hook calculated interval
spinTiming={{
duration: intervalMs,
easing: 'linear',
}}
// Slow horizontal shift animation (due to differing number widths)
transformTiming={{
duration: counterIntervalMs,
duration: 1000,
easing: 'linear',
}}
value={value}
trend={1}
continuous
isolate
willChange
/>
</span>
Expand All @@ -43,10 +52,8 @@ const NpmDownloadCounter = ({
}: {
npmData: Parameters<typeof useNpmDownloadCounter>[0]
}) => {
const liveNpmDownloadCount = useNpmDownloadCounter(npmData, {
intervalMs: counterIntervalMs,
})
return <StableCounter value={liveNpmDownloadCount} />
const { count, intervalMs } = useNpmDownloadCounter(npmData)
return <StableCounter value={count} intervalMs={intervalMs} />
}

export default function OssStats() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@convex-dev/react-query": "0.0.0-alpha.8",
"@docsearch/css": "^3.5.2",
"@docsearch/react": "^3.5.2",
"@erquhart/convex-oss-stats": "0.4.2",
"@erquhart/convex-oss-stats": "^0.5.0",
"@headlessui/react": "1.7.18",
"@number-flow/react": "^0.4.1",
"@octokit/graphql": "^7.0.2",
Expand Down
Loading

0 comments on commit 6228002

Please sign in to comment.