{"version":3,"file":"use-animated-state.mjs","sources":["../../../../src/animation/hooks/use-animated-state.ts"],"sourcesContent":["\"use client\"\n\nimport {\n    animateVisualElement,\n    createBox,\n    ResolvedValues,\n    TargetAndTransition,\n    VisualElement,\n} from \"motion-dom\"\nimport { useLayoutEffect, useState } from \"react\"\nimport { makeUseVisualState } from \"../../motion/utils/use-visual-state\"\nimport { useConstant } from \"../../utils/use-constant\"\n\ninterface AnimatedStateOptions {\n    initialState: ResolvedValues\n}\n\nconst createObject = () => ({})\n\nclass StateVisualElement extends VisualElement<\n    ResolvedValues,\n    {},\n    AnimatedStateOptions\n> {\n    type: \"state\"\n    build() {}\n    measureInstanceViewportBox = createBox\n    resetTransform() {}\n    restoreTransform() {}\n    removeValueFromRenderState() {}\n    renderInstance() {}\n    scrapeMotionValuesFromProps() {\n        return createObject()\n    }\n    getBaseTargetFromProps() {\n        return undefined\n    }\n\n    readValueFromInstance(\n        _state: ResolvedValues,\n        key: string,\n        options: AnimatedStateOptions\n    ) {\n        return options.initialState[key] || 0\n    }\n\n    sortInstanceNodePosition() {\n        return 0\n    }\n}\n\nconst useVisualState = makeUseVisualState({\n    scrapeMotionValuesFromProps: createObject,\n    createRenderState: createObject,\n})\n\n/**\n * This is not an officially supported API and may be removed\n * on any version.\n */\nexport function useAnimatedState(initialState: any) {\n    const [animationState, setAnimationState] = useState(initialState)\n    const visualState = useVisualState({}, false)\n\n    const element = useConstant(() => {\n        return new StateVisualElement(\n            {\n                props: {\n                    onUpdate: (v) => {\n                        setAnimationState({ ...v })\n                    },\n                },\n                visualState,\n                presenceContext: null,\n            },\n            { initialState }\n        )\n    })\n\n    useLayoutEffect(() => {\n        element.mount({})\n        return () => element.unmount()\n    }, [element])\n\n    const startAnimation = useConstant(\n        () => (animationDefinition: TargetAndTransition) => {\n            return animateVisualElement(element, animationDefinition)\n        }\n    )\n\n    return [animationState, startAnimation]\n}\n"],"names":[],"mappings":";;;;;;AAiBA;AAEA;AAAA;;;;AAMI;AAEA;AACA;AACA;AACA;;;;;AAKI;;AAGJ;;;;AASI;;AAEP;AAED;AACI;AACA;AACH;AAED;;;AAGG;AACG;;;AAIF;;AAGY;AACI;AACI;;AAEP;;AAED;AACH;AAGT;;AAGI;AACA;AACJ;;AAIQ;AACJ;AAGJ;AACJ;;"}