# SVG

> Inline SVG renders the same DOM, in SVG's namespace, on every target, with SVG's case-exact element and attribute names.

An `<svg>` element starts SVG, and everything inside it is SVG. Write it as you would in HTML, with SVG's own names: every target renders the same elements, in SVG's namespace, from the server and in the browser.

<!-- prettier-ignore -->
```tsx [StatusIcon.uf.tsx]
export interface StatusIconProps {
  label: string;
  size?: number;
  colour?: string;
}

export default function StatusIcon({ label, size = 24, colour = "#1f6feb" }: StatusIconProps) {
  return (
    <svg class="status-icon" role="img" viewBox="0 0 24 24" width={size} height={size} fill="none">
      <title>{label}</title>
      <defs>
        <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" stop-color={colour} />
          <stop offset="1" stop-color="#0b3d91" />
        </linearGradient>
      </defs>
      <circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)" />
      <path
        d="M7 12.5l3 3 7-7"
        stroke="#ffffff"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
      />
    </svg>
  );
}
```

::framework-switcher
#react

<!-- prettier-ignore -->
```tsx [StatusIcon.tsx]
import type { CSSProperties } from "react";

export interface StatusIconProps {
  label: string;
  size?: number;
  colour?: string;
}

export interface StatusIconAttributes {
  className?: string;
  style?: CSSProperties;
}

export default function StatusIcon({
  label,
  size = 24,
  colour = "#1f6feb",
  className,
  style,
  ...attributes
}: StatusIconProps & StatusIconAttributes) {
  return (
    <svg
      className={cx("status-icon", className)}
      role="img"
      viewBox="0 0 24 24"
      width={size}
      height={size}
      fill="none"
      style={style}
      {...scopeAttributes(attributes)}
    >
      <title>{label}</title>
      <defs>
        <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" stopColor={colour} />
          <stop offset="1" stopColor="#0b3d91" />
        </linearGradient>
      </defs>
      <circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)" />
      <path
        d="M7 12.5l3 3 7-7"
        stroke="#ffffff"
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
}

/** Joins class names, and the keys of an object's truthy entries, into one `className`. */
function cx(...parts: unknown[]): string {
  const names: string[] = [];
  for (const part of parts) {
    if (typeof part === "string") {
      if (part) names.push(part);
    } else if (part && typeof part === "object") {
      for (const [key, on] of Object.entries(part)) {
        if (on) names.push(key);
      }
    }
  }
  return names.join(" ");
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

#vue

<!-- prettier-ignore -->
```vue [StatusIcon.vue]
<script setup lang="ts">
export interface StatusIconProps {
  label: string;
  size?: number;
  colour?: string;
}

const { label, size = 24, colour = "#1f6feb" } = defineProps<StatusIconProps>();
</script>

<template>
  <svg class="status-icon" role="img" viewBox="0 0 24 24" :width="size" :height="size" fill="none">
    <title>{{ label }}</title>
    <defs>
      <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
        <stop offset="0" :stop-color="colour" />
        <stop offset="1" stop-color="#0b3d91" />
      </linearGradient>
    </defs>
    <circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)" />
    <path
      d="M7 12.5l3 3 7-7"
      stroke="#ffffff"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
    />
  </svg>
</template>
```

#svelte

<!-- prettier-ignore -->
```svelte [StatusIcon.svelte]
<svelte:options runes={true} preserveWhitespace={false} />

<script lang="ts" module>
  export interface StatusIconProps {
    label: string;
    size?: number;
    colour?: string;
  }
</script>

<script lang="ts">
  import type { ClassValue } from "svelte/elements";

  type Props = StatusIconProps & { class?: ClassValue; style?: string };

  let {
    label,
    size = 24,
    colour = "#1f6feb",
    class: className,
    style,
    ...attributes
  }: Props = $props();

  /** The scope attributes of the styled parents that render this component, for its root. */
  function scopeAttributes(props: object): Record<string, ""> {
    const scope: Record<string, ""> = {};
    for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
    return scope;
  }
