UNPKG

framer-motion

Version:

A simple and powerful JavaScript animation library

1 lines 1.74 kB
{"version":3,"file":"use-motion-ref.mjs","sources":["../../../../src/motion/utils/use-motion-ref.ts"],"sourcesContent":["\"use client\"\n\nimport * as React from \"react\"\nimport { useCallback } from \"react\"\nimport type { VisualElement } from \"../../render/VisualElement\"\nimport { isRefObject } from \"../../utils/is-ref-object\"\nimport { VisualState } from \"./use-visual-state\"\n\n/**\n * Creates a ref function that, when called, hydrates the provided\n * external ref and VisualElement.\n */\nexport function useMotionRef<Instance, RenderState>(\n visualState: VisualState<Instance, RenderState>,\n visualElement?: VisualElement<Instance> | null,\n externalRef?: React.Ref<Instance>\n): React.Ref<Instance> {\n return useCallback(\n (instance: Instance) => {\n if (instance) {\n visualState.onMount && visualState.onMount(instance)\n }\n\n if (visualElement) {\n if (instance) {\n visualElement.mount(instance)\n } else {\n visualElement.unmount()\n }\n }\n\n if (externalRef) {\n if (typeof externalRef === \"function\") {\n externalRef(instance)\n } else if (isRefObject(externalRef)) {\n ;(externalRef as any).current = instance\n }\n }\n },\n /**\n * Include externalRef in dependencies to ensure the callback updates\n * when the ref changes, allowing proper ref forwarding.\n */\n [visualElement]\n )\n}\n"],"names":[],"mappings":";;;;AAQA;;;AAGG;;AAMC;;;;;;AAQgB;;;;;;;AAOJ;;;AAEO;AACD;;;;AAId;;;AAGG;;AAGX;;"}