SVG Image

Draws an svg file straight into the page instead of as a picture, so its lines take the text colour and the stroke width you give it, in a 24px square unless size sets another.

import { SvgImage } from "@produkto/ui/SvgImage";

const Example = () => <SvgImage size={64} src="/icon.svg" />;

export default Example;

Usage

import { SvgImage } from "@produkto/ui/SvgImage";
<SvgImage src="/icon.svg" />

Examples

Sizes

size is its width and height in pixels, as an Icon's is, 24 by default.

import { SvgImage } from "@produkto/ui/SvgImage";

const icon: string = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="5" /><path d="M12 8v8" /><path d="M8 12h8" /></svg>`;

const sizes: number[] = [16, 20, 24, 32, 40];

const Example = () => (
  <div className="flex flex-wrap items-center gap-4">
    {sizes.map((size) => (
      <SvgImage key={size} size={size} src={icon} />
    ))}
  </div>
);

export default Example;

Markup

src takes the svg's markup as well as a url, so a drawing stored as text needs no request.

import { SvgImage } from "@produkto/ui/SvgImage";

const icon: string = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="5" /><path d="M12 8v8" /><path d="M8 12h8" /></svg>`;

const Example = () => <SvgImage size={48} src={icon} />;

export default Example;

Stroke width

strokeWidth rewrites every stroke-width in the svg's markup, 1.5 by default.

import { SvgImage } from "@produkto/ui/SvgImage";

const icon: string = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="5" /><path d="M12 8v8" /><path d="M8 12h8" /></svg>`;

const strokeWidths: number[] = [1, 1.5, 2, 2.5];

const Example = () => (
  <div className="flex flex-wrap items-center gap-4">
    {strokeWidths.map((strokeWidth) => (
      <SvgImage
        key={strokeWidth}
        size={48}
        src={icon}
        strokeWidth={strokeWidth}
      />
    ))}
  </div>
);

export default Example;

Fallback

Its children stand in when there is nothing to draw: no src, a file that fails to load, or a browser that can't draw inline svg.

import { SvgImage } from "@produkto/ui/SvgImage";
import { ImageNotFound01Icon } from "@hugeicons/core-free-icons";
import { Icon } from "@produkto/ui/Icon";

const Example = () => (
  <SvgImage size={48} src="">
    <Icon icon={ImageNotFound01Icon} />
  </SvgImage>
);

export default Example;

API Reference

SvgImage

SvgImageProps ยท components/SvgImage/SvgImage.types.ts
PropTypeDefault
srcrequiredstring-
sizenumber24
strokeWidthnumber | string-

It also takes the props of <span>.