Skip to content

Commit 45d26de

Browse files
committed
Variablize backdrop for modal and offcanvas
1 parent 088ef62 commit 45d26de

File tree

8 files changed

+81
-50
lines changed

8 files changed

+81
-50
lines changed

js/src/offcanvas.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const DefaultType = {
4545
}
4646

4747
const CLASS_NAME_SHOW = 'show'
48+
const CLASS_NAME_BACKDROP = 'offcanvas-backdrop'
4849
const OPEN_SELECTOR = '.offcanvas.show'
4950

5051
const EVENT_SHOW = `show${EVENT_KEY}`
@@ -177,6 +178,7 @@ class Offcanvas extends BaseComponent {
177178

178179
_initializeBackDrop() {
179180
return new Backdrop({
181+
className: CLASS_NAME_BACKDROP,
180182
isVisible: this._config.backdrop,
181183
isAnimated: true,
182184
rootElement: this._element.parentNode,

js/src/util/backdrop.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ import EventHandler from '../dom/event-handler'
99
import { execute, executeAfterTransition, getElement, reflow, typeCheckConfig } from './index'
1010

1111
const Default = {
12+
className: 'modal-backdrop',
1213
isVisible: true, // if false, we use the backdrop helper without adding any element to the dom
1314
isAnimated: false,
1415
rootElement: 'body', // give the choice to place backdrop under different elements
1516
clickCallback: null
1617
}
1718

1819
const DefaultType = {
20+
className: 'string',
1921
isVisible: 'boolean',
2022
isAnimated: 'boolean',
2123
rootElement: '(element|string)',
2224
clickCallback: '(function|null)'
2325
}
2426
const NAME = 'backdrop'
25-
const CLASS_NAME_BACKDROP = 'modal-backdrop'
2627
const CLASS_NAME_FADE = 'fade'
2728
const CLASS_NAME_SHOW = 'show'
2829

@@ -73,7 +74,7 @@ class Backdrop {
7374
_getElement() {
7475
if (!this._element) {
7576
const backdrop = document.createElement('div')
76-
backdrop.className = CLASS_NAME_BACKDROP
77+
backdrop.className = this._config.className
7778
if (this._config.isAnimated) {
7879
backdrop.classList.add(CLASS_NAME_FADE)
7980
}

js/tests/unit/util/backdrop.spec.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -230,46 +230,62 @@ describe('Backdrop', () => {
230230
})
231231
})
232232
})
233-
234-
describe('rootElement initialization', () => {
235-
it('Should be appended on "document.body" by default', done => {
236-
const instance = new Backdrop({
237-
isVisible: true
238-
})
239-
const getElement = () => document.querySelector(CLASS_BACKDROP)
240-
instance.show(() => {
241-
expect(getElement().parentElement).toEqual(document.body)
242-
done()
233+
describe('Config', () => {
234+
describe('rootElement initialization', () => {
235+
it('Should be appended on "document.body" by default', done => {
236+
const instance = new Backdrop({
237+
isVisible: true
238+
})
239+
const getElement = () => document.querySelector(CLASS_BACKDROP)
240+
instance.show(() => {
241+
expect(getElement().parentElement).toEqual(document.body)
242+
done()
243+
})
243244
})
244-
})
245245

246-
it('Should find the rootElement if passed as a string', done => {
247-
const instance = new Backdrop({
248-
isVisible: true,
249-
rootElement: 'body'
250-
})
251-
const getElement = () => document.querySelector(CLASS_BACKDROP)
252-
instance.show(() => {
253-
expect(getElement().parentElement).toEqual(document.body)
254-
done()
246+
it('Should find the rootElement if passed as a string', done => {
247+
const instance = new Backdrop({
248+
isVisible: true,
249+
rootElement: 'body'
250+
})
251+
const getElement = () => document.querySelector(CLASS_BACKDROP)
252+
instance.show(() => {
253+
expect(getElement().parentElement).toEqual(document.body)
254+
done()
255+
})
255256
})
256-
})
257257

258-
it('Should appended on any element given by the proper config', done => {
259-
fixtureEl.innerHTML = [
260-
'<div id="wrapper">',
261-
'</div>'
262-
].join('')
258+
it('Should appended on any element given by the proper config', done => {
259+
fixtureEl.innerHTML = [
260+
'<div id="wrapper">',
261+
'</div>'
262+
].join('')
263263

264-
const wrapper = fixtureEl.querySelector('#wrapper')
265-
const instance = new Backdrop({
266-
isVisible: true,
267-
rootElement: wrapper
264+
const wrapper = fixtureEl.querySelector('#wrapper')
265+
const instance = new Backdrop({
266+
isVisible: true,
267+
rootElement: wrapper
268+
})
269+
const getElement = () => document.querySelector(CLASS_BACKDROP)
270+
instance.show(() => {
271+
expect(getElement().parentElement).toEqual(wrapper)
272+
done()
273+
})
268274
})
269-
const getElement = () => document.querySelector(CLASS_BACKDROP)
270-
instance.show(() => {
271-
expect(getElement().parentElement).toEqual(wrapper)
272-
done()
275+
})
276+
277+
describe('ClassName', () => {
278+
it('Should be able to have different classNames than default', done => {
279+
const instance = new Backdrop({
280+
isVisible: true,
281+
className: 'foo'
282+
})
283+
const getElement = () => document.querySelector('.foo')
284+
instance.show(() => {
285+
expect(getElement()).toEqual(instance._getElement())
286+
instance.dispose()
287+
done()
288+
})
273289
})
274290
})
275291
})

scss/_mixins.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
// Components
2424
@import "mixins/alert";
25+
@import "mixins/backdrop";
2526
@import "mixins/buttons";
2627
@import "mixins/caret";
2728
@import "mixins/pagination";

scss/_modal.scss

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,7 @@
8585

8686
// Modal background
8787
.modal-backdrop {
88-
position: fixed;
89-
top: 0;
90-
left: 0;
91-
z-index: $zindex-modal-backdrop;
92-
width: 100vw;
93-
height: 100vh;
94-
background-color: $modal-backdrop-bg;
95-
96-
// Fade for backdrop
97-
&.fade { opacity: 0; }
98-
&.show { opacity: $modal-backdrop-opacity; }
88+
@include overlay-backdrop($zindex-modal-backdrop, $modal-backdrop-bg, $modal-backdrop-opacity);
9989
}
10090

10191
// Modal header

scss/_offcanvas.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
@include transition(transform $offcanvas-transition-duration ease-in-out);
1515
}
1616

17+
.offcanvas-backdrop {
18+
@include overlay-backdrop($zindex-offcanvas-backdrop, $offcanvas-backdrop-bg, $offcanvas-backdrop-opacity);
19+
}
20+
1721
.offcanvas-header {
1822
display: flex;
1923
align-items: center;

scss/_variables.scss

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,10 @@ $form-validation-states: (
907907
$zindex-dropdown: 1000 !default;
908908
$zindex-sticky: 1020 !default;
909909
$zindex-fixed: 1030 !default;
910-
$zindex-modal-backdrop: 1040 !default;
911-
$zindex-offcanvas: 1050 !default;
912-
$zindex-modal: 1060 !default;
910+
$zindex-offcanvas-backdrop: 1040 !default;
911+
$zindex-offcanvas: 1045 !default;
912+
$zindex-modal-backdrop: 1050 !default;
913+
$zindex-modal: 1055 !default;
913914
$zindex-popover: 1070 !default;
914915
$zindex-tooltip: 1080 !default;
915916
// scss-docs-end zindex-stack
@@ -1453,6 +1454,8 @@ $offcanvas-title-line-height: $modal-title-line-height !default;
14531454
$offcanvas-bg-color: $modal-content-bg !default;
14541455
$offcanvas-color: $modal-content-color !default;
14551456
$offcanvas-box-shadow: $modal-content-box-shadow-xs !default;
1457+
$offcanvas-backdrop-bg: $modal-backdrop-bg !default;
1458+
$offcanvas-backdrop-opacity: $modal-backdrop-opacity !default;
14561459
// scss-docs-end offcanvas-variables
14571460

14581461
// Code

scss/mixins/_backdrop.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Shared between modals and offcanvases
2+
@mixin overlay-backdrop($zindex, $backdrop-bg, $backdrop-opacity) {
3+
position: fixed;
4+
top: 0;
5+
left: 0;
6+
z-index: $zindex;
7+
width: 100vw;
8+
height: 100vh;
9+
background-color: $backdrop-bg;
10+
11+
// Fade for backdrop
12+
&.fade { opacity: 0; }
13+
&.show { opacity: $backdrop-opacity; }
14+
}

0 commit comments

Comments
 (0)