{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "avatar",
  "title": "Avatar",
  "description": "A person or organisation as a square cell (Base UI Avatar): the photo rises in from the bottom keyline, initials hold the cell while it loads or after it fails, status as its floor keyline (a line shape per kind, never over the face); AvatarGroup joins avatars on shared seams with a +N count cell.",
  "dependencies": [
    "@base-ui/react@1.9.0",
    "class-variance-authority"
  ],
  "registryDependencies": [
    "@keyline-ui/keyline",
    "@keyline-ui/keyline-render",
    "utils"
  ],
  "files": [
    {
      "path": "src/registry/keyline/ui/avatar.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Avatar as AvatarPrimitive } from \"@base-ui/react/avatar\";\nimport { mergeProps } from \"@base-ui/react/merge-props\";\nimport { useRender } from \"@base-ui/react/use-render\";\nimport { cva } from \"class-variance-authority\";\n\nimport { cn } from \"@/lib/utils\";\nimport { dataAttrs, resolveClassName, resolveStyle, type PartProps } from \"../lib/keyline-render\";\n\ntype AvatarSize = \"sm\" | \"md\" | \"lg\" | \"xl\";\ntype AvatarStatus = \"normal\" | \"waiting\" | \"attention\" | \"done\" | \"none\";\ntype AvatarLayout = \"control\" | \"cell\";\n\n/**\n * Initials for a name: the first grapheme of the first word and of the last word (\"Nora van der Berg\" →\n * \"NB\"), one grapheme for a single word or a script without word spaces (\"李小龍\" → \"李\"). Grapheme-aware,\n * so \"Ægir Øster\" → \"ÆØ\" and combining marks stay whole. Emails and handles use the part before \"@\".\n */\nfunction avatarInitials(name: string | null | undefined): string {\n  const clean = ((name ?? \"\").split(\"@\")[0] ?? \"\")\n    .replace(/[_.\\-+]+/g, \" \")\n    .replace(/[^\\p{L}\\p{N}\\p{M}\\s]/gu, \"\")\n    .trim();\n  if (!clean) return \"\";\n  const words = clean.split(/\\s+/u);\n  const seg =\n    typeof Intl !== \"undefined\" && \"Segmenter\" in Intl\n      ? new Intl.Segmenter(undefined, { granularity: \"grapheme\" })\n      : null;\n  const first = (w: string) =>\n    seg ? (seg.segment(w)[Symbol.iterator]().next().value?.segment ?? \"\") : (Array.from(w)[0] ?? \"\");\n  // a script written without spaces (Han, Kana, Hangul, Thai…) gives one grapheme: two would be two words' worth\n  if (\n    words.length === 1 ||\n    /^[\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}\\p{Script=Hangul}\\p{Script=Thai}]/u.test(clean)\n  )\n    return first(words[0] ?? \"\").toLocaleUpperCase();\n  return (first(words[0] ?? \"\") + first(words.at(-1) ?? \"\")).toLocaleUpperCase();\n}\n\n/* the inner keyline of an attention avatar that has no frame of its own: as a cell, or as a cell of a group */\nconst cellAttention = cn(\n  \"after:pointer-events-none after:absolute after:inset-0 after:content-['']\",\n  \"after:border-(length:--kl-k1) after:border-kl-red forced-colors:after:border-[CanvasText]\",\n);\nconst groupCellAttention = cn(\n  \"*:data-[slot=avatar]:data-[status=attention]:after:pointer-events-none\",\n  \"*:data-[slot=avatar]:data-[status=attention]:after:absolute\",\n  \"*:data-[slot=avatar]:data-[status=attention]:after:inset-0\",\n  \"*:data-[slot=avatar]:data-[status=attention]:after:content-['']\",\n  \"*:data-[slot=avatar]:data-[status=attention]:after:border-(length:--kl-k1)\",\n  \"*:data-[slot=avatar]:data-[status=attention]:after:border-kl-red\",\n  \"forced-colors:*:data-[slot=avatar]:data-[status=attention]:after:border-[CanvasText]\",\n);\n\nconst STATUS_TEXT: Record<AvatarStatus, string> = {\n  normal: \"active\",\n  waiting: \"away\",\n  attention: \"needs attention\",\n  done: \"done\",\n  none: \"offline\",\n};\n\n/* The avatar's name, for the initials AvatarFallback shows when it is given none. */\nconst NameContext = React.createContext<string>(\"\");\n\n/*\n * A person as a square cell: the photo fills it, the initials hold it while the photo loads or after it fails. A 1px\n * keyline of its own (an avatar is a token-sized thing, like a badge: k1, never cut); as a cell (layout=\"cell\") it is\n * a square inside the cell it sits in. No radius anywhere: the square IS the character, and it does not change with\n * --kl-cut. Nothing is drawn over the face: status lives on the avatar's floor, as its bottom keyline.\n *\n * --kl-av (the square), --kl-av-fs (the initials) and --kl-av-st (the status floor) are set by plain classes, so a\n * container that sizes its avatars with a more specific rule wins: AvatarGroup (its own size) and Message's gutter.\n */\nconst avatarVariants = cva(\n  [\n    \"group/avatar relative box-border inline-grid flex-none overflow-hidden align-middle select-none\",\n    \"bg-kl-tint font-(family-name:--kl-font) text-kl-ink [--kl-av-st:3px] *:[grid-area:1/1]\",\n  ],\n  {\n    variants: {\n      size: {\n        sm: \"[--kl-av-fs:10px] [--kl-av:24px]\",\n        md: \"[--kl-av-fs:13px] [--kl-av:32px]\",\n        lg: \"[--kl-av-fs:15px] [--kl-av:40px]\",\n        xl: \"[--kl-av-fs:20px] [--kl-av-st:4px] [--kl-av:56px]\",\n      },\n      layout: {\n        control: \"size-(--kl-av) border-(length:--kl-k1) border-kl-line forced-colors:border-[CanvasText]\",\n        // a square that fills the cell's width, centred in it, never a slab stretched to the row; no edge of its own\n        cell: \"aspect-square h-auto w-full self-center border-0 [--kl-av-fs:18px]\",\n      },\n      /* the floor is a signal keyline, a line shape per kind as well as a colour (they survive grayscale and forced\n         colours): active solid cobalt, away dashed, needs attention solid red with the whole frame red, done a double\n         line, offline no line and the photo in grayscale */\n      status: {\n        normal: \"border-b-(length:--kl-av-st) border-b-kl-cobalt\",\n        waiting: \"border-b-(length:--kl-av-st) [border-bottom-style:dashed] border-b-kl-ink-2\",\n        attention: \"border-b-(length:--kl-av-st) border-b-kl-red\",\n        done: \"border-b-[length:max(var(--kl-av-st),3px)] [border-bottom-style:double] border-b-kl-ink\",\n        none: \"border-b-(length:--kl-k1) border-b-kl-line\",\n      },\n    },\n    compoundVariants: [\n      {\n        status: [\"normal\", \"waiting\", \"attention\", \"done\", \"none\"],\n        className: \"forced-colors:border-b-[CanvasText]\",\n      },\n      {\n        layout: \"control\",\n        status: \"attention\",\n        className: \"border-kl-red forced-colors:border-2 forced-colors:border-b-(length:--kl-av-st)\",\n      },\n      // as a cell there is no own frame to turn red: an inner keyline draws it\n      { layout: \"cell\", status: \"attention\", className: cellAttention },\n      { layout: \"cell\", status: \"none\", className: \"border-b-0\" },\n    ],\n  },\n);\n\ntype AvatarState = AvatarPrimitive.Root.State & {\n  size: AvatarSize | undefined;\n  layout: AvatarLayout;\n  status: AvatarStatus | undefined;\n};\n\ntype AvatarProps = Omit<AvatarPrimitive.Root.Props, \"className\" | \"style\"> & {\n  /** The person or organisation. Names the avatar (role=\"img\") and gives the initials. */\n  name: string;\n  /** Photo or logo. While it loads, and if it fails, the initials show: never a broken image. */\n  src?: string;\n  /** Overrides the initials, e.g. a two-letter org code or a 16px glyph. */\n  fallback?: React.ReactNode;\n  /** sm 24 · md 32 (default; a list row) · lg 40 · xl 56 (a profile header). Ignored with layout=\"cell\". */\n  size?: AvatarSize;\n  /** control: its own 1px keyline. cell: a square filling the width of a square-ish cell of an enclosing\n   *  frame (a hover card's header); it draws no edge. In a list row use a control avatar in the media cell. */\n  layout?: AvatarLayout;\n  /** Status on the avatar's floor: its bottom keyline becomes a signal line, never drawn over the face.\n   *  Each kind has its own line shape (active solid, away dashed, attention red with a red frame, done\n   *  double, offline none and a grayscale photo), so it survives grayscale and forced colours. Its words\n   *  join the accessible name. */\n  status?: AvatarStatus;\n  /** Words for the status (default by kind: active, away, needs attention, done, offline). */\n  statusLabel?: string;\n  /** The name is already printed beside the avatar (a list row, a header): hide it from assistive\n   *  technology so the name isn't read twice. */\n  decorative?: boolean;\n  /** Base UI: wait this long before showing the initials, so a fast image never flashes them. Default 0. */\n  fallbackDelay?: number;\n  imageProps?: Omit<AvatarImageProps, \"src\">;\n  className?: string | ((state: AvatarState) => string | undefined);\n  style?: React.CSSProperties | ((state: AvatarState) => React.CSSProperties | undefined);\n};\n\n/**\n * A person or organisation as a square cell (Base UI Avatar). The photo fills the cell; while it loads or\n * after it fails, the initials hold the cell in the UI voice. There is no radius in the language, so an\n * avatar is never a circle; with the cut off or on it is the same square (an avatar is a cell, never cut).\n *\n * Given `name` (and `src`), it renders its image and initials itself. Pass children (AvatarImage,\n * AvatarFallback) to compose them instead.\n */\nfunction Avatar({\n  name,\n  src,\n  fallback,\n  size = \"md\",\n  layout = \"control\",\n  status,\n  statusLabel,\n  decorative,\n  fallbackDelay,\n  imageProps,\n  className,\n  style,\n  children,\n  ...props\n}: AvatarProps): React.ReactElement {\n  const label = status ? `${name}, ${statusLabel ?? STATUS_TEXT[status]}` : name;\n  const ownSize = layout === \"cell\" ? undefined : size;\n  const extra = { size: ownSize, layout, status };\n  return (\n    <NameContext.Provider value={name}>\n      <AvatarPrimitive.Root\n        {...dataAttrs({ size: ownSize, \"kl-layout\": layout, status })}\n        role={decorative ? undefined : \"img\"}\n        aria-label={decorative ? undefined : label}\n        aria-hidden={decorative || undefined}\n        className={(state) =>\n          resolveClassName(\n            cn(avatarVariants({ size: ownSize ?? \"md\", layout, status })),\n            className,\n          )({\n            ...state,\n            ...extra,\n          })\n        }\n        style={(state) => resolveStyle(undefined, style)({ ...state, ...extra })}\n        {...props}\n        data-slot=\"avatar\"\n      >\n        {children ?? (\n          <>\n            {src ? <AvatarImage src={src} {...imageProps} /> : null}\n            <AvatarFallback delay={src ? fallbackDelay : undefined}>{fallback}</AvatarFallback>\n          </>\n        )}\n      </AvatarPrimitive.Root>\n    </NameContext.Provider>\n  );\n}\n\n/* the photo: portrait crops keep the face (upper third) in a square; it rises from the bottom keyline when it arrives\n   late (Unfold); reduced motion (--kl-t-unfold: 0) makes it a cut */\nconst image = cn(\n  \"block size-full object-cover object-[50%_30%] [clip-path:inset(0)]\",\n  \"[transition:clip-path_var(--kl-t-unfold)_var(--kl-ease-out),filter_var(--kl-t-unflood)_linear]\",\n  \"data-starting-style:[clip-path:inset(100%_0_0_0)]\",\n  \"group-data-[status=none]/avatar:[filter:grayscale(1)_contrast(0.9)]\",\n);\n\ntype AvatarImageProps = AvatarPrimitive.Image.Props;\n\n/** The photo or logo (Base UI Avatar.Image). It shows once loaded; until then, or if it fails, the fallback holds\n * the cell. Decorative: the avatar carries the name. */\nfunction AvatarImage({ className, ...props }: AvatarImageProps): React.ReactElement {\n  return (\n    <AvatarPrimitive.Image alt=\"\" className={resolveClassName(image, className)} {...props} data-slot=\"avatar-image\" />\n  );\n}\n\n/* initials: the UI voice at strong weight, centred on the cap height (not the line box), so \"MN\" and \"李\" sit in the\n   optical middle of the square */\nconst fallbackClass = cn(\n  \"grid size-full min-w-0 place-items-center\",\n  \"text-(length:--kl-av-fs)/none font-(--kl-weight-strong) tracking-[0.02em] whitespace-nowrap tabular-nums\",\n  \"[text-box:trim-both_cap_alphabetic] [&_svg]:size-[calc(var(--kl-av-fs)+4px)]\",\n  \"group-data-[status=none]/avatar:text-kl-ink-3\",\n);\n\ntype AvatarFallbackProps = AvatarPrimitive.Fallback.Props;\n\n/** What holds the cell while the image loads or after it fails (Base UI Avatar.Fallback): the avatar's initials\n * unless given children. Hidden from assistive technology: the avatar carries the name. */\nfunction AvatarFallback({ className, children, ...props }: AvatarFallbackProps): React.ReactElement {\n  const name = React.useContext(NameContext);\n  return (\n    <AvatarPrimitive.Fallback\n      aria-hidden\n      className={resolveClassName(fallbackClass, className)}\n      {...props}\n      data-slot=\"avatar-fallback\"\n    >\n      {children ?? avatarInitials(name)}\n    </AvatarPrimitive.Fallback>\n  );\n}\n\n/*\n * GROUP: cells of one keyline frame on shared seams (C1). Every face stays whole. The frame owns the outer edge; each\n * later cell owns the seam before it. The group's size governs its cells: these rules key on the avatar's slot and\n * are more specific than the avatar's own size classes. A cell keeps its status floor (no rule here sets a floor\n * when the avatar has a status), and its seam turns red with it when it needs attention.\n */\nconst groupVariants = cva(\n  [\n    \"inline-flex w-fit max-w-full items-stretch align-middle\",\n    \"border-(length:--kl-k1) border-kl-line bg-kl-cell forced-colors:border-[CanvasText]\",\n    \"*:data-[slot=avatar]:data-[size]:[--kl-av-fs:inherit] *:data-[slot=avatar]:data-[size]:[--kl-av:inherit]\",\n    \"*:data-[slot=avatar]:size-[calc(var(--kl-av)-2*var(--kl-k1))]\",\n    \"*:data-[slot=avatar]:border-e-0 *:data-[slot=avatar]:border-t-0\",\n    \"*:data-[slot=avatar]:not-data-[status]:border-b-0 *:data-[slot=avatar]:data-[status=none]:border-b-0\",\n    \"*:not-first:border-s-(length:--kl-k1) *:first:border-s-0\",\n    \"*:not-first:not-data-[status=attention]:border-s-kl-seam\",\n    groupCellAttention,\n  ],\n  {\n    variants: {\n      size: {\n        sm: \"[--kl-av-fs:10px] [--kl-av:24px]\",\n        md: \"[--kl-av-fs:13px] [--kl-av:32px]\",\n        lg: \"[--kl-av-fs:15px] [--kl-av:40px]\",\n        xl: \"[--kl-av-fs:20px] [--kl-av:56px]\",\n      },\n    },\n    defaultVariants: { size: \"md\" },\n  },\n);\n\ntype AvatarGroupState = { size: AvatarSize };\n\ntype AvatarGroupProps = PartProps<\"div\", AvatarGroupState> & {\n  /** Names the group, e.g. \"Assignees\". The visible names and the hidden count are read after it. */\n  label: string;\n  /** Show at most this many cells; the rest fold into a count cell (+N) that takes the last place. */\n  max?: number;\n  /** Everyone in the group, when more exist than the children passed (a server count). */\n  total?: number;\n  size?: AvatarSize;\n  /** Words for the count cell. Default (n) => `${n} more`. */\n  countLabel?: (hidden: number) => string;\n};\n\n/**\n * Several avatars as cells of one keyline frame, sharing seams (C1: related parts touch). Not a stack of\n * overlapping circles: every face stays whole. With `max`, the overflow becomes one count cell. For a\n * count that opens the full list, leave out `max` and compose AvatarGroupCount with a PopoverTrigger.\n */\nfunction AvatarGroup({\n  label,\n  max,\n  total,\n  size = \"md\",\n  countLabel = (n) => `${n} more`,\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: AvatarGroupProps): React.ReactElement {\n  const all = React.Children.toArray(children).filter(React.isValidElement);\n  const count = Math.max(total ?? all.length, all.length);\n  const fits = max == null || count <= max;\n  const shown = fits ? all : all.slice(0, Math.max(0, max - 1));\n  const hidden = count - shown.length;\n  const state: AvatarGroupState = { size };\n  const own = {\n    ...dataAttrs({ size }),\n    \"data-slot\": \"avatar-group\",\n    className: resolveClassName(cn(groupVariants({ size })), className)(state),\n    style: resolveStyle(undefined, style)(state),\n    children: (\n      <>\n        {shown}\n        {hidden > 0 ? <AvatarGroupCount count={hidden} label={countLabel(hidden)} /> : null}\n      </>\n    ),\n  };\n  return useRender({\n    defaultTagName: \"div\",\n    render,\n    state,\n    props: mergeProps<\"div\">({ role: \"group\", \"aria-label\": label }, props, own),\n  });\n}\n\n/* the overflow cell: tabular, \"+10\" keeps its sign first in RTL. As a button (render={<button />}) it presses. */\nconst count = cn(\n  \"box-border grid min-w-[calc(var(--kl-av)-2*var(--kl-k1))] flex-none place-items-center px-1.5\",\n  \"bg-kl-cell font-(family-name:--kl-font) text-[length:var(--kl-av-fs,13px)]/none text-kl-ink\",\n  \"font-(--kl-weight-strong) whitespace-nowrap tabular-nums [direction:ltr] [unicode-bidi:isolate]\",\n  \"[button&]:m-0 [button&]:cursor-pointer [button&]:border-y-0 [button&]:border-e-0\",\n  \"[button&]:not-data-popup-open:hover:bg-kl-tint [button&]:not-data-popup-open:active:bg-kl-press\",\n  \"focus-visible:outline-offset-(--kl-ring-in) data-popup-open:bg-kl-inverse data-popup-open:text-kl-on-inverse\",\n);\n\ntype AvatarGroupCountState = { count: number };\n\ntype AvatarGroupCountProps = PartProps<\"span\", AvatarGroupCountState> & {\n  /** How many are not shown. */\n  count: number;\n  label?: string;\n};\n\n/** The overflow cell (+N), tabular. Pass `render={<button type=\"button\" />}` (or put it in a trigger's\n *  `render`) when pressing it should show everyone. */\nfunction AvatarGroupCount({\n  count: hidden,\n  label,\n  render,\n  className,\n  style,\n  ...props\n}: AvatarGroupCountProps): React.ReactElement {\n  const state: AvatarGroupCountState = { count: hidden };\n  const own = {\n    \"data-slot\": \"avatar-group-count\",\n    className: resolveClassName(cn(count, render && \"kl-key\"), className)(state),\n    style: resolveStyle(undefined, style)(state),\n    children: <span aria-hidden>+{hidden}</span>,\n  };\n  return useRender({\n    defaultTagName: \"span\",\n    render,\n    state,\n    props: mergeProps<\"span\">(\n      { role: render ? undefined : \"img\", \"aria-label\": label ?? `${hidden} more` },\n      props,\n      own,\n    ),\n  });\n}\n\nexport {\n  Avatar,\n  AvatarImage,\n  AvatarFallback,\n  AvatarGroup,\n  AvatarGroupCount,\n  avatarInitials,\n  type AvatarProps,\n  type AvatarImageProps,\n  type AvatarFallbackProps,\n  type AvatarGroupProps,\n  type AvatarGroupCountProps,\n  type AvatarSize,\n  type AvatarStatus,\n};\n",
      "type": "registry:ui",
      "target": "components/keyline/ui/avatar.tsx"
    }
  ],
  "type": "registry:ui"
}