score:0

there are already 2 good answers, but i'll add this one too, because other answers are overcomplicating the solution

  1. flickering

it doesn't matter if you use ref or state, you should just extract your cursor to separate component, that way your app component will not rerender

  1. scrollbars

as other anwers mentioned, using body { overflow: hidden; } will solve this problem, but partially. your cursor is trying to go beyond page size, hence page is showing scrollbars, adding limitation for cursor position will solve this: cursor.y = math.max(0, math.min(currentpositiony, page.width)) (pseudo-code) now cursor.y will not exceed 0 or page.width

score:1

edit - overflowing body (aka third problem)

if you'll add this to your body tag it should solve it:

  margin: 0;
  height: 100%;
  overflow: hidden

edit - regarding your second problem

to prevent scroll bars to appear, you can use overflow-y: hidden; (to disable on the x-axis just change the overflow-y to overflow-x, overflow: hidden; for both)

but if you would like to enable scrolling but just hide the scrollbar, use the following code:

/* hide scrollbar but allow scrolling */
body {
  -ms-overflow-style: none; /* for internet explorer, edge */
  scrollbar-width: none; /* for firefox */
  overflow-y: scroll; 
}

body::-webkit-scrollbar {
  display: none; /* for chrome, safari, and opera */
}

here is a gif of a working example on my browser: https://imgur.com/a/wov7car


it doesn't get cut off for me on the right side (see image below). it sounds like the second problem happens because your cursor gets re-rendered every time you move it, and that's a ton of work for your site!

you should remove the style attributes from the cursor component and adjust the code inside your event listener for a mousemove event. it will look like this:

onmousemove = {e => {
    const cursor = document.queryselector('.cursor')
    cursor.style.top = ׳${e.pagey}px׳
    cursor.style.left = ׳${e.pagex}px׳
}}

not getting cut off

score:1

flickering:

#01:

simply introduce a transition style on the cursor component, eg transition: "all 50ms". it makes the position change much more smoother and cancels the flickering.

#02:

however as guy mentioned above, handling the cursor's position in a state means a lot of re-rendering for your component which makes you app slower in the end.

i'd recommend making a ref for the cursor and update it directly from an event listener. with that change you can even remove the usemouse hook:

const app = () => {
  const containernoderef = useref(null);
  const cursorref = useref(null)

  const updatemouse = (e) => {
    // you can directly access the mouse's position in `e`
    // you don't even need the usemouse hook
    cursorref.current.style.top = `${e.y - size / 2}px`
    cursorref.current.style.left = `${e.x - size / 2}px`
  }

  useeffect(() => {
        window.addeventlistener('mousemove', updatemouse)
        return () => {
            window.removeeventlistener('mousemove', updatemouse)
        }
    })

  return (
    <div
      ref={containernoderef}
      style={{
        width: window.innerwidth,
        height: window.innerheight,
        display: "flex",
        flexdirection: "column",
        justifycontent: "center",
        alignitems: "center",
        cursor: 'none'
      }}
    >
      <cursor ref={cursorref} />
      <h1>hello codesandbox</h1>
      <h2>start editing to see some magic happen!</h2>
    </div>
  );
}

const cursor = forwardref(({ size = size }, ref) => (
    <div
      ref={ref}
      style={{
        position: "absolute",
        width: size,
        height: size,
        backgroundcolor: "black",
        color: "white",
        fontweight: "bold",
        display: "flex",
        justifycontent: "center",
        alignitems: "center"
      }}>
      hello
    </div>
  )
)

cut off issue:

since you're moving around an actual div as your cursor, when it reaches the border of your document, it stretches it to make the div fit -> this is why the document doesn't fit into your window anymore thus the scrollbars are rendered.

you can fix it via css:

body { overflow: hidden }

+1:

i'm not sure how your cursor has to look like in the end, but if it'd be an image, there is an other possible solution. add this css rule to your container, which loads an image as a cursor and then the browser takes care about the rendering automatically:

<div
      ref={containernoderef}
      style={{
        // ...
        cursor: 'url("url"), auto'
      }}
    >

if you'd use this solution then the whole cursor component and position calculation wouldn't be needed anymore. however the downside is, that it only works with images.


Related Query

More Query from same tag