All files / src/motion/features/layout MeasureLayout.tsx

84.52% Statements 71/84
49.15% Branches 29/59
81.82% Functions 9/11
85.51% Lines 59/69

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 16933x 33x 33x 33x       33x 33x 33x 33x 33x                   33x               35x 35x 35x 35x   35x   35x 35x   35x       35x 35x     35x   37x       35x     37x 37x 37x   37x                 37x   37x         37x         37x                                 37x     33x 37x 37x 37x 37x 37x         33x   35x 35x 35x 35x 35x   35x 35x 35x 35x         33x 74x 74x     33x 148x   33x   33x 148x 148x   148x                     33x                                
import sync from "framesync"
import React, { useContext } from "react"
import { usePresence } from "../../../components/AnimatePresence/use-presence"
import {
    LayoutGroupContext,
    LayoutGroupContextProps,
} from "../../../context/LayoutGroupContext"
import { SwitchLayoutGroupContext } from "../../../context/SwitchLayoutGroupContext"
import { globalProjectionState } from "../../../projection/node/create-projection-node"
import { correctBorderRadius } from "../../../projection/styles/scale-border-radius"
import { correctBoxShadow } from "../../../projection/styles/scale-box-shadow"
import { addScaleCorrector } from "../../../projection/styles/scale-correction"
import { FeatureProps } from "../types"
 
interface MeasureContextProps {
    layoutGroup?: LayoutGroupContextProps
    switchLayoutGroup?: SwitchLayoutGroupContext
    isPresent: boolean
    safeToRemove?: VoidFunction | null
}
 
class MeasureLayoutWithContext extends React.Component<
    FeatureProps & MeasureContextProps
> {
    /**
     * This only mounts projection nodes for components that
     * need measuring, we might want to do it for all components
     * in order to incorporate transforms
     */
    componentDidMount() {
        const { visualElement, layoutGroup, switchLayoutGroup, layoutId } =
            this.props
        const { projection } = visualElement
 
        addScaleCorrector(defaultScaleCorrectors)
 
        Eif (projection) {
            Iif (layoutGroup?.group) layoutGroup.group.add(projection)
 
            Iif (switchLayoutGroup?.register && layoutId) {
                switchLayoutGroup.register(projection)
            }
 
            projection.root!.didUpdate()
            projection.addEventListener("animationComplete", () => {
                this.safeToRemove()
            })
            projection.setOptions({
                ...projection.options,
                onExitComplete: () => this.safeToRemove(),
            })
        }
 
        globalProjectionState.hasEverUpdated = true
    }
 
    getSnapshotBeforeUpdate(prevProps: FeatureProps & MeasureContextProps) {
        const { layoutDependency, visualElement, drag, isPresent } = this.props
        const projection = visualElement.projection
 
        Iif (!projection) return null
 
        /**
         * TODO: We use this data in relegate to determine whether to
         * promote a previous element. There's no guarantee its presence data
         * will have updated by this point - if a bug like this arises it will
         * have to be that we markForRelegation and then find a new lead some other way,
         * perhaps in didUpdate
         */
        projection.isPresent = isPresent
 
        Eif (
            drag ||
            prevProps.layoutDependency !== layoutDependency ||
            layoutDependency === undefined
        ) {
            projection.willUpdate()
        } else {
            this.safeToRemove()
        }
 
        Iif (prevProps.isPresent !== isPresent) {
            if (isPresent) {
                projection.promote()
            } else if (!projection.relegate()) {
                /**
                 * If there's another stack member taking over from this one,
                 * it's in charge of the exit animation and therefore should
                 * be in charge of the safe to remove. Otherwise we call it here.
                 */
                sync.postRender(() => {
                    if (!projection.getStack()?.members.length) {
                        this.safeToRemove()
                    }
                })
            }
        }
 
        return null
    }
 
    componentDidUpdate() {
        const { projection } = this.props.visualElement
        Eif (projection) {
            projection.root!.didUpdate()
            Eif (!projection.currentAnimation && projection.isLead()) {
                this.safeToRemove()
            }
        }
    }
 
    componentWillUnmount() {
        const {
            visualElement,
            layoutGroup,
            switchLayoutGroup: promoteContext,
        } = this.props
        const { projection } = visualElement
 
        Eif (projection) {
            projection.scheduleCheckAfterUnmount()
            Iif (layoutGroup?.group) layoutGroup.group.remove(projection)
            Iif (promoteContext?.deregister)
                promoteContext.deregister(projection)
        }
    }
 
    safeToRemove() {
        const { safeToRemove } = this.props
        safeToRemove?.()
    }
 
    render() {
        return null
    }
}
 
export function MeasureLayout(props: FeatureProps) {
    const [isPresent, safeToRemove] = usePresence()
    const layoutGroup = useContext(LayoutGroupContext)
 
    return (
        <MeasureLayoutWithContext
            {...props}
            layoutGroup={layoutGroup}
            switchLayoutGroup={useContext(SwitchLayoutGroupContext)}
            isPresent={isPresent}
            safeToRemove={safeToRemove}
        />
    )
}
 
const defaultScaleCorrectors = {
    borderRadius: {
        ...correctBorderRadius,
        applyTo: [
            "borderTopLeftRadius",
            "borderTopRightRadius",
            "borderBottomLeftRadius",
            "borderBottomRightRadius",
        ],
    },
    borderTopLeftRadius: correctBorderRadius,
    borderTopRightRadius: correctBorderRadius,
    borderBottomLeftRadius: correctBorderRadius,
    borderBottomRightRadius: correctBorderRadius,
    boxShadow: correctBoxShadow,
}