Skip to content

SvgImage

Defined in: web-components-integration/src/SvgImage.ts:58

Basic SVG image. Implements SVG sprites. Adds <svg> element with the symbols to the <body>.

Pass SVG source code to the src property, so the image will be rendered.

Every attribute that starts with svg- string will be passed down to the <svg> element.

Every attribute that starts with use- string will be passed down to the <use> element.

// Import SVG source code:
import imgSrc from "@/assets/image.svg?src";
// Import web component:
import { SvgImage } from "vite-awesome-svg-loader/web-components-integration";
// Define a custom element:
SvgImage.define();
// Create image:
const img = new SvgImage();
// or:
const img = document.createElement("svg-image");
// Assign SVG source code to the image:
img.src = imgSrc;
// Add image to the DOM:
document.body.appendChild(img);
// Set attributes to <svg> element:
img.setAttribute("svg-preserveAspectRatio", "xMaxYMin"); // Will set preserveAspectRatio="xMaxYMin"
// Set attributes to <use> element:
img.setAttribute("use-data-test", "use-el"); // will set data-test="use-el"
  • CustomElement

new SvgImage(): SvgImage

Defined in: web-components-integration/src/SvgImage.ts:90

SvgImage

WebComponent.constructor

src: string

Defined in: web-components-integration/src/SvgImage.ts:66

SVG source code


protected _state: WebComponentState = "uninitialized"

Defined in: web-components-integration/src/WebComponent.ts:284

Current component state. See WebComponentState for the details.

WebComponent._state


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

Defined in: web-components-integration/src/SvgImage.ts:71

Last src update result


static optional observedAttributes: string[]

Defined in: web-components-integration/src/WebComponent.ts:270

Doesn’t do anything. All attributes are already observed. This property will be deleted when custom element will be defined.

This property is recognized for compatibility reasons only.

WebComponent.observedAttributes


readonly static props: object[]

Defined in: web-components-integration/src/SvgImage.ts:59

Properties that will be synced with attributes.

Properties must be in camelCase. Synchronized attributes will be in kebab-case.

To not confuse cases, prefer single-word names.

default: string = EMPTY_SVG

name: string = "src"

WebComponent.props

get svgEl(): SVGElement

Defined in: web-components-integration/src/SvgImage.ts:86

<svg> element

SVGElement


get useEl(): SVGUseElement

Defined in: web-components-integration/src/SvgImage.ts:79

<use> element

SVGUseElement

attributeChangedCallback(name, oldValue, newValue): void

Defined in: web-components-integration/src/SvgImage.ts:110

Called when:

  1. Component has just finished initialization. Then:

    1. _state property will be attrs-first-change.
    2. oldValue argument will be null.
    3. newValue will have a default value or null, if there’s no default.
    4. Thus, following may be true: oldValue === newValue && newValue === null.
  2. Any of the element’s attributes has actually changed (unlike default custom elements’ behavior where this method is called even if the same value has been set). Then:

    1. _state property will be normal.
    2. oldValue and newValue will be always different.

string

string | null

string | null

void

CustomElement.attributeChangedCallback

WebComponent.attributeChangedCallback


connectedCallback(): void

Defined in: web-components-integration/src/SvgImage.ts:97

Called when the element is added to a document.

If overridden, super.connectedCallback() must be the first statement.

void

CustomElement.connectedCallback

WebComponent.connectedCallback


disconnectedCallback(): void

Defined in: web-components-integration/src/SvgImage.ts:106

Called when the element is removed from a document.

void

CustomElement.disconnectedCallback


getAttribute(name): string | null

Defined in: web-components-integration/src/WebComponent.ts:412

The getAttribute() method of the element.

MDN Reference

string

string | null

CustomElement.getAttribute

WebComponent.getAttribute


getStyleId(): string

Defined in: web-components-integration/src/SvgImage.ts:131

string

ID of a <style> element that styles this component


removeAttribute(name): void

Defined in: web-components-integration/src/WebComponent.ts:423

The Element method removeAttribute() removes the attribute with the specified name from the element.

MDN Reference

string

void

CustomElement.removeAttribute

WebComponent.removeAttribute


setAttribute(name, value): void

Defined in: web-components-integration/src/WebComponent.ts:419

The setAttribute() method of the Element interface sets the value of an attribute on the specified element.

MDN Reference

string

string

void

CustomElement.setAttribute

WebComponent.setAttribute


toggleAttribute(name, force?): boolean

Defined in: web-components-integration/src/WebComponent.ts:427

The toggleAttribute() method of the present and adding it if it is not present) on the given element.

MDN Reference

string

boolean

boolean

CustomElement.toggleAttribute

WebComponent.toggleAttribute


protected _ctor(): typeof WebComponent

Defined in: web-components-integration/src/WebComponent.ts:485

Returns a constructor of this class

If you need to refer to your own class, type it like so:

class MyClass extends WebComponent {
declare protected _ctor: () => typeof MyClass
}

typeof WebComponent

constructor of this class

WebComponent._ctor


protected _init(): void

Defined in: web-components-integration/src/WebComponent.ts:353

Initializes component state.

Custom elements may not add attributes or children in a constructor, so separate initialization is required. Thus, this method is called whenever an attribute is accessed or in a microtask after component’s constructor has run.

Initialization is performed only once.

void

WebComponent._init


static define(options): void

Defined in: web-components-integration/src/SvgImage.ts:159

Defines a custom element for this web component.

Won’t throw an error if called multiple times with the same tag.

Will throw an error (unless suppressed via BasicWebComponentDefinitionOptions.noRedefinitionError) if defined under a different tag.

WebComponentDefinitionOptions = {}

Definition options

void

WebComponent.define


protected static _initClass(): void

Defined in: web-components-integration/src/WebComponent.ts:527

Initializes class itself. Called by define and by the constructor. Initialization happens only once.

void

WebComponent._initClass