Skip to content

Commit 81e575a

Browse files
committed
fix: review comments
1 parent aa82357 commit 81e575a

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/Shared/Hooks/useStickyEvent/types.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ export type UseStickyEventProps<T extends HTMLElement = HTMLDivElement> = {
1919
* - If the sticky element is always rendered, this flag can be ignored.
2020
*/
2121
isStickyElementMounted?: boolean
22+
/**
23+
* The top offset value of the sticky element.
24+
* This can be a CSS value such as '10px', '1rem', or 'calc(100% + 10px)'.
25+
*
26+
* If the top value is specified in 'px' or 'rem', this value can be ignored.
27+
* Use this only if the top value is specified in percentage or uses 'calc'.
28+
*/
29+
topOffset?: string
2230
} & (
2331
| {
2432
/**
2533
* Reference to the scroll container element that contains the sticky element
2634
*
27-
* Either the reference can be passed or its class name
35+
* Either the reference can be passed or its querySelector
2836
*/
2937
containerRef: MutableRefObject<T>
30-
containerClassName?: never
38+
containerSelector?: never
3139
}
32-
| { containerClassName: string; containerRef?: never }
40+
| { containerSelector: string; containerRef?: never }
3341
)

src/Shared/Hooks/useStickyEvent/useStickyEvent.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,31 @@ import './styles.scss'
1313
*/
1414
const useStickyEvent = <T extends HTMLElement = HTMLDivElement>({
1515
callback,
16-
containerClassName,
16+
containerSelector,
1717
containerRef,
18-
isStickyElementMounted,
1918
identifier,
19+
topOffset,
20+
isStickyElementMounted = true,
2021
}: UseStickyEventProps<T>) => {
2122
const stickyElementRef = useRef<T>(null)
2223

2324
useEffect(
2425
() => {
25-
if (!stickyElementRef.current || (!isNullOrUndefined(isStickyElementMounted) && !isStickyElementMounted)) {
26+
if (!stickyElementRef.current || !isStickyElementMounted) {
2627
return noop
2728
}
2829

2930
const stickyElementParent = containerRef
3031
? containerRef.current
31-
: Array.from(document.getElementsByClassName(containerClassName)).find((element) =>
32+
: Array.from(document.querySelectorAll(containerSelector)).find((element) =>
3233
element.contains(stickyElementRef.current),
3334
)
3435

3536
if (!stickyElementParent) {
3637
return noop
3738
}
3839

40+
// NOTE: these values are being used as closures
3941
let previousStuckState: boolean
4042
let hasSentinelLeftView: boolean
4143
let hasHeaderLeftView: boolean
@@ -75,14 +77,15 @@ const useStickyEvent = <T extends HTMLElement = HTMLDivElement>({
7577
// The sentinel element's height must exceed the sticky element's top CSS value.
7678
// This guarantees that when the sticky element sticks to the container's edge,
7779
// the sentinel element will extend beyond the scroll container.
78-
sentinelElement.style.height =
79-
window.getComputedStyle(stickyElementRef.current).top?.replace(/[0-9]+/g, (match) => {
80-
const nMatch = Number(match)
81-
if (Number.isNaN(nMatch)) {
82-
return FALLBACK_SENTINEL_HEIGHT
83-
}
84-
return `${nMatch + 1}`
85-
}) ?? FALLBACK_SENTINEL_HEIGHT
80+
sentinelElement.style.height = topOffset
81+
? `calc(${topOffset} + 1px)`
82+
: (window.getComputedStyle(stickyElementRef.current).top?.replace(/[0-9]+/g, (match) => {
83+
const nMatch = Number(match)
84+
if (Number.isNaN(nMatch)) {
85+
return FALLBACK_SENTINEL_HEIGHT
86+
}
87+
return `${nMatch + 1}`
88+
}) ?? FALLBACK_SENTINEL_HEIGHT)
8689

8790
stickyElementRef.current.appendChild(sentinelElement)
8891

0 commit comments

Comments
 (0)