-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltips.js
More file actions
191 lines (164 loc) · 5.91 KB
/
tooltips.js
File metadata and controls
191 lines (164 loc) · 5.91 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
188
189
190
191
// Enhanced Tooltip Functionality
document.addEventListener('DOMContentLoaded', function() {
// Add keyboard accessibility for tooltips
const tooltips = document.querySelectorAll('.tooltip');
tooltips.forEach(tooltip => {
// Add ARIA attributes for accessibility
tooltip.setAttribute('aria-describedby', tooltip.id || 'tooltip-' + Math.random().toString(36).substr(2, 9));
tooltip.setAttribute('tabindex', '0');
// Handle keyboard events
tooltip.addEventListener('keydown', function(e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
toggleTooltip(this);
}
});
// Handle focus events for keyboard users
tooltip.addEventListener('focus', function() {
this.classList.add('tooltip-focus');
});
tooltip.addEventListener('blur', function() {
this.classList.remove('tooltip-focus');
});
});
// Mobile touch support
if ('ontouchstart' in window) {
tooltips.forEach(tooltip => {
let touchTimer;
tooltip.addEventListener('touchstart', function(e) {
e.preventDefault();
touchTimer = setTimeout(() => {
showTooltip(this);
}, 500); // Show after 500ms hold
});
tooltip.addEventListener('touchend', function(e) {
e.preventDefault();
clearTimeout(touchTimer);
hideTooltip(this);
});
tooltip.addEventListener('touchmove', function() {
clearTimeout(touchTimer);
});
});
}
// Auto-hide tooltips when scrolling
let scrollTimer;
window.addEventListener('scroll', function() {
clearTimeout(scrollTimer);
scrollTimer = setTimeout(() => {
hideAllTooltips();
}, 150);
});
// Hide tooltips when clicking outside
document.addEventListener('click', function(e) {
if (!e.target.closest('.tooltip')) {
hideAllTooltips();
}
});
// Function to toggle tooltip visibility
function toggleTooltip(tooltip) {
const tooltipText = tooltip.querySelector('.tooltiptext');
if (tooltipText) {
const isVisible = tooltipText.style.visibility === 'visible';
if (isVisible) {
hideTooltip(tooltip);
} else {
showTooltip(tooltip);
}
}
}
// Function to show tooltip
function showTooltip(tooltip) {
const tooltipText = tooltip.querySelector('.tooltiptext');
if (tooltipText) {
hideAllTooltips(); // Hide other tooltips first
tooltipText.style.visibility = 'visible';
tooltipText.style.opacity = '1';
tooltipText.style.transform = 'translateY(0)';
}
}
// Function to hide tooltip
function hideTooltip(tooltip) {
const tooltipText = tooltip.querySelector('.tooltiptext');
if (tooltipText) {
tooltipText.style.visibility = 'hidden';
tooltipText.style.opacity = '0';
tooltipText.style.transform = 'translateY(10px)';
}
}
// Function to hide all tooltips
function hideAllTooltips() {
tooltips.forEach(tooltip => {
hideTooltip(tooltip);
});
}
// Add tooltip analytics (optional)
tooltips.forEach(tooltip => {
tooltip.addEventListener('mouseenter', function() {
// Track tooltip usage for analytics
if (typeof gtag !== 'undefined') {
gtag('event', 'tooltip_view', {
'tooltip_text': this.querySelector('.tooltiptext')?.textContent?.substring(0, 50) || 'unknown'
});
}
});
});
// Dynamic tooltip positioning for edge cases
function adjustTooltipPosition(tooltip) {
const tooltipText = tooltip.querySelector('.tooltiptext');
if (!tooltipText) return;
const rect = tooltip.getBoundingClientRect();
const tooltipRect = tooltipText.getBoundingClientRect();
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// Check if tooltip goes off screen and adjust position
if (rect.left + tooltipRect.width > viewportWidth) {
tooltip.classList.add('tooltip-left');
} else if (rect.right - tooltipRect.width < 0) {
tooltip.classList.add('tooltip-right');
}
if (rect.top - tooltipRect.height < 0) {
tooltip.classList.add('tooltip-bottom');
}
}
// Apply positioning adjustments on hover
tooltips.forEach(tooltip => {
tooltip.addEventListener('mouseenter', function() {
setTimeout(() => adjustTooltipPosition(this), 10);
});
});
});
// CSS for focus states and mobile enhancements
const additionalStyles = `
.tooltip-focus {
outline: 2px solid #6366f1;
outline-offset: 2px;
}
.tooltip-bottom .tooltiptext {
top: 125%;
bottom: auto;
}
.tooltip-bottom .tooltiptext::after {
top: -5px;
bottom: auto;
border-color: transparent transparent rgba(0, 0, 0, 0.9) transparent;
}
@media (max-width: 768px) {
.tooltip {
touch-action: manipulation;
}
.tooltip .tooltiptext {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 90vw;
max-width: 300px;
z-index: 10000;
}
}
`;
// Inject additional styles
const styleSheet = document.createElement('style');
styleSheet.textContent = additionalStyles;
document.head.appendChild(styleSheet);