Skip to content

Commit 04396e4

Browse files
committed
feat: dynamise useLockedBody anchor id
1 parent 1c56f2a commit 04396e4

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/useLockedBody/useLockedBody.demo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const fakeScrollableStyle: CSSProperties = {
1616

1717
// Example 1: useLockedBody as useState()
1818
export default function App() {
19-
const [locked, setLocked] = useLockedBody()
19+
const [locked, setLocked] = useLockedBody(false, 'root')
2020

2121
const toggleLocked = () => {
2222
setLocked(!locked)
@@ -39,7 +39,7 @@ export function App2() {
3939
setLocked(!locked)
4040
}
4141

42-
useLockedBody(locked)
42+
useLockedBody(locked, 'root')
4343

4444
return (
4545
<div style={fakeScrollableStyle}>

src/useLockedBody/useLockedBody.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { useIsomorphicLayoutEffect } from '..'
44

55
type UseLockedBodyOutput = [boolean, (locked: boolean) => void]
66

7-
function useLockedBody(initialLocked = false): UseLockedBodyOutput {
7+
function useLockedBody(
8+
initialLocked = false,
9+
rootId = '___gatsby', // Default to `___gatsby` to not introduce breaking change
10+
): UseLockedBodyOutput {
811
const [locked, setLocked] = useState(initialLocked)
912

1013
// Do the side effect before render
@@ -21,7 +24,7 @@ function useLockedBody(initialLocked = false): UseLockedBodyOutput {
2124
document.body.style.overflow = 'hidden'
2225

2326
// Get the scrollBar width
24-
const root = document.getElementById('___gatsby') // or root
27+
const root = document.getElementById(rootId) // or root
2528
const scrollBarWidth = root ? root.offsetWidth - root.scrollWidth : 0
2629

2730
// Avoid width reflow

0 commit comments

Comments
 (0)