framer-motion
Version:
A simple and powerful JavaScript animation library
1 lines • 5.18 kB
Source Map (JSON)
{"version":3,"file":"use-visual-state.mjs","sources":["../../../../src/motion/utils/use-visual-state.ts"],"sourcesContent":["\"use client\"\n\nimport {\n AnyResolvedKeyframe,\n isAnimationControls,\n isControllingVariants as checkIsControllingVariants,\n isVariantNode as checkIsVariantNode,\n ResolvedValues,\n resolveVariantFromProps,\n} from \"motion-dom\"\nimport { useContext } from \"react\"\nimport { MotionContext, MotionContextProps } from \"../../context/MotionContext\"\nimport {\n PresenceContext,\n type PresenceContextProps,\n} from \"../../context/PresenceContext\"\nimport { ScrapeMotionValuesFromProps } from \"../../render/types\"\nimport { useConstant } from \"../../utils/use-constant\"\nimport { resolveMotionValue } from \"motion-dom\"\nimport { MotionProps } from \"../types\"\n\nexport interface VisualState<Instance, RenderState> {\n renderState: RenderState\n latestValues: ResolvedValues\n onMount?: (instance: Instance) => void\n}\n\nexport type UseVisualState<Instance, RenderState> = (\n props: MotionProps,\n isStatic: boolean\n) => VisualState<Instance, RenderState>\n\nexport interface UseVisualStateConfig<RenderState> {\n scrapeMotionValuesFromProps: ScrapeMotionValuesFromProps\n createRenderState: () => RenderState\n}\n\nfunction makeState<I, RS>(\n {\n scrapeMotionValuesFromProps,\n createRenderState,\n }: UseVisualStateConfig<RS>,\n props: MotionProps,\n context: MotionContextProps,\n presenceContext: PresenceContextProps | null\n) {\n const state: VisualState<I, RS> = {\n latestValues: makeLatestValues(\n props,\n context,\n presenceContext,\n scrapeMotionValuesFromProps\n ),\n renderState: createRenderState(),\n }\n\n return state\n}\n\nfunction makeLatestValues(\n props: MotionProps,\n context: MotionContextProps,\n presenceContext: PresenceContextProps | null,\n scrapeMotionValues: ScrapeMotionValuesFromProps\n) {\n const values: ResolvedValues = {}\n\n const motionValues = scrapeMotionValues(props, {})\n for (const key in motionValues) {\n values[key] = resolveMotionValue(motionValues[key])\n }\n\n let { initial, animate } = props\n const isControllingVariants = checkIsControllingVariants(props)\n const isVariantNode = checkIsVariantNode(props)\n\n if (\n context &&\n isVariantNode &&\n !isControllingVariants &&\n props.inherit !== false\n ) {\n if (initial === undefined) initial = context.initial\n if (animate === undefined) animate = context.animate\n }\n\n let isInitialAnimationBlocked = presenceContext\n ? presenceContext.initial === false\n : false\n isInitialAnimationBlocked = isInitialAnimationBlocked || initial === false\n\n const variantToSet = isInitialAnimationBlocked ? animate : initial\n\n if (\n variantToSet &&\n typeof variantToSet !== \"boolean\" &&\n !isAnimationControls(variantToSet)\n ) {\n const list = Array.isArray(variantToSet) ? variantToSet : [variantToSet]\n for (let i = 0; i < list.length; i++) {\n const resolved = resolveVariantFromProps(props, list[i] as any)\n if (resolved) {\n const { transitionEnd, transition, ...target } = resolved\n for (const key in target) {\n let valueTarget = target[key as keyof typeof target]\n\n if (Array.isArray(valueTarget)) {\n /**\n * Take final keyframe if the initial animation is blocked because\n * we want to initialise at the end of that blocked animation.\n */\n const index = isInitialAnimationBlocked\n ? valueTarget.length - 1\n : 0\n valueTarget = valueTarget[index] as any\n }\n\n if (valueTarget !== null) {\n values[key] = valueTarget as AnyResolvedKeyframe\n }\n }\n for (const key in transitionEnd) {\n values[key] = transitionEnd[\n key as keyof typeof transitionEnd\n ] as AnyResolvedKeyframe\n }\n }\n }\n }\n\n return values\n}\n\nexport const makeUseVisualState =\n <I, RS>(config: UseVisualStateConfig<RS>): UseVisualState<I, RS> =>\n (props: MotionProps, isStatic: boolean): VisualState<I, RS> => {\n const context = useContext(MotionContext)\n const presenceContext = useContext(PresenceContext)\n const make = () => makeState(config, props, context, presenceContext)\n\n return isStatic ? make() : useConstant(make)\n }\n"],"names":[],"mappings":";;;;;;;AAqCA;AASI;;;;AAUA;AACJ;AAEA;;;AASI;;;AAIA;AACA;AACA;AAEA;;AAGI;AACA;;AAE2B;;AACA;;;AAI3B;;AAEJ;;AAIA;;AAGI;AAEA;AACA;;;;AAIQ;AACI;AAEA;AACI;;;AAGG;;AAEC;;AAEJ;;AAGJ;AACI;;;AAGR;;;;;;AASZ;AACJ;AAEO;AAGC;AACA;AACA;AAEA;AACJ;;"}