Skip to content

Commit

Permalink
add check if element is not full-width, according to twbs#30621
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Feb 24, 2021
1 parent dc1d8bc commit 8288e9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion js/src/util/scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const SELECTOR_STICKY_CONTENT = '.sticky-top'

const getWidth = () => {
// https://muffinman.io/blog/get-scrollbar-width-in-javascript/
const documentWidth = document.documentElement.clientWidth
const documentWidth = Math.max(document.documentElement.clientWidth, document.body.clientWidth)
// const documentWidth = document.body.getBoundingClientRect().width
return Math.abs(window.innerWidth - documentWidth)
}
Expand All @@ -26,8 +26,13 @@ const hide = (width = getWidth()) => {
}

const _setElementAttributes = (selector, styleProp, callback) => {
const scrollbarWidth = getWidth()
SelectorEngine.find(selector)
.forEach(element => {
if (element !== document.body && window.innerWidth > element.clientWidth + scrollbarWidth) {
return
}

const actualValue = element.style[styleProp]
const calculatedValue = window.getComputedStyle(element)[styleProp]
Manipulator.setDataAttribute(element, styleProp, actualValue)
Expand Down
20 changes: 20 additions & 0 deletions js/tests/unit/util/scrollbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,25 @@ describe('ScrollBar', () => {
expect(currentMargin).toEqual(originalMargin, 'sticky element margin should be reset after closing')
done()
})

it('should not adjust the inline margin and padding of sticky and fixed elements when element do not have full width', () => {
fixtureEl.innerHTML = [
'<div class="sticky-top" style="margin-right: 0px; padding-right: 0px; width: calc(100vw - 50%)"></div>'
].join('')

const stickyTopEl = fixtureEl.querySelector('.sticky-top')
const originalMargin = Number.parseInt(window.getComputedStyle(stickyTopEl).marginRight, 10)
const originalPadding = Number.parseInt(window.getComputedStyle(stickyTopEl).paddingRight, 10)

Scrollbar.hide()

const currentMargin = Number.parseInt(window.getComputedStyle(stickyTopEl).marginRight, 10)
const currentPadding = Number.parseInt(window.getComputedStyle(stickyTopEl).paddingRight, 10)

expect(currentMargin).toEqual(originalMargin, 'sticky element\'s margin should not be adjusted while opening')
expect(currentPadding).toEqual(originalPadding, 'sticky element\'s padding should not be adjusted while opening')

Scrollbar.reset()
})
})
})

0 comments on commit 8288e9a

Please sign in to comment.