@michal.cupak/react-idle-cursor-hide
v1.0.4
Published
Hide the mouse cursor in React after a period of inactivity.
Maintainers
Readme
react-idle-cursor-hide
Small React utility package that hides the mouse cursor after a period of inactivity.
It provides:
useIdleCursorHide()hookIdleCursorBoundarywrapper component
Useful for kiosk apps, signage players, fullscreen dashboards, media viewers, and touch-first interfaces.
Installation
npm i @michal.cupak/react-idle-cursor-hideRequired CSS
Import the packaged CSS once in your app:
import "@michal.cupak/react-idle-cursor-hide/styles.css";Quick start
Apply to the entire page
import { useIdleCursorHide } from "@michal.cupak/react-idle-cursor-hide";
import "@michal.cupak/react-idle-cursor-hide/styles.css";
export default function App() {
useIdleCursorHide({
idleMs: 2000,
target: "body",
});
return <main>My app</main>;
}Apply only inside a container
import { IdleCursorBoundary } from "@michal.cupak/react-idle-cursor-hide";
import "@michal.cupak/react-idle-cursor-hide/styles.css";
export default function App() {
return (
<IdleCursorBoundary idleMs={1500} wrapperStyle={{ minHeight: "100vh" }}>
<main>My app</main>
</IdleCursorBoundary>
);
}API
useIdleCursorHide(options)
Returns a boolean hidden state.
Options
idleMs?: number— inactivity timeout in milliseconds, default2000disabled?: boolean— disable behavior, defaultfalseinitiallyHidden?: boolean— start hidden, defaultfalseclassName?: string— class applied when hidden, defaultidle-cursor-hiddentarget?: "body" | "element"— where to apply the class, defaultbodyelement?: HTMLElement | null— target element whentarget: "element"events?: Array<keyof WindowEventMap>— events that reset inactivity timer
IdleCursorBoundary
Props are the same as the hook options except element, plus:
wrapperClassName?: stringwrapperStyle?: React.CSSProperties
By default, the component wraps children in a div and hides the cursor on that container.
Advanced example
import { useRef } from "react";
import { useIdleCursorHide } from "@michal.cupak/react-idle-cursor-hide";
import "@michal.cupak/react-idle-cursor-hide/styles.css";
export default function Player() {
const ref = useRef<HTMLDivElement>(null);
const hidden = useIdleCursorHide({
idleMs: 3000,
target: "element",
element: ref.current,
events: ["mousemove", "mousedown", "touchstart", "keydown"],
});
return (
<div ref={ref} style={{ minHeight: "100vh" }}>
Cursor hidden: {String(hidden)}
</div>
);
}Build
npm install
npm run buildOutput goes to dist/.
Notes
- The package is SSR-safe because DOM access happens inside
useEffect. - This hides the cursor via CSS inside the browser window. It does not control the operating system cursor outside the browser.
- For Raspberry Pi kiosk deployments, browser-level hiding is often enough, but compositor-level cursor suppression can still be more robust.
License
MIT
