UNPKG

2.46 kBJavaScriptView Raw
1import addClass from 'dom-helpers/addClass';
2import css from 'dom-helpers/css';
3import qsa from 'dom-helpers/querySelectorAll';
4import removeClass from 'dom-helpers/removeClass';
5import ModalManager from '@restart/ui/ModalManager';
6const Selector = {
7 FIXED_CONTENT: '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top',
8 STICKY_CONTENT: '.sticky-top',
9 NAVBAR_TOGGLER: '.navbar-toggler'
10};
11class BootstrapModalManager extends ModalManager {
12 adjustAndStore(prop, element, adjust) {
13 const actual = element.style[prop];
14 // TODO: DOMStringMap and CSSStyleDeclaration aren't strictly compatible
15 // @ts-ignore
16 element.dataset[prop] = actual;
17 css(element, {
18 [prop]: `${parseFloat(css(element, prop)) + adjust}px`
19 });
20 }
21 restore(prop, element) {
22 const value = element.dataset[prop];
23 if (value !== undefined) {
24 delete element.dataset[prop];
25 css(element, {
26 [prop]: value
27 });
28 }
29 }
30 setContainerStyle(containerState) {
31 super.setContainerStyle(containerState);
32 const container = this.getElement();
33 addClass(container, 'modal-open');
34 if (!containerState.scrollBarWidth) return;
35 const paddingProp = this.isRTL ? 'paddingLeft' : 'paddingRight';
36 const marginProp = this.isRTL ? 'marginLeft' : 'marginRight';
37 qsa(container, Selector.FIXED_CONTENT).forEach(el => this.adjustAndStore(paddingProp, el, containerState.scrollBarWidth));
38 qsa(container, Selector.STICKY_CONTENT).forEach(el => this.adjustAndStore(marginProp, el, -containerState.scrollBarWidth));
39 qsa(container, Selector.NAVBAR_TOGGLER).forEach(el => this.adjustAndStore(marginProp, el, containerState.scrollBarWidth));
40 }
41 removeContainerStyle(containerState) {
42 super.removeContainerStyle(containerState);
43 const container = this.getElement();
44 removeClass(container, 'modal-open');
45 const paddingProp = this.isRTL ? 'paddingLeft' : 'paddingRight';
46 const marginProp = this.isRTL ? 'marginLeft' : 'marginRight';
47 qsa(container, Selector.FIXED_CONTENT).forEach(el => this.restore(paddingProp, el));
48 qsa(container, Selector.STICKY_CONTENT).forEach(el => this.restore(marginProp, el));
49 qsa(container, Selector.NAVBAR_TOGGLER).forEach(el => this.restore(marginProp, el));
50 }
51}
52let sharedManager;
53export function getSharedManager(options) {
54 if (!sharedManager) sharedManager = new BootstrapModalManager(options);
55 return sharedManager;
56}
57export default BootstrapModalManager;
\No newline at end of file