{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "components-diagram",
  "type": "registry:component",
  "title": "Diagram",
  "description": "A Mermaid source, drawn by Pele",
  "author": "sudo-kw <https://github.com/sudo-kw>",
  "dependencies": [
    "@gpuix/native@0.11.0",
    "@gpuix/react@0.11.0",
    "effect@4.1.2",
    "pele@1.13.0",
    "react@^19.2.4"
  ],
  "registryDependencies": [
    "https://gpuixui.sudokw.com/gpuix-tokens.json",
    "https://gpuixui.sudokw.com/gpuix-tw.json"
  ],
  "files": [
    {
      "path": "packages/components/src/diagram.tsx",
      "target": "@ui/gpuixui/components/diagram.tsx",
      "type": "registry:component",
      "content": "/** @jsxImportSource @gpuix/react */\nimport type { Props } from \"@gpuix/react\";\nimport { mergeStyles } from \"@gpuix/react/floating\";\nimport { cn } from \"../tw/index.js\";\nimport { themeColors } from \"../tokens/index.js\";\nimport * as Arr from \"effect/Array\";\nimport * as Option from \"effect/Option\";\nimport { useMemo, type ReactNode } from \"react\";\nimport { recipeStyle } from \"./internal/recipe.js\";\nimport { useTheme } from \"./internal/theme.js\";\nimport { diagramDataUrl, renderDiagram } from \"./diagram/logic.js\";\n\nexport const DIAGRAM_ROOT = \"flex flex-col gap-2\";\n\nexport const DIAGRAM_CANVAS = \"flex items-center justify-center\";\n\nexport const DIAGRAM_CAPTION = \"text-xs text-muted-foreground\";\n\nexport const DIAGRAM_ERROR =\n  \"rounded-md border border-destructive/50 bg-destructive/10 p-3 text-xs text-destructive\";\n\nexport interface DiagramProps extends Omit<Props, \"children\"> {\n  readonly source: string;\n\n  readonly label?: string;\n\n  readonly caption?: string;\n\n  readonly maxWidth?: number;\n\n  readonly fontSize?: number;\n\n  readonly idPrefix?: string;\n\n  readonly className?: string;\n\n  readonly testId?: string;\n\n  readonly children?: ReactNode;\n}\n\nexport const peleRoles = (\n  theme: ReturnType<typeof useTheme>,\n): Record<string, string> => {\n  const colors = themeColors(theme);\n  return {\n    \"--pele-bg\": colors.card ?? colors.background ?? \"#ffffff\",\n    \"--pele-fg\": colors.foreground ?? \"#000000\",\n    \"--pele-muted\": colors.mutedForeground ?? \"#666666\",\n    \"--pele-line\": colors.borderStrong ?? colors.border ?? \"#000000\",\n    \"--pele-surface\": colors.muted ?? \"#f3f3f3\",\n    \"--pele-surface-alt\": colors.accent ?? \"#e6e6e6\",\n    \"--pele-accent\": colors.primary ?? colors.accent ?? \"#3b82f6\",\n    \"--pele-border\": colors.border ?? \"#cccccc\",\n\n    \"--pele-radius\": \"4px\",\n    \"--pele-font\": \"sans-serif\",\n    \"--pele-font-mono\": \"monospace\",\n  };\n};\n\nexport const themedDrawing = (\n  svg: string,\n  roles: Record<string, string>,\n): string => {\n  const baked = svg.replace(\n    /var\\((--pele-[a-z-]+)\\s*,\\s*[^)]*\\)/g,\n    (whole, name: string) => roles[name] ?? whole,\n  );\n\n  const defined = new Map(\n    Arr.map(\n      [...baked.matchAll(/(--_[a-z-]+):([^;\"]+)/g)],\n      (match) => [match[1] ?? \"\", (match[2] ?? \"\").trim()] as const,\n    ),\n  );\n  return baked.replace(/var\\((--_[a-z-]+)\\)/g, (whole, name: string) => {\n    const value = defined.get(name);\n    return value === undefined || value === \"\" ? whole : value;\n  });\n};\n\nexport const Diagram = ({\n  source,\n  label,\n  caption,\n  maxWidth,\n  fontSize,\n  idPrefix,\n  className,\n  testId,\n  children,\n  style,\n  ...rest\n}: DiagramProps) => {\n  const theme = useTheme();\n  const roles = peleRoles(theme);\n\n  const name = label ?? rest[\"aria-label\"];\n  const description = rest[\"aria-description\"];\n\n  const rendered = useMemo(\n    () => renderDiagram(source, { idPrefix, fontSize, maxWidth }),\n    [source, idPrefix, fontSize, maxWidth],\n  );\n\n  const drawing = rendered.drawing;\n  const failure = Option.getOrUndefined(rendered.failure);\n\n  return (\n    <div\n      {...rest}\n      testId={testId}\n      style={mergeStyles(\n        recipeStyle(cn(DIAGRAM_ROOT, className), theme),\n        style,\n      )}\n    >\n      <div style={recipeStyle(DIAGRAM_CANVAS, theme)}>\n        {Option.match(drawing, {\n          onNone: () => (\n            <div\n              role=\"status\"\n              testId={testId === undefined ? undefined : `${testId}-error`}\n              style={recipeStyle(DIAGRAM_ERROR, theme)}\n            >\n              <text>\n                {failure?.message ?? \"The diagram could not be read.\"}\n              </text>\n            </div>\n          ),\n          onSome: (made) => (\n            <img\n              role=\"img\"\n              aria-label={name}\n              aria-description={description}\n              testId={testId === undefined ? undefined : `${testId}-drawing`}\n              src={diagramDataUrl(themedDrawing(made.svg, roles))}\n              style={{\n                width: made.width,\n                height: made.height,\n                maxWidth: \"100%\",\n              }}\n            />\n          ),\n        })}\n      </div>\n      {caption === undefined ? undefined : (\n        <text style={recipeStyle(DIAGRAM_CAPTION, theme)}>{caption}</text>\n      )}\n      {children}\n    </div>\n  );\n};\n\nexport const diagramMeta = {\n  name: \"Diagram\",\n  tier: 5,\n  status: \"done\",\n  gpuixSupport: \"partial\",\n  limitations: [\n    \"the drawing is painted as **one image** — the runtime's own SVG decoder paints the whole document — so a mark inside it is not an accessibility element and cannot be selected\",\n    \"an image cannot carry a **link**: Pele reports the links in its result, so a caller can read them, but the pixels are not clickable\",\n    \"Pele writes its theme as CSS custom properties, which a raster cannot read, so the values are read from the theme and **baked into** the drawing — a theme switch re-renders the diagram\",\n    \"the drawing's font is Pele's `sans-serif` and `monospace`, not the theme's family, because a raster carries no font of its own\",\n    \"a source the parser refuses paints the parser's sentence in the drawing's place; there is no partial render\",\n  ],\n  docs: \"docs/diagram.mdx\",\n  example: \"examples/diagram.example.tsx\",\n  testFile: \"src/behaviour/diagram.test.tsx\",\n} as const;\n"
    },
    {
      "path": "packages/components/src/diagram/logic.ts",
      "target": "@ui/gpuixui/components/diagram/logic.ts",
      "type": "registry:lib",
      "content": "import { Effect, Exit, Result } from \"effect\";\nimport * as Option from \"effect/Option\";\nimport * as Schema from \"effect/Schema\";\nimport { detectType, render } from \"pele\";\n\nexport class DiagramError extends Schema.TaggedError<DiagramError>()(\n  \"DiagramError\",\n  {\n    message: Schema.String,\n\n    line: Schema.optional(Schema.Number),\n\n    column: Schema.optional(Schema.Number),\n\n    snippet: Schema.optional(Schema.String),\n  },\n  { description: \"A Mermaid source that Pele's parser refused.\" },\n) {}\n\nexport interface DiagramDrawing {\n  readonly svg: string;\n\n  readonly width: number;\n\n  readonly height: number;\n\n  readonly type: string;\n}\n\nexport interface DiagramOptions {\n  readonly idPrefix?: string;\n\n  readonly fontSize?: number;\n\n  readonly maxWidth?: number;\n\n  readonly autoDirection?: boolean;\n}\n\nconst failureOf = (cause: unknown): DiagramError => {\n  const failure: {\n    readonly message?: string;\n    readonly line?: number;\n    readonly column?: number;\n    readonly snippet?: string;\n  } = typeof cause === \"object\" && cause !== null ? cause : {};\n  return new DiagramError({\n    message:\n      failure.message !== undefined && failure.message !== \"\"\n        ? failure.message\n        : \"The diagram could not be read.\",\n    line: failure.line,\n    column: failure.column,\n    snippet: failure.snippet,\n  });\n};\n\nexport const diagramDrawing = (\n  source: string,\n  options: DiagramOptions = {},\n): Effect.Effect<DiagramDrawing, DiagramError> =>\n  Effect.try({\n    try: () => {\n      const result = render(source, {\n        idPrefix: options.idPrefix,\n        fontSize: options.fontSize,\n        maxWidth: options.maxWidth,\n        autoDirection: options.autoDirection,\n      });\n      return {\n        svg: result.svg,\n        width: result.width,\n        height: result.height,\n        type: result.type,\n      };\n    },\n    catch: failureOf,\n  });\n\nexport const renderDiagram = (\n  source: string,\n  options: DiagramOptions = {},\n): DiagramResult => {\n  const exit = Effect.runSyncExit(diagramDrawing(source, options));\n  if (Exit.isSuccess(exit)) {\n    return { drawing: Option.some(exit.value), failure: Option.none() };\n  }\n\n  const found = Exit.findError(exit);\n  return {\n    drawing: Option.none(),\n    failure: Result.isSuccess(found)\n      ? Option.some(found.success)\n      : Option.none(),\n  };\n};\n\nexport interface DiagramResult {\n  readonly drawing: Option.Option<DiagramDrawing>;\n\n  readonly failure: Option.Option<DiagramError>;\n}\n\nexport const diagramType = (\n  source: string,\n): Effect.Effect<Option.Option<string>, DiagramError> =>\n  Effect.try({\n    try: () => {\n      const type = detectType(source);\n      return type === null ? Option.none() : Option.some(type);\n    },\n    catch: failureOf,\n  });\n\nexport const diagramDataUrl = (svg: string): string =>\n  `data:image/svg+xml;base64,${base64(svg)}`;\n\nconst base64 = (value: string): string => {\n  if (typeof Buffer !== \"undefined\") {\n    return Buffer.from(value, \"utf8\").toString(\"base64\");\n  }\n  const bytes = new TextEncoder().encode(value);\n  return btoa(Array.from(bytes, (byte) => String.fromCharCode(byte)).join(\"\"));\n};\n"
    },
    {
      "path": "packages/components/src/internal/recipe.ts",
      "target": "@ui/gpuixui/components/internal/recipe.ts",
      "type": "registry:lib",
      "content": "import type { StyleDesc } from \"@gpuix/react\";\nimport { createRecipeCache } from \"../../tw/index.js\";\nimport type { Theme } from \"./theme.js\";\n\nexport const recipeStyle: (recipe: string, theme: Theme) => StyleDesc =\n  createRecipeCache(512);\n"
    },
    {
      "path": "packages/components/src/internal/theme.ts",
      "target": "@ui/gpuixui/components/internal/theme.ts",
      "type": "registry:lib",
      "content": "import type { StyleDesc } from \"@gpuix/react\";\nimport * as Schema from \"effect/Schema\";\nimport {\n  alpha,\n  avatarPixels,\n  avatarSize,\n  brandTextOn,\n  controlFocusRing,\n  controlHeight,\n  controlPaddingX,\n  currentPlatform,\n  darkTheme,\n  disabledAction,\n  disabledOpacity,\n  elevationShadow,\n  filledHover,\n  filledPressed,\n  findHighlight,\n  focusRing,\n  keyboardFocusRing,\n  lightTheme,\n  nativeTextTheme,\n  pointerWash,\n  selectionOn,\n  statusTone,\n  syntaxColors,\n  targetSize,\n  textStyle,\n  type BoxShadow,\n  type ElevationLevel,\n  type FindHighlightSource,\n  type NativeTextFill,\n  type PlatformName,\n  type PointerWashSurface,\n  type StatusTone,\n  type SyntaxColors,\n  type SyntaxRole,\n  type TextRole,\n  type Theme,\n  type ThemeColors,\n} from \"../../tokens/index.js\";\n\nexport {\n  alpha,\n  avatarPixels,\n  avatarSize,\n  controlFocusRing,\n  controlHeight,\n  controlPaddingX,\n  currentPlatform,\n  darkTheme,\n  disabledAction,\n  disabledOpacity,\n  elevationShadow,\n  filledHover,\n  filledPressed,\n  findHighlight,\n  focusRing,\n  keyboardFocusRing,\n  lightTheme,\n  nativeTextTheme,\n  statusTone,\n  syntaxColors,\n  targetSize,\n  textStyle,\n};\nexport type {\n  BoxShadow,\n  ElevationLevel,\n  FindHighlightSource,\n  NativeTextFill,\n  PlatformName,\n  StatusTone,\n  SyntaxColors,\n  SyntaxRole,\n  TextRole,\n  Theme,\n};\nexport { ThemeProvider, useTheme } from \"../../tokens/react.js\";\n\nexport const keyboardRing = (theme: Theme, invalid = false): StyleDesc =>\n  controlFocusRing(theme, { invalid }) ?? {};\n\nexport interface ControlRingState {\n  readonly disabled?: boolean;\n\n  readonly invalid?: boolean;\n}\n\nexport const controlRing = (\n  theme: Theme,\n  state: ControlRingState = {},\n): StyleDesc => controlFocusRing(theme, state) ?? {};\n\nexport const pressWash = (theme: Theme, replaced: PointerWashSurface): string =>\n  pointerWash(theme, \"active\", replaced) ?? theme.colors.active;\n\nexport const selectionWash = (theme: Theme): StyleDesc => ({\n  selectionColor: theme.colors.selection,\n});\n\nexport const keepContent: StyleDesc = { keyboardFocusDim: false };\n\nexport const disabledFade = (\n  disabled: boolean,\n): { readonly opacity: number } | undefined =>\n  disabled ? { opacity: disabledOpacity } : undefined;\n\nconst Tones = Schema.Literals([\"info\", \"success\", \"warning\", \"danger\"]);\n\nexport type Tone = typeof Tones.Type;\n\nexport const toneOf = (value: unknown): Tone =>\n  Schema.is(Tones)(value) ? value : \"info\";\n\nexport interface ToneStyle {\n  readonly fg: string;\n  readonly bg: string;\n  readonly border: string;\n}\n\nconst TOKEN_TONE: Readonly<Record<Tone, StatusTone>> = {\n  info: \"info\",\n  success: \"success\",\n  warning: \"warning\",\n  danger: \"destructive\",\n};\n\nexport const tone = (theme: Theme, value: Tone): ToneStyle => {\n  const { background, border, foreground } = statusTone(\n    theme,\n    TOKEN_TONE[value],\n  );\n  return { bg: background, border, fg: foreground };\n};\n\nconst softRoles: Readonly<Record<Tone, keyof ThemeColors>> = {\n  info: \"infoSoft\",\n  success: \"successSoft\",\n  warning: \"warningSoft\",\n  danger: \"destructiveSoft\",\n};\n\nexport const messageLink = (theme: Theme, value: Tone): string =>\n  brandTextOn(theme, softRoles[value]);\n\nexport const messageSelection = (theme: Theme, value: Tone): string =>\n  selectionOn(theme, softRoles[value]);\n\nexport const toneSurface = (theme: Theme, value: Tone): StyleDesc => {\n  const { bg, border, fg } = tone(theme, value);\n  return {\n    backgroundColor: bg,\n    color: fg,\n    borderWidth: theme.borderWidth.thin,\n    borderColor: border,\n  };\n};\n"
    }
  ],
  "docs": "[Diagram](https://gpuixui.sudokw.com/components/diagram) — These are GPUix native components: the files render through GPUix's reconciler against `@gpuix/react`, not the DOM, and need the per-platform `@gpuix/native` binding. The CLI installs the `@gpuixui/*` runtime the components import as registry items, so the copied tree resolves on its own; wrap it in `ThemeProvider` before it renders.",
  "meta": {
    "url": "/components/diagram",
    "kind": "component",
    "runtime": "gpuix",
    "registry": "gpuixui"
  }
}