Skip to content

Commit d765e51

Browse files
Merge pull request #154 from T-kesh/feat/css-animation-performance
Enhance CSS animation performance by optimizing transform handling an…
2 parents 2115117 + e78fd82 commit d765e51

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/components/animations/InteractiveAnimations.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ export default function InteractiveAnimations({
2525
if (!el) return;
2626

2727
let dragging = false;
28-
29-
function getCoord(e: PointerEvent | MouseEvent) {
30-
return (e as PointerEvent).clientX ?? (e as MouseEvent).clientX;
31-
}
28+
const resetCompositorHints = () => {
29+
el.style.willChange = '';
30+
};
3231

3332
const onPointerDown = (e: PointerEvent) => {
3433
dragging = true;
3534
startRef.current = axis === 'x' ? e.clientX : e.clientY;
3635
el.setPointerCapture(e.pointerId);
3736
if (ctrlRef.current) ctrlRef.current.stop();
37+
el.style.willChange = 'transform';
38+
el.style.transform = axis === 'x' ? 'translate3d(0,0,0)' : 'translate3d(0,0,0)';
3839
};
3940

4041
const onPointerMove = (e: PointerEvent) => {
@@ -66,6 +67,10 @@ export default function InteractiveAnimations({
6667
ctrlRef.current = engine.spring(delta, 0, (v) => {
6768
el.style.transform = axis === 'x' ? `translate3d(${v}px,0,0)` : `translate3d(0,${v}px,0)`;
6869
});
70+
ctrlRef.current.onStop = () => {
71+
el.style.transform = 'translate3d(0,0,0)';
72+
resetCompositorHints();
73+
};
6974
}
7075
};
7176

@@ -77,11 +82,16 @@ export default function InteractiveAnimations({
7782
el.removeEventListener('pointerdown', onPointerDown);
7883
window.removeEventListener('pointermove', onPointerMove);
7984
window.removeEventListener('pointerup', onPointerUp);
85+
resetCompositorHints();
8086
};
8187
}, [axis, onDismiss, threshold]);
8288

8389
return (
84-
<div ref={ref} className={className} style={{ touchAction: 'pan-y' }}>
90+
<div
91+
ref={ref}
92+
className={className}
93+
style={{ touchAction: axis === 'x' ? 'pan-y' : 'pan-x', transform: 'translate3d(0,0,0)' }}
94+
>
8595
{children}
8696
</div>
8797
);

src/components/dashboard/AdvancedDashboard.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ const SortablePanel = React.memo<SortablePanelProps>(({ panel, children }) => {
4545
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
4646
id: panel.id,
4747
});
48+
const transformValue = CSS.Transform.toString(transform);
4849

4950
const style: React.CSSProperties = {
50-
transform: CSS.Transform.toString(transform),
51+
transform: transformValue ? `${transformValue} translate3d(0,0,0)` : 'translate3d(0,0,0)',
5152
transition,
5253
zIndex: isDragging ? 50 : undefined,
5354
opacity: isDragging ? 0.75 : 1,
55+
willChange: isDragging ? 'transform' : undefined,
5456
};
5557

5658
return (
@@ -205,10 +207,10 @@ export const AdvancedDashboard = React.memo<AdvancedDashboardProps>(({ className
205207
{panels.map((panel, idx) => (
206208
<SortablePanel key={panel.id} panel={panel}>
207209
<motion.div
208-
layout
209210
initial={{ opacity: 0, y: 12 }}
210211
animate={{ opacity: 1, y: 0 }}
211212
transition={{ duration: 0.25, delay: idx * 0.05 }}
213+
style={{ willChange: 'transform, opacity', transform: 'translate3d(0,0,0)' }}
212214
className="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 p-5 pr-10"
213215
>
214216
{/* Panel header */}

0 commit comments

Comments
 (0)