</script>

<svg
  class={["status-icon", className]}
  role="img"
  viewBox="0 0 24 24"
  width={size}
  height={size}
  fill="none"
  {style}
  {...scopeAttributes(attributes)}
>
  <title>{label}</title
  ><defs>
    <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
      <stop offset="0" stop-color={colour} /><stop offset="1" stop-color="#0b3d91" />
    </linearGradient>
  </defs
  ><circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)"
  /><path
    d="M7 12.5l3 3 7-7"
    stroke="#ffffff"
    stroke-width="2"
    stroke-linecap="round"
    stroke-linejoin="round"
  />
</svg>
```

#angular

<!-- prettier-ignore -->
```ts [status-icon.ts]
import {
  Component,
  Directive,
  ElementRef,
  type OnInit,
  Renderer2,
  inject,
  input,
} from "@angular/core";

export interface StatusIconProps {
  label: string;
  size?: number;
  colour?: string;
}

/** The scope attributes taken off each host, for the roots its component renders later. */
const scopes = new WeakMap<Element, string[]>();

/**
 * Gives a root node of its component the scope attributes the component's styled parents put on
 * its host (ADR-0063), and takes them off the host, which keeps only `display: contents`.
 */
@Directive({ selector: "[ufScope]" })
export class ScopeAttributes implements OnInit {
  readonly ufScope = input.required<HTMLElement>();
  private readonly element = inject<ElementRef<Element>>(ElementRef);
  private readonly renderer = inject(Renderer2);

  ngOnInit(): void {
    const host = this.ufScope();
    let names = scopes.get(host);
    if (!names) {
      names = host.getAttributeNames().filter((name) => name.startsWith("data-uf-"));
      for (const name of names) this.renderer.removeAttribute(host, name);
      scopes.set(host, names);
    }
    for (const name of names) this.renderer.setAttribute(this.element.nativeElement, name, "");
  }
}

@Component({
  selector: "uf-status-icon",
  imports: [ScopeAttributes],
  host: { "[attr.class]": "null", "[attr.style]": "'display: contents;'" },
  preserveWhitespaces: false,
  template: `
    @let label = this.label();
    @let size = this.size();
    @let colour = this.colour();
    <svg
      class="status-icon"
      role="img"
      viewBox="0 0 24 24"
      [attr.width]="size"
      [attr.height]="size"
      fill="none"
      [class]="this.class()"
      [style]="this.style()"
      [ufScope]="this.host"
    >
      <svg:title>{{ label }}</svg:title>
      <defs>
        <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" [attr.stop-color]="colour" />
          <stop offset="1" stop-color="#0b3d91" />
        </linearGradient>
      </defs>
      <circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)" />
      <path
        d="M7 12.5l3 3 7-7"
        stroke="#ffffff"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
      />
    </svg>
  `,
})
export default class StatusIcon {
  readonly label = input.required<string>();
  readonly size = input<number, number | undefined>(24, {
    transform: (value) => (value === undefined ? 24 : value),
  });
  readonly colour = input<string, string | undefined>("#1f6feb", {
    transform: (value) => (value === undefined ? "#1f6feb" : value),
  });
  readonly class = input<string>();
  readonly style = input<Record<string, string | null | undefined>>();
  protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
}
```

#solid

<!-- prettier-ignore -->
```tsx [StatusIcon.tsx]
import { type JSX, mergeProps } from "solid-js";

export interface StatusIconProps {
  label: string;
  size?: number;
  colour?: string;
}

