UNPKG

framer-motion

Version:

A simple and powerful JavaScript animation library

1 lines 1.31 kB
{"version":3,"file":"use-in-view.mjs","sources":["../../../src/utils/use-in-view.ts"],"sourcesContent":["\"use client\"\n\nimport { RefObject, useEffect, useState } from \"react\"\nimport { inView, InViewOptions } from \"../render/dom/viewport\"\n\nexport interface UseInViewOptions\n extends Omit<InViewOptions, \"root\" | \"amount\"> {\n root?: RefObject<Element | null>\n once?: boolean\n amount?: \"some\" | \"all\" | number\n initial?: boolean\n}\n\nexport function useInView(\n ref: RefObject<Element | null>,\n {\n root,\n margin,\n amount,\n once = false,\n initial = false,\n }: UseInViewOptions = {}\n) {\n const [isInView, setInView] = useState(initial)\n\n useEffect(() => {\n if (!ref.current || (once && isInView)) return\n\n const onEnter = () => {\n setInView(true)\n\n return once ? undefined : () => setInView(false)\n }\n\n const options: InViewOptions = {\n root: (root && root.current) || undefined,\n margin,\n amount,\n }\n\n return inView(ref.current, onEnter, options)\n }, [root, ref, margin, once, amount])\n\n return isInView\n}\n"],"names":[],"mappings":";;;;AAaM;;;;;;;AAkBM;AACJ;AAEA;;;;;;AAOJ;AAEA;AACJ;;"}