React
The React adapter provides a SupermouseProvider context and a useSupermouse hook. It wraps @supermousejs/core in React's context API and handles cleanup on unmount — including React 18 Strict Mode's double-mount behavior.
01 Installation
pnpm add @supermousejs/react @supermousejs/core @supermousejs/dot02 Root Provider
Wrap your application in SupermouseProvider. This creates the engine instance and makes it available via context. options accepts the same fields as the Supermouse constructor.
import { SupermouseProvider } from '@supermousejs/react';
import { Dot } from '@supermousejs/dot';
export default function App() {
return (
<SupermouseProvider
options={{
smoothness: 0.15,
hideCursor: true
}}
plugins={[
Dot({ size: 8, color: 'black' })
]}
>
<YourAppContent />
</SupermouseProvider>
);
}03 Usage in Components
Call useSupermouse() in any descendant component to get the live instance. It returns Supermouse | null — guard before accessing state or calling methods like destroy.
import { useSupermouse } from '@supermousejs/react';
export const CustomButton = () => {
// Returns the Supermouse instance or null before mount
const mouse = useSupermouse();
const handleClick = () => {
if (mouse) {
console.log('Cursor at:', mouse.state.pointer);
}
};
return <button onClick={handleClick}>Log Position</button>;
};04 Next.js (App Router)
Because Supermouse accesses window on initialization, it must run on the client. SupermouseProvider is already compiled with "use client" directives, so you can import it directly in a server-rendered layout. For more complex setups, extract it into a dedicated client component.
import { SupermouseProvider } from '@supermousejs/react';
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{/* SupermouseProvider is already marked 'use client' internally */}
<SupermouseProvider options={{ smoothness: 0.15, hideCursor: true }}>
{children}
</SupermouseProvider>
</body>
</html>
);
}SupermouseProvider tracks mount count internally, ensuring only one engine instance is active at a time.