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

Reduce animation duration for count up effect #624

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
14 changes: 13 additions & 1 deletion web-server/src/components/DoraMetricsConfigurationSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ export const DoraMetricsConfigurationSettings = () => {
Settings
</FlexBox>
</Button>
<Menu anchorEl={anchorEl.current} open={open.value} onClose={open.false}>
<Menu
anchorEl={anchorEl.current}
open={open.value}
onClose={open.false}
MenuListProps={{
sx: {
padding: 0
}
}}
sx={{
padding: 0
}}
>
<MenuItem
onClick={() => {
open.false();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ export const ChangeFailureRateCard = () => {
.incident_count
);

const changeFailureRateCount = useCountUp(
changeFailureRateProps.count || 0,
1500, // animation duration
2 // decimal place
);
const changeFailureRateCount = useCountUp(changeFailureRateProps.count || 0);

const series = useMemo(
() => [
Expand Down
27 changes: 13 additions & 14 deletions web-server/src/hooks/useCountUp.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { useState, useEffect } from 'react';

const FRAME_DURATION_MS = 16; // Average frame duration for 60fps
const STEPS = 7; // number of steps to reach the target value
const INTERVAL = 75; // in ms

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);
let currentStep = 0;
const stepValue = targetValue / STEPS;

const animateCount = () => {
start += increment;
const timer = setInterval(() => {
currentStep++;

if (start >= targetValue) {
if (currentStep >= STEPS) {
setCount(parseFloat(targetValue.toFixed(decimalPlaces)));
clearInterval(timer);
} else {
setCount(parseFloat(start.toFixed(decimalPlaces)));
requestAnimationFrame(animateCount);
const newValue = stepValue * currentStep;
setCount(parseFloat(newValue.toFixed(decimalPlaces)));
}
};
}, INTERVAL);

animateCount();

return () => {};
}, [targetValue, duration, decimalPlaces]);
return () => clearInterval(timer);
}, [targetValue, decimalPlaces]);

return count;
};
Loading