1
1
import { useRef , useEffect } from 'react' ;
2
+ import Platform from 'react-native-web/dist/cjs/exports/Platform' ;
2
3
3
4
const computeScrollbarWidth = ( ) => {
4
5
const scrollDiv = document . createElement ( 'div' ) ;
@@ -14,7 +15,11 @@ const computeScrollbarWidth = () => {
14
15
return scrollbarWidth ;
15
16
} ;
16
17
17
- export default function useModalEffects ( { modalRef, active } ) {
18
+ export default function useScrollbarEffects ( { modalRef, active } ) {
19
+ if ( Platform . OS !== 'web' ) {
20
+ return ;
21
+ }
22
+
18
23
const scrollbarWidth = useRef ( ) ;
19
24
20
25
useEffect ( ( ) => {
@@ -36,24 +41,21 @@ export default function useModalEffects({ modalRef, active }) {
36
41
const rect = document . body . getBoundingClientRect ( ) ;
37
42
const isBodyOverflowing = rect . left + rect . right < window . innerWidth ;
38
43
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
+ ] ;
42
49
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
+ ) ;
49
53
50
54
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
+ } ) ;
57
59
}
58
60
59
61
const isModalOverflowing =
@@ -75,12 +77,10 @@ export default function useModalEffects({ modalRef, active }) {
75
77
76
78
return ( ) => {
77
79
// 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
+ } ) ;
84
84
85
85
// Remove class .modal-open from body element.
86
86
document . body . classList . remove ( 'modal-open' ) ;
0 commit comments