Skip to content

Commit 132fdbb

Browse files
committed
Added 'rootEl' oprional property to next/Image component resembling 'root' option of the Intersection Observer API
1 parent 5d2730b commit 132fdbb

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/next/client/image.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ export type ImageProps = Omit<
101101
quality?: number | string
102102
priority?: boolean
103103
loading?: LoadingValue
104+
//added 'rootEl' property of type HTMLElement, nullable for being able to set default value to null (see Below)
105+
rootEl?: HTMLElement | null
104106
lazyBoundary?: string
105107
placeholder?: PlaceholderValue
106108
blurDataURL?: string
@@ -315,6 +317,7 @@ export default function Image({
315317
unoptimized = false,
316318
priority = false,
317319
loading,
320+
rootEl = null, //setting to null for the Intersection Observer API to set html root element as 'root' by default
318321
lazyBoundary = '200px',
319322
className,
320323
quality,
@@ -505,6 +508,8 @@ export default function Image({
505508
}
506509

507510
const [setRef, isIntersected] = useIntersection<HTMLImageElement>({
511+
//added 'root' to Options
512+
root: rootEl,
508513
rootMargin: lazyBoundary,
509514
disabled: !isLazy,
510515
})

packages/next/client/use-intersection.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
cancelIdleCallback,
55
} from './request-idle-callback'
66

7-
type UseIntersectionObserverInit = Pick<IntersectionObserverInit, 'rootMargin'>
7+
type UseIntersectionObserverInit = Pick<
8+
IntersectionObserverInit,
9+
'rootMargin' | 'root'
10+
> //added 'root' as a property
11+
812
type UseIntersection = { disabled?: boolean } & UseIntersectionObserverInit
913
type ObserveCallback = (isVisible: boolean) => void
1014
type Observer = {
@@ -16,6 +20,8 @@ type Observer = {
1620
const hasIntersectionObserver = typeof IntersectionObserver !== 'undefined'
1721

1822
export function useIntersection<T extends Element>({
23+
//added 'root' as an option
24+
root,
1925
rootMargin,
2026
disabled,
2127
}: UseIntersection): [(element: T | null) => void, boolean] {
@@ -37,11 +43,11 @@ export function useIntersection<T extends Element>({
3743
unobserve.current = observe(
3844
el,
3945
(isVisible) => isVisible && setVisible(isVisible),
40-
{ rootMargin }
46+
{ root, rootMargin } //added 'root' as an option
4147
)
4248
}
4349
},
44-
[isDisabled, rootMargin, visible]
50+
[isDisabled, root, rootMargin, visible] //added 'root' as a dependency
4551
)
4652

4753
useEffect(() => {

0 commit comments

Comments
 (0)