Skip to content

Commit 0017c2e

Browse files
authored
Merge pull request #402 from vtfk/dialog-disable-scrolling
Dialog: disable scrolling when open
2 parents 86175ce + b8577ba commit 0017c2e

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/ui/Dialog/Dialog.stories.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,51 @@ export function Nested () {
113113
</>
114114
)
115115
}
116+
117+
export function PreventScrollingBehind () {
118+
const [dialogOpen, setIsDialogOpen] = useState(false)
119+
const [dialog2Open, setDialog2Open] = useState(false)
120+
121+
return (
122+
<>
123+
<div style={{ height: '200vh' }}>
124+
This is big div twice the height of the screen, it should not scroll when dialog is open
125+
<Button onClick={() => { setIsDialogOpen(!dialogOpen) }}>{`${!dialogOpen ? 'Åpne' : 'Lukk'} modal`}</Button>
126+
</div>
127+
<p />
128+
129+
<Dialog
130+
isOpen={dialogOpen}
131+
onDismiss={() => { setIsDialogOpen(false) }}
132+
showCloseButton
133+
width='60%'
134+
height='50%'
135+
>
136+
<DialogTitle>This is the first dialog</DialogTitle>
137+
<DialogBody>
138+
Click to open another dialog
139+
</DialogBody>
140+
<DialogActions>
141+
<Button size='small' onClick={() => setDialog2Open(true)}>Open nested dialog</Button>
142+
<Button size='small' type='secondary' onClick={() => { setIsDialogOpen(false) }}>Close</Button>
143+
</DialogActions>
144+
</Dialog>
145+
<Dialog
146+
isOpen={dialog2Open}
147+
onDismiss={() => setDialog2Open(false)}
148+
width='50%'
149+
height='30%'
150+
>
151+
<DialogTitle>
152+
This is the second nested dialog
153+
</DialogTitle>
154+
<DialogBody>
155+
Test
156+
</DialogBody>
157+
<DialogActions>
158+
<Button size='small' onClick={() => setDialog2Open(false)}>Close</Button>
159+
</DialogActions>
160+
</Dialog>
161+
</>
162+
)
163+
}

src/ui/Dialog/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { nanoid } from 'nanoid'
55
import { ReactComponent as CloseIcon } from './icon-close.svg'
66

77
import './styles.scss'
8+
import ScrollLock from 'react-scrolllock'
89

910
export function Dialog ({ isOpen, title, className, persistent, width, height, showCloseButton, onDismiss, onCloseBtnClick, onClickOutside, onPressEscape, style, ...props }) {
1011
// Set an unique ID for the dialog
@@ -74,17 +75,25 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
7475
// Render the component
7576
return (
7677
isOpen === true &&
77-
<>
78+
<ScrollLock isActive={isOpen}>
7879
<div id={`dialog-backdrop-${id}`} className={`dialog-backdrop ${className}`} onClick={(e) => handleBackdropClick(e)}>
79-
<div id={`dialog-${id}`} className='dialog' aria-label='dialog' aria-modal='true' role='dialog' style={parsedStyles}>
80+
<div
81+
id={`dialog-${id}`}
82+
className='dialog'
83+
aria-label='dialog'
84+
aria-modal='true'
85+
role='dialog'
86+
style={parsedStyles}
87+
onClick={(e) => { e.preventDefault(); e.stopPropagation(); }}
88+
>
8089
{!persistent && showCloseButton &&
8190
<button className='dialog-close-btn' onClick={(e) => { handleCloseBtnClick(); e.preventDefault() }} aria-label='Lukk'>
8291
<CloseIcon alt='' />
8392
</button>}
8493
{props.children}
8594
</div>
8695
</div>
87-
</>
96+
</ScrollLock>
8897
)
8998
}
9099

0 commit comments

Comments
 (0)