Skip to content

Commit

Permalink
fix: do not adjust padding right when body has overflow-y: scroll (#2664
Browse files Browse the repository at this point in the history
)
  • Loading branch information
limonte committed Aug 18, 2023
1 parent 101e1e2 commit 4d86677
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 18 additions & 0 deletions cypress/e2e/scrollbar.cy.js
Expand Up @@ -64,6 +64,24 @@ describe('Vertical scrollbar', () => {
Swal.close()
})

it('should not adjust body padding if overflow-y: scroll is set on body', () => {
const talltDiv = document.createElement('div')
talltDiv.innerHTML = Array(100).join('<div>lorem ipsum</div>')
document.body.appendChild(talltDiv)
document.body.style.overflowY = 'scroll'
document.body.style.paddingRight = '30px'

SwalWithoutAnimation.fire({
title: 'no padding right adjustment when overflow-y: scroll is set on body',
didClose: () => {
document.body.removeChild(talltDiv)
},
})

const bodyStyles = window.getComputedStyle(document.body)
expect(bodyStyles.paddingRight).to.equal('30px')
})

it('should be restored before a toast is fired after a modal', (done) => {
const talltDiv = document.createElement('div')
talltDiv.innerHTML = Array(100).join('<div>lorem ipsum</div>')
Expand Down
7 changes: 6 additions & 1 deletion src/utils/scrollbarFix.js
Expand Up @@ -7,14 +7,19 @@ import { measureScrollbar } from './dom/measureScrollbar.js'
let previousBodyPadding = null

export const fixScrollbar = () => {
const bodyStyles = window.getComputedStyle(document.body)
// https://github.com/sweetalert2/sweetalert2/issues/2663
if (bodyStyles.overflowY === 'scroll') {
return
}
// for queues, do not do this more than once
if (previousBodyPadding !== null) {
return
}
// if the body has overflow
if (document.body.scrollHeight > window.innerHeight) {
// add padding so the content doesn't shift after removal of scrollbar
previousBodyPadding = parseInt(window.getComputedStyle(document.body).getPropertyValue('padding-right'))
previousBodyPadding = parseInt(bodyStyles.getPropertyValue('padding-right'))
document.body.style.paddingRight = `${previousBodyPadding + measureScrollbar()}px`
}
}
Expand Down

0 comments on commit 4d86677

Please sign in to comment.