{"version":3,"file":"index.mjs","sources":["../../../../src/effects/svg/index.ts"],"sourcesContent":["import { frame } from \"../../frameloop\"\nimport { isCSSVar } from \"../../render/dom/is-css-var\"\nimport { camelToDash } from \"../../render/dom/utils/camel-to-dash\"\nimport { cssStyleProperties } from \"../../render/svg/utils/build-attrs\"\nimport { transformProps } from \"../../render/utils/keys-transform\"\nimport { isSVGElement } from \"../../utils/is-svg-element\"\nimport { MotionValue } from \"../../value\"\nimport { numberValueTypes } from \"../../value/types/maps/number\"\nimport { addAttrValue } from \"../attr\"\nimport { MotionValueState } from \"../MotionValueState\"\nimport { addStyleValue, originProps, readStyleValue } from \"../style\"\nimport { createSelectorEffect } from \"../utils/create-dom-effect\"\nimport { createEffect } from \"../utils/create-effect\"\n\nfunction addSVGPathValue(\n    element: SVGElement,\n    state: MotionValueState,\n    key: string,\n    value: MotionValue\n) {\n    frame.render(() => element.setAttribute(\"pathLength\", \"1\"))\n\n    if (key === \"pathOffset\") {\n        return state.set(key, value, () => {\n            // Use unitless value to avoid Safari zoom bug\n            const offset = value.get()\n            element.setAttribute(\"stroke-dashoffset\", `${-offset}`)\n        })\n    } else {\n        if (!state.get(\"stroke-dasharray\")) {\n            state.set(\"stroke-dasharray\", new MotionValue(\"1 1\"), () => {\n                const pathLength = state.get(\"pathLength\")?.get() ?? 1\n                const pathSpacing = state.get(\"pathSpacing\")?.get()\n\n                // Use unitless values to avoid Safari zoom bug\n                element.setAttribute(\n                    \"stroke-dasharray\",\n                    `${pathLength} ${pathSpacing ?? 1 - Number(pathLength)}`\n                )\n            })\n        }\n\n        return state.set(key, value, undefined, state.get(\"stroke-dasharray\"))\n    }\n}\n\nexport const addSVGValue = (\n    element: SVGElement,\n    state: MotionValueState,\n    key: string,\n    value: MotionValue\n) => {\n    if (key.startsWith(\"path\") && !transformProps.has(key)) {\n        return addSVGPathValue(element, state, key, value)\n    } else if (key.startsWith(\"attr\")) {\n        return addAttrValue(element, state, key, value, convertAttrKey(key))\n    }\n\n    /**\n     * Transforms and origins aren't CSS properties (bar `x`/`y` in some\n     * browsers), so check them by name rather than `key in element.style`.\n     */\n    const handler =\n        transformProps.has(key) ||\n        originProps.has(key) ||\n        isCSSVar(key) ||\n        key in element.style\n            ? addStyleValue\n            : addAttrValue\n    return handler(element, state, key, value)\n}\n\n/**\n * Reads the current value of `key` from an SVG element as the origin of\n * an animation, as the SVG VisualElement does: transforms start from\n * their defaults, CSS variables and the few CSS-only properties come from\n * computed style, falling back to the attribute when that's empty (e.g.\n * the element isn't rendered), and everything else is read from the\n * attribute, dash-cased (`strokeWidth` -> `stroke-width`) or, failing\n * that, as written (`baseFrequency`).\n */\nexport const readSVGValue = (element: SVGElement, key: string) => {\n    if (transformProps.has(key)) {\n        return numberValueTypes[key]?.default || 0\n    }\n\n    if (isCSSVar(key) || cssStyleProperties.includes(key)) {\n        return (\n            readStyleValue(element, key) ||\n            element.getAttribute(camelToDash(key)) ||\n            0\n        )\n    }\n\n    key = convertAttrKey(key)\n\n    return (\n        element.getAttribute(camelToDash(key)) ??\n        element.getAttribute(key) ??\n        undefined\n    )\n}\n\n/**\n * The per-element effect `animate()` binds through, so SVG values share\n * state with a direct `svgEffect()` call.\n */\nexport const svgSubjectEffect = /*@__PURE__*/ createEffect(addSVGValue, {\n    test: isSVGElement,\n    read: readSVGValue,\n})\n\nexport const svgEffect = /*@__PURE__*/ createSelectorEffect(svgSubjectEffect)\n\nfunction convertAttrKey(key: string) {\n    return key.replace(/^attr([A-Z])/, (_, firstChar) =>\n        firstChar.toLowerCase()\n    )\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AAcA,SAAS,eAAe,CACpB,OAAmB,EACnB,KAAuB,EACvB,GAAW,EACX,KAAkB,EAAA;AAElB,IAAA,KAAK,CAAC,MAAM,CAAC,MAAM,OAAO,CAAC,YAAY,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;AAE3D,IAAA,IAAI,GAAG,KAAK,YAAY,EAAE;QACtB,OAAO,KAAK,CAAC,GAAG,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