diff --git a/js/src/modal.js b/js/src/modal.js index ba7454640..8dac75265 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -61,7 +61,6 @@ const CLASS_NAME_OPEN = 'modal-open' const CLASS_NAME_FADE = 'fade' const CLASS_NAME_SHOW = 'show' const CLASS_NAME_STATIC = 'modal-static' -const CLASS_NAME_BACKDROP = 'modal-backdrop' const SELECTOR_DIALOG = '.modal-dialog' const SELECTOR_MODAL_BODY = '.modal-body' @@ -203,8 +202,7 @@ class Modal extends BaseComponent { _initializeBackDrop() { return new Backdrop({ isVisible: Boolean(this._config.backdrop), // 'static' option will be translated to true, and booleans will keep their value - isAnimated: this._isAnimated(), - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: this._isAnimated() }) } diff --git a/js/src/offcanvas.js b/js/src/offcanvas.js index bac355249..016260437 100644 --- a/js/src/offcanvas.js +++ b/js/src/offcanvas.js @@ -46,7 +46,6 @@ const DefaultType = { const CLASS_NAME_SHOW = 'show' const CLASS_NAME_BACKDROP = 'offcanvas-backdrop' - const OPEN_SELECTOR = '.offcanvas.show' const EVENT_SHOW = `show${EVENT_KEY}` @@ -179,10 +178,10 @@ class Offcanvas extends BaseComponent { _initializeBackDrop() { return new Backdrop({ + className: CLASS_NAME_BACKDROP, isVisible: this._config.backdrop, isAnimated: true, rootElement: this._element.parentNode, - backdropClassName: CLASS_NAME_BACKDROP, clickCallback: () => this.hide() }) } diff --git a/js/src/util/backdrop.js b/js/src/util/backdrop.js index 63c539f5a..fbe32445e 100644 --- a/js/src/util/backdrop.js +++ b/js/src/util/backdrop.js @@ -9,6 +9,7 @@ import EventHandler from '../dom/event-handler' import { execute, executeAfterTransition, getElement, reflow, typeCheckConfig } from './index' const Default = { + className: 'modal-backdrop', isVisible: true, // if false, we use the backdrop helper without adding any element to the dom isAnimated: false, rootElement: 'body', // give the choice to place backdrop under different elements @@ -16,6 +17,7 @@ const Default = { } const DefaultType = { + className: 'string', isVisible: 'boolean', isAnimated: 'boolean', rootElement: '(element|string)', @@ -72,7 +74,7 @@ class Backdrop { _getElement() { if (!this._element) { const backdrop = document.createElement('div') - backdrop.className = this._config.backdropClassName + backdrop.className = this._config.className if (this._config.isAnimated) { backdrop.classList.add(CLASS_NAME_FADE) } diff --git a/js/tests/unit/util/backdrop.spec.js b/js/tests/unit/util/backdrop.spec.js index e11dad42d..59b789071 100644 --- a/js/tests/unit/util/backdrop.spec.js +++ b/js/tests/unit/util/backdrop.spec.js @@ -3,7 +3,6 @@ import { getTransitionDurationFromElement } from '../../../src/util/index' import { clearFixture, getFixture } from '../../helpers/fixture' const CLASS_BACKDROP = '.modal-backdrop' -const CLASS_NAME_BACKDROP = 'modal-backdrop' const CLASS_NAME_FADE = 'fade' const CLASS_NAME_SHOW = 'show' @@ -27,8 +26,7 @@ describe('Backdrop', () => { it('if it is "shown", should append the backdrop html once, on show, and contain "show" class', done => { const instance = new Backdrop({ isVisible: true, - isAnimated: false, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: false }) const getElements = () => document.querySelectorAll(CLASS_BACKDROP) @@ -47,8 +45,7 @@ describe('Backdrop', () => { it('if it is not "shown", should not append the backdrop html', done => { const instance = new Backdrop({ isVisible: false, - isAnimated: true, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: true }) const getElements = () => document.querySelectorAll(CLASS_BACKDROP) @@ -62,8 +59,7 @@ describe('Backdrop', () => { it('if it is "shown" and "animated", should append the backdrop html once, and contain "fade" class', done => { const instance = new Backdrop({ isVisible: true, - isAnimated: true, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: true }) const getElements = () => document.querySelectorAll(CLASS_BACKDROP) @@ -83,8 +79,7 @@ describe('Backdrop', () => { it('should remove the backdrop html', done => { const instance = new Backdrop({ isVisible: true, - isAnimated: true, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: true }) const getElements = () => document.body.querySelectorAll(CLASS_BACKDROP) @@ -102,8 +97,7 @@ describe('Backdrop', () => { it('should remove "show" class', done => { const instance = new Backdrop({ isVisible: true, - isAnimated: true, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: true }) const elem = instance._getElement() @@ -117,8 +111,7 @@ describe('Backdrop', () => { it('if it is not "shown", should not try to remove Node on remove method', done => { const instance = new Backdrop({ isVisible: false, - isAnimated: true, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: true }) const getElements = () => document.querySelectorAll(CLASS_BACKDROP) const spy = spyOn(instance, 'dispose').and.callThrough() @@ -142,8 +135,7 @@ describe('Backdrop', () => { const instance = new Backdrop({ isVisible: true, isAnimated: true, - rootElement: wrapper, - backdropClassName: CLASS_NAME_BACKDROP + rootElement: wrapper }) const getElements = () => document.querySelectorAll(CLASS_BACKDROP) @@ -165,7 +157,6 @@ describe('Backdrop', () => { const instance = new Backdrop({ isVisible: true, isAnimated: false, - backdropClassName: CLASS_NAME_BACKDROP, clickCallback: () => spy() }) const endTest = () => { @@ -188,8 +179,7 @@ describe('Backdrop', () => { it('if it is animated, should show and hide backdrop after counting transition duration', done => { const instance = new Backdrop({ isVisible: true, - isAnimated: true, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: true }) const spy2 = jasmine.createSpy('spy2') @@ -212,8 +202,7 @@ describe('Backdrop', () => { const spy = jasmine.createSpy('spy', getTransitionDurationFromElement) const instance = new Backdrop({ isVisible: true, - isAnimated: false, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: false }) const spy2 = jasmine.createSpy('spy2') @@ -230,8 +219,7 @@ describe('Backdrop', () => { it('if it is not "shown", should not call delay callbacks', done => { const instance = new Backdrop({ isVisible: false, - isAnimated: true, - backdropClassName: CLASS_NAME_BACKDROP + isAnimated: true }) const spy = jasmine.createSpy('spy', getTransitionDurationFromElement) @@ -242,49 +230,62 @@ describe('Backdrop', () => { }) }) }) - - describe('rootElement initialization', () => { - it('Should be appended on "document.body" by default', done => { - const instance = new Backdrop({ - isVisible: true, - backdropClassName: CLASS_NAME_BACKDROP + describe('Config', () => { + describe('rootElement initialization', () => { + it('Should be appended on "document.body" by default', done => { + const instance = new Backdrop({ + isVisible: true + }) + const getElement = () => document.querySelector(CLASS_BACKDROP) + instance.show(() => { + expect(getElement().parentElement).toEqual(document.body) + done() + }) }) - const getElement = () => document.querySelector(CLASS_BACKDROP) - instance.show(() => { - expect(getElement().parentElement).toEqual(document.body) - done() + + it('Should find the rootElement if passed as a string', done => { + const instance = new Backdrop({ + isVisible: true, + rootElement: 'body' + }) + const getElement = () => document.querySelector(CLASS_BACKDROP) + instance.show(() => { + expect(getElement().parentElement).toEqual(document.body) + done() + }) + }) + + it('Should appended on any element given by the proper config', done => { + fixtureEl.innerHTML = [ + '