1- import type { Ref } from 'vue'
21import { ref , unref , watchEffect } from 'vue'
32import type { Options } from '@/types/state'
43import { 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