2

I'm trying to create a custom cursor in React, and want the cursor to change in appearance when it hovers over any link on the page.

I can get this to work on a specific single link by using a ref, like so:

<a href="#" ref={myLink}>My link</a>

And then using this to attach event handlers:

myLink.addEventListener('pointerenter', handlePointerEnter); myLink.addEventListener('pointerleave', handlePointerLeave);

But what I want is for the cursor to change when hovering over every possible link on the page, and I won't always have control over the content of this website so adding refs to every link in this way is obviously unrealistic. Any help finding a better solution would be hugely appreciated!

Edit: This is the markup for my custom cursor:

<div ref={cursorSm} className="cursor--sm"></div>
<div ref={cursorLg} className="cursor--lg"></div>

I'm using refs there because I'm using gsap to animate it.

3
  • Use CSS to change all cursors over a tags. Commented Jan 11, 2023 at 16:42
  • Thanks @OriDrori, how would this work though if the custom cursor is a div and not an image though? Commented Jan 11, 2023 at 16:52
  • Please add more code - your cursor, the event handlers that add the custom cursor, and a sample component with a link. Commented Jan 11, 2023 at 16:55

1 Answer 1

1

You can use the CSS cursor property.

a {
    cursor: some-value
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't this only work if the cursor was an image? In my case, the custom cursor consists of a div inside a div.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.