Basic SVG image. Implements SVG sprites.

Will create <svg> element that contains all symbols, add passed source code to it and reuse it later.

Basic usage

import imageSrc from "./path/to/image.svg";
import anotherImageSrc from "./path/to/another/image.svg";
import { SvgImage } from "vite-awesome-svg-loader/vanilla-integration";

const container = document.createElement("div"); // Where to mount SVG
const image = new SvgImage(imageSrc, container); // Create image and mount it to the container
image.setSvgElAttrs({ id: "my-svg-symbol" }); // Change <svg> element attributes
image.setUseElAttrs({ className: "my-svg-symbol-use-el" }); // Change <use> element attributes
image.setSrc(anotherImageSrc); // Change SVG source code
console.log(image.getSrc()); // Get and print image source code
console.log(image.getContainer()); // Get and print image container
console.log(image.getSvgEl()); // Get and print image <svg> element
console.log(image.getUseEl()); // Get and print image <use> element
image.unmount(); // Remove image from the container

// All operations are chainable, so you can do this:

const image2 = new SvgImage(imageSrc)
.mount(container)
.setSvgElAttrs({ id: "my-svg-symbol" })
.setUseElAttrs({ className: "my-svg-symbol-use-el" });

Internal API

You can use this API to extend SvgImage.

Use constructor() and SvgImage#mount to change component markup.

Use SvgImage._updateSvgBeforeUserAttrsSet, SvgImage._updateSvgAfterUserAttrsSet, SvgImage._updateUseElBeforeUserAttrsSet, SvgImage._updateUseElAfterUserAttrsSet hooks to set custom elements attributes.

You probably don't need to override required element's attributes. If you actually need to do so, override SvgImage._updateSvgEl

Param: src

SVG source code

Param: mountTo

Element or selector of an element to mount image to. If not provided, image won't be mounted.

Hierarchy (view full)

Constructors

Properties

_container: undefined | Element
_src: undefined | string

User-provided source code

_svgAttrs: Record<string, any> = {}

User-provided attributes

_svgEl: SVGElement
_updateSrcRes: {
    attrs?: undefined;
    id?: undefined;
} | {
    attrs: {
        height: string;
        viewBox: string;
        width: string;
    };
    id: string;
} | {
    attrs?: undefined;
    id: string;
} = {}

Last src update result

Type declaration

  • Optional attrs?: undefined
  • Optional id?: undefined

Type declaration

  • attrs: {
        height: string;
        viewBox: string;
        width: string;
    }
    • height: string
    • viewBox: string
    • width: string
  • id: string

Type declaration

  • Optional attrs?: undefined
  • id: string
_useEl: SVGUseElement
_useElAttrs: Record<string, any> = {}

User-provided attributes

Methods

  • Called after user-provided attributes are set. You can use this function to set custom SVG element attributes.

    Returns void

  • Called before user-provided attributes are set. You can use this function to set custom SVG element attributes.

    Returns void

  • Called after user-provided attributes are set. You can use this function to set custom <use> element attributes.

    Returns void

  • Called before user-provided attributes are set. You can use this function to set custom <use> element attributes.

    Returns void

Generated using TypeDoc