-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathModalManager.ts
179 lines (140 loc) · 4.42 KB
/
ModalManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import css from 'dom-helpers/css';
import getSetScrollTop from 'dom-helpers/scrollTop';
import getSetScrollLeft from 'dom-helpers/scrollLeft';
import getScrollParent from 'dom-helpers/scrollParent';
import isDocument from 'dom-helpers/isDocument';
import { dataAttr } from './DataKey';
import getBodyScrollbarWidth from './getScrollbarWidth';
export interface ModalInstance {
dialog: Element;
backdrop: Element;
}
export interface ModalManagerOptions {
ownerDocument?: Document;
handleContainerOverflow?: boolean;
isRTL?: boolean;
}
export type ContainerState = {
scrollBarWidth: number;
style: Record<string, any>;
[key: string]: any;
};
export const OPEN_DATA_ATTRIBUTE = dataAttr('modal-open');
/**
* Manages a stack of Modals as well as ensuring
* body scrolling is is disabled and padding accounted for
*/
class ModalManager {
readonly handleContainerOverflow: boolean;
readonly isRTL: boolean;
readonly modals: ModalInstance[];
protected state!: ContainerState;
protected ownerDocument: Document | undefined;
constructor({
ownerDocument,
handleContainerOverflow = true,
isRTL = false,
}: ModalManagerOptions = {}) {
this.handleContainerOverflow = handleContainerOverflow;
this.isRTL = isRTL;
this.modals = [];
this.ownerDocument = ownerDocument;
}
getScrollbarWidth() {
return getBodyScrollbarWidth(this.ownerDocument);
}
protected getScollingElement() {
const element = getScrollParent(this.getElement());
return isDocument(element) ? element.defaultView! : element;
}
getElement() {
return (this.ownerDocument || document).body;
}
setModalAttributes(_modal: ModalInstance) {
// For overriding
}
removeModalAttributes(_modal: ModalInstance) {
// For overriding
}
setContainerStyle(containerState: ContainerState) {
const style: Partial<CSSStyleDeclaration> = { overflow: 'hidden' };
// we are only interested in the actual `style` here
// because we will override it
const paddingProp = this.isRTL ? 'paddingLeft' : 'paddingRight';
const container = this.getElement();
containerState.style = {
overflow: container.style.overflow,
[paddingProp]: container.style[paddingProp],
};
if (containerState.scrollBarWidth) {
// use computed style, here to get the real padding
// to add our scrollbar width
style[paddingProp] = `${
parseInt(css(container, paddingProp) || '0', 10) +
containerState.scrollBarWidth
}px`;
}
container.setAttribute(OPEN_DATA_ATTRIBUTE, '');
css(container, style as any);
}
reset() {
[...this.modals].forEach((m) => this.remove(m));
}
removeContainerStyle(containerState: ContainerState) {
const container = this.getElement();
container.removeAttribute(OPEN_DATA_ATTRIBUTE);
Object.assign(container.style, containerState.style);
}
add(modal: ModalInstance) {
let modalIdx = this.modals.indexOf(modal);
if (modalIdx !== -1) {
return modalIdx;
}
modalIdx = this.modals.length;
this.modals.push(modal);
this.setModalAttributes(modal);
if (modalIdx !== 0) {
return modalIdx;
}
const scrollElement = this.getScollingElement() as Element;
this.state = {
scrollBarWidth: this.getScrollbarWidth(),
scrollTop: getSetScrollTop(scrollElement),
scrollLeft: getSetScrollLeft(scrollElement),
style: {},
};
if (this.handleContainerOverflow) {
this.setContainerStyle(this.state);
}
return modalIdx;
}
maybeResetScrollPosition() {
const scrollElement = this.getScollingElement() as Element;
const { scrollTop, scrollLeft } = this.state;
if (getSetScrollTop(scrollElement) !== scrollTop) {
getSetScrollTop(scrollElement, scrollTop);
}
if (getSetScrollLeft(scrollElement) !== scrollLeft) {
getSetScrollLeft(scrollElement, scrollLeft);
}
}
remove(modal: ModalInstance) {
const modalIdx = this.modals.indexOf(modal);
if (modalIdx === -1) {
return;
}
this.modals.splice(modalIdx, 1);
// if that was the last modal in a container,
// clean up the container
if (!this.modals.length && this.handleContainerOverflow) {
this.removeContainerStyle(this.state);
}
this.removeModalAttributes(modal);
}
isTopModal(modal: ModalInstance) {
return (
!!this.modals.length && this.modals[this.modals.length - 1] === modal
);
}
}
export default ModalManager;