Skip to content

Commit 8d581ff

Browse files
committed
refactor: replace hardcoded sentinel height with constant for better maintainability
1 parent 4fa194c commit 8d581ff

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

src/Shared/Hooks/useStickyEvent/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
export const FALLBACK_SENTINEL_HEIGHT = '2px'
17+
export const SENTINEL_HEIGHT_BUFFER = 2
18+
export const FALLBACK_SENTINEL_HEIGHT = `${SENTINEL_HEIGHT_BUFFER}px`
1819
export const OBSERVER_THRESHOLD = 1
1920
export const OBSERVER_ROOT_MARGIN = '1px'

src/Shared/Hooks/useStickyEvent/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { FALLBACK_SENTINEL_HEIGHT } from './constants'
17+
import { FALLBACK_SENTINEL_HEIGHT, SENTINEL_HEIGHT_BUFFER } from './constants'
1818
import { UseStickyEventReturnType } from './types'
1919

2020
export const getHeightForStickyElementTopOffset = <T extends HTMLElement>({
@@ -30,7 +30,7 @@ export const getHeightForStickyElementTopOffset = <T extends HTMLElement>({
3030
const doesTopOffsetContainCalc = calcRegex.test(topValue)
3131

3232
if (doesTopOffsetContainCalc) {
33-
return topValue.replace(calcRegex, (match) => `calc(${match} + 2px)`)
33+
return topValue.replace(calcRegex, (match) => `calc(${match} + ${SENTINEL_HEIGHT_BUFFER}px)`)
3434
}
3535

3636
return topValue.replace(/\d+(\.\d+)?/g, (match) => {
@@ -40,6 +40,6 @@ export const getHeightForStickyElementTopOffset = <T extends HTMLElement>({
4040
return FALLBACK_SENTINEL_HEIGHT
4141
}
4242

43-
return `${nMatch + 2}`
43+
return `${nMatch + SENTINEL_HEIGHT_BUFFER}`
4444
})
4545
}

0 commit comments

Comments
 (0)