export default function StatusIcon(
  rawProps: StatusIconProps & { class?: string; style?: JSX.CSSProperties },
) {
  const props = mergeProps(
    { size: 24, colour: "#1f6feb" } satisfies Partial<StatusIconProps>,
    rawProps,
  );
  return (
    <svg
      class={props.class ? `status-icon ${props.class}` : "status-icon"}
      role="img"
      viewBox="0 0 24 24"
      width={props.size}
      height={props.size}
      fill="none"
      style={props.style}
      {...scopeAttributes(props)}
    >
      <title>{props.label}</title>
      <defs>
        <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" stop-color={props.colour} />
          <stop offset="1" stop-color="#0b3d91" />
        </linearGradient>
      </defs>
      <circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)" />
      <path
        d="M7 12.5l3 3 7-7"
        stroke="#ffffff"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
      />
    </svg>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
```

#qwik

<!-- prettier-ignore -->
```tsx [StatusIcon.tsx]
import { type CSSProperties, type ClassList, component$ } from "@qwik.dev/core";

export interface StatusIconProps {
  label: string;
  size?: number;
  colour?: string;
}

export default component$<StatusIconProps & { class?: ClassList; style?: CSSProperties }>(
  ({ label, size = 24, colour = "#1f6feb", class: className, style, ...attributes }) => {
    return (
      <svg
        class={["status-icon", className]}
        role="img"
        viewBox="0 0 24 24"
        width={size}
        height={size}
        fill="none"
        {...scopeAttributes(attributes)}
        style={style}
      >
        <title>{label}</title>
        <defs>
          <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0" stop-color={colour} />
            <stop offset="1" stop-color="#0b3d91" />
          </linearGradient>
        </defs>
        <circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)" />
        <path
          d="M7 12.5l3 3 7-7"
          stroke="#ffffff"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        />
      </svg>
    );
  },
);

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

#astro

<!-- prettier-ignore -->
```astro [StatusIcon.astro]
---
export interface StatusIconProps {
  label: string;
  size?: number;
  colour?: string;
}

interface Props extends StatusIconProps {
  class?: string;
  style?: Record<string, string | number | undefined>;
}

const { label, size = 24, colour = "#1f6feb", class: className, style } = Astro.props;

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
---

<svg
  class:list={["status-icon", className]}
  role="img"
  viewBox="0 0 24 24"
  width={size}
  height={size}
  fill="none"
  style={style}
  {...scopeAttributes(Astro.props)}
>
  <title>{label}</title>
  <defs>
    <linearGradient id="status-icon-fill" x1="0" y1="0" x2="1" y2="1">
      <stop offset="0" stop-color={colour} />
      <stop offset="1" stop-color="#0b3d91" />
    </linearGradient>
  </defs>
  <circle cx="12" cy="12" r="10" fill="url(#status-icon-fill)" />
  <path
    d="M7 12.5l3 3 7-7"
    stroke="#ffffff"
    stroke-width="2"
    stroke-linecap="round"
    stroke-linejoin="round"
  />
</svg>
```

::

