Skip to content

Commit

Permalink
feat: dynamise useLockedBody anchor id
Browse files Browse the repository at this point in the history
  • Loading branch information
juliencrn committed Oct 13, 2022
1 parent 1c56f2a commit 04396e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/useLockedBody/useLockedBody.demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const fakeScrollableStyle: CSSProperties = {

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

const toggleLocked = () => {
setLocked(!locked)
Expand All @@ -39,7 +39,7 @@ export function App2() {
setLocked(!locked)
}

useLockedBody(locked)
useLockedBody(locked, 'root')

return (
<div style={fakeScrollableStyle}>
Expand Down
7 changes: 5 additions & 2 deletions src/useLockedBody/useLockedBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { useIsomorphicLayoutEffect } from '..'

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

function useLockedBody(initialLocked = false): UseLockedBodyOutput {
function useLockedBody(
initialLocked = false,
rootId = '___gatsby', // Default to `___gatsby` to not introduce breaking change
): UseLockedBodyOutput {
const [locked, setLocked] = useState(initialLocked)

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

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

// Avoid width reflow
Expand Down

0 comments on commit 04396e4

Please sign in to comment.