{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"motion-avatar","type":"registry:component","title":"Avatar (Motion)","description":"Avatar with motion: a new picture crosses over the old once it has loaded, a change of status pops the dot with one ripple, and AvatarGroup spreads apart on hover and makes room as people join or leave.","dependencies":["motion"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/motion-reduced.json","https://dashral.com/r/avatar.json","https://dashral.com/r/motion-tooltip.json"],"files":[{"path":"registry/dashral/motion/avatar.tsx","type":"registry:file","target":"components/dashral/motion/avatar.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion } from \"motion/react\"\nimport type { AvatarGroupProps as KitAvatarGroupProps, AvatarProps as KitAvatarProps, AvatarSize, AvatarStatus } from \"../avatar\"\nimport { cn } from \"../lib/utils\"\n\nimport { useReducedMotion } from \"../lib/motion-reduced\"\nimport { Tooltip } from \"./tooltip\"\n\nexport type { AvatarSize, AvatarStatus } from \"../avatar\"\n\n/** The smooth curve every move uses: no spring, no overshoot. */\nconst smooth = [0.4, 0, 0.2, 1] as const\n\n// The kit's sizes and status colors: the same look.\nconst sizes: Record<AvatarSize, string> = {\n  xs: \"size-6 text-[10px]\",\n  sm: \"size-7 text-xs\",\n  md: \"size-8 text-xs\",\n  lg: \"size-10 text-sm\",\n  xl: \"size-16 text-lg\",\n}\n\nconst statusColors: Record<AvatarStatus, string> = {\n  online: \"bg-success\",\n  away: \"bg-warning\",\n  busy: \"bg-destructive\",\n  offline: \"bg-muted-foreground\",\n}\n\n/** \"Olivia Martin\" → \"OM\"; one name gives one letter. */\nconst initials = (name: string) =>\n  name\n    .trim()\n    .split(/\\s+/)\n    .slice(0, 2)\n    .map((part) => part[0]?.toUpperCase() ?? \"\")\n    .join(\"\")\n\nexport type AvatarProps = Omit<KitAvatarProps, \"asChild\"> & {\n  /** The status dot ripples gently all the time, e.g. someone live on a call. A change of status always ripples once. */\n  pulse?: boolean\n}\n\n/** The picture whose loading has finished, so a new one can cross over the old instead of the old vanishing first. */\nfunction useLoaded(src: string | undefined) {\n  const [loaded, setLoaded] = React.useState<string | undefined>(undefined)\n  React.useEffect(() => {\n    if (!src) {\n      setLoaded(undefined)\n      return\n    }\n    let live = true\n    const image = new Image()\n    image.onload = () => live && setLoaded(src)\n    // A picture that fails leaves the last one (or the initials) in place.\n    image.onerror = () => live && setLoaded((current) => (current === src ? undefined : current))\n    image.src = src\n    return () => {\n      live = false\n    }\n  }, [src])\n  return loaded\n}\n\n/**\n * The kit's Avatar with motion: the same picture, initials, sizes, shapes and status. A picture fades in once it has\n * loaded, and a new one crosses over the old (it loads first, so nothing goes blank in between). A change of status\n * pops the dot in its new color and sends one ripple out from it; pulse keeps a gentle ripple going. With reduced\n * motion pictures only fade and nothing ripples.\n */\nexport function Avatar({ src, name, alt = name, fallback, size = \"md\", shape = \"circle\", status, pulse = false, className, classNames, ...props }: AvatarProps) {\n  const reduced = useReducedMotion()\n  const loaded = useLoaded(src)\n  const radius = shape === \"circle\" ? \"rounded-full\" : \"rounded-md\"\n  // A change of status after the first draw ripples once; the first status just shows. Counted while rendering (not in\n  // an effect), so the new dot already knows it's a change and pops in.\n  const [lastStatus, setLastStatus] = React.useState(status)\n  const [ripples, setRipples] = React.useState(0)\n  if (lastStatus !== status) {\n    setLastStatus(status)\n    if (status && !reduced) setRipples(ripples + 1)\n  }\n\n  return (\n    <span data-slot=\"avatar\" aria-hidden={alt === \"\" || undefined} className={cn(\"relative inline-flex shrink-0\", radius, sizes[size], className)} {...props}>\n      <span className={cn(\"relative flex size-full overflow-hidden\", radius)}>\n        {/* The initials sit under the picture: shown until it loads, and when there's none. */}\n        <span className={cn(\"flex size-full items-center justify-center bg-muted font-medium text-muted-foreground\", radius, classNames?.fallback)}>\n          {fallback ?? (name ? initials(name) : null)}\n          {!loaded && alt && <span className=\"sr-only\">{alt}</span>}\n        </span>\n        <AnimatePresence initial={false}>\n          {loaded && (\n            <motion.img\n              key={loaded}\n              src={loaded}\n              alt={alt ?? \"\"}\n              className={cn(\"absolute inset-0 aspect-square size-full object-cover\", classNames?.image)}\n              initial={{ opacity: 0 }}\n              animate={{ opacity: 1 }}\n              // The old picture stays under the new one until it has come in.\n              exit={{ opacity: 0, transition: { duration: 0.2, delay: reduced ? 0 : 0.24 } }}\n              transition={{ duration: reduced ? 0.15 : 0.32, ease: smooth }}\n            />\n          )}\n        </AnimatePresence>\n      </span>\n      {status && (\n        <span className={cn(\"absolute end-0 bottom-0 size-[28%] min-w-2\", classNames?.status)}>\n          {/* One ripple per change of status, and a slow, endless one with pulse. */}\n          {!reduced &&\n            (pulse ? (\n              <motion.span\n                aria-hidden\n                className={cn(\"absolute inset-0 rounded-full\", statusColors[status])}\n                animate={{ scale: [1, 2.4], opacity: [0.45, 0] }}\n                transition={{ duration: 1.8, ease: \"easeOut\", repeat: Infinity, repeatDelay: 0.6 }}\n              />\n            ) : (\n              ripples > 0 && (\n                <motion.span\n                  key={ripples}\n                  aria-hidden\n                  className={cn(\"absolute inset-0 rounded-full\", statusColors[status])}\n                  initial={{ scale: 1, opacity: 0.5 }}\n                  animate={{ scale: 2.6, opacity: 0 }}\n                  transition={{ duration: 0.9, ease: smooth }}\n                />\n              )\n            ))}\n          <motion.span\n            key={status}\n            className={cn(\"absolute inset-0 rounded-full ring-2 ring-card\", statusColors[status])}\n            initial={reduced || ripples === 0 ? false : { scale: 0.5 }}\n            animate={{ scale: 1 }}\n            transition={{ duration: 0.3, ease: smooth }}\n          >\n            <span className=\"sr-only\">{status}</span>\n          </motion.span>\n        </span>\n      )}\n    </span>\n  )\n}\n\nexport type AvatarGroupProps = KitAvatarGroupProps\n\n// How far each avatar overlaps the one before: the kit's -space-x-2, and spread apart a little on hover or focus.\nconst overlap = { rest: -8, spread: 2 }\n\n/**\n * The kit's AvatarGroup with motion: the same items, max, size and names. Point at it (or tab into it) and the stack\n * spreads apart so every face shows, the one under the pointer rising a little; avatars that join fade in and the\n * rest make room, ones that leave fade out and the rest close up, and \"+N\" changes in place. With reduced motion it\n * doesn't spread, and avatars only fade.\n */\nexport function AvatarGroup({ items, max, size = \"md\", showNames = false, className, ...props }: AvatarGroupProps) {\n  const reduced = useReducedMotion()\n  const shown = max === undefined ? items : items.slice(0, max)\n  const rest = items.slice(shown.length)\n  const restNames = rest.map((item) => item.name).filter(Boolean)\n  const [spread, setSpread] = React.useState(false)\n  const lift = \"relative rounded-full outline-none hover:z-10 focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-ring\"\n  const move = { duration: 0.3, ease: smooth }\n\n  const slot = (key: React.Key, index: number, child: React.ReactNode) => (\n    <motion.span\n      key={key}\n      layout={reduced ? false : \"position\"}\n      initial={reduced ? { opacity: 0 } : { opacity: 0, scale: 0.6 }}\n      animate={{ opacity: 1, scale: 1, marginInlineStart: index === 0 ? 0 : overlap[spread && !reduced ? \"spread\" : \"rest\"] }}\n      exit={{ opacity: 0, scale: reduced ? 1 : 0.6, transition: { duration: 0.2, ease: smooth } }}\n      transition={{ ...move, layout: move }}\n      whileHover={reduced ? undefined : { y: -2, transition: { duration: 0.16, ease: smooth } }}\n      className=\"flex\"\n    >\n      {child}\n    </motion.span>\n  )\n\n  return (\n    <div\n      data-slot=\"avatar-group\"\n      role=\"group\"\n      onPointerEnter={() => setSpread(true)}\n      onPointerLeave={() => setSpread(false)}\n      onFocus={() => setSpread(true)}\n      onBlur={(event) => {\n        if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setSpread(false)\n      }}\n      className={cn(\"flex items-center\", className)}\n      {...props}\n    >\n      <AnimatePresence initial={false} mode=\"popLayout\">\n        {shown.map(({ id, ...item }, i) =>\n          slot(\n            id ?? item.name ?? i,\n            i,\n            showNames && item.name ? (\n              <Tooltip content={item.name}>\n                <span tabIndex={0} className={cn(\"flex\", lift)}>\n                  <Avatar {...item} size={size} className=\"ring-2 ring-card\" />\n                </span>\n              </Tooltip>\n            ) : (\n              <Avatar {...item} size={size} className={cn(\"ring-2 ring-card\", lift)} />\n            )\n          )\n        )}\n        {rest.length > 0 &&\n          slot(\n            \"more\",\n            shown.length,\n            (() => {\n              const more = (\n                <span\n                  tabIndex={showNames ? 0 : undefined}\n                  className={cn(\"relative grid shrink-0 place-items-center overflow-hidden bg-muted font-medium text-muted-foreground tabular-nums ring-2 ring-card\", lift, sizes[size])}\n                >\n                  {/* A new count rises in over the old. */}\n                  <AnimatePresence initial={false} mode=\"popLayout\">\n                    <motion.span\n                      key={rest.length}\n                      className=\"col-start-1 row-start-1\"\n                      initial={reduced ? { opacity: 0 } : { opacity: 0, y: 6 }}\n                      animate={{ opacity: 1, y: 0 }}\n                      exit={reduced ? { opacity: 0 } : { opacity: 0, y: -6 }}\n                      transition={{ duration: 0.24, ease: smooth }}\n                    >\n                      +{rest.length}\n                    </motion.span>\n                  </AnimatePresence>\n                </span>\n              )\n              return showNames && restNames.length > 0 ? <Tooltip content={<span className=\"block max-w-56\">{restNames.join(\", \")}</span>}>{more}</Tooltip> : more\n            })()\n          )}\n      </AnimatePresence>\n    </div>\n  )\n}\n"}],"cssVars":{"theme":{"color-success":"var(--success)","color-success-foreground":"var(--success-foreground)","color-success-text":"var(--success-text)","color-warning":"var(--warning)","color-warning-foreground":"var(--warning-foreground)","color-warning-text":"var(--warning-text)"},"light":{"success":"oklch(0.508 0.118 165.612)","success-foreground":"oklch(0.985 0 0)","success-text":"oklch(0.432 0.095 166.913)","warning":"#f59200","warning-foreground":"oklch(0.3 0.07 60)","warning-text":"#964e00"},"dark":{"success":"oklch(0.696 0.17 162.48)","success-foreground":"oklch(0.2 0.04 160)","success-text":"oklch(0.765 0.177 163.223)","warning":"#f59200","warning-foreground":"oklch(0.25 0.06 60)","warning-text":"#ffba42"}}}