mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-12 08:34:08 +02:00
Variablize backdrop for modal and offcanvas
This commit is contained in:
@@ -45,6 +45,7 @@ const DefaultType = {
|
||||
}
|
||||
|
||||
const CLASS_NAME_SHOW = 'show'
|
||||
const CLASS_NAME_BACKDROP = 'offcanvas-backdrop'
|
||||
const OPEN_SELECTOR = '.offcanvas.show'
|
||||
|
||||
const EVENT_SHOW = `show${EVENT_KEY}`
|
||||
@@ -177,6 +178,7 @@ class Offcanvas extends BaseComponent {
|
||||
|
||||
_initializeBackDrop() {
|
||||
return new Backdrop({
|
||||
className: CLASS_NAME_BACKDROP,
|
||||
isVisible: this._config.backdrop,
|
||||
isAnimated: true,
|
||||
rootElement: this._element.parentNode,
|
||||
|
@@ -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,13 +17,13 @@ const Default = {
|
||||
}
|
||||
|
||||
const DefaultType = {
|
||||
className: 'string',
|
||||
isVisible: 'boolean',
|
||||
isAnimated: 'boolean',
|
||||
rootElement: '(element|string)',
|
||||
clickCallback: '(function|null)'
|
||||
}
|
||||
const NAME = 'backdrop'
|
||||
const CLASS_NAME_BACKDROP = 'modal-backdrop'
|
||||
const CLASS_NAME_FADE = 'fade'
|
||||
const CLASS_NAME_SHOW = 'show'
|
||||
|
||||
@@ -73,7 +74,7 @@ class Backdrop {
|
||||
_getElement() {
|
||||
if (!this._element) {
|
||||
const backdrop = document.createElement('div')
|
||||
backdrop.className = CLASS_NAME_BACKDROP
|
||||
backdrop.className = this._config.className
|
||||
if (this._config.isAnimated) {
|
||||
backdrop.classList.add(CLASS_NAME_FADE)
|
||||
}
|
||||
|
@@ -230,46 +230,62 @@ describe('Backdrop', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('rootElement initialization', () => {
|
||||
it('Should be appended on "document.body" by default', done => {
|
||||
const instance = new Backdrop({
|
||||
isVisible: true
|
||||
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 = [
|
||||
'<div id="wrapper">',
|
||||
'</div>'
|
||||
].join('')
|
||||
|
||||
const wrapper = fixtureEl.querySelector('#wrapper')
|
||||
const instance = new Backdrop({
|
||||
isVisible: true,
|
||||
rootElement: wrapper
|
||||
})
|
||||
const getElement = () => document.querySelector(CLASS_BACKDROP)
|
||||
instance.show(() => {
|
||||
expect(getElement().parentElement).toEqual(wrapper)
|
||||
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 = [
|
||||
'<div id="wrapper">',
|
||||
'</div>'
|
||||
].join('')
|
||||
|
||||
const wrapper = fixtureEl.querySelector('#wrapper')
|
||||
const instance = new Backdrop({
|
||||
isVisible: true,
|
||||
rootElement: wrapper
|
||||
})
|
||||
const getElement = () => document.querySelector(CLASS_BACKDROP)
|
||||
instance.show(() => {
|
||||
expect(getElement().parentElement).toEqual(wrapper)
|
||||
done()
|
||||
describe('ClassName', () => {
|
||||
it('Should be able to have different classNames than default', done => {
|
||||
const instance = new Backdrop({
|
||||
isVisible: true,
|
||||
className: 'foo'
|
||||
})
|
||||
const getElement = () => document.querySelector('.foo')
|
||||
instance.show(() => {
|
||||
expect(getElement()).toEqual(instance._getElement())
|
||||
instance.dispose()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user