Skip to content

Commit

Permalink
fix: don't initially return 0,0 as its a valid mouse position but is …
Browse files Browse the repository at this point in the history
…wrong (#13)

* don't initially return 0,0 as its a valid mouse position but is wrong

* types

* types
  • Loading branch information
yhattav authored Dec 3, 2024
1 parent 9cb281f commit 4d9700d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/hooks/useMousePosition.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { useEffect, useState, useCallback, useRef } from 'react';
import { Position, TargetPosition } from '../types';
import { Position } from '../types';

export function useMousePosition(
containerRef: React.RefObject<HTMLElement> | undefined,
offsetX: number,
offsetY: number
) {
): {
position: Position;
setPosition: React.Dispatch<React.SetStateAction<Position>>;
targetPosition: Position;
isVisible: boolean;
} {
const [position, setPosition] = useState<Position>({ x: null, y: null });
const [targetPosition, setTargetPosition] = useState<TargetPosition>({
x: 0,
y: 0,
const [targetPosition, setTargetPosition] = useState<Position>({
x: null,
y: null,
});
const [isVisible, setIsVisible] = useState(containerRef?.current !== null);

Expand Down
11 changes: 8 additions & 3 deletions src/hooks/useSmoothAnimation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { useEffect, useCallback } from 'react';
import { Position, TargetPosition } from '../types';
import { Position } from '../types';

const SMOOTHING_THRESHOLD = 0.1;

export function useSmoothAnimation(
targetPosition: TargetPosition,
targetPosition: Position,
smoothFactor: number,
setPosition: React.Dispatch<React.SetStateAction<Position>>
): void {
// Memoize the smoothing calculation
const calculateNewPosition = useCallback(
(currentPosition: Position) => {
if (currentPosition.x === null || currentPosition.y === null) {
if (
currentPosition.x === null ||
currentPosition.y === null ||
targetPosition.x === null ||
targetPosition.y === null
) {
return currentPosition;
}

Expand Down

0 comments on commit 4d9700d

Please sign in to comment.