Skip to content

Commit

Permalink
feat: add onVisibilityChanged to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yhattav committed Nov 20, 2024
1 parent 107ebd5 commit 2ec8755
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ function App() {

## Props

| Prop | Type | Default | Description |
| -------------- | -------------------------------- | ------- | -------------------------------------------------------- |
| `children` | `ReactNode` | - | The component to use as cursor |
| `className` | `string` | `''` | Additional CSS classes |
| `style` | `CSSProperties` | `{}` | Additional inline styles |
| `offsetX` | `number` | `0` | Horizontal offset from cursor position |
| `offsetY` | `number` | `0` | Vertical offset from cursor position |
| `zIndex` | `number` | `9999` | Z-index of the cursor element |
| `smoothFactor` | `number` | `1` | Movement smoothing (1 = no smoothing, higher = smoother) |
| `containerRef` | `RefObject<HTMLElement>` | - | Reference to container element for bounded cursor |
| `onMove` | `(x: number, y: number) => void` | - | Callback fired on cursor movement |
| Prop | Type | Default | Description |
| --------------------- | -------------------------------- | ------- | -------------------------------------------------------- |
| `children` | `ReactNode` | - | The component to use as cursor |
| `className` | `string` | `''` | Additional CSS classes |
| `style` | `CSSProperties` | `{}` | Additional inline styles |
| `offsetX` | `number` | `0` | Horizontal offset from cursor position |
| `offsetY` | `number` | `0` | Vertical offset from cursor position |
| `zIndex` | `number` | `9999` | Z-index of the cursor element |
| `smoothFactor` | `number` | `1` | Movement smoothing (1 = no smoothing, higher = smoother) |
| `containerRef` | `RefObject<HTMLElement>` | - | Reference to container element for bounded cursor |
| `onMove` | `(x: number, y: number) => void` | - | Callback fired on cursor movement |
| `onVisibilityChanged` | `(isVisible: boolean) => void` | - | Callback fired when cursor visibility changes |

## Advanced Usage

Expand Down Expand Up @@ -120,6 +121,30 @@ function InteractiveCursor() {
}
```

### Visibility Change Handler

```tsx
function VisibilityAwareCursor() {
const handleVisibilityChange = (isVisible: boolean) => {
console.log('Cursor visibility:', isVisible);
};

return (
<CustomCursor onVisibilityChanged={handleVisibilityChange}>
<div
style={{
width: '20px',
height: '20px',
backgroundColor: '#3b82f6',
borderRadius: '50%',
transform: 'translate(-50%, -50%)',
}}
/>
</CustomCursor>
);
}
```

## Development

To start development, you can run both the component and the example app concurrently using:
Expand Down Expand Up @@ -157,6 +182,9 @@ const MyComponent: React.FC = () => {
containerRef={containerRef}
smoothFactor={2}
onMove={(x: number, y: number) => console.log(x, y)}
onVisibilityChanged={(isVisible: boolean) =>
console.log('Visible:', isVisible)
}
>
{/* Your cursor content */}
</CustomCursor>
Expand Down
8 changes: 6 additions & 2 deletions src/CustomCursor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface CustomCursorProps {
containerRef?: React.RefObject<HTMLElement>;
onMove?: (x: number, y: number) => void;
hideNativeCursor?: boolean;
onVisibilityChanged?: (isVisible: boolean) => void;
}

const ANIMATION_DURATION = '0.3s';
Expand Down Expand Up @@ -89,10 +90,10 @@ export const CustomCursor: React.FC<CustomCursorProps> = React.memo(
containerRef,
onMove,
hideNativeCursor = true,
onVisibilityChanged,
}) => {
const { position, setPosition, targetPosition, isVisible } =
useMousePosition(containerRef, offsetX, offsetY);
console.log('>>>>>>>>>>>>isVisible', isVisible);
useSmoothAnimation(position, targetPosition, smoothFactor, setPosition);

const [portalContainer, setPortalContainer] =
Expand Down Expand Up @@ -145,6 +146,10 @@ export const CustomCursor: React.FC<CustomCursorProps> = React.memo(
}
}, [position, onMove]);

React.useEffect(() => {
onVisibilityChanged?.(isVisible);
}, [isVisible, onVisibilityChanged]);

const cursorStyle = React.useMemo(
() =>
({
Expand Down Expand Up @@ -176,7 +181,6 @@ export const CustomCursor: React.FC<CustomCursorProps> = React.memo(
// !portalContainer
// ),
// });
console.log('>>>>>>>>>>>>position', position);
if (
!isVisible ||
position.x === null ||
Expand Down

0 comments on commit 2ec8755

Please sign in to comment.