vite-plugin-svg-sprite
Version:
SVG sprite plugin for Vite
41 lines (40 loc) • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function createAddSymbol() {
if (typeof window === 'undefined' || typeof document === 'undefined') {
return () => () => { };
}
const idSet = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window._SVG_SPRITE_IDS_ = window._SVG_SPRITE_IDS_ || new Set());
const root = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
root.style.position = 'absolute';
root.style.width = '0';
root.style.height = '0';
root.style.overflow = 'hidden';
root.ariaHidden = 'true';
// DO NOT SET THIS
// root.style.visibility = 'hidden';
function insertRoot() {
document.body.insertBefore(root, document.body.firstChild);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', insertRoot);
}
else {
insertRoot();
}
return function addSymbol(symbol, id) {
if (idSet.has(id) || document.getElementById(id)) {
console.warn(`Icon #${id} was repeatedly registered. It must be globally unique.`);
}
idSet.add(id);
root.insertAdjacentHTML('beforeend', symbol);
const el = root.lastChild;
return function dispose() {
idSet.delete(id);
el === null || el === void 0 ? void 0 : el.remove();
};
};
}
exports.default = createAddSymbol();