-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature number-count animation for DORA-cards
* chore: downgrade axios version * feat: add count-up hook * feat: add the count-up animation in Dora Cards * refactor: update code as per review comment * fix: linting issue
- Loading branch information
Showing
5 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,33 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const FRAME_DURATION_MS = 16; // Average frame duration for 60fps | ||
|
||
export const useCountUp = ( | ||
targetValue: number, | ||
duration: number = 1500, | ||
decimalPlaces: number = 0 | ||
): number => { | ||
const [count, setCount] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
let start = 0; | ||
const increment = targetValue / (duration / FRAME_DURATION_MS); | ||
|
||
const animateCount = () => { | ||
start += increment; | ||
|
||
if (start >= targetValue) { | ||
setCount(parseFloat(targetValue.toFixed(decimalPlaces))); | ||
} else { | ||
setCount(parseFloat(start.toFixed(decimalPlaces))); | ||
requestAnimationFrame(animateCount); | ||
} | ||
}; | ||
|
||
animateCount(); | ||
|
||
return () => {}; | ||
}, [targetValue, duration, decimalPlaces]); | ||
|
||
return count; | ||
}; |