-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp-modal.js
More file actions
145 lines (122 loc) · 3.63 KB
/
help-modal.js
File metadata and controls
145 lines (122 loc) · 3.63 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
/**
* HelpModal - A reusable, dependency-free help modal system
*
* Usage:
* HelpModal.init({
* triggerSelector: '#btn-help',
* content: helpContent,
* theme: 'auto'
* });
*/
class HelpModal {
constructor(options = {}) {
this.options = {
triggerSelector: '#btn-help',
content: '',
theme: 'auto', // 'light', 'dark', or 'auto'
customStyles: {},
...options
};
this.isOpen = false;
this.modal = null;
this.trigger = null;
this.init();
}
init() {
this.createModal();
this.bindEvents();
}
createModal() {
// Create modal container
this.modal = document.createElement('div');
this.modal.className = 'help-modal';
this.modal.innerHTML = `
<div class="help-modal-backdrop"></div>
<div class="help-modal-content">
<div class="help-modal-header">
<h2>Help / User Guide</h2>
<button class="help-modal-close" type="button" aria-label="Close help">×</button>
</div>
<div class="help-modal-body">
${this.options.content}
</div>
</div>
`;
// Initially hidden
this.modal.style.display = 'none';
document.body.appendChild(this.modal);
}
bindEvents() {
// Find trigger element
this.trigger = document.querySelector(this.options.triggerSelector);
if (!this.trigger) {
console.warn(`HelpModal: Trigger element '${this.options.triggerSelector}' not found`);
return;
}
// Convert link to button if needed
if (this.trigger.tagName === 'A') {
this.trigger.addEventListener('click', (e) => {
e.preventDefault();
this.open();
});
} else {
this.trigger.addEventListener('click', () => this.open());
}
// Close button
const closeBtn = this.modal.querySelector('.help-modal-close');
closeBtn.addEventListener('click', () => this.close());
// Backdrop click
const backdrop = this.modal.querySelector('.help-modal-backdrop');
backdrop.addEventListener('click', () => this.close());
// ESC key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && this.isOpen) {
this.close();
}
});
// Handle internal navigation links
this.modal.addEventListener('click', (e) => {
if (e.target.matches('a[href^="#"]')) {
e.preventDefault();
const targetId = e.target.getAttribute('href').substring(1);
const targetElement = this.modal.querySelector(`#${targetId}`);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
}
}
});
}
open() {
if (this.isOpen) return;
this.isOpen = true;
this.modal.style.display = 'block';
document.body.style.overflow = 'hidden'; // Prevent background scrolling
// Focus management
const closeBtn = this.modal.querySelector('.help-modal-close');
closeBtn.focus();
// Trigger custom event
this.trigger.dispatchEvent(new CustomEvent('helpModal:open', { detail: this }));
}
close() {
if (!this.isOpen) return;
this.isOpen = false;
this.modal.style.display = 'none';
document.body.style.overflow = ''; // Restore scrolling
// Return focus to trigger
this.trigger.focus();
// Trigger custom event
this.trigger.dispatchEvent(new CustomEvent('helpModal:close', { detail: this }));
}
// Public API methods
static init(options) {
return new HelpModal(options);
}
destroy() {
if (this.modal && this.modal.parentNode) {
this.modal.parentNode.removeChild(this.modal);
}
document.body.style.overflow = '';
}
}
// Export for use
window.HelpModal = HelpModal;