Skip to content

Commit 7caa257

Browse files
committed
chore: rearrange things for re-use within the react SDK
1 parent 7dd3ef8 commit 7caa257

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { MouseEventHandler, useEffect, useMemo, useRef } from 'react'
2+
import { isNull } from '../../utils/type-utils'
3+
4+
/**
5+
* VisibilityAndClickTracker is an internal component,
6+
* its API might change without warning and without being signalled as a breaking change
7+
*
8+
*/
9+
export function VisibilityAndClickTracker({
10+
children,
11+
onIntersect,
12+
onClick,
13+
trackView,
14+
options,
15+
...props
16+
}: {
17+
children: React.ReactNode
18+
onIntersect: (entry: IntersectionObserverEntry) => void
19+
onClick?: MouseEventHandler<HTMLDivElement>
20+
trackView: boolean
21+
options?: IntersectionObserverInit
22+
}): JSX.Element {
23+
const ref = useRef<HTMLDivElement>(null)
24+
25+
const observerOptions = useMemo(
26+
() => ({
27+
threshold: 0.1,
28+
...options,
29+
}),
30+
// eslint-disable-next-line react-hooks/exhaustive-deps
31+
[options?.threshold, options?.root, options?.rootMargin]
32+
)
33+
34+
useEffect(() => {
35+
if (isNull(ref.current) || !trackView) return
36+
37+
// eslint-disable-next-line compat/compat
38+
const observer = new IntersectionObserver(([entry]) => onIntersect(entry), observerOptions)
39+
observer.observe(ref.current)
40+
return () => observer.disconnect()
41+
}, [observerOptions, trackView, onIntersect])
42+
43+
return (
44+
<div ref={ref} {...props} onClick={onClick}>
45+
{children}
46+
</div>
47+
)
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { MouseEventHandler, useEffect, useMemo, useRef } from 'react'
2+
import { isNull } from '../../utils/type-utils'
3+
4+
/**
5+
* VisibilityAndClickTracker is an internal component,
6+
* its API might change without warning and without being signalled as a breaking change
7+
*
8+
*/
9+
export function VisibilityAndClickTracker({
10+
children,
11+
onIntersect,
12+
onClick,
13+
trackView,
14+
options,
15+
...props
16+
}: {
17+
children: React.ReactNode
18+
onIntersect: (entry: IntersectionObserverEntry) => void
19+
onClick?: MouseEventHandler<HTMLDivElement>
20+
trackView: boolean
21+
options?: IntersectionObserverInit
22+
}): JSX.Element {
23+
const ref = useRef<HTMLDivElement>(null)
24+
25+
const observerOptions = useMemo(
26+
() => ({
27+
threshold: 0.1,
28+
...options,
29+
}),
30+
// eslint-disable-next-line react-hooks/exhaustive-deps
31+
[options?.threshold, options?.root, options?.rootMargin]
32+
)
33+
34+
useEffect(() => {
35+
if (isNull(ref.current) || !trackView) return
36+
37+
// eslint-disable-next-line compat/compat
38+
const observer = new IntersectionObserver(([entry]) => onIntersect(entry), observerOptions)
39+
observer.observe(ref.current)
40+
return () => observer.disconnect()
41+
}, [observerOptions, trackView, onIntersect])
42+
43+
return (
44+
<div ref={ref} {...props} onClick={onClick}>
45+
{children}
46+
</div>
47+
)
48+
}

0 commit comments

Comments
 (0)