{"version":3,"file":"index.mjs","sources":["../../../../../src/motion/features/viewport/index.ts"],"sourcesContent":["import { Feature } from \"motion-dom\"\nimport { MotionProps } from \"../../types\"\nimport { observeIntersection } from \"./observers\"\n\nconst thresholdNames = {\n    some: 0,\n    all: 1,\n}\n\nexport class InViewFeature extends Feature<Element> {\n    private hasEnteredView = false\n\n    private isInView = false\n\n    private stopObserver?: () => void\n\n    private startObserver() {\n        this.stopObserver?.()\n\n        const { viewport = {} } = this.node.getProps()\n        const { root, margin: rootMargin, amount = \"some\", once } = viewport\n\n        const options = {\n            root: root ? root.current : undefined,\n            rootMargin,\n            threshold:\n                typeof amount === \"number\" ? amount : thresholdNames[amount],\n        }\n\n        const onIntersectionUpdate = (entry: IntersectionObserverEntry) => {\n            const { isIntersecting } = entry\n\n            /**\n             * If there's been no change in the viewport state, early return.\n             */\n            if (this.isInView === isIntersecting) return\n\n            this.isInView = isIntersecting\n\n            /**\n             * Handle hasEnteredView. If this is only meant to run once, and\n             * element isn't visible, early return. Otherwise set hasEnteredView to true.\n             */\n            if (once && !isIntersecting && this.hasEnteredView) {\n                return\n            } else if (isIntersecting) {\n                this.hasEnteredView = true\n            }\n\n            if (this.node.animationState) {\n                this.node.animationState.setActive(\n                    \"whileInView\",\n                    isIntersecting\n                )\n            }\n\n            /**\n             * Use the latest committed props rather than the ones in scope\n             * when this observer is created\n             */\n            const { onViewportEnter, onViewportLeave } = this.node.getProps()\n            const callback = isIntersecting ? onViewportEnter : onViewportLeave\n            callback && callback(entry)\n        }\n\n        this.stopObserver = observeIntersection(\n            this.node.current!,\n            options,\n            onIntersectionUpdate\n        )\n    }\n\n    mount() {\n        this.startObserver()\n    }\n\n    update() {\n        if (typeof IntersectionObserver === \"undefined\") return\n\n        const { props, prevProps } = this.node\n        const hasOptionsChanged = [\"amount\", \"margin\", \"root\"].some(\n            hasViewportOptionChanged(props, prevProps)\n        )\n\n        if (hasOptionsChanged) {\n            this.startObserver()\n        }\n    }\n\n    unmount() {\n        this.stopObserver?.()\n        this.hasEnteredView = false\n        this.isInView = false\n    }\n}\n\nfunction hasViewportOptionChanged(\n    { viewport = {} }: MotionProps,\n    { viewport: prevViewport = {} }: MotionProps = {}\n) {\n    return (name: keyof typeof viewport) =>\n        viewport[name] !== prevViewport[name]\n}\n"],"names":[],"mappings":";;;AAIA,MAAM,cAAc,GAAG;AACnB,IAAA,IAAI,EAAE,CAAC;AACP,IAAA,GAAG,EAAE,CAAC;CACT;AAEK,MAAO,aAAc,SAAQ,OAAgB,CAAA;AAAnD,IAAA,WAAA,GAAA;;QACY,IAAA,CAAA,cAAc,GAAG,KAAK;QAEtB,IAAA,CAAA,QAAQ,GAAG,KAAK;IAkF5B;IA9EY,aAAa,GAAA;AACjB,QAAA,IAAI,CAAC,YAAY,IAAI;AAErB,QAAA,MAAM,EAAE,QAAQ,GAAG,EAAE,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;AAC9C,QAAA,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ;AAEpE,QAAA,MAAM,OAAO,GAAG;YACZ,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC,OAAO,GAAG,SAAS;YACrC,UAAU;AACV,YAAA,SAAS,EACL,OAAO,MAAM,KAAK,QAAQ,GAAG,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC;SACnE;AAED,QAAA,MAAM,oBAAoB,GAAG,CAAC,KAAgC,KAAI;AAC9D,YAAA,MAAM,EAAE,cAAc,EAAE,GAAG,KAAK;AAEhC;;AAEG;AACH,YAAA,IAAI,IAAI,CAAC,QAAQ,KAAK,cAAc;gBAAE;AAEtC,YAAA,IAAI,CAAC,QAAQ,GAAG,cAAc;AAE9B;;;AAGG;YACH,IAAI,IAAI,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,cAAc,EAAE;gBAChD;YACJ;iBAAO,IAAI,cAAc,EAAE;AACvB,gBAAA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