All files / src/render/html visual-element.ts

90.57% Statements 48/53
72.22% Branches 26/36
80% Functions 8/10
89.58% Lines 43/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 14534x     34x   34x 34x 34x 34x 34x 34x 34x 34x   34x 45x     34x               64x 19x 19x   45x 45x                           8x       14x     80x 80x                                               193x 193x 193x                 348x 348x 348x   348x         348x 16x 1x 16x 16x     348x 249x 249x           249x 249x     348x                   1611x           1611x                     34x  
import { visualElement } from ".."
import { HTMLRenderState } from "./types"
import { VisualElementConfig } from "../types"
import { checkTargetForNewValues, getOrigin } from "../utils/setters"
import { DOMVisualElementOptions } from "../dom/types"
import { buildHTMLStyles } from "./utils/build-styles"
import { isCSSVariable } from "../dom/utils/is-css-variable"
import { parseDomVariant } from "../dom/utils/parse-dom-variant"
import { isTransformProp } from "./utils/transform"
import { scrapeMotionValuesFromProps } from "./utils/scrape-motion-values"
import { renderHTML } from "./utils/render"
import { getDefaultValueType } from "../dom/value-types/defaults"
import { measureViewportBox } from "../../projection/utils/measure"
 
export function getComputedStyle(element: HTMLElement) {
    return window.getComputedStyle(element)
}
 
export const htmlConfig: VisualElementConfig<
    HTMLElement,
    HTMLRenderState,
    DOMVisualElementOptions
> = {
    treeType: "dom",
 
    readValueFromInstance(domElement, key) {
        if (isTransformProp(key)) {
            const defaultType = getDefaultValueType(key)
            return defaultType ? defaultType.default || 0 : 0
        } else {
            const computedStyle = getComputedStyle(domElement)
            return (
                (isCSSVariable(key)
                    ? computedStyle.getPropertyValue(key)
                    : computedStyle[key]) || 0
            )
        }
    },
 
    sortNodePosition(a, b) {
        /**
         * compareDocumentPosition returns a bitmask, by using the bitwise &
         * we're returning true if 2 in that bitmask is set to true. 2 is set
         * to true if b preceeds a.
         */
        return a.compareDocumentPosition(b) & 2 ? 1 : -1
    },
 
    getBaseTarget(props, key) {
        return props.style?.[key]
    },
 
    measureViewportBox(element, { transformPagePoint }) {
        return measureViewportBox(element, transformPagePoint)
    },
 
    /**
     * Reset the transform on the current Element. This is called as part
     * of a batched process across the entire layout tree. To remove this write
     * cycle it'd be interesting to see if it's possible to "undo" all the current
     * layout transforms up the tree in the same way this.getBoundingBoxWithoutTransforms
     * works
     */
    resetTransform(element, domElement, props) {
        const { transformTemplate } = props
        domElement.style.transform = transformTemplate
            ? transformTemplate({}, "")
            : "none"
 
        // Ensure that whatever happens next, we restore our transform on the next frame
        element.scheduleRender()
    },
 
    restoreTransform(instance, mutableState) {
        instance.style.transform = mutableState.style.transform as string
    },
 
    removeValueFromRenderState(key, { vars, style }) {
        delete vars[key]
        delete style[key]
    },
 
    /**
     * Ensure that HTML and Framer-specific value types like `px`->`%` and `Color`
     * can be animated by Motion.
     */
    makeTargetAnimatable(
        element,
        { transition, transitionEnd, ...target },
        { transformValues },
        IisMounted = true
    ) {
        let origin = getOrigin(target as any, transition || {}, element)
 
        /**
         * If Framer has provided a function to convert `Color` etc value types, convert them
         */
        if (transformValues) {
            if (transitionEnd)
                transitionEnd = transformValues(transitionEnd as any)
            Eif (target) target = transformValues(target as any)
            Eif (origin) origin = transformValues(origin as any)
        }
 
        if (isMounted) {
            checkTargetForNewValues(element, target, origin as any)
            const parsed = parseDomVariant(
                element,
                target,
                origin,
                transitionEnd
            )
            transitionEnd = parsed.transitionEnd
            target = parsed.target
        }
 
        return {
            transition,
            transitionEnd,
            ...target,
        }
    },
 
    scrapeMotionValuesFromProps,
 
    build(element, renderState, latestValues, options, props) {
        Iif (element.isVisible !== undefined) {
            renderState.style.visibility = element.isVisible
                ? "visible"
                : "hidden"
        }
 
        buildHTMLStyles(
            renderState,
            latestValues,
            options,
            props.transformTemplate
        )
    },
 
    render: renderHTML,
}
 
export const htmlVisualElement = visualElement(htmlConfig)