UNPKG

framer-motion

Version:

A simple and powerful JavaScript animation library

1 lines • 1.95 kB
{"version":3,"file":"use-scope-motion.mjs","sources":["../../../../src/animation/hooks/use-scope-motion.ts"],"sourcesContent":["\"use client\"\n\nimport { scopeMotion } from \"motion-dom\"\nimport type { MotionScope, ScopeOptions, ScopeSetup } from \"motion-dom\"\nimport { DependencyList, RefObject, useRef } from \"react\"\nimport { useIsomorphicLayoutEffect } from \"../../utils/use-isomorphic-effect\"\n\nexport interface UseScopeMotionOptions extends ScopeOptions {\n /**\n * Cancel the scope and run setup again when any of these change.\n */\n deps?: DependencyList\n}\n\n/**\n * Run `setup` in a `scopeMotion()` scope rooted at `ref`, so selectors\n * resolve inside it. The scope is cancelled on unmount, or when `deps`\n * change, which stops everything started in it and restores inline styles.\n *\n * ```jsx\n * const ref = useRef(null)\n * useScopeMotion(ref, () => {\n * inView(\"li\", (el) => animate(el, { opacity: 1 }))\n * })\n * return <ul ref={ref}>...</ul>\n * ```\n *\n * Returns a ref to the current scope, for `run()` and `bind()` in event\n * handlers.\n *\n * @public\n */\nexport function useScopeMotion(\n ref: RefObject<Element | null> | null | undefined,\n setup: ScopeSetup,\n { deps = [], ...options }: UseScopeMotionOptions = {}\n): RefObject<MotionScope | null> {\n const scope = useRef<MotionScope | null>(null)\n const latestSetup = useRef(setup)\n latestSetup.current = setup\n\n useIsomorphicLayoutEffect(() => {\n const current = (scope.current = scopeMotion(\n ref?.current || undefined,\n (s) => latestSetup.current(s),\n options\n ))\n\n return () => {\n current.cancel()\n if (scope.current === current) scope.current = null\n }\n }, deps)\n\n return scope\n}\n"],"names":[],"mappings":";;;;;AAcA;;;;;;;;;;;;;;;;;AAiBG;AACG;AAKF;AACA;AACA;;AAGI;AAMA;;AAEI;AAA+B;AACnC;;AAGJ;AACJ;;"}