mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-07 09:22:27 +00:00
chore: provide default options
This commit is contained in:
parent
dbdb3fe6a1
commit
9cec31ea49
@ -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)
|
||||
|
@ -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?.()
|
||||
|
Loading…
Reference in New Issue
Block a user