Skip to content

Commit cc64026

Browse files
refactor: streamline hover animation handling and improve performance in icon components
Signed-off-by: Rajesh-Nagarajan-11 <rajeshnagarajan36@gmail.com>
1 parent 281641d commit cc64026

4 files changed

Lines changed: 22 additions & 23 deletions

File tree

src/icons/Configuration/ConfigurationIcon.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import React, { FC } from 'react';
22
import { DEFAULT_HEIGHT, DEFAULT_WIDTH } from '../../constants/constants';
33
import { IconProps } from '../types';
4-
import { PathStyle, injectKeyframes } from './style';
5-
6-
injectKeyframes();
4+
import { PathStyle, keyframesCSS } from './style';
75

86
export interface ConfigurationIconProps extends IconProps {
97
isHoverEffect?: boolean;
@@ -25,6 +23,7 @@ export const ConfigurationIcon: FC<ConfigurationIconProps> = ({
2523
className={isHoverEffect ? 'configuration-icon-go' : undefined}
2624
{...props}
2725
>
26+
{isHoverEffect ? <style>{keyframesCSS}</style> : null}
2827
<path
2928
className="configuration-icon-path1"
3029
style={PathStyle}

src/icons/Configuration/style.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import React from 'react';
22

3-
export const StyleID = 'configuration-icon-keyframes';
4-
53
export const keyframesCSS = `
64
@keyframes config-wrench-shake {
75
0% { transform: rotate(-20deg); }
@@ -21,12 +19,3 @@ export const PathStyle: React.CSSProperties = {
2119
transformBox: 'fill-box',
2220
transformOrigin: 'center',
2321
};
24-
25-
export function injectKeyframes(): void {
26-
if (typeof document === 'undefined') return;
27-
if (document.getElementById(StyleID)) return;
28-
const style = document.createElement('style');
29-
style.id = StyleID;
30-
style.textContent = keyframesCSS;
31-
document.head.appendChild(style);
32-
}

src/icons/Dashboard/useAnimationTrigger.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ export function useAnimationTrigger(
1919
svg.classList.add('dashboard-icon-go');
2020

2121
const lastPath = svg.querySelector('.dashboard-icon-bl');
22-
lastPath?.addEventListener(
23-
'animationend',
24-
() => svg.classList.remove('dashboard-icon-go'),
25-
{ once: true }
26-
);
22+
const handleAnimationEnd = () => {
23+
svg.classList.remove('dashboard-icon-go');
24+
};
25+
26+
lastPath?.addEventListener('animationend', handleAnimationEnd, { once: true });
27+
28+
return () => {
29+
lastPath?.removeEventListener('animationend', handleAnimationEnd);
30+
};
2731
}, [isHoverEffect, svgRef]);
2832
}

src/icons/Performance/useNeedleSpring.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,25 @@ export function useNeedleSpring(
4444
s.current = { cur: ROT_ZERO, vel: 0 };
4545
set(ROT_ZERO);
4646

47-
const tick = () => {
47+
let lastTime = performance.now();
48+
49+
const tick = (now: number) => {
50+
const dt = Math.min((now - lastTime) / 1000, 0.1);
51+
lastTime = now;
4852
const { cur, vel } = s.current;
49-
const v = vel + (-32.5 * (cur - ROT_REST) - 9 * vel) * 0.016;
50-
const c = cur + v * 0.016;
53+
const v = vel + (-32.5 * (cur - ROT_REST) - 9 * vel) * dt;
54+
const c = cur + v * dt;
5155
s.current = { cur: c, vel: v };
5256
set(c);
5357
if (Math.abs(c - ROT_REST) > 0.05 || Math.abs(v) > 0.05)
5458
raf.current = requestAnimationFrame(tick);
5559
else { set(ROT_REST); s.current = { cur: ROT_REST, vel: 0 }; raf.current = null; }
5660
};
5761

58-
timer.current = setTimeout(() => { raf.current = requestAnimationFrame(tick); }, 60);
62+
timer.current = setTimeout(() => {
63+
lastTime = performance.now();
64+
raf.current = requestAnimationFrame(tick);
65+
}, 60);
5966

6067
return () => {
6168
if (timer.current) {

0 commit comments

Comments
 (0)