UNPKG

vite-awesome-svg-loader

Version:

A universal Vite SVG loader. Imports SVGs as source code, base64 and data URI. Preserves stroke width. Replaces colors with currentColor or custom colors. Creates SVG sprites. Optimizes SVGs.

1 lines 4.99 kB
{"version":3,"file":"index-Bp2I1MnI.cjs","sources":["../../../common-utils/src/misc.ts","../../../common-utils/src/dom.ts"],"sourcesContent":["import type { MaybeArray } from \"@/types\";\n\n/**\n * Converts value to an array:\n *\n * 1. If value is already an array, returns it as-is.\n * 1. Otherwise wraps it in array: `[value]`\n *\n * @example\n *\n * toArray([\"apples\", \"oranges\"]); // [\"apples\", \"oranges\"]\n * toArray([\"apples\"]); // [\"apples\"]\n * toArray(\"apples\"); // [\"apples\"]\n *\n * @param value Value\n * @returns Value as an array\n */\nexport function toArray<T>(value: MaybeArray<T>) {\n return Array.isArray(value) ? value : [value];\n}\n\n/**\n * Transforms string in `camelCase` or `PascalCase` into `kebab-case`\n * @param str String in `camelCase`\n * @returns String in `kebab-case`\n */\nexport function camelCaseToKebabCase(str: string) {\n let kebab = \"\";\n\n for (let i = 0; i < str.length; i++) {\n const char = str[i];\n const lowercase = char.toLowerCase();\n\n if (i !== 0 && char !== lowercase) {\n kebab += \"-\";\n }\n\n kebab += lowercase;\n }\n\n return kebab;\n}\n","/**\n * HTML element or selector that will be passed to `document.querySelector()`\n */\nexport type ElementOrSelector = Element | string;\n\n/**\n *\n */\nexport type SettableAttributeValue = string | null | undefined;\n\n/**\n * Appends given element to the given container\n * @param el Element to append\n * @param to Container\n * @returns Container\n */\nexport function mount(el: Element, to: ElementOrSelector) {\n const container = typeof to === \"string\" ? document.querySelector(to) : to;\n\n if (!container) {\n console.error(`No elements found for selector \"${to}\"`);\n return;\n }\n\n container.appendChild(el);\n return container;\n}\n\n/**\n * Sets attributes of an element. Ignores `id`, `class` and `style` attributes.\n *\n * @param el Element to set attributes of\n * @param attrs Attributes object\n * @param clear If `true`, all element attributes will be cleared\n */\nexport function setAttrs(el: Element, attrs: Record<string, SettableAttributeValue>) {\n for (const attr in attrs) {\n setAttr(el, attr, attrs[attr]);\n }\n}\n\n/**\n * Clears attributes of an element.\n *\n * @param el Element whose attributes must be cleared\n */\nexport function clearAttrs(el: Element) {\n // Copy attrs because el.removeAttribute() will break iteration order\n const attrs: string[] = [];\n\n for (const attr of el.attributes) {\n attrs.push(attr.name);\n }\n\n for (const attr of attrs) {\n el.removeAttribute(attr);\n }\n}\n\n/**\n * Sets an attribute of an element\n *\n * @param el Element\n * @param name Attribute name\n * @param value Attribute value\n */\nexport function setAttr(el: Element, name: string, value: SettableAttributeValue) {\n if (typeof value === \"string\") {\n el.setAttribute(name, value);\n } else {\n el.removeAttribute(name);\n }\n}\n\n/**\n * Creates and appends to the `<head>` a unique style element. Behavior:\n *\n * 1. If there's no element with the given ID, will create a style element.\n * 1. If existing element **is** a style, won't do anything.\n * 1. If existing element **is not** a style, it will be replaced with a style element\n *\n * @param id Style ID\n * @param css CSS styles\n * @returns Created style element\n */\nexport function createStyle(id: string, css: string) {\n let el = document.getElementById(id);\n\n if (el) {\n if (el instanceof HTMLStyleElement) {\n return el;\n }\n\n el.remove();\n }\n\n el = document.createElement(\"style\");\n el.innerHTML = css;\n el.id = id;\n document.head.appendChild(el);\n\n return el;\n}\n"],"names":["toArray","value","camelCaseToKebabCase","str","kebab","i","char","lowercase","mount","el","to","container","setAttrs","attrs","attr","setAttr","clearAttrs","name","createStyle","id","css"],"mappings":"aAiBO,SAASA,EAAWC,EAAsB,CAC/C,OAAO,MAAM,QAAQA,CAAK,EAAIA,EAAQ,CAACA,CAAK,CAC9C,CAOO,SAASC,EAAqBC,EAAa,CAChD,IAAIC,EAAQ,GAEZ,QAASC,EAAI,EAAGA,EAAIF,EAAI,OAAQE,IAAK,CACnC,MAAMC,EAAOH,EAAIE,CAAC,EACZE,EAAYD,EAAK,YAAA,EAEnBD,IAAM,GAAKC,IAASC,IACtBH,GAAS,KAGXA,GAASG,CACX,CAEA,OAAOH,CACT,CCzBO,SAASI,EAAMC,EAAa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