Presence Cursor
Another person's pointer on a shared surface, like a canvas or a document. It shows a colored arrow with a name tag, or with an avatar.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/presence-cursor.jsonCopies Presence Cursor into your app, installs the packages, and adds the styles. Then import it from @/components/ui/presence-cursor. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-mergeAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Copy and paste the following code into your project.
import type { ComponentProps, CSSProperties, ReactNode } from 'react'; import { cn } from '@/lib/sprout/classes'; export interface PresenceCursorProps extends Omit<ComponentProps<'div'>, 'children' | 'color'> { name: string; x: number; y: number; color: string; avatar?: ReactNode; } export function PresenceCursor({ name, x, y, color, avatar, className, style, ...props }: PresenceCursorProps) { return ( <div data-slot="presence-cursor" role="img" aria-label={name} className={cn('sprout-presence-cursor', className)} style={ { '--sprout-presence-color': color, transform: `translate(${x}px, ${y}px)`, ...style, } as CSSProperties } {...props} > <svg className="sprout-presence-cursor-arrow" viewBox="0 0 16 16" aria-hidden="true"> <path d="M1.5 1.5 14.5 6.6 8.4 8.4 6.6 14.5Z" /> </svg> {avatar != null ? ( <span data-slot="presence-cursor-avatar" className="sprout-presence-cursor-avatar" aria-hidden="true"> {avatar} </span> ) : ( <span data-slot="presence-cursor-label" className="sprout-presence-cursor-label" aria-hidden="true"> {name} </span> )} </div> ); }Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-presence-cursor { z-index: 1; pointer-events: none; user-select: none; font-family: var(--sprout-font); position: absolute; top: 0; left: 0; } .sprout-presence-cursor-arrow { width: 18px; height: 18px; fill: var(--sprout-presence-color); stroke: oklch(100% 0 0); stroke-width: 1.5px; stroke-linejoin: round; filter: drop-shadow(0 1px 1px oklch(0% 0 0 / .2)); display: block; } .sprout-presence-cursor-label, .sprout-presence-cursor-avatar { box-shadow: var(--sprout-shadow-control); position: absolute; top: 16px; left: 14px; } .sprout-presence-cursor-label { box-sizing: border-box; border-radius: var(--sprout-radius-control); background: var(--sprout-presence-color); color: oklch(100% 0 0); max-width: 16rem; font-size: var(--sprout-text-sm); white-space: nowrap; text-overflow: ellipsis; padding: 4px 8px; font-weight: 500; line-height: 1.25rem; overflow: hidden; } .sprout-presence-cursor-avatar { border: 2px solid var(--sprout-presence-color); border-radius: 9999px; display: flex; } @media (prefers-reduced-motion: no-preference) { .sprout-presence-cursor { transition: transform var(--sprout-motion-normal) var(--sprout-motion-ease); } } @media (forced-colors: active) { .sprout-presence-cursor-arrow { fill: canvastext; stroke: canvas; } .sprout-presence-cursor-label { border: 1px solid canvastext; } } }Import it from
@/components/ui/presence-cursor. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/presence-cursor.
Name tags
Each cursor shows the person's email in their color.
Initials
Pass an Avatar with initials to show it next to the arrow instead of the tag.
Face photos
An Avatar with a photo. The initials show until the photo loads.
Usage
import { PresenceCursor } from "@tulipfarm/sprout/presence-cursor";
import { Avatar, AvatarFallback, AvatarImage } from "@tulipfarm/sprout/avatar";
<div className="relative">
{peers.map(peer => (
<PresenceCursor
key={peer.id}
name={peer.email}
color={peer.color}
x={peer.x}
y={peer.y}
/>
))}
</div>
<PresenceCursor
name="[email protected]"
color="#b55309"
x={120}
y={80}
avatar={
<Avatar size="sm">
<AvatarImage src={jenny.image} alt="" />
<AvatarFallback>JE</AvatarFallback>
</Avatar>
}
/>x and y place the arrow tip, in px, from the top left of the nearest positioned parent. Give that parent position: relative. Your app sends the positions, for example from a realtime channel. The cursor does not track anything by itself.
color fills the arrow, the tag, and the avatar ring. The tag text is white, so pick colors with at least 4.5:1 contrast against white. Passavatar to show it in place of the tag. A small Avatar fits best. Long names cut off with an ellipsis at 16rem.
Accessibility
Each cursor is an image named after the person, so screen readers read the name in both forms. It ignores pointer events, so clicks go through it. It glides to a new spot over 180ms, and jumps when reduced motion is requested. In forced colors mode the arrow uses the system text color.
Theme tokens
The tag uses --sprout-radius-control and --sprout-shadow-control. Motion uses --sprout-motion-normal and --sprout-motion-ease. The color comes from the color prop, stored in --sprout-presence-color.