Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve npm download count animation #329

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.2",
"@headlessui/react": "1.7.18",
"@number-flow/react": "^0.4.1",
"@octokit/graphql": "^7.0.2",
Expand Down
Loading
Loading