A childless SVG element closes itself in every output, and bound attributes follow the rules of [Attributes](/docs/components/attributes#bound-values): `width={size}` takes a number, and `stop-color={colour}` a string. React writes its own camelCase names for SVG's attributes, such as `strokeWidth` and `stopColor`. Angular writes SVG's `<title>` as `<svg:title>`: it reads an unprefixed `title` as the HTML element, whose content is text it does not parse.

## Elements

Inside an `<svg>`, a tag is one of SVG's elements, in SVG's case: `circle`, `clipPath`, `defs`, `desc`, `ellipse`, the filter primitives (`feBlend` to `feTurbulence`), `filter`, `g`, `image`, `line`, `linearGradient`, `marker`, `mask`, `metadata`, `mpath`, `path`, `pattern`, `polygon`, `polyline`, `radialGradient`, `rect`, `stop`, `svg`, `switch`, `symbol`, `text`, `textPath`, `title`, `tspan`, `use` and `view`. A name in the wrong case has a fix.

- An HTML element inside an `<svg>`, or an SVG element outside one, is [UF3001](/docs/reference/diagnostics#UF3001): the HTML parser leaves SVG at an HTML tag such as `<div>` or `<p>`.
- `<script>`, `<style>` and SMIL animation (`<animate>`, `<animateMotion>`, `<animateTransform>`, `<set>`, `<discard>`) are [UF3002](/docs/reference/diagnostics#UF3002): animation makes the rendering depend on time, and can set any attribute.
- SVG's `<a>` and `<foreignObject>` are [UF1002](/docs/reference/diagnostics#UF1002): the targets do not render them alike yet. So is MathML's `<math>`.
- A component whose root is an SVG child, such as `<g>`, lands in M8: it needs its parent's namespace, which Vue's templates have no way to declare.

## Attributes

SVG's attribute names are case-exact, as SVG spells them: `viewBox`, `preserveAspectRatio`, and the presentation attributes in kebab case, such as `stroke-width` and `stop-color`. `class`, `id`, `style`, `tabindex`, `lang`, `role`, ARIA and `data-*` work as in HTML. `<title>` and `<desc>` take only `class`, `id`, `lang`, `style`, ARIA and `data-*`.

- SVG has no boolean attributes, so a bare SVG attribute is [UF3004](/docs/reference/diagnostics#UF3004).
- `href` replaces SVG's old `xlink:href`, which is UF3004 with a fix. Other `xlink:` and `xml:` names, and `xmlns`, are [UF3005](/docs/reference/diagnostics#UF3005): the targets render their namespaces differently.
- A number in a `style` needs no unit only where CSS needs none. On SVG's stroke and opacity properties, Qwik adds `px` to a number and the other targets leave it bare, so `strokeWidth: 2` is [UF3018](/docs/reference/diagnostics#UF3018), with a fix that writes `"2"`. The attribute, `stroke-width="2"`, needs neither.

## Text

SVG renders text only in `<text>`, `<tspan>`, `<textPath>`, `<title>` and `<desc>`, so an expression child can sit only there. Outside a `<text>`, text that is only whitespace is [UF3003](/docs/reference/diagnostics#UF3003): Svelte drops it and the other targets keep it. Elsewhere the fix removes it, and a formatted source has none. In a `<title>` or a `<desc>`, a space between two values, as in `<title>{name} {status}</title>`, is part of the accessible name, so there is no fix: write the parts as one template literal, ``<title>{`${name} ${status}`}</title>``.

A `<title>` labels its `<svg>` for assistive technology, as in the example, which also gives the `<svg>` `role="img"`. `<title>` and `<desc>` hold text only: the HTML parser and Vue read an element inside them as HTML, and React as SVG ([UF3003](/docs/reference/diagnostics#UF3003)). Their text can still be built from several parts:

<!-- prettier-ignore -->
```tsx [FileTransfer.uf.tsx]
<title>
  {fileName}: {percent}% sent. {eta}
</title>
```

::framework-switcher
#react

<!-- prettier-ignore -->
```tsx [FileTransfer.tsx]
<title>{`${fileName}: ${percent}% sent. ${eta ?? ""}`}</title>
```

#vue

<!-- prettier-ignore -->
```vue [FileTransfer.vue]
<title>{{ fileName }}: {{ percent }}% sent. {{ eta }}</title>
```

#svelte

<!-- prettier-ignore -->
```svelte [FileTransfer.svelte]
<title>{fileName}: {percent}% sent. {eta}</title
```

#angular

<!-- prettier-ignore -->
```ts [file-transfer.ts]
<svg:title>{{ fileName }}: {{ percent }}% sent. {{ eta }}</svg:title>
```

#solid

<!-- prettier-ignore -->
```tsx [FileTransfer.tsx]
<title>
  {props.fileName}: {props.percent}% sent. {props.eta}
</title>
```

#qwik

<!-- prettier-ignore -->
```tsx [FileTransfer.tsx]
<title>{`${fileName}: ${percent}% sent. ${eta ?? ""}`}</title>
```

#astro

<!-- prettier-ignore -->
```astro [FileTransfer.astro]
<title>{fileName}: {percent}% sent. {eta}</title>
```

::

React's server renderer writes a title's text only when its children are one value, and Qwik types a title's child as a string, so both join the parts into one template literal, with `?? ""` for a part that may be absent. The other targets write the parts as they are.

The decision behind this page is [ADR-0040](https://github.com/uxfront-com/unframework/blob/main/docs/adrs/0040-svg.md).
