-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawer.js
More file actions
187 lines (153 loc) · 4.78 KB
/
drawer.js
File metadata and controls
187 lines (153 loc) · 4.78 KB
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
180
181
182
183
184
185
186
187
/**
* @file drawer.js
* @description Wudo Drawer Component (Web Cmp) with Integrated Portal and Auto-close
* @version 1.2.0
*/
class WudoDrawer extends HTMLElement {
constructor() {
super();
this._triggerElement = null;
this._focusTrap = null;
this._isOpen = false;
this._resizeObserver = null;
}
static get observedAttributes() {
return ['source-selector', 'wrapper-selector', 'slot-id'];
}
static cleanupInert() {
const inertElements = document.querySelectorAll('[data-drawer-inert]');
inertElements.forEach(el => {
el.removeAttribute('inert');
el.removeAttribute('data-drawer-inert');
});
if (!document.querySelector('wudo-drawer[open]')) {
document.body.style.overflow = '';
}
}
connectedCallback() {
WudoDrawer.cleanupInert();
this._stopResizeObserver();
/** @listens drawer:open */
document.addEventListener('drawer:open', (e) => {
if (e.detail.id === this.id) {
this._triggerElement = e.detail.trigger || document.activeElement;
this.open();
this._initResizeObserver();
}
});
/** @listens drawer:close */
document.addEventListener('drawer:close', (e) => {
if (e.detail.id === this.id) this.close();
});
/** @listens focusTrap:escape */
this.addEventListener('focusTrap:escape', () => this.close());
}
/**
* Observes the trigger element becomes hidden.
* @private
*/
_initResizeObserver() {
if (!this._triggerElement || this._resizeObserver) return;
this._resizeObserver = new ResizeObserver(() => {
const isTriggerHidden = window.getComputedStyle(this._triggerElement).display === 'none';
if (this._isOpen && isTriggerHidden) {
this.close();
}
});
this._resizeObserver.observe(this._triggerElement);
}
_stopResizeObserver() {
if (this._resizeObserver) {
this._resizeObserver.disconnect();
this._resizeObserver = null;
}
}
_teleportIn() {
const sourceSelector = this.getAttribute('source-selector');
const slotId = this.getAttribute('slot-id');
if (!sourceSelector || !slotId) {
return;
}
const source = document.querySelector(sourceSelector);
const slot = this.querySelector(`#${slotId}`);
if (source && slot && !slot.contains(source)) {
slot.appendChild(source);
document.dispatchEvent(new CustomEvent('wudo:portal:moved-in', {
detail: { id: this.id, source: source },
bubbles: true
}));
} else if (!source || !slot) {
console.warn(`[WudoDrawer] Teleport failed for ${this.id}: Source or Slot not found.`);
}
}
_teleportOut() {
const sourceSelector = this.getAttribute('source-selector');
const wrapperSelector = this.getAttribute('wrapper-selector');
if (!sourceSelector || !wrapperSelector) return;
const source = document.querySelector(sourceSelector);
const wrapper = document.querySelector(wrapperSelector);
if (source && wrapper && !wrapper.contains(source)) {
wrapper.appendChild(source);
}
}
/**
* Opens the drawer, activates focus trap and sets background to inert.
*/
open() {
if (this._isOpen) return;
this._teleportIn();
this._isOpen = true;
this.setAttribute('open', '');
this.manageFocus(true);
// FocusTrap activation
if (window.focusTrapManager) {
this._focusTrap = window.focusTrapManager.activate(this);
}
this.dispatchEvent(new CustomEvent('drawer:after-open', {
detail: { id: this.id },
bubbles: true
}));
}
close() {
if (!this._isOpen) return;
this._isOpen = false;
this._stopResizeObserver();
this.removeAttribute('open');
this.manageFocus(false);
this._teleportOut();
document.dispatchEvent(new CustomEvent('drawer:after-close', {
detail: { id: this.id },
bubbles: true
}));
if (this._focusTrap && window.focusTrapManager) {
window.focusTrapManager.deactivate(this._focusTrap);
this._focusTrap = null;
}
if (this._triggerElement) {
requestAnimationFrame(() => {
this._triggerElement.focus({ preventScroll: true });
});
}
}
/**
* Toggles inert attribute on sibling elements to hide them from assistive tech.
* @param {boolean} isOpen
*/
manageFocus(isOpen) {
const rootElements = Array.from(document.body.children);
rootElements.forEach(el => {
if (!el.contains(this) && !['SCRIPT', 'STYLE', 'NOSCRIPT'].includes(el.tagName)) {
el.setAttribute('data-drawer-inert', '');
el.setAttribute('inert', '');
}
});
document.body.style.overflow = 'hidden';
}
disconnectedCallback() {
this._stopResizeObserver();
WudoDrawer.cleanupInert();
}
}
if (!customElements.get('wudo-drawer')) {
customElements.define('wudo-drawer', WudoDrawer);
}