Repository navigation
feat: add odometer style animation to star count #986
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
Open
imfaizanyousaf
wants to merge
2
commits into
dev-null-sh:production
Choose a base branch
from
imfaizanyousaf:feat/star-odometer-animation
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−24
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { useLayoutEffect, useRef, useState } from 'react'; | ||
| import { formatCount } from '@/lib/numbers'; | ||
| import { odometerPlaces } from '@/lib/odometer'; | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| const ANIMATION_MS = 250; | ||
|
|
||
| interface OdometerCountProps { | ||
| value: number; | ||
| className?: string; | ||
| emptyWhenZero?: boolean; | ||
| } | ||
|
|
||
| function displayedCount(value: number, emptyWhenZero: boolean): string { | ||
| return emptyWhenZero && value === 0 ? '' : formatCount(value); | ||
| } | ||
|
|
||
| export function OdometerCount({ | ||
| value, | ||
| className, | ||
| emptyWhenZero = false, | ||
| }: OdometerCountProps) { | ||
| const previousValueRef = useRef(value); | ||
| const [previous, setPrevious] = useState(() => | ||
| displayedCount(value, emptyWhenZero), | ||
| ); | ||
| const [isAnimating, setIsAnimating] = useState(false); | ||
| const [direction, setDirection] = useState<'up' | 'down'>('up'); | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (value === previousValueRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| const oldValue = previousValueRef.current; | ||
| previousValueRef.current = value; | ||
| setPrevious(displayedCount(oldValue, emptyWhenZero)); | ||
| setDirection(value >= oldValue ? 'up' : 'down'); | ||
| setIsAnimating(true); | ||
|
|
||
| const timer = setTimeout(() => setIsAnimating(false), ANIMATION_MS); | ||
|
|
||
| return () => clearTimeout(timer); | ||
| }, [value, emptyWhenZero]); | ||
|
|
||
| const current = displayedCount(value, emptyWhenZero); | ||
| const places = odometerPlaces(previous, current); | ||
|
|
||
| return ( | ||
| <span | ||
| data-test="star-count" | ||
| className={cn( | ||
| 'inline-flex min-w-[1ch] items-center text-left tabular-nums empty:hidden', | ||
| className, | ||
| )} | ||
| > | ||
| {isAnimating | ||
| ? places.map(({ char, previousChar, isRolling }, index) => { | ||
| const place = places.length - index; | ||
|
|
||
| if (!isRolling) { | ||
| return ( | ||
| <span key={place} className="inline-block"> | ||
| {char} | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <span | ||
| key={`${place}-${previousChar}-${char}-${direction}`} | ||
| className="relative inline-flex h-[1.25em] overflow-hidden leading-[1.25em]" | ||
| > | ||
| {previousChar !== ' ' && ( | ||
| <span | ||
| aria-hidden="true" | ||
| className={cn( | ||
| 'absolute inset-0 flex items-center justify-center motion-reduce:hidden', | ||
| direction === 'up' | ||
| ? 'motion-safe:animate-odometer-up-out' | ||
| : 'motion-safe:animate-odometer-down-out', | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| )} | ||
| > | ||
| {previousChar} | ||
| </span> | ||
| )} | ||
| {char !== ' ' ? ( | ||
| <span | ||
| className={cn( | ||
| 'flex items-center justify-center', | ||
| direction === 'up' | ||
| ? 'motion-safe:animate-odometer-up-in' | ||
| : 'motion-safe:animate-odometer-down-in', | ||
| )} | ||
| > | ||
| {char} | ||
| </span> | ||
| ) : ( | ||
| <span className="invisible inline-block"> | ||
| {previousChar} | ||
| </span> | ||
| )} | ||
| </span> | ||
| ); | ||
| }) | ||
| : current} | ||
| </span> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| export interface OdometerPlace { | ||
| /** The character shown at this place now. */ | ||
| char: string; | ||
| /** The character it replaces, or a space when the count grows into this place. */ | ||
| previousChar: string; | ||
| /** Whether this place rolls from the previous character. */ | ||
| isRolling: boolean; | ||
| } | ||
|
|
||
| function isDigit(char: string): boolean { | ||
| return char >= '0' && char <= '9'; | ||
| } | ||
|
|
||
| function rolls(char: string, previousChar: string): boolean { | ||
| if (char === previousChar) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!isDigit(char) && !isDigit(previousChar)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (isDigit(char) && isDigit(previousChar)) { | ||
| return true; | ||
| } | ||
|
|
||
| return isDigit(char) ? previousChar === ' ' : char === ' '; | ||
| } | ||
|
|
||
| /** | ||
| * The places of the new count, paired with the character each replaces, so the view rolls only what changed. A digit | ||
| * that appears or disappears rolls; a separator or a shortened count's suffix changes in place. | ||
| */ | ||
| export function odometerPlaces( | ||
| previous: string, | ||
| current: string, | ||
| ): OdometerPlace[] { | ||
| const length = Math.max(previous.length, current.length); | ||
| const currentChars = current.padStart(length, ' '); | ||
| const previousChars = previous.padStart(length, ' '); | ||
|
|
||
| return Array.from({ length }, (_, index) => { | ||
| const char = currentChars[index]; | ||
| const previousChar = previousChars[index]; | ||
|
|
||
| return { char, previousChar, isRolling: rolls(char, previousChar) }; | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
| import { odometerPlaces } from '../../resources/js/lib/odometer.ts'; | ||
|
|
||
| // A space is a place the count does not have; `·` makes it visible in the expectations. | ||
| const shown = (previous: string, current: string): string[] => | ||
| odometerPlaces(previous, current).map( | ||
| ({ char, previousChar, isRolling }) => { | ||
| const place = (value: string) => (value === ' ' ? '·' : value); | ||
|
|
||
| return isRolling | ||
| ? `${place(previousChar)}→${place(char)}` | ||
| : place(char); | ||
| }, | ||
| ); | ||
|
|
||
| void test('leaves every place still when the count does not change', () => { | ||
| assert.deepEqual(shown('1,250', '1,250'), ['1', ',', '2', '5', '0']); | ||
| }); | ||
|
|
||
| void test('rolls only the digit that changes', () => { | ||
| assert.deepEqual(shown('120', '121'), ['1', '2', '0→1']); | ||
| assert.deepEqual(shown('121', '120'), ['1', '2', '1→0']); | ||
| }); | ||
|
|
||
| void test('rolls a new place in when the count grows', () => { | ||
| assert.deepEqual(shown('9', '10'), ['·→1', '9→0']); | ||
| assert.deepEqual(shown('99', '100'), ['·→1', '9→0', '9→0']); | ||
| }); | ||
|
|
||
| void test('rolls the old place out when the count shrinks', () => { | ||
| assert.deepEqual(shown('10', '9'), ['1→·', '0→9']); | ||
| assert.deepEqual(shown('100', '99'), ['1→·', '0→9', '0→9']); | ||
| }); | ||
|
|
||
| void test('leaves the thousands separator still while the digits roll', () => { | ||
| assert.deepEqual(shown('999', '1,000'), ['·→1', ',', '9→0', '9→0', '9→0']); | ||
| }); | ||
|
|
||
| void test("leaves a shortened count's suffix still while the digits roll", () => { | ||
| // Covers both directions: the comma and `k` stay still; a digit replacing `k` also stays still. | ||
| assert.deepEqual(shown('9,999', '10k'), ['9→·', '·', '9→1', '9→0', 'k']); | ||
| assert.deepEqual(shown('10k', '9,999'), ['·→9', ',', '1→9', '0→9', '9']); | ||
| }); | ||
|
|
||
| void test('rolls the first digit in and out when the count appears or disappears', () => { | ||
| assert.deepEqual(shown('', '1'), ['·→1']); | ||
| assert.deepEqual(shown('1', ''), ['1→·']); | ||
| assert.deepEqual(shown('', ''), []); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.