11import { useRef , useEffect } from 'react' ;
2+ import Platform from 'react-native-web/dist/cjs/exports/Platform' ;
23
34const computeScrollbarWidth = ( ) => {
45 const scrollDiv = document . createElement ( 'div' ) ;
@@ -14,7 +15,11 @@ const computeScrollbarWidth = () => {
1415 return scrollbarWidth ;
1516} ;
1617
17- export default function useModalEffects ( { modalRef, active } ) {
18+ export default function useScrollbarEffects ( { modalRef, active } ) {
19+ if ( Platform . OS !== 'web' ) {
20+ return ;
21+ }
22+
1823 const scrollbarWidth = useRef ( ) ;
1924
2025 useEffect ( ( ) => {
@@ -36,24 +41,21 @@ export default function useModalEffects({ modalRef, active }) {
3641 const rect = document . body . getBoundingClientRect ( ) ;
3742 const isBodyOverflowing = rect . left + rect . right < window . innerWidth ;
3843
39- // Set body padding adjustments.
40- const contentElement = document . getElementById ( 'content' ) ;
41- const navbarElement = document . getElementsByClassName ( 'navbar' ) [ 0 ] ;
44+ // Set body and fixed elements padding adjustments.
45+ const elements = [
46+ document . body ,
47+ ...document . querySelectorAll ( '[data-fixed="true"]' ) ,
48+ ] ;
4249
43- const originalContentPadding = contentElement
44- ? contentElement . style . paddingRight
45- : '' ;
46- const originalNavbarPadding = navbarElement
47- ? navbarElement . style . paddingRight
48- : '' ;
50+ const originalBodyPadding = elements . map (
51+ ( element ) => element . style . paddingRight || '' ,
52+ ) ;
4953
5054 if ( isBodyOverflowing ) {
51- if ( contentElement ) {
52- contentElement . style . paddingRight = `${ scrollbarWidth . current } px` ;
53- }
54- if ( navbarElement ) {
55- navbarElement . style . paddingRight = `${ scrollbarWidth . current } px` ;
56- }
55+ elements . forEach ( ( element ) => {
56+ // eslint-disable-next-line no-param-reassign
57+ element . style . paddingRight = `${ scrollbarWidth . current } px` ;
58+ } ) ;
5759 }
5860
5961 const isModalOverflowing =
@@ -75,12 +77,10 @@ export default function useModalEffects({ modalRef, active }) {
7577
7678 return ( ) => {
7779 // Reset body padding adjustments.
78- if ( contentElement ) {
79- contentElement . style . paddingRight = originalContentPadding ;
80- }
81- if ( navbarElement ) {
82- navbarElement . style . paddingRight = originalNavbarPadding ;
83- }
80+ elements . forEach ( ( element , key ) => {
81+ // eslint-disable-next-line no-param-reassign
82+ element . style . paddingRight = originalBodyPadding [ key ] || '' ;
83+ } ) ;
8484
8585 // Remove class .modal-open from body element.
8686 document . body . classList . remove ( 'modal-open' ) ;
0 commit comments