# 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 { cx } from "./_unframework/cx";
import type { ComponentPropsWithRef } from "react";

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

export type StatusIconAttributes = Omit<
  ComponentPropsWithRef<"svg">,
  "label" | "size" | "colour" | "children"
>;

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}
      {...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>
  );
}
```

#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, SvelteHTMLElements } from "svelte/elements";

  type Props = Omit<
    SvelteHTMLElements["svg"],
    "label" | "size" | "colour" | "children" | "class" | "style" | "ref"
  > &
    StatusIconProps & {
      class?: ClassValue;
      style?: string;
      ref?: SVGElementTagNameMap["svg"] | null;
    };

  let {
    label,
    size = 24,
    colour = "#1f6feb",
    class: className,
    style,
    ref = $bindable(null),
    ...attributes
  }: Props = $props();
</script>

<svg
  class={["status-icon", className]}
  role="img"
  viewBox="0 0 24 24"
  width={size}
  height={size}
  fill="none"
  {style}
  bind:this={ref}
  {...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 { Fallthrough } from "./_unframework/Fallthrough";
import type { SvgListeners } from "./_unframework/Listeners";
import { ScopeAttributes } from "./_unframework/ScopeAttributes";
import { Component, ElementRef, inject, input, viewChild } from "@angular/core";

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

@Component({
  selector: "uf-status-icon",
  imports: [ScopeAttributes, Fallthrough],
  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]="'width' in (this.ufAttributes() ?? {}) ? this.ufAttributes()?.['width'] : size"
      [attr.height]="'height' in (this.ufAttributes() ?? {}) ? this.ufAttributes()?.['height'] : size"
      fill="none"
      [class]="this.class()"
      [style]="this.style()"
      [ufFallthrough]="{ attributes: this.ufAttributes(), listeners: this.ufListeners() }"
      [ufScope]="this.host"
      #ufElement
    >
      <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>>();
  readonly ufAttributes = input<Record<string, unknown>>();
  readonly ufListeners = input<SvgListeners>();
  readonly ufElement = viewChild<ElementRef<SVGSVGElement>>("ufElement");
  protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
}
```

#solid

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

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

export default function StatusIcon(
  rawProps: StatusIconProps &
    Omit<ComponentProps<"svg">, "label" | "size" | "colour" | "style"> & {
      style?: JSX.CSSProperties;
    },
) {
  const props = mergeProps(
    { size: 24, colour: "#1f6feb" } satisfies Partial<StatusIconProps>,
    rawProps,
  );
  const [, others] = splitProps(props, ["label", "size", "colour", "class"]);

  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"
      {...others}
    >
      <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>
  );
}
```

#qwik

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

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

export default component$<
  StatusIconProps &
    Omit<PropsOf<"svg">, "label" | "size" | "colour" | "class" | "style"> & {
      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"
      style={style}
      {...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>
  );
});
```

#astro

<!-- prettier-ignore -->
```astro [StatusIcon.astro]
---
import type { HTMLAttributes } from "astro/types";

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

interface Props extends StatusIconProps, Omit<HTMLAttributes<"svg">, "label" | "size" | "colour"> {
  class?: string;
  style?: Record<string, string | number | undefined>;
}

const {
  label,
  size = 24,
  colour = "#1f6feb",
  class: className,
  style,
  role,
  viewBox,
  width,
  height,
  fill,
  ...attrs
} = Astro.props;
---

<svg
  class:list={["status-icon", className]}
  role={"role" in Astro.props ? role : "img"}
  viewBox={"viewBox" in Astro.props ? viewBox : "0 0 24 24"}
  width={"width" in Astro.props ? width : size}
  height={"height" in Astro.props ? height : size}
  fill={"fill" in Astro.props ? fill : "none"}
  style={style}
  {...attrs}
>
  <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).
