A Higher-Order Component (HOC) library for React that enables you to easily add a custom cursor to any component.
npm install with-custom-cursorA Higher-Order Component (HOC) library for React that enables you to easily add a custom cursor to any component.
``bash`
npm install with-custom-cursoror
yarn add with-custom-cursor
`tsx
import { type RefObject } from "react"
type CursorProps = {
ref: RefObject
}
export const Cursor = ({ ref }: CursorProps) => {
return (
)
}
`$3
`css
.cursor {
width: 16rem;
height: 16rem;
background-color: #fff;
align-items: center;
justify-content: center;
border-radius: 50%;
position: absolute;
z-index: 9;
display: flex;
}
.cursor label {
color: #252525;
font-size: 1.8rem;
font-weight: 400;
}
`
`tsx
import { WithCustomCursor } from "with-custom-cursor"
import { Cursor } from "./cursor"
const Card = () => {
return (
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, voluptatibus.
export const CardWithCursor = WithCustomCursor(Card, Cursor)
`
: The component you want to enhance with a custom cursor.
- CursorComponent: The cursor component (must accept a ref prop).Returns a new component that renders your cursor and the wrapped component.
Example

Adding Animations
Animating your custom cursor is straightforward with
with-custom-cursor , as it is fully compatible with any _React-friendly_ animation or CSS library.$3
First, remove
position: absolute and pointer-events: none from your cursor's style. These will be handled by the wrapper, allowing your cursor to animate freely:`css
.cursor {
width: 16rem;
height: 16rem;
background-color: #fff;
align-items: center;
justify-content: center;
border-radius: 50%;
z-index: 9;
display: flex;
}
`
$3
Wrap your cursor component in a
(recommended) or any html element with position: absolute and pointer-events: none (these styles were removed from the cursor itself). You can now use any animation library, such as framer-motion , tailwindcss , or gsap , etc... To animate your cursor.Below is an example using
framer-motion to create a pulsing effect:`tsx
import { motion } from "framer-motion"
import { type RefObject } from "react"type CursorProps = {
ref: RefObject
label: string
color?: string
}
export const Cursor = ({ label, color="white", ref}: CursorProps) => {
return (
className="cursor"
style={{ backgroundColor: color }}
animate={{
scale: [1, 1.2, 1]
}}
transition={{
duration: 1.5,
repeat: Infinity,
ease: "easeInOut",
}}
>
)
}
``That's it! You can now add any animation to your custom cursor component. Feel free to experiment with different libraries and effects to achieve the look and feel you want.
A special thanks to my friend Gabriel Chelles!
MIT © Andre Quintero dos Santos