diff --git a/js/src/modal.js b/js/src/modal.js index 87c22943a..ea79420c8 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -461,21 +461,11 @@ class Modal extends BaseComponent { // Adjust fixed content padding SelectorEngine.find(SELECTOR_FIXED_CONTENT) - .forEach(element => { - const actualPadding = element.style.paddingRight - const calculatedPadding = window.getComputedStyle(element)['padding-right'] - Manipulator.setDataAttribute(element, 'padding-right', actualPadding) - element.style.paddingRight = `${Number.parseFloat(calculatedPadding) + this._scrollbarWidth}px` - }) + .forEach(element => this._setElementAttributes(element, 'paddingRight')) // Adjust sticky content margin SelectorEngine.find(SELECTOR_STICKY_CONTENT) - .forEach(element => { - const actualMargin = element.style.marginRight - const calculatedMargin = window.getComputedStyle(element)['margin-right'] - Manipulator.setDataAttribute(element, 'margin-right', actualMargin) - element.style.marginRight = `${Number.parseFloat(calculatedMargin) - this._scrollbarWidth}px` - }) + .forEach(element => this._setElementAttributes(element, 'marginRight')) // Adjust body padding const actualPadding = document.body.style.paddingRight @@ -491,23 +481,11 @@ class Modal extends BaseComponent { _resetScrollbar() { // Restore fixed content padding SelectorEngine.find(SELECTOR_FIXED_CONTENT) - .forEach(element => { - const padding = Manipulator.getDataAttribute(element, 'padding-right') - if (typeof padding !== 'undefined') { - Manipulator.removeDataAttribute(element, 'padding-right') - element.style.paddingRight = padding - } - }) + .forEach(element => this._removeElementAttributes(element, 'paddingRight')) // Restore sticky content and navbar-toggler margin SelectorEngine.find(`${SELECTOR_STICKY_CONTENT}`) - .forEach(element => { - const margin = Manipulator.getDataAttribute(element, 'margin-right') - if (typeof margin !== 'undefined') { - Manipulator.removeDataAttribute(element, 'margin-right') - element.style.marginRight = margin - } - }) + .forEach(element => this._removeElementAttributes(element, 'marginRight')) // Restore body padding const padding = Manipulator.getDataAttribute(document.body, 'padding-right') @@ -528,6 +506,29 @@ class Modal extends BaseComponent { return scrollbarWidth } + _setElementAttributes(element, cssProp) { + if (window.innerWidth > element.clientWidth + this._scrollbarWidth) { + return + } + + const actualValue = element.style[cssProp] + const computedValue = window.getComputedStyle(element)[cssProp] + + Manipulator.setDataAttribute(element, cssProp, actualValue) + + element.style[cssProp] = cssProp === 'marginRight' ? + `${Number.parseFloat(computedValue) - this._scrollbarWidth}px` : + `${Number.parseFloat(computedValue) + this._scrollbarWidth}px` + } + + _removeElementAttributes(element, cssProp) { + const cssValue = Manipulator.getDataAttribute(element, cssProp) + if (typeof cssValue !== 'undefined') { + Manipulator.removeDataAttribute(element, cssProp) + element.style[cssProp] = cssValue + } + } + // Static static jQueryInterface(config, relatedTarget) { diff --git a/js/tests/unit/modal.spec.js b/js/tests/unit/modal.spec.js index f645e9892..ca76439eb 100644 --- a/js/tests/unit/modal.spec.js +++ b/js/tests/unit/modal.spec.js @@ -142,6 +142,30 @@ describe('Modal', () => { modal.toggle() }) + it('should not adjust the inline margin and padding of sticky and fixed elements when element do not have full width', done => { + fixtureEl.innerHTML = [ + '
', + '