Skip to content

Commit f0681ad

Browse files
committed
Update modal scrollbar effects
1 parent 6bc29a1 commit f0681ad

File tree

3 files changed

+24
-30
lines changed

3 files changed

+24
-30
lines changed

src/components/modal/useModal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useRef, useEffect, useMemo } from 'react';
22
import invariant from 'fbjs/lib/invariant';
33
import useIdentifier from '../../hooks/useIdentifier';
4-
import useModalEffects from './useModalEffects';
4+
import useScrollbarEffects from './useScrollbarEffects';
55

66
export default function useModal(visible, setVisible) {
77
const identifier = useIdentifier('modal');
@@ -14,7 +14,7 @@ export default function useModal(visible, setVisible) {
1414
setMounted(true);
1515
}, []);
1616

17-
useModalEffects({
17+
useScrollbarEffects({
1818
modalRef: ref,
1919
active: mounted && visible,
2020
});

src/components/modal/useModalEffects.js renamed to src/components/modal/useScrollbarEffects.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useRef, useEffect } from 'react';
2+
import Platform from 'react-native-web/dist/cjs/exports/Platform';
23

34
const 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');

src/components/navbar/Navbar.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const propTypes = {
1313
children: PropTypes.node.isRequired,
1414
variant: PropTypes.oneOf(['light', 'dark']),
1515
expand: PropTypes.oneOf([true, 'sm', 'md', 'lg', 'xl']),
16-
fixed: PropTypes.oneOf(['top', 'bottom']),
17-
sticky: PropTypes.oneOf(['top']),
1816
defaultExpanded: PropTypes.bool,
1917
expanded: PropTypes.bool,
2018
onToggle: PropTypes.func,
@@ -24,8 +22,6 @@ const Navbar = React.forwardRef((props, ref) => {
2422
const {
2523
variant = 'light',
2624
expand,
27-
fixed,
28-
sticky,
2925
defaultExpanded = false,
3026
expanded,
3127
onToggle = () => {},
@@ -40,8 +36,6 @@ const Navbar = React.forwardRef((props, ref) => {
4036
`navbar-${variant}`,
4137
// variable classes
4238
expand && expand === true ? 'navbar-expand' : `navbar-expand-${expand}`,
43-
fixed && `fixed-${fixed}`,
44-
sticky && `sticky-${sticky}`,
4539
);
4640

4741
return (

0 commit comments

Comments
 (0)