-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Modal & Input updates for marketing site (#478)
- Loading branch information
Showing
13 changed files
with
1,397 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Adapted from https://usehooks-ts.com/react-hook/use-locked-body | ||
// Modified to calculate scrollbar width via document.documentElement if no | ||
// rootId is provided, and to be sure to measure scrollbar width before | ||
// overflow is set to hidden (and thus removing the scrollbar). | ||
import { useEffect, useState } from 'react' | ||
|
||
import { useIsomorphicLayoutEffect } from 'usehooks-ts' | ||
|
||
import canUseDOM from '../utils/canUseDOM' | ||
|
||
type UseLockedBodyOutput = [boolean, (locked: boolean) => void] | ||
|
||
function useLockedBody( | ||
initialLocked = false, | ||
rootId: string = undefined | ||
): UseLockedBodyOutput { | ||
const [locked, setLocked] = useState(initialLocked) | ||
|
||
// Do the side effect before render | ||
useIsomorphicLayoutEffect(() => { | ||
if (!locked) { | ||
return | ||
} | ||
|
||
// Save initial body style | ||
const originalOverflow = document.body.style.overflow | ||
const originalPaddingRight = document.body.style.paddingRight | ||
|
||
// Get the scrollBar width | ||
let scrollBarWidth = 0 | ||
|
||
if (canUseDOM) { | ||
if (rootId) { | ||
const root = document.getElementById(rootId) | ||
|
||
scrollBarWidth = root ? root.offsetWidth - root.scrollWidth : 0 | ||
} else { | ||
scrollBarWidth = | ||
window.innerWidth - document.documentElement.clientWidth | ||
} | ||
} | ||
|
||
// Lock body scroll | ||
document.body.style.overflow = 'hidden' | ||
|
||
// Avoid width reflow | ||
if (scrollBarWidth) { | ||
document.body.style.paddingRight = `${scrollBarWidth}px` | ||
} | ||
|
||
return () => { | ||
document.body.style.overflow = originalOverflow | ||
|
||
if (scrollBarWidth) { | ||
document.body.style.paddingRight = originalPaddingRight | ||
} | ||
} | ||
}, [locked]) | ||
|
||
// Update state if initialValue changes | ||
useEffect(() => { | ||
if (locked !== initialLocked) { | ||
setLocked(initialLocked) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [initialLocked]) | ||
|
||
return [locked, setLocked] | ||
} | ||
|
||
export default useLockedBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.