Skip to content

Commit

Permalink
fix: align types and remove unneeded deps (#12)
Browse files Browse the repository at this point in the history
* fix: align types and remove unneeded deps

* remove unneeded arg

* remove from test

* remove return null
  • Loading branch information
yhattav authored Dec 2, 2024
1 parent cf8c9cf commit 9cb281f
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/CustomCursor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const CustomCursor: React.FC<CustomCursorProps> = React.memo(
}) => {
const { position, setPosition, targetPosition, isVisible } =
useMousePosition(containerRef, offsetX, offsetY);
useSmoothAnimation(position, targetPosition, smoothFactor, setPosition);
useSmoothAnimation(targetPosition, smoothFactor, setPosition);

const [portalContainer, setPortalContainer] =
React.useState<HTMLElement | null>(null);
Expand Down
19 changes: 2 additions & 17 deletions src/hooks/useSmoothAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { Position, TargetPosition } from '../types';
const SMOOTHING_THRESHOLD = 0.1;

export function useSmoothAnimation(
position: Position,
targetPosition: TargetPosition,
smoothFactor: number,
setPosition: React.Dispatch<React.SetStateAction<Position>>
) {
): void {
// Memoize the smoothing calculation
const calculateNewPosition = useCallback(
(currentPosition: Position) => {
Expand Down Expand Up @@ -69,20 +68,6 @@ export function useSmoothAnimation(
return;
}

// Skip animation if position is null
if (position.x === null || position.y === null) {
return;
}

return animate();
}, [
smoothFactor,
position.x,
position.y,
targetPosition,
animate,
setPosition,
]);

return null;
}, [smoothFactor, targetPosition, animate, setPosition]);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { default as CustomCursor } from './CustomCursor';
// If you have types to export:
export type { CustomCursorProps } from './CustomCursor';

export type { Position } from './types';
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { ReactNode } from 'react';

export type Position = {
x: number | null;
y: number | null;
Expand Down
5 changes: 1 addition & 4 deletions test/hooks/useSmoothAnimation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ describe('useSmoothAnimation', () => {

it('smoothly animates position changes', () => {
const setPosition = jest.fn();
const position = { x: 0, y: 0 };
const targetPosition = { x: 100, y: 100 };

renderHook(() =>
useSmoothAnimation(position, targetPosition, 0.5, setPosition)
);
renderHook(() => useSmoothAnimation(targetPosition, 0.5, setPosition));

jest.advanceTimersByTime(16); // One frame
expect(setPosition).toHaveBeenCalled();
Expand Down

0 comments on commit 9cb281f

Please sign in to comment.