New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Remove dom-helpers dependency #14877
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { assert } from 'chai'; | ||
import getScrollbarSize from 'dom-helpers/util/scrollbarSize'; | ||
import getScrollbarSize from '../utils/getScrollbarSize'; | ||
import ModalManager from './ModalManager'; | ||
|
||
describe('ModalManager', () => { | ||
|
@@ -9,6 +9,15 @@ describe('ModalManager', () => { | |
before(() => { | ||
modalManager = new ModalManager(); | ||
container1 = document.createElement('div'); | ||
container1.style.padding = '20px'; | ||
Object.defineProperty(container1, 'scrollHeight', { | ||
value: 100, | ||
writable: false, | ||
}); | ||
Object.defineProperty(container1, 'clientHeight', { | ||
value: 90, | ||
writable: false, | ||
}); | ||
document.body.appendChild(container1); | ||
}); | ||
|
||
|
@@ -113,15 +122,22 @@ describe('ModalManager', () => { | |
it('should handle the scroll', () => { | ||
const modal = {}; | ||
const paddingRightBefore = container1.style.paddingRight; | ||
const paddingFixedRightBefore = fixedNode.style.paddingRight; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this just refactoring or did the test actually break? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is a breaking change after all or only breaking in jsdom? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that it's only breaking jsdom. |
||
modalManager.add(modal, container1); | ||
modalManager.mount(modal); | ||
assert.strictEqual(container1.style.overflow, 'hidden'); | ||
assert.strictEqual(container1.style.paddingRight, `${getScrollbarSize()}px`); | ||
assert.strictEqual(fixedNode.style.paddingRight, `${14 + getScrollbarSize()}px`); | ||
assert.strictEqual( | ||
container1.style.paddingRight, | ||
`${parseInt(paddingRightBefore, 10) + getScrollbarSize()}px`, | ||
); | ||
assert.strictEqual( | ||
fixedNode.style.paddingRight, | ||
`${parseInt(paddingFixedRightBefore, 10) + getScrollbarSize()}px`, | ||
); | ||
modalManager.remove(modal); | ||
assert.strictEqual(container1.style.overflow, ''); | ||
assert.strictEqual(container1.style.paddingRight, paddingRightBefore); | ||
assert.strictEqual(fixedNode.style.paddingRight, '14px'); | ||
assert.strictEqual(fixedNode.style.paddingRight, paddingFixedRightBefore); | ||
}); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,18 @@ | ||
import isWindow from 'dom-helpers/query/isWindow'; | ||
import ownerDocument from '../utils/ownerDocument'; | ||
import ownerWindow from '../utils/ownerWindow'; | ||
|
||
export function isBody(node) { | ||
return node && node.tagName.toLowerCase() === 'body'; | ||
} | ||
|
||
// Do we have a vertical scroll bar? | ||
// Do we have a vertical scrollbar? | ||
export default function isOverflowing(container) { | ||
const doc = ownerDocument(container); | ||
const win = ownerWindow(doc); | ||
|
||
/* istanbul ignore next */ | ||
if (!isWindow(doc) && !isBody(container)) { | ||
return container.scrollHeight > container.clientHeight; | ||
if (doc.body === container) { | ||
return win.innerWidth > doc.documentElement.clientWidth; | ||
} | ||
|
||
// Takes in account potential non zero margin on the body. | ||
const style = win.getComputedStyle(doc.body); | ||
const marginLeft = parseInt(style.getPropertyValue('margin-left'), 10); | ||
const marginRight = parseInt(style.getPropertyValue('margin-right'), 10); | ||
|
||
return marginLeft + doc.body.clientWidth + marginRight < win.innerWidth; | ||
return container.scrollHeight > container.clientHeight; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// A change of the browser zoom change the scrollbar size. | ||
// Credit https://github.com/twbs/bootstrap/blob/3ffe3a5d82f6f561b82ff78d82b32a7d14aed558/js/src/modal.js#L512-L519 | ||
function getScrollbarSize() { | ||
eps1lon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const scrollDiv = document.createElement('div'); | ||
scrollDiv.style.width = '99px'; | ||
scrollDiv.style.height = '99px'; | ||
scrollDiv.style.position = 'absolute'; | ||
scrollDiv.style.top = '-9999px'; | ||
scrollDiv.style.overflow = 'scroll'; | ||
|
||
document.body.appendChild(scrollDiv); | ||
const scrollbarSize = scrollDiv.offsetWidth - scrollDiv.clientWidth; | ||
document.body.removeChild(scrollDiv); | ||
|
||
return scrollbarSize; | ||
} | ||
|
||
export default getScrollbarSize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not really prevent writing. All it does is throw an error. I would rathersinon.stub
this with a fake setter:sinon.stub(container1, 'scrollHeight').get(() => 100).set(() => {})
writable: false
will actually not trigger an error. Would've been nice if sinon would support this out of the box.