Skip to content

Commit fd5f7d5

Browse files
committed
disable body scoll on modal,tests
1 parent d7380a7 commit fd5f7d5

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

packages/react/src/SelectPanel/SelectPanel.module.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
}
227227

228228
.Backdrop {
229-
position: absolute;
229+
position: fixed;
230230
inset: 0;
231231
background-color: var(--overlay-backdrop-bgColor);
232232
}

packages/react/src/SelectPanel/SelectPanel.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,30 @@ for (const usingRemoveActiveDescendant of [false, true]) {
11931193
expect(screen.getByRole('button', {name: 'Save'})).toBeInTheDocument()
11941194
expect(screen.getByRole('button', {name: 'Cancel'})).toBeInTheDocument()
11951195
})
1196+
1197+
it('locks body scroll when modal is open', async () => {
1198+
const user = userEvent.setup()
1199+
1200+
renderWithProp(<BasicSelectPanel variant="modal" onCancel={() => {}} />, usingRemoveActiveDescendant)
1201+
1202+
expect(document.body.style.overflow).not.toBe('hidden')
1203+
1204+
await user.click(screen.getByText('Select items'))
1205+
1206+
expect(document.body.style.overflow).toBe('hidden')
1207+
})
1208+
1209+
it('restores body scroll when modal is closed', async () => {
1210+
const user = userEvent.setup()
1211+
1212+
renderWithProp(<BasicSelectPanel variant="modal" onCancel={() => {}} />, usingRemoveActiveDescendant)
1213+
1214+
await user.click(screen.getByText('Select items'))
1215+
expect(document.body.style.overflow).toBe('hidden')
1216+
1217+
await user.click(screen.getByRole('button', {name: 'Cancel'}))
1218+
expect(document.body.style.overflow).not.toBe('hidden')
1219+
})
11961220
})
11971221

11981222
describe('sorting', () => {

packages/react/src/SelectPanel/SelectPanel.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ function Panel({
362362
[items, itemsInViewSet, onSelectedChange, selected],
363363
)
364364

365-
// disable body scroll when the panel is open on narrow screens
365+
// disable body scroll when the panel is open in modal mode or on narrow screens
366366
useEffect(() => {
367-
if (open && isNarrowScreenSize && usingFullScreenOnNarrow) {
367+
if (open && (variant === 'modal' || (isNarrowScreenSize && usingFullScreenOnNarrow))) {
368368
const bodyOverflowStyle = document.body.style.overflow || ''
369369
// If the body is already set to overflow: hidden, it likely means
370370
// that there is already a modal open. In that case, we should bail
@@ -379,7 +379,7 @@ function Panel({
379379
document.body.style.overflow = bodyOverflowStyle
380380
}
381381
}
382-
}, [isNarrowScreenSize, open, usingFullScreenOnNarrow])
382+
}, [isNarrowScreenSize, open, usingFullScreenOnNarrow, variant])
383383

384384
useEffect(() => {
385385
if (open) {

0 commit comments

Comments
 (0)