Svg Label
A small annotation rendered inside an SVG at (x, y) user coordinates. The SVG-foundation primitive for "text floating over a viz" — point markers on charts, edge labels on graphs, spot annotations on vector visualizations.
Customize
Visual
accent
md
middle
Installation
npx shadcn@latest add https://craftbits.dev/r/svg-label.jsonUsage
import { SvgLabel } from "@craft-bits/core";
<svg viewBox="0 0 200 100">
<SvgLabel x={100} y={50} tone="accent">peak</SvgLabel>
</svg>Understanding the component
- Drop inside
<svg>. SvgLabel renders an SVG<g transform="translate(x, y)">containing an optional pill<rect>and a<text>element. It must live inside an<svg>. - Tone drives the palette.
tone="default"uses--cb-fgtext on--cb-bg-elevated;tone="accent"uses--cb-accent(or yourhexoverride) with a faint tinted background;tone="muted"uses--cb-fg-mutedwith a near-transparent fill. - Approximate width, not measured. The pill background's width is approximated from
children.length * fontSize * 0.6. This avoids a render-and-measure trick that would break SSR. For long labels, passwithBackground={false}and size your own backdrop. - Anchor controls alignment.
anchorsetstext-anchoron the<text>and shifts the pill horizontally so the rect always wraps the visible text correctly.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
x | number | required | X position in SVG user coordinates. |
y | number | required | Y position in SVG user coordinates. |
children | string | required | Label text (single string, not arbitrary nodes). |
anchor | 'start' | 'middle' | 'end' | 'middle' | Text + background alignment. |
tone | 'default' | 'accent' | 'muted' | 'default' | Color tone. |
size | 'sm' | 'md' | 'md' | Font size tier (10 or 12 px). |
pad | number | 4 | Background padding around the text (px). |
withBackground | boolean | true | Render the pill behind the text. |
hex | string | — | Override the accent hex for tone="accent". |
Accessibility
- SvgLabel sets
pointer-events: noneanduser-select: noneby default since labels are decorative annotations. - The hosting
<svg>should carryrole="img"andaria-labeldescribing the viz. - Color tones consume
--cb-fg/--cb-fg-muted/--cb-accentso contrast follows the user's theme.
Credits
- Extracted from:
craftingattention(app/src/components/ui/SvgLabel.tsx).