import { createEffect, createSignal, For } from "solid-js"; import preview from "../../../.storybook/preview.js"; import { makeBodyCursor, createBodyCursor, createElementCursor, createDragCursor, cursorRef, } from "@solid-primitives/cursor"; import type { CursorProperty } from "@solid-primitives/cursor"; import readme from "../README.md?raw"; import { Container, Button, Card, StatRow, BoolRow } from "../../../.storybook/ui/index.js"; const meta = preview.meta({ title: "DOM/Cursor", tags: ["autodocs"], parameters: { layout: "centered", docs: { description: { component: readme, }, }, }, }); export default meta; export const LoadingState = meta.story({ name: "Loading state", parameters: { docs: { description: { story: "`makeBodyCursor` applies a cursor to `document.body` immediately and returns a cleanup function. No reactive owner needed — ideal for imperative async flows where you want to show `wait` while an operation runs, then restore the previous cursor.", }, }, }, render: () => { const [loading, setLoading] = createSignal(false); const simulate = () => { if (loading()) return; setLoading(true); const restore = makeBodyCursor("wait"); setTimeout(() => { restore(); setLoading(false); }, 2000); }; return (

makeBodyCursor

Move the pointer around the page — wait appears everywhere and is automatically restored when the operation finishes.

); }, }); const BODY_CURSORS: CursorProperty[] = [ "pointer", "crosshair", "help", "wait", "not-allowed", "zoom-in", "grab", "progress", ]; export const BodyCursorReactive = meta.story({ name: "Reactive body cursor", parameters: { docs: { description: { story: "`createBodyCursor` binds a signal to the body cursor reactively. Returning a falsy value unsets it. The cursor applies page-wide — move the pointer anywhere to see it.", }, }, }, render: () => { const [cursor, setCursor] = createSignal(false); createBodyCursor(cursor); return (

createBodyCursor

{c => ( )}

Click a cursor to activate it page-wide. Click the active button again to deactivate.

); }, }); const ELEMENT_CURSORS: CursorProperty[] = [ "pointer", "crosshair", "move", "help", "not-allowed", "zoom-in", "text", "copy", ]; export const ElementCursorToggle = meta.story({ name: "Enable/disable on an element", parameters: { docs: { description: { story: "`createElementCursor` applies a cursor to a specific element reactively. Passing a falsy value for the target is all you need to disable it — no teardown code required.", }, }, }, render: () => { const [el, setEl] = createSignal(); const [enabled, setEnabled] = createSignal(true); const [cursor, setCursor] = createSignal("pointer"); createElementCursor(() => (enabled() ? el() : false), cursor); return (

createElementCursor

{enabled() ? `cursor: ${cursor()}` : "cursor: (default)"}
{c => ( )}
); }, }); export const DragHandle = meta.story({ name: "Grab/grabbing on drag", parameters: { docs: { description: { story: "`createDragCursor` shows `grab` on the target element and switches to `grabbing` on the body when a pointer is held down. Setting `grabbing` on the body ensures the cursor stays consistent even when the pointer moves off the element mid-drag.", }, }, }, render: () => { const [el, setEl] = createSignal(); const [dragging, setDragging] = createSignal(false); createDragCursor(el); // Mirror createDragCursor's internals: pointerdown on element, pointerup/cancel on // document so releasing outside the element boundary still resets the display. createEffect( () => el(), target => { if (!target) return; const down = () => setDragging(true); const up = () => setDragging(false); target.addEventListener("pointerdown", down); document.addEventListener("pointerup", up); document.addEventListener("pointercancel", up); return () => { target.removeEventListener("pointerdown", down); document.removeEventListener("pointerup", up); document.removeEventListener("pointercancel", up); }; }, ); return (

createDragCursor

⠿
Drag handle
{dragging() ? "holding — grabbing cursor on body" : "hover to see grab"}

While holding, move the pointer off the element — the grabbing cursor follows everywhere.

); }, }); const SHOWCASE_CURSORS: CursorProperty[] = [ "pointer", "crosshair", "grab", "move", "not-allowed", "help", "zoom-in", "text", "copy", "wait", "progress", "cell", ]; export const CursorShowcase = meta.story({ name: "Cursor sampler via ref", parameters: { docs: { description: { story: "`cursorRef` is a ref factory for applying a cursor inline in JSX. It accepts a static value or a reactive signal — the cursor is removed when the component unmounts. Hover each tile to preview that cursor type.", }, }, }, render: () => { return (

cursorRef

{c => (
{c}
)}

Each tile uses {'ref={cursorRef("…")}'}. Hover to see the cursor.

); }, });