{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"avatar","type":"registry:component","title":"Avatar","description":"A face, or initials until there is one: the photo rises out of a blur once it has loaded, and a stack of avatars fans open when you point at it.","author":"Hairline UI","dependencies":[],"registryDependencies":["https://hairlineui.com/r/number-roll.json?as=dependency"],"files":[{"path":"registry/avatar.tsx","type":"registry:component","content":"// Hairline UI · avatar · MIT License\n// Copyright (c) 2026 Hairline UI · https://hairlineui.com/license#components\n\n\"use client\";\n\nimport * as React from \"react\";\nimport { NumberRoll } from \"./number-roll\";\nimport { EASE_OUT, useInteractionReducedMotion } from \"./ui-interaction-motion\";\n\n/* ─────────────────────────────────────────────────────────\n * AVATAR: a face, or initials until there is one\n *\n *   load     initials show at once; the photo comes up out of a\n *            blur over them once it has decoded, never a broken\n *            image or a flash of empty circle\n *   status   a dot on the edge (online, away, busy, offline), cut\n *            out of the photo by a ring of the background\n *   group    faces overlap; pointing at the stack fans it open\n *            so each face shows whole, and the \"+N\" rolls\n * ───────────────────────────────────────────────────────── */\n\nexport type AvatarStatus = \"online\" | \"away\" | \"busy\" | \"offline\";\nexport type AvatarSize = \"xs\" | \"sm\" | \"md\" | \"lg\";\n\nexport interface AvatarProps extends Omit<React.HTMLAttributes<HTMLSpanElement>, \"children\"> {\n  name: string;\n  src?: string;\n  size?: AvatarSize;\n  status?: AvatarStatus;\n  /** Square-ish (for teams and workspaces) instead of round. */\n  shape?: \"circle\" | \"square\";\n}\n\nconst PX: Record<AvatarSize, number> = { xs: 20, sm: 28, md: 36, lg: 48 };\nconst STATUS: Record<AvatarStatus, string> = { online: \"bg-emerald-500\", away: \"bg-amber-500\", busy: \"bg-red-500\", offline: \"bg-muted-foreground/50\" };\n\nexport function initials(name: string) {\n  const parts = name.trim().split(/\\s+/).filter(Boolean);\n  return ((parts[0]?.[0] ?? \"\") + (parts.length > 1 ? parts[parts.length - 1][0] : \"\")).toUpperCase();\n}\n\nexport function Avatar({ name, src, size = \"md\", status, shape = \"circle\", className = \"\", style, ...props }: AvatarProps) {\n  const reduced = useInteractionReducedMotion();\n  const [loaded, setLoaded] = React.useState<string | null>(null);\n  const ready = !!src && loaded === src;\n  const px = PX[size];\n  const radius = shape === \"circle\" ? \"rounded-full\" : px >= 36 ? \"rounded-[10px]\" : \"rounded-[7px]\";\n\n  return (\n    <span\n      role=\"img\"\n      aria-label={status ? `${name}, ${status}` : name}\n      className={`relative inline-flex shrink-0 select-none ${className}`}\n      style={{ width: px, height: px, ...style }}\n      {...props}\n    >\n      <span className={`absolute inset-0 flex items-center justify-center overflow-hidden bg-muted font-medium text-muted-foreground shadow-[inset_0_0_0_1px_var(--border)] ${radius}`} style={{ fontSize: Math.round(px * 0.38) }}>\n        {initials(name)}\n        {src && (\n          // eslint-disable-next-line @next/next/no-img-element\n          <img\n            src={src}\n            alt=\"\"\n            decoding=\"async\"\n            onLoad={() => setLoaded(src)}\n            // A cached photo can finish before React listens; take it from the element.\n            ref={(el) => {\n              if (el?.complete && el.naturalWidth && loaded !== src) setLoaded(src);\n            }}\n            className=\"absolute inset-0 size-full object-cover\"\n            style={{\n              opacity: ready ? 1 : 0,\n              filter: ready || reduced ? \"none\" : \"blur(6px)\",\n              scale: ready || reduced ? \"1\" : \"1.08\",\n              transition: reduced ? \"opacity 150ms\" : `opacity 360ms ${EASE_OUT}, filter 420ms ${EASE_OUT}, scale 520ms ${EASE_OUT}`,\n            }}\n          />\n        )}\n      </span>\n      {status && (\n        <span\n          aria-hidden=\"true\"\n          className={`absolute bottom-0 right-0 rounded-full shadow-[0_0_0_2px_var(--background)] transition-colors duration-300 ${STATUS[status]}`}\n          style={{ width: Math.max(6, Math.round(px * 0.26)), height: Math.max(6, Math.round(px * 0.26)) }}\n        />\n      )}\n    </span>\n  );\n}\n\nexport interface AvatarGroupProps {\n  people: { name: string; src?: string }[];\n  /** How many faces before \"+N\". */\n  max?: number;\n  size?: AvatarSize;\n  className?: string;\n}\n\nexport function AvatarGroup({ people, max = 4, size = \"sm\", className = \"\" }: AvatarGroupProps) {\n  const reduced = useInteractionReducedMotion();\n  const [open, setOpen] = React.useState(false);\n  const shown = people.slice(0, max);\n  const rest = people.length - shown.length;\n  const px = PX[size];\n  const overlap = Math.round(px * 0.22);\n  const gap = open && !reduced ? 4 : -overlap;\n\n  return (\n    <div\n      role=\"group\"\n      aria-label={`${people.length} people`}\n      className={`flex items-center ${className}`}\n      onPointerEnter={(e) => e.pointerType === \"mouse\" && setOpen(true)}\n      onPointerLeave={() => setOpen(false)}\n    >\n      {shown.map((p, i) => (\n        <span\n          key={p.name + i}\n          // A block-level wrapper the size of the face, so the cut-out ring is a true circle\n          // and every face (and the +N) sits on the same line.\n          className=\"relative flex shrink-0 rounded-full shadow-[0_0_0_2px_var(--background)]\"\n          style={{ marginLeft: i ? gap : 0, zIndex: shown.length - i, transition: reduced ? \"none\" : `margin-left 380ms ${EASE_OUT} ${i * 20}ms` }}\n        >\n          <Avatar name={p.name} src={p.src} size={size} />\n        </span>\n      ))}\n      {rest > 0 && (\n        <span\n          className=\"relative flex shrink-0 items-center justify-center rounded-full bg-muted font-medium tabular-nums text-muted-foreground shadow-[inset_0_0_0_1px_var(--border),0_0_0_2px_var(--background)]\"\n          style={{ width: px, height: px, fontSize: Math.round(px * 0.34), marginLeft: gap, transition: reduced ? \"none\" : `margin-left 380ms ${EASE_OUT} ${shown.length * 20}ms` }}\n          aria-label={`and ${rest} more`}\n        >\n          +<NumberRoll value={rest} />\n        </span>\n      )}\n    </div>\n  );\n}\n\nexport default Avatar;\n"},{"path":"registry/ui-interaction-motion.ts","type":"registry:component","content":"// Hairline UI · avatar · MIT License\n// Copyright (c) 2026 Hairline UI · https://hairlineui.com/license#components\n\n'use client';\n\nimport * as React from 'react';\n\nexport const EASE_OUT = 'cubic-bezier(0.16, 1, 0.3, 1)';\nexport const EASE_SETTLE = 'cubic-bezier(0.22, 1.18, 0.36, 1)';\n/** Thrown things (indicators, thumbs): a slight overshoot on transform only. */\nexport const EASE_THROW = 'cubic-bezier(0.34, 1.36, 0.64, 1)';\n/**\n * The press every button primitive shares: a 3% give that eases in and settles back.\n * Tailwind v4's scale-* writes the `scale` property, so `scale` has to be in the list\n * or the press snaps.\n */\nexport const INTERACTION_PRESS =\n  'transition-[background-color,color,box-shadow,opacity,scale] duration-200 ease-[cubic-bezier(0.16,1,0.3,1)] active:scale-[0.97] motion-reduce:transition-none motion-reduce:active:scale-100';\nexport const INTERACTION_FOCUS ='focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:ring-offset-2 focus-visible:ring-offset-background';\n\nconst query = '(prefers-reduced-motion: reduce)';\nconst subscribe = (notify: () => void) => {\n  const media = window.matchMedia(query);\n  media.addEventListener('change', notify);\n  return () => media.removeEventListener('change', notify);\n};\nexport const useInteractionReducedMotion = () => React.useSyncExternalStore(subscribe, () => window.matchMedia(query).matches, () => false);\n\nexport function useInteractionActivity<T extends Element>() {\n  const ref = React.useRef<T>(null);\n  const [visible, setVisible] = React.useState(false);\n  const [hidden, setHidden] = React.useState(false);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const observer = new IntersectionObserver(([entry]) => setVisible(entry.isIntersecting));\n    observer.observe(el);\n    const visibility = () => setHidden(document.hidden);\n    visibility(); document.addEventListener('visibilitychange', visibility);\n    return () => { observer.disconnect(); document.removeEventListener('visibilitychange', visibility); };\n  }, []);\n  return { ref, active: visible && !hidden };\n}\n\nexport function interactionIcon(on: boolean, reduced: boolean): React.CSSProperties {\n  return { opacity: on ? 1 : 0, transform: on ? 'none' : 'scale(0.6)', filter: on ? 'blur(0px)' : 'blur(3px)', transition: reduced ? 'none' : `opacity 220ms ${EASE_OUT}, transform 380ms ${EASE_OUT}, filter 220ms ${EASE_OUT}` };\n}\n\n/** A raised object: one tight contact shadow, one short ambient, a hairline of light on its top edge. */\nexport const MATERIAL_LIFT =\n  'shadow-[0_1px_1.5px_rgb(0_0_0/0.07),0_3px_8px_-2px_rgb(0_0_0/0.1),inset_0_0.5px_0_rgb(255_255_255/0.8)] dark:shadow-[0_1px_2px_rgb(0_0_0/0.5),0_4px_10px_-2px_rgb(0_0_0/0.35),inset_0_0.5px_0_rgb(255_255_255/0.1)]';\n/** A trough something sits in, shaded from above, lit along its bottom edge. */\nexport const MATERIAL_RECESS =\n  'shadow-[inset_0_1px_2.5px_rgb(0_0_0/0.09),inset_0_-0.5px_0_rgb(255_255_255/0.7)] dark:shadow-[inset_0_1px_3px_rgb(0_0_0/0.45),inset_0_-0.5px_0_rgb(255_255_255/0.07)]';\n\nexport type SpringConfig = { stiffness: number; damping: number; mass?: number };\nexport interface Spring {\n  get(): number;\n  getVelocity(): number;\n  getTarget(): number;\n  /** Aim somewhere new from wherever it is now, keeping its speed. */\n  set(target: number): void;\n  /** Add speed (units per second), on top of what it already has. */\n  push(velocity: number): void;\n  /** Be there now, at rest. */\n  jump(value: number): void;\n  configure(config: SpringConfig): void;\n  stop(): void;\n}\n\n// Every moving spring on the page shares one frame loop, which sleeps when the last one rests. Each spring's onFrame runs\n// once per frame after all of them have stepped, so several springs that draw one shape paint it once.\nconst SPRING_STEP = 1 / 240;\ntype Running = { step: () => void };\nconst runningSprings = new Set<Running>();\nconst springPainters = new Set<() => void>();\nlet springFrame = 0;\nlet springLast = 0;\nlet springSpare = 0;\n\nfunction springTick(now: number) {\n  // A hidden tab stops the frames, so the first one back must not count the whole gap as time passed.\n  springSpare += Math.min((now - springLast) / 1000, 0.05);\n  springLast = now;\n  while (springSpare >= SPRING_STEP) {\n    runningSprings.forEach((spring) => spring.step());\n    springSpare -= SPRING_STEP;\n  }\n  const painters = [...springPainters];\n  springPainters.clear();\n  painters.forEach((paint) => paint());\n  springFrame = runningSprings.size ? requestAnimationFrame(springTick) : 0;\n}\n\nfunction wakeSprings(spring: Running) {\n  runningSprings.add(spring);\n  if (springFrame) return;\n  springSpare = 0;\n  springFrame = requestAnimationFrame((now) => {\n    springLast = now - 1000 / 60;\n    springTick(now);\n  });\n}\n\n/**\n * A spring that carries its speed: re-aim it mid-flight and it curves on from where it is, push it while it swings and the\n * push adds to the swing. Use it for anything that can be interrupted; a curve restarts from a standstill every time.\n * `onFrame` should write straight to the DOM. `precision` is how close counts as there, in the value's own units.\n */\nexport function createSpring(initial: number, config: SpringConfig, onFrame: () => void, precision = 0.01): Spring {\n  let value = initial;\n  let velocity = 0;\n  let target = initial;\n  let { stiffness, damping, mass = 1 } = config;\n  const running: Running = {\n    step() {\n      velocity += ((-stiffness * (value - target) - damping * velocity) / mass) * SPRING_STEP;\n      value += velocity * SPRING_STEP;\n      if (Math.abs(value - target) < precision && Math.abs(velocity) < precision * 10) {\n        value = target;\n        velocity = 0;\n        runningSprings.delete(running);\n      }\n      springPainters.add(onFrame);\n    },\n  };\n  return {\n    get: () => value,\n    getVelocity: () => velocity,\n    getTarget: () => target,\n    set(next) {\n      if (next === target && !runningSprings.has(running)) return;\n      target = next;\n      wakeSprings(running);\n    },\n    push(speed) {\n      velocity += speed;\n      wakeSprings(running);\n    },\n    jump(next) {\n      value = next;\n      target = next;\n      velocity = 0;\n      runningSprings.delete(running);\n      onFrame();\n    },\n    configure(next) {\n      ({ stiffness, damping, mass = 1 } = next);\n    },\n    stop() {\n      runningSprings.delete(running);\n    },\n  };\n}\n\n/** Sample recent movement rather than averaging a long drag and losing its release velocity. */\nexport type PointerSample = { x: number; time: number };\nexport function samplePointer(samples: PointerSample[], x: number, time: number) {\n  samples.push({ x, time });\n  while (samples.length > 2 && time - samples[0].time > 100) samples.shift();\n}\nexport function releaseVelocity(samples: PointerSample[], now: number) {\n  if (samples.length < 2 || now - samples[samples.length - 1].time > 100) return 0;\n  const first = samples[0], last = samples[samples.length - 1];\n  return (last.x - first.x) / Math.max(1, last.time - first.time);\n}\n\n/** Smooth Apple/rational rubber-band curve: continuous exponential-like resistance without linear kinks. */\nexport function rubberBand(overshoot: number, dimension = 320, constant = 0.55): number {\n  const d = dimension || 320;\n  const abs = Math.abs(overshoot);\n  return Math.sign(overshoot) * (1 - 1 / ((abs * constant) / d + 1)) * d;\n}\n\n/** Projects resting travel from release velocity (velocity in px/ms; time in seconds). */\nexport function projectInertia(distance: number, velocity: number, time = 0.12): number {\n  return distance + velocity * 1000 * time;\n}\n\n/** Smooths incoming release velocity toward a boundary so springs don't slam or clip into walls. */\nexport function softenVelocity(velocity: number, distanceToTarget: number, factor = 10): number {\n  const maxAllowed = Math.abs(distanceToTarget) * factor;\n  return Math.sign(velocity) * Math.min(Math.abs(velocity), maxAllowed);\n}\n\n/** A damped spring sampled by WAAPI: tuned for organic lightness with a hint of life (3% breath). */\nexport function springFrames(\n  from: number,\n  to: number,\n  velocity = 0,\n  duration = 480,\n  config: { omega?: number; damping?: number; steps?: number } = {},\n) {\n  const { omega = 16, damping = 0.74, steps = 36 } = config;\n  const damped = omega * Math.sqrt(Math.max(0.001, 1 - damping * damping));\n  const displacement = from - to;\n  const coefficient = (velocity * 1000 + damping * omega * displacement) / damped;\n  return Array.from({ length: steps + 1 }, (_, i) => {\n    const t = (i / steps) * (duration / 1000);\n    const value = to + Math.exp(-damping * omega * t) * (displacement * Math.cos(damped * t) + coefficient * Math.sin(damped * t));\n    return { value: i === steps ? to : value, offset: i / steps };\n  });\n}\n\n"}],"css":{},"docs":"Docs and live preview: https://hairlineui.com/ui/avatar. MIT licensed: https://hairlineui.com/license#components","meta":{"license":"MIT"}}