@@ -13,29 +13,31 @@ import './styles.scss'
13
13
*/
14
14
const useStickyEvent = < T extends HTMLElement = HTMLDivElement > ( {
15
15
callback,
16
- containerClassName ,
16
+ containerSelector ,
17
17
containerRef,
18
- isStickyElementMounted,
19
18
identifier,
19
+ topOffset,
20
+ isStickyElementMounted = true ,
20
21
} : UseStickyEventProps < T > ) => {
21
22
const stickyElementRef = useRef < T > ( null )
22
23
23
24
useEffect (
24
25
( ) => {
25
- if ( ! stickyElementRef . current || ( ! isNullOrUndefined ( isStickyElementMounted ) && ! isStickyElementMounted ) ) {
26
+ if ( ! stickyElementRef . current || ! isStickyElementMounted ) {
26
27
return noop
27
28
}
28
29
29
30
const stickyElementParent = containerRef
30
31
? containerRef . current
31
- : Array . from ( document . getElementsByClassName ( containerClassName ) ) . find ( ( element ) =>
32
+ : Array . from ( document . querySelectorAll ( containerSelector ) ) . find ( ( element ) =>
32
33
element . contains ( stickyElementRef . current ) ,
33
34
)
34
35
35
36
if ( ! stickyElementParent ) {
36
37
return noop
37
38
}
38
39
40
+ // NOTE: these values are being used as closures
39
41
let previousStuckState : boolean
40
42
let hasSentinelLeftView : boolean
41
43
let hasHeaderLeftView : boolean
@@ -75,14 +77,15 @@ const useStickyEvent = <T extends HTMLElement = HTMLDivElement>({
75
77
// The sentinel element's height must exceed the sticky element's top CSS value.
76
78
// This guarantees that when the sticky element sticks to the container's edge,
77
79
// 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 )
86
89
87
90
stickyElementRef . current . appendChild ( sentinelElement )
88
91
0 commit comments