# Live cursors

> Other people’s cursors gliding over the same page: names that turn into speech bubbles, click ripples, and selections that follow them around.

Multiplayer is the feature people screenshot, and most cursor overlays step from one network update to the next. Here every cursor eases toward its latest position on a spring, so ten updates a second look like a hand moving. Each one carries a pill with the person’s name and colour; near the right or bottom edge the pill swings to the other side so it stays in view. When someone says something the pill grows into a bubble around their words and folds back when they’re done. Clicks send a hairline ring out from the tip, the thing someone is working on gets a 1px outline in their colour that glides to whatever they pick next, and a cursor that hasn’t moved in a while folds its name into the dot and dims until it moves again. Your own side is included: press / and a bubble opens at your pointer.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/live-cursors.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/live-cursors
- Registry item (source): https://hairlineui.com/r/live-cursors.json

## Usage

```tsx
import { LiveCursors } from "@/components/live-cursors";

<LiveCursors
  cursors={[
    { id: "ana", name: "Ana", x: 0.42, y: 0.3, message: "Love this headline" },
    { id: "kofi", name: "Kofi", x: 0.7, y: 0.8, selection: { x: 0.36, y: 0.72, width: 0.28, height: 0.21 } },
  ]}
>
  <Canvas />
</LiveCursors>
```

## With a realtime channel

Send your pointer (already in 0–1 units) on every move, and pass everyone else’s straight back in; the springs smooth whatever rate the channel manages.

```tsx
"use client";
import { useEffect, useState } from "react";
import { LiveCursors, type LiveCursor } from "@/components/live-cursors";
import { channel, me } from "@/lib/realtime"; // Liveblocks, PartyKit, Supabase Realtime…

export function Room({ children }: { children: React.ReactNode }) {
  const [others, setOthers] = useState<Record<string, LiveCursor>>({});

  useEffect(
    () =>
      channel.subscribe((event) => {
        setOthers((all) => {
          if (event.type === "leave") {
            const { [event.id]: _, ...rest } = all;
            return rest;
          }
          return { ...all, [event.id]: { ...all[event.id], ...event.cursor } };
        });
      }),
    [],
  );

  return (
    <LiveCursors
      cursors={Object.values(others)}
      onCursorMove={(point) => channel.send({ type: point ? "move" : "leave", id: me.id, cursor: { name: me.name, ...point } })}
      onMessage={(message) => channel.send({ type: "move", id: me.id, cursor: { message } })}
    >
      {children}
    </LiveCursors>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `cursors` | `{ id, name, x, y, color?, message?, clicks?, selection? }[]` | – | Everyone else. x and y run from 0 to 1 across the area; raise clicks by one per click; selection is a box in the same units. |
| `children` | `ReactNode` | – | What they’re all looking at; the cursors float over it. |
| `idleAfter` | `number` | `8000` | Milliseconds without moving before a name folds into its dot. |
| `onCursorMove` | `(point: { x, y } \| null) => void` | – | Your own mouse over the area in 0–1 units, or null when it leaves. |
| `onMessage` | `(text: string) => void` | – | Turns on "/" to say something at your pointer (mouse and trackpad only). |

## States

- **move**: A spring (stiffness 150, damping 24) per cursor, run in one requestAnimationFrame loop that writes transforms directly and stops once everyone has settled. Someone new appears where they are.
- **say**: The name pill eases its width and height (360ms) to hold the message, which blurs in (300ms); clearing it folds the bubble back to the pill.
- **click**: A 36px hairline ring in their colour scales from 0.3 to 1 and fades (620ms).
- **select**: A 1px outline with a name tag; its box eases to the next selection (420ms) and fades when cleared.
- **idle / leave**: After idleAfter without moving, the name folds into its dot and the cursor dims to 55%. Someone who leaves shrinks and fades (260ms).
- **you**: With onMessage, "/" opens a bubble at your pointer that follows it; Enter sends, Escape or clicking away closes. What you sent stays for 4 seconds.

## What it handles

Multiplayer is the feature people screenshot, and most cursor overlays step from one network update to the next. This eases every cursor so that ten updates a second look like a hand moving.

- **Smooth movement**: Each cursor eases toward its latest position on a spring, in one frame loop that sleeps when nobody is moving.
- **Names that stay in view**: Each cursor carries a name pill in its own colour, and the pill swings to the other side near the right or bottom edge.
- **Speech bubbles**: When someone says something, their pill grows into a bubble around the words. Press / to say something at your own pointer.
- **Click ripples**: A click sends a hairline ring out from the tip of the cursor.
- **Selections that follow**: What someone is working on gets a 1px outline in their colour that glides to their next selection.
- **Any realtime channel**: Positions are fractions from 0 to 1, so they work on a phone and a wide screen. Pass everyone else’s in whatever rate the channel manages.

## Replaces

cursor smoothing, name labels, click ripples, selection outlines, idle handling, a chat-at-cursor box

## Where it fits

- **Collaborative documents**: See where teammates are reading and typing.
- **Whiteboards and design canvases**: Show everyone’s pointer in a shared canvas.
- **Shared dashboards and forms**: Show who else is looking at the same page.
- **Landing page demos**: Show off multiplayer in a hero without a real backend.

## Notes

- People take the five chart colours in the order they first appear and keep them; pass color to choose your own.
- The cursors are decoration to assistive tech; what people say is announced through a polite status region, once per message.
- Positions are fractions, so the same numbers work on a phone and a wide screen as long as what’s underneath scales the same way.
- "/" is only taken while your pointer is over the area, so a page’s own "/" shortcut (search, usually) keeps working everywhere else.
- The loop sleeps when nobody is moving. With reduced motion cursors jump to where they are and bubbles change size in place.

License: MIT (https://hairlineui.com/license#components)
