chore: provide default options

This commit is contained in:
tbitw2549 2024-06-14 19:44:13 +03:00
parent dbdb3fe6a1
commit 9cec31ea49
2 changed files with 4 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { useNuxtApp } from './nuxt'
import defu from 'defu'
export function toArray<T> (value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
@ -8,7 +8,7 @@ export function toArray<T> (value: T | T[]): T[] {
export type CallbackFn = () => void
export type ObserveFn = (element: Element, callback: CallbackFn) => () => void
export function useIntersectionObserver (options?: IntersectionObserverInit): { observe: ObserveFn } {
export function useIntersectionObserver (options?: Partial<IntersectionObserverInit>): { observe: ObserveFn } {
if (import.meta.server) { return {observe: () => () => {}} }
const nuxtApp = useNuxtApp()
@ -17,7 +17,6 @@ export function useIntersectionObserver (options?: IntersectionObserverInit): {
}
let observer: IntersectionObserver | null = null
const callbacks = new Map<Element, CallbackFn>()
const observe: ObserveFn = (element, callback) => {
@ -28,7 +27,7 @@ export function useIntersectionObserver (options?: IntersectionObserverInit): {
const isVisible = entry.isIntersecting || entry.intersectionRatio > 0
if (isVisible && callback) { callback() }
}
},options)
},defu(options ?? {},{root: null, rootMargin: "0px", threshold: 0}))
}
callbacks.set(element, callback)
observer.observe(element)

View File

@ -37,7 +37,7 @@ export const createLazyIOClientPage = (componentLoader: Component) => {
if (!isIntersecting.value) {
onMounted(() => {
const observer = useIntersectionObserver(attrs.loader ?? {})
const observer = useIntersectionObserver(attrs.loader)
unobserve = observer!.observe(el.value as Element, () => {
isIntersecting.value = true
unobserve?.()