| 33 | : never |
| 34 | : never |
| 35 | export function useInView<TElement extends HTMLElement>( |
| 36 | props?: (() => UseSpringProps<any>) | IntersectionArgs, |
| 37 | args?: IntersectionArgs |
| 38 | ) { |
| 39 | const [isInView, setIsInView] = useState(false) |
| 40 | const ref = useRef<TElement>(undefined) |
| 41 | |
| 42 | const propsFn = is.fun(props) && props |
| 43 | |
| 44 | const springsProps = propsFn ? propsFn() : {} |
| 45 | const { to = {}, from = {}, ...restSpringProps } = springsProps |
| 46 | |
| 47 | const intersectionArguments = propsFn ? args : props |
| 48 | |
| 49 | const [springs, api] = useSpring(() => ({ from, ...restSpringProps }), []) |
| 50 | |
| 51 | useIsomorphicLayoutEffect(() => { |
| 52 | const element = ref.current |
| 53 | const { |
| 54 | root, |
| 55 | once, |
| 56 | amount = 'any', |
| 57 | ...restArgs |
| 58 | } = intersectionArguments ?? {} |
| 59 | |
| 60 | if ( |
| 61 | !element || |
| 62 | (once && isInView) || |
| 63 | typeof IntersectionObserver === 'undefined' |
| 64 | ) |
| 65 | return |
| 66 | |
| 67 | const activeIntersections = new WeakMap<Element, VoidFunction>() |
| 68 | |
| 69 | const onEnter = () => { |
| 70 | if (to) { |
| 71 | // @ts-expect-error – TODO: fix this type error |
| 72 | api.start(to) |
| 73 | } |
| 74 | |
| 75 | setIsInView(true) |
| 76 | |
| 77 | const cleanup = () => { |
| 78 | if (from) { |
| 79 | api.start(from) |
| 80 | } |
| 81 | setIsInView(false) |
| 82 | } |
| 83 | |
| 84 | return once ? undefined : cleanup |
| 85 | } |
| 86 | |
| 87 | const handleIntersection: IntersectionObserverCallback = entries => { |
| 88 | entries.forEach(entry => { |
| 89 | const onLeave = activeIntersections.get(entry.target) |
| 90 | |
| 91 | if (entry.isIntersecting === Boolean(onLeave)) { |
| 92 | return |