{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "avatar",
  "title": "Avatar",
  "description": "A person's photo in a circle over their initials, and a stack of them with a count for the rest. The photo fades in once it loads, the picture of a face pointed at in a stack lifts so the whole of it shows, and faces glide aside when someone joins or leaves.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core",
    "class-variance-authority"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/flow.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/roll.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/avatar.tsx",
      "content": "'use client'\n\nimport { followFlow } from '@/registry/lib/flow'\nimport { markMounted, mergeRefs } from '@/registry/lib/refs'\nimport { Roll } from '@/registry/lib/roll'\nimport {\n  AvatarFallback as AvatarFallbackPrimitive,\n  AvatarImage as AvatarImagePrimitive,\n  Avatar as AvatarPrimitive,\n} from '@ui-sdk/react/avatar'\nimport { mergeProps, type PartProps, renderElement } from '@ui-sdk/react/primitive'\nimport { cva, type VariantProps } from 'class-variance-authority'\nimport {\n  type ComponentProps,\n  createContext,\n  use,\n  useEffect,\n  useRef,\n  useState,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A person's photo in a circle, over their initials, and a stack of them.\n *\n * The photo sits over the initials. One that is there when the avatar\n * appears (from the page's HTML, or from the cache) is simply there; one\n * that has to load fades in on `enter` once it arrives, and one that fails\n * leaves the initials. Sizes are the controls' heights, so an avatar lines\n * up with a button of the same size, at every density.\n *\n * In a stack, each face sits over the one before, cut out of it by a ring\n * in the colour behind the stack (`--avatar-stack-ring`), with a count for\n * the rest. The picture of a face you can act on (a link or a button) lifts\n * `--avatar-stack-lift` on `layout` when pointed at, and every face keeps\n * its place in the stack; the link itself stays put, so the pointer never\n * slips off it. One focused from the keyboard comes to the front, so its\n * ring shows whole. When someone joins or\n * leaves, the others glide to their new places on `layout` (`followFlow`):\n * a face that arrives scales in on `enter`, and one that goes fades and\n * shrinks where it was on `exit`.\n */\n\n/** Each size's circle, as `--avatar-size`, and the initials' text. */\nconst avatarSizes = {\n  // Two initials fit the 24px circle at 10px; Tailwind's smallest text is 12px.\n  xs: '[--avatar-size:var(--control-height-xs)] text-[0.625rem]/none',\n  sm: '[--avatar-size:var(--control-height-sm)] text-xs/none',\n  default: '[--avatar-size:var(--control-height)] text-xs/none',\n  lg: '[--avatar-size:var(--control-height-lg)] text-sm/none',\n}\n\ntype AvatarSize = keyof typeof avatarSizes\n\nconst avatarVariants = cva(\n  'bg-muted text-foreground relative inline-flex size-(--avatar-size) shrink-0 items-center justify-center overflow-hidden rounded-full align-middle font-medium select-none',\n  { variants: { size: avatarSizes }, defaultVariants: { size: 'default' } },\n)\n\n/** The size a stack gives the avatars in it. */\nconst AvatarSizeContext = createContext<AvatarSize | undefined>(undefined)\n\n/** `size` defaults to that of the stack it sits in, and to `default` outside one. */\nfunction Avatar({\n  className,\n  size,\n  ...props\n}: Omit<ComponentProps<typeof AvatarPrimitive>, 'className'> & {\n  className?: string\n} & VariantProps<typeof avatarVariants>) {\n  const inherited = use(AvatarSizeContext)\n  return (\n    <AvatarPrimitive\n      className={cn(avatarVariants({ size: size ?? inherited }), className)}\n      {...props}\n    />\n  )\n}\n\n/**\n * The photo, over the initials. `alt` is the person's name, or empty where\n * the name is given beside it.\n */\nfunction AvatarImage({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof AvatarImagePrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <AvatarImagePrimitive\n      className={cn(\n        // ui-sdk keeps an image out of sight until it has loaded, and fades it\n        // in on `enter` as it does. One in the page's HTML that it hasn't\n        // checked yet shows, so a photo already there is there from the first\n        // paint, and only one that has to load fades in.\n        'absolute inset-0 size-full object-cover transition-opacity duration-(--motion-enter-duration) ease-(--motion-enter-easing) data-[loading-status=idle]:size-full! data-[loading-status=idle]:opacity-100!',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/** The initials, under the photo: they show while it loads, and when there is none or it fails. */\nfunction AvatarFallback({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof AvatarFallbackPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <AvatarFallbackPrimitive\n      className={cn('flex size-full items-center justify-center uppercase', className)}\n      {...props}\n    />\n  )\n}\n\n/** A name's initials, for `AvatarFallback`: its first and last words' first letters, a hyphen parting words. */\nfunction initials(name: string) {\n  const words = name.split(/[\\s-]+/).filter(Boolean)\n  return `${words[0]?.[0] ?? ''}${words.length > 1 ? (words.at(-1)?.[0] ?? '') : ''}`\n}\n\n/**\n * How far each face sits over the one before: a quarter of a face, but never\n * so far that a face you can act on shows less than a 24px target.\n */\nconst stackOverlap =\n  '[--avatar-stack-overlap:calc(var(--avatar-size)/4)] has-[>:is(a,button)]:[--avatar-stack-overlap:min(calc(var(--avatar-size)/4),max(0px,calc(var(--avatar-size)-1.5rem)))]'\n\n/** The ring that cuts each face out of the one before, in the colour behind the stack. */\nconst stackRing =\n  'ring-2 ring-(--avatar-stack-ring,var(--color-background)) forced-colors:ring-0'\n\n/** The row of faces: how they overlap, and how one that comes or goes moves. */\nconst stackClassName = cn(\n  stackOverlap,\n  '[&>:not([data-leaving])~:not([data-leaving])]:-ms-(--avatar-stack-overlap) [&>*]:rounded-full [&_[data-slot=avatar]]:ring-2 [&_[data-slot=avatar]]:ring-(--avatar-stack-ring,var(--color-background)) forced-colors:[&_[data-slot=avatar]]:ring-0',\n  // A face glides to a new place on `layout`, and one that arrives after\n  // the stack has painted scales in on `enter`.\n  'data-mounted:[&>*]:starting:opacity-0 motion-safe:data-mounted:[&>*]:starting:scale-95',\n  // The copy of one that left fades and shrinks where it was.\n  '[&>[data-leaving]]:opacity-0 motion-safe:[&>[data-leaving]]:scale-95 [&>[data-leaving]]:[transition:opacity_var(--motion-exit-duration)_var(--motion-exit-easing),scale_var(--motion-exit-duration)_var(--motion-exit-easing)]',\n)\n\n/** `size` is every avatar's in it; `aria-label` says who they are, such as \"Viewers\". */\nfunction AvatarStack({\n  className,\n  size = 'default',\n  children,\n  ref,\n  ...props\n}: ComponentProps<'div'> & { size?: AvatarSize }) {\n  const row = useRef<HTMLDivElement>(null)\n  useEffect(() => {\n    if (!row.current) return\n    return followFlow(row.current, '*')\n  }, [])\n  return (\n    <AvatarSizeContext value={size}>\n      <div\n        ref={mergeRefs(row, markMounted, ref)}\n        role='group'\n        data-slot='avatar-stack'\n        className={cn(\n          'relative isolate inline-flex shrink-0 items-center align-middle',\n          avatarSizes[size],\n          stackClassName,\n          '[&>*]:[transition:translate_var(--motion-layout-duration)_var(--motion-layout-easing),opacity_var(--motion-enter-duration)_var(--motion-enter-easing),scale_var(--motion-enter-duration)_var(--motion-enter-easing),background-color_var(--motion-press-duration)_var(--motion-press-fade-easing),color_var(--motion-press-duration)_var(--motion-press-fade-easing)] [&>[data-flow=start]]:[transition:opacity_var(--motion-enter-duration)_var(--motion-enter-easing),scale_var(--motion-enter-duration)_var(--motion-enter-easing)] motion-reduce:[&>*]:[transition:opacity_var(--motion-enter-duration)_var(--motion-enter-easing)]',\n          // A face focused from the keyboard comes to the front, so its ring\n          // shows whole; one pointed at keeps its place and its picture\n          // lifts on `layout`.\n          '[&>:is(:focus-visible,:has(:focus-visible))]:z-10 [&>:is(a,button)]:focus-visible:ring-3 [&>:is(a,button)]:focus-visible:ring-ring/50 [&>:is(a,button)]:focus-visible:outline-hidden',\n          '[&>:is(a,button)>[data-slot=avatar]]:transition-[translate] [&>:is(a,button)>[data-slot=avatar]]:duration-(--motion-layout-duration) [&>:is(a,button)>[data-slot=avatar]]:ease-(--motion-layout-easing) motion-safe:[&>:is(a,button):hover>[data-slot=avatar]]:-translate-y-(--avatar-stack-lift,0.25rem) motion-reduce:[&>:is(a,button)>[data-slot=avatar]]:transition-none',\n          className,\n        )}\n        {...props}\n      >\n        {children}\n      </div>\n    </AvatarSizeContext>\n  )\n}\n\n/**\n * How many more there are, as a face of its own: +4, or 99+ past 99.\n * Renders nothing at 0. Its text reads as it shows, then the full number\n * past 99.\n */\nfunction AvatarStackCount({\n  count,\n  className,\n  render,\n  ...props\n}: Omit<PartProps<'span'>, 'children' | 'className'> & {\n  count: number\n  className?: string\n}) {\n  const size = use(AvatarSizeContext) ?? 'default'\n  const [seen, setSeen] = useState({ count, trend: 1 })\n  if (seen.count !== count) setSeen({ count, trend: count - seen.count })\n  const { shown, spoken } = stackCountLabel(count)\n  const element = renderElement(\n    'span',\n    mergeProps(\n      {\n        'data-slot': 'avatar-stack-count',\n        className: cn(\n          'bg-muted text-foreground relative inline-flex size-(--avatar-size) shrink-0 items-center justify-center rounded-full font-medium tabular-nums select-none forced-colors:border',\n          avatarSizes[size],\n          stackRing,\n          // A count that is a button, such as one that lists the rest.\n          '[button&]:hover:bg-accent [button&]:hover:text-accent-foreground',\n          className,\n        ),\n        children: (\n          <>\n            <Roll value={shown} trend={seen.trend} />\n            <span className='sr-only'>{spoken}</span>\n          </>\n        ),\n      },\n      props,\n    ),\n    render,\n  )\n  return count > 0 ? element : null\n}\n\n/**\n * What a stack's count shows, and what it says: +4 and \"+4 more\", or past\n * 99, 99+ and \"99+ more, 137 to be exact\". The spoken text leads with what shows,\n * so a person using voice control can say it.\n */\nfunction stackCountLabel(count: number) {\n  const shown = count > 99 ? '99+' : `+${count}`\n  return {\n    shown,\n    spoken: count > 99 ? `${shown} more, ${count} to be exact` : `${shown} more`,\n  }\n}\n\nexport {\n  Avatar,\n  AvatarFallback,\n  AvatarImage,\n  AvatarStack,\n  AvatarStackCount,\n  initials,\n  stackCountLabel,\n}\nexport type { AvatarSize }\n",
      "type": "registry:component",
      "target": "@components/kino/avatar.tsx"
    }
  ],
  "type": "registry:component"
}