-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️🚸Dialog: refactor to use native dialog (#2950)
* wip implement native dialog * update dialog tests * adding dialog ref, deprecate returnfocus * Improve useHideBodyScroll * some documentation fixes * close only with onclose also with escape key * change deprecation note
- Loading branch information
Showing
7 changed files
with
117 additions
and
64 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
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 |
---|---|---|
@@ -1,17 +1,39 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
type Styles = { | ||
overflow: string | ||
paddingRight: string | ||
} | ||
|
||
export const useHideBodyScroll = (active: boolean): void => { | ||
const overflowState = useRef<string | undefined>() | ||
const originalStyles = useRef<Styles | undefined>() | ||
useEffect(() => { | ||
if (typeof document === 'undefined') return | ||
const html = document.documentElement | ||
const { body } = document | ||
|
||
if (active) { | ||
overflowState.current = document.body.style.overflow | ||
document.body.style.overflow = 'hidden' | ||
} else { | ||
document.body.style.overflow = overflowState.current | ||
const scrollBarWidth = window.innerWidth - html.clientWidth | ||
const bodyPaddingRight = | ||
parseInt( | ||
window.getComputedStyle(body).getPropertyValue('padding-right'), | ||
) || 0 | ||
const oldStyle: Styles = { | ||
overflow: body.style.overflow, | ||
paddingRight: body.style.paddingRight, | ||
} | ||
originalStyles.current = oldStyle | ||
|
||
body.style.overflow = 'hidden' | ||
body.style.paddingRight = `${bodyPaddingRight + scrollBarWidth}px` | ||
} else if (originalStyles.current) { | ||
body.style.overflow = originalStyles.current.overflow | ||
body.style.paddingRight = originalStyles.current.paddingRight | ||
} | ||
const originalState = overflowState.current | ||
const originalState = originalStyles.current | ||
return () => { | ||
document.body.style.overflow = originalState | ||
body.style.overflow = originalState?.overflow | ||
body.style.paddingRight = originalState?.paddingRight | ||
} | ||
}, [active]) | ||
} |