Skip to content

Commit da2be09

Browse files
authored
Merge pull request #192 from motiondivision/feat/use-inview
update useInView to accept root as MaybeRef
2 parents 6f71920 + 267c116 commit da2be09

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

packages/motion/src/utils/use-in-view.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
import type { Ref } from 'vue'
21
import { ref, unref, watchEffect } from 'vue'
32
import type { Options } from '@/types/state'
43
import { inView } from 'framer-motion/dom'
5-
import type { MaybeRef } from '@vueuse/core'
4+
import { type MaybeComputedElementRef, type MaybeRef, unrefElement } from '@vueuse/core'
65

7-
export function useInView<T extends Element = any>(
8-
domRef: Ref<T | null>,
9-
options?: MaybeRef<Options['inViewOptions']>,
6+
type InViewOptions = Options['inViewOptions']
7+
export interface UseInViewOptions extends Omit<InViewOptions, 'root'> {
8+
root?: MaybeRef<Element | Document>
9+
}
10+
11+
export function useInView(
12+
domRef: MaybeComputedElementRef,
13+
options?: MaybeRef<UseInViewOptions>,
1014
) {
1115
const isInView = ref(false)
1216

1317
watchEffect((onCleanup) => {
1418
const realOptions = unref(options) || {}
1519
const { once } = realOptions
16-
if (!domRef.value || (once && isInView.value)) {
20+
const el = unrefElement(domRef)
21+
if (!el || (once && isInView.value)) {
1722
return
1823
}
1924
const onEnter = () => {
@@ -24,7 +29,10 @@ export function useInView<T extends Element = any>(
2429
isInView.value = false
2530
}
2631
}
27-
const cleanup = inView(domRef.value, onEnter, realOptions)
32+
const cleanup = inView(el, onEnter, {
33+
...realOptions,
34+
root: unref(realOptions.root),
35+
})
2836
onCleanup(() => {
2937
cleanup()
3038
})

0 commit comments

Comments
 (0)