SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:42
Basic SVG image. Implements SVG sprites. Adds <svg> element with the symbols to the <body>.
Basic usage
Section titled “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 SVGconst image = new SvgImage(imageSrc, container); // Create image and mount it to the containerimage.setSvgElAttrs({ id: "my-svg-symbol" }); // Change <svg> element attributesimage.setUseElAttrs({ className: "my-svg-symbol-use-el" }); // Change <use> element attributesimage.setSrc(anotherImageSrc); // Change SVG source codeconsole.log(image.getSrc()); // Get and print image source codeconsole.log(image.getContainer()); // Get and print image containerconsole.log(image.getSvgEl()); // Get and print image <svg> elementconsole.log(image.getUseEl()); // Get and print image <use> elementimage.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
Section titled “Internal API”You can use this API to extend SvgImage.
Use constructor() and SvgImage#mount to change component markup.
You probably don’t need to override required elements’ attributes. If you actually need to do so, override SvgImage._setRequiredSvgElAttrs and SvgImage._setRequiredUseElAttrs.
Extended by
Section titled “Extended by”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new SvgImage(
src,mountTo?):SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:72
Parameters
Section titled “Parameters”string
SVG source code
mountTo?
Section titled “mountTo?”ElementOrSelector
Element or selector of an element to mount image to. If not provided, image won’t be mounted.
Returns
Section titled “Returns”SvgImage
Properties
Section titled “Properties”_container
Section titled “_container”
protected_container:Element|undefined
Defined in: vanilla-integration/src/SvgImage.ts:51
Element containing this SvgImage (element passed to mount)
protected_src:string|undefined
Defined in: vanilla-integration/src/SvgImage.ts:46
User-provided source code
_svgEl
Section titled “_svgEl”
protectedreadonly_svgEl:SVGElement
Defined in: vanilla-integration/src/SvgImage.ts:56
<svg> element
_updateSrcRes
Section titled “_updateSrcRes”
protected_updateSrcRes: {attrs?:undefined;id?:undefined; } | {attrs: {height:string;viewBox:string;width:string; };id:string; } | {attrs?:undefined;id:string; } ={}
Defined in: vanilla-integration/src/SvgImage.ts:66
Last onSrcUpdate call result
_useEl
Section titled “_useEl”
protectedreadonly_useEl:SVGUseElement
Defined in: vanilla-integration/src/SvgImage.ts:61
<use> element
Methods
Section titled “Methods”getContainer()
Section titled “getContainer()”getContainer():
Element|undefined
Defined in: vanilla-integration/src/SvgImage.ts:176
Returns
Section titled “Returns”Element | undefined
A container of this image, or undefined, if image is not mounted
getSrc()
Section titled “getSrc()”getSrc():
string|undefined
Defined in: vanilla-integration/src/SvgImage.ts:169
Returns
Section titled “Returns”string | undefined
SVG source code
getSvgEl()
Section titled “getSvgEl()”getSvgEl():
SVGElement
Defined in: vanilla-integration/src/SvgImage.ts:187
Returns <svg> element.
To assign attributes, use setSvgElAttrs instead.
Returns
Section titled “Returns”SVGElement
<svg> element
getUseEl()
Section titled “getUseEl()”getUseEl():
SVGUseElement
Defined in: vanilla-integration/src/SvgImage.ts:198
Returns <use> element.
To assign attributes, use setSvgElAttrs instead.
Returns
Section titled “Returns”SVGUseElement
<use> element
mount()
Section titled “mount()”mount(
to):SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:89
Mounts image to the given element
Parameters
Section titled “Parameters”ElementOrSelector
Element or selector of an element to mount image to
Returns
Section titled “Returns”SvgImage
this
setSrc()
Section titled “setSrc()”setSrc(
src):SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:158
Sets SVG source code
Parameters
Section titled “Parameters”string
SVG source code
Returns
Section titled “Returns”SvgImage
this
setSvgElAttrs()
Section titled “setSvgElAttrs()”setSvgElAttrs(
attrs):SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:110
Sets <svg> element attributes. It won’t remove id, class and style.
Parameters
Section titled “Parameters”Record<string, SettableAttributeValue>
Attributes to set
Returns
Section titled “Returns”SvgImage
this
setUseElAttrs()
Section titled “setUseElAttrs()”setUseElAttrs(
attrs):SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:134
Sets <use> element attributes. It won’t remove id, class and style.
Parameters
Section titled “Parameters”Record<string, SettableAttributeValue>
Attributes to set
Returns
Section titled “Returns”SvgImage
this
unmount()
Section titled “unmount()”unmount():
SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:98
Removes image from the container
Returns
Section titled “Returns”SvgImage
this
_setRequiredSvgElAttrs()
Section titled “_setRequiredSvgElAttrs()”
protected_setRequiredSvgElAttrs():SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:121
Sets required attributes to the <svg> element
Returns
Section titled “Returns”SvgImage
this
_setRequiredUseElAttrs()
Section titled “_setRequiredUseElAttrs()”
protected_setRequiredUseElAttrs():SvgImage
Defined in: vanilla-integration/src/SvgImage.ts:145
Sets required attributes to the <use> element
Returns
Section titled “Returns”SvgImage
this