Skip to content

Commit

Permalink
feat(use-is-visible): added "once" option
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocherg1n committed Nov 8, 2024
1 parent 828cf9f commit 8dc9123
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/use-is-visible/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {useEffect, useState} from 'react'
/** Tracks the intersection of an element with its parent or document's viewport. */
export function useIsVisible(
element: Element,
options: IntersectionObserverInit = {threshold: 1}
options: IntersectionObserverInit & {once: boolean} = {
threshold: 1,
once: false
}
): boolean {
const [isVisible, setIsVisible] = useState(false)

Expand All @@ -23,6 +26,10 @@ export function useIsVisible(
thresholds.some((threshold) => entry.intersectionRatio >= threshold)

setIsVisible(isIntersecting)

if (isIntersecting && options.once) {
observer.disconnect()
}
})
},
options
Expand Down

0 comments on commit 8dc9123

Please sign in to comment.