askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
standards-overlay.js · 26.0 KB · 770 lines
| 1 | /** |
| 2 | * DBBasic Usability Standards Overlay System |
| 3 | * |
| 4 | * Visual overlay system to show usability standards on demo pages. |
| 5 | * Prevents GeoCities 2.0 by teaching standards visually. |
| 6 | * |
| 7 | * Version: 1.0 |
| 8 | * Author: Dan Quellhorst / dbbasic.com |
| 9 | */ |
| 10 | |
| 11 | class StandardsOverlay { |
| 12 | constructor() { |
| 13 | this.activeOverlays = new Set(); |
| 14 | this.initialized = false; |
| 15 | this.overlayContainer = null; |
| 16 | this.appType = null; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Detect what type of application this is |
| 21 | * Returns: 'brochure', 'app', or 'social' |
| 22 | */ |
| 23 | detectApplicationType() { |
| 24 | if (this.appType) return this.appType; // Cache result |
| 25 | |
| 26 | let brochureScore = 0; |
| 27 | let appScore = 0; |
| 28 | let socialScore = 0; |
| 29 | |
| 30 | // Check for brochure/marketing indicators |
| 31 | if (document.querySelector('.hero, [class*="hero"]')) brochureScore += 3; |
| 32 | if (document.querySelector('.pricing, [class*="pricing"]')) brochureScore += 2; |
| 33 | if (document.querySelector('.testimonial, [class*="testimonial"]')) brochureScore += 2; |
| 34 | if (document.querySelector('.cta, [class*="cta"], .btn-primary')) brochureScore += 1; |
| 35 | const navLinks = document.querySelectorAll('nav a, header a'); |
| 36 | if (navLinks.length >= 3 && navLinks.length <= 7) brochureScore += 1; |
| 37 | |
| 38 | // Check for app/dashboard indicators |
| 39 | if (document.querySelector('aside, .sidebar, [class*="sidebar"]')) appScore += 3; |
| 40 | if (document.querySelector('table, .data-table, [class*="table"]')) appScore += 2; |
| 41 | if (document.querySelector('.dashboard, [class*="dashboard"]')) appScore += 3; |
| 42 | if (document.querySelector('input[type="search"], .search')) appScore += 1; |
| 43 | if (document.querySelectorAll('form').length > 1) appScore += 1; |
| 44 | if (document.querySelector('.breadcrumb, [class*="breadcrumb"]')) appScore += 2; |
| 45 | |
| 46 | // Check for social/content indicators |
| 47 | if (document.querySelector('.feed, .post, [class*="feed"], [class*="post"]')) socialScore += 3; |
| 48 | if (document.querySelector('.comment, [class*="comment"]')) socialScore += 2; |
| 49 | if (document.querySelector('.like, .share, [class*="reaction"]')) socialScore += 2; |
| 50 | if (document.querySelector('.avatar, [class*="avatar"]')) socialScore += 1; |
| 51 | if (document.querySelector('.infinite-scroll, [data-infinite-scroll]')) socialScore += 2; |
| 52 | |
| 53 | // Determine winner |
| 54 | const max = Math.max(brochureScore, appScore, socialScore); |
| 55 | if (max === 0) { |
| 56 | this.appType = 'brochure'; // Default |
| 57 | } else if (brochureScore === max) { |
| 58 | this.appType = 'brochure'; |
| 59 | } else if (appScore === max) { |
| 60 | this.appType = 'app'; |
| 61 | } else { |
| 62 | this.appType = 'social'; |
| 63 | } |
| 64 | |
| 65 | console.log(`📱 Detected app type: ${this.appType} (scores: brochure=${brochureScore}, app=${appScore}, social=${socialScore})`); |
| 66 | return this.appType; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get standards for the detected app type |
| 71 | */ |
| 72 | getAppTypeStandards() { |
| 73 | const type = this.detectApplicationType(); |
| 74 | |
| 75 | const standards = { |
| 76 | brochure: { |
| 77 | name: 'Brochure/Marketing', |
| 78 | density: 'Low (airy)', |
| 79 | spacing: '64-96px sections', |
| 80 | navigation: 'Simple top bar', |
| 81 | interaction: 'Scroll + Click', |
| 82 | goal: 'Convert visitors' |
| 83 | }, |
| 84 | app: { |
| 85 | name: 'App/Dashboard', |
| 86 | density: 'High (dense)', |
| 87 | spacing: '24-32px sections', |
| 88 | navigation: 'Persistent sidebar', |
| 89 | interaction: 'Keyboard + Click', |
| 90 | goal: 'Complete tasks' |
| 91 | }, |
| 92 | social: { |
| 93 | name: 'Social/Content', |
| 94 | density: 'Medium', |
| 95 | spacing: '16-24px posts', |
| 96 | navigation: 'Sticky top bar', |
| 97 | interaction: 'Scroll + React', |
| 98 | goal: 'Engage users' |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | return standards[type]; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Initialize the overlay system |
| 107 | */ |
| 108 | init() { |
| 109 | if (this.initialized) return; |
| 110 | |
| 111 | // Create main overlay container |
| 112 | this.overlayContainer = document.createElement('div'); |
| 113 | this.overlayContainer.id = 'standards-overlay-container'; |
| 114 | this.overlayContainer.className = 'standards-overlay-container'; |
| 115 | document.body.appendChild(this.overlayContainer); |
| 116 | |
| 117 | // Listen for window resize to update overlays |
| 118 | window.addEventListener('resize', () => this.refreshActiveOverlays()); |
| 119 | |
| 120 | this.initialized = true; |
| 121 | console.log('📐 DBBasic Standards Overlay initialized'); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Toggle a specific overlay type |
| 126 | */ |
| 127 | toggle(type) { |
| 128 | if (this.activeOverlays.has(type)) { |
| 129 | this.hide(type); |
| 130 | } else { |
| 131 | this.show(type); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Show a specific overlay type |
| 137 | */ |
| 138 | show(type) { |
| 139 | if (!this.initialized) this.init(); |
| 140 | |
| 141 | // Remove existing overlay of this type |
| 142 | this.hide(type); |
| 143 | |
| 144 | // Create new overlay |
| 145 | let overlay; |
| 146 | switch(type) { |
| 147 | case 'grid': |
| 148 | overlay = this.createGridOverlay(); |
| 149 | break; |
| 150 | case 'touch': |
| 151 | overlay = this.createTouchZonesOverlay(); |
| 152 | break; |
| 153 | case 'spacing': |
| 154 | overlay = this.createSpacingOverlay(); |
| 155 | break; |
| 156 | case 'flow': |
| 157 | overlay = this.createVisualFlowOverlay(); |
| 158 | break; |
| 159 | case 'targets': |
| 160 | overlay = this.createTouchTargetsOverlay(); |
| 161 | break; |
| 162 | case 'boundaries': |
| 163 | overlay = this.createContentBoundariesOverlay(); |
| 164 | break; |
| 165 | default: |
| 166 | console.warn(`Unknown overlay type: ${type}`); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if (overlay) { |
| 171 | overlay.dataset.overlayType = type; |
| 172 | this.overlayContainer.appendChild(overlay); |
| 173 | this.activeOverlays.add(type); |
| 174 | console.log(`✅ Showing ${type} overlay`); |
| 175 | |
| 176 | // Show app type badge if this is the first overlay |
| 177 | this.updateAppTypeBadge(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Show/update app type badge |
| 183 | */ |
| 184 | updateAppTypeBadge() { |
| 185 | if (this.activeOverlays.size === 0) { |
| 186 | // Remove badge if no overlays active |
| 187 | const existing = document.getElementById('app-type-badge'); |
| 188 | if (existing) existing.remove(); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // Get or create badge |
| 193 | let badge = document.getElementById('app-type-badge'); |
| 194 | if (!badge) { |
| 195 | badge = document.createElement('div'); |
| 196 | badge.id = 'app-type-badge'; |
| 197 | badge.style.cssText = ` |
| 198 | position: fixed; |
| 199 | top: 24px; |
| 200 | left: 50%; |
| 201 | transform: translateX(-50%); |
| 202 | background: rgba(17, 24, 39, 0.95); |
| 203 | backdrop-filter: blur(10px); |
| 204 | color: white; |
| 205 | padding: 12px 20px; |
| 206 | border-radius: 8px; |
| 207 | font-family: 'Inter', -apple-system, sans-serif; |
| 208 | z-index: 10001; |
| 209 | pointer-events: none; |
| 210 | box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2); |
| 211 | `; |
| 212 | document.body.appendChild(badge); |
| 213 | } |
| 214 | |
| 215 | const standards = this.getAppTypeStandards(); |
| 216 | badge.innerHTML = ` |
| 217 | <div style="font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; opacity: 0.7; margin-bottom: 4px;"> |
| 218 | Application Type |
| 219 | </div> |
| 220 | <div style="font-size: 14px; font-weight: 700; margin-bottom: 8px;"> |
| 221 | ${standards.name} |
| 222 | </div> |
| 223 | <div style="font-size: 12px; opacity: 0.9; display: grid; grid-template-columns: auto auto; gap: 4px 12px; text-align: left;"> |
| 224 | <span style="opacity: 0.7;">Density:</span> <span>${standards.density}</span> |
| 225 | <span style="opacity: 0.7;">Spacing:</span> <span>${standards.spacing}</span> |
| 226 | <span style="opacity: 0.7;">Navigation:</span> <span>${standards.navigation}</span> |
| 227 | <span style="opacity: 0.7;">Goal:</span> <span>${standards.goal}</span> |
| 228 | </div> |
| 229 | `; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Hide a specific overlay type |
| 234 | */ |
| 235 | hide(type) { |
| 236 | const existing = this.overlayContainer?.querySelector(`[data-overlay-type="${type}"]`); |
| 237 | if (existing) { |
| 238 | existing.remove(); |
| 239 | this.activeOverlays.delete(type); |
| 240 | console.log(`❌ Hiding ${type} overlay`); |
| 241 | |
| 242 | // Update app type badge |
| 243 | this.updateAppTypeBadge(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Show all overlays |
| 249 | */ |
| 250 | showAll() { |
| 251 | ['grid', 'touch', 'spacing', 'flow', 'targets', 'boundaries'].forEach(type => { |
| 252 | this.show(type); |
| 253 | }); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Hide all overlays |
| 258 | */ |
| 259 | hideAll() { |
| 260 | this.activeOverlays.forEach(type => this.hide(type)); |
| 261 | // Badge will be removed by updateAppTypeBadge() in hide() |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Refresh all active overlays (on resize) |
| 266 | */ |
| 267 | refreshActiveOverlays() { |
| 268 | const active = Array.from(this.activeOverlays); |
| 269 | active.forEach(type => { |
| 270 | this.hide(type); |
| 271 | this.show(type); |
| 272 | }); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Create 12-column grid overlay |
| 277 | */ |
| 278 | createGridOverlay() { |
| 279 | const overlay = document.createElement('div'); |
| 280 | overlay.className = 'standards-overlay grid-overlay'; |
| 281 | |
| 282 | const width = window.innerWidth; |
| 283 | let cols, gutter, margin; |
| 284 | |
| 285 | // Responsive column count |
| 286 | if (width < 768) { |
| 287 | cols = 4; |
| 288 | gutter = 16; |
| 289 | margin = 16; |
| 290 | } else if (width < 1024) { |
| 291 | cols = 8; |
| 292 | gutter = 24; |
| 293 | margin = 24; |
| 294 | } else { |
| 295 | cols = 12; |
| 296 | gutter = 24; |
| 297 | margin = 48; |
| 298 | } |
| 299 | |
| 300 | // Create grid container |
| 301 | const grid = document.createElement('div'); |
| 302 | grid.className = 'grid-container'; |
| 303 | grid.style.cssText = ` |
| 304 | position: fixed; |
| 305 | top: 0; |
| 306 | left: ${margin}px; |
| 307 | right: ${margin}px; |
| 308 | height: 100vh; |
| 309 | display: grid; |
| 310 | grid-template-columns: repeat(${cols}, 1fr); |
| 311 | gap: ${gutter}px; |
| 312 | pointer-events: none; |
| 313 | z-index: 10000; |
| 314 | `; |
| 315 | |
| 316 | // Create columns |
| 317 | for (let i = 0; i < cols; i++) { |
| 318 | const col = document.createElement('div'); |
| 319 | col.className = 'grid-column'; |
| 320 | col.style.cssText = ` |
| 321 | background: rgba(99, 102, 241, 0.1); |
| 322 | border-left: 1px dashed rgba(99, 102, 241, 0.3); |
| 323 | border-right: 1px dashed rgba(99, 102, 241, 0.3); |
| 324 | `; |
| 325 | grid.appendChild(col); |
| 326 | } |
| 327 | |
| 328 | overlay.appendChild(grid); |
| 329 | |
| 330 | // Add annotation |
| 331 | const annotation = this.createAnnotation( |
| 332 | `${cols}-Column Grid`, |
| 333 | `Gutter: ${gutter}px | Margin: ${margin}px`, |
| 334 | 'top-left' |
| 335 | ); |
| 336 | overlay.appendChild(annotation); |
| 337 | |
| 338 | return overlay; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Create touch zones overlay (mobile) |
| 343 | */ |
| 344 | createTouchZonesOverlay() { |
| 345 | const overlay = document.createElement('div'); |
| 346 | overlay.className = 'standards-overlay touch-zones-overlay'; |
| 347 | |
| 348 | const height = window.innerHeight; |
| 349 | const greenZoneHeight = height / 3; // Bottom 1/3 |
| 350 | const yellowZoneHeight = height / 3; // Middle 1/3 |
| 351 | const redZoneHeight = height / 3; // Top 1/3 |
| 352 | |
| 353 | // Red zone (hard to reach) |
| 354 | const redZone = document.createElement('div'); |
| 355 | redZone.className = 'touch-zone red-zone'; |
| 356 | redZone.style.cssText = ` |
| 357 | position: fixed; |
| 358 | top: 0; |
| 359 | left: 0; |
| 360 | right: 0; |
| 361 | height: ${redZoneHeight}px; |
| 362 | background: rgba(239, 68, 68, 0.1); |
| 363 | border-bottom: 2px dashed rgba(239, 68, 68, 0.5); |
| 364 | pointer-events: none; |
| 365 | z-index: 10000; |
| 366 | `; |
| 367 | overlay.appendChild(redZone); |
| 368 | |
| 369 | const redAnnotation = this.createAnnotation( |
| 370 | '🔴 Hard to Reach Zone', |
| 371 | 'Avoid primary actions', |
| 372 | 'top-left', |
| 373 | redZone |
| 374 | ); |
| 375 | redAnnotation.style.top = '20px'; |
| 376 | redAnnotation.style.left = '20px'; |
| 377 | overlay.appendChild(redAnnotation); |
| 378 | |
| 379 | // Yellow zone (reachable with stretch) |
| 380 | const yellowZone = document.createElement('div'); |
| 381 | yellowZone.className = 'touch-zone yellow-zone'; |
| 382 | yellowZone.style.cssText = ` |
| 383 | position: fixed; |
| 384 | top: ${redZoneHeight}px; |
| 385 | left: 0; |
| 386 | right: 0; |
| 387 | height: ${yellowZoneHeight}px; |
| 388 | background: rgba(245, 158, 11, 0.1); |
| 389 | border-bottom: 2px dashed rgba(245, 158, 11, 0.5); |
| 390 | pointer-events: none; |
| 391 | z-index: 10000; |
| 392 | `; |
| 393 | overlay.appendChild(yellowZone); |
| 394 | |
| 395 | const yellowAnnotation = this.createAnnotation( |
| 396 | '🟡 Reachable Zone', |
| 397 | 'Content & secondary actions', |
| 398 | 'top-left', |
| 399 | yellowZone |
| 400 | ); |
| 401 | yellowAnnotation.style.top = `${redZoneHeight + 20}px`; |
| 402 | yellowAnnotation.style.left = '20px'; |
| 403 | overlay.appendChild(yellowAnnotation); |
| 404 | |
| 405 | // Green zone (thumb-friendly) |
| 406 | const greenZone = document.createElement('div'); |
| 407 | greenZone.className = 'touch-zone green-zone'; |
| 408 | greenZone.style.cssText = ` |
| 409 | position: fixed; |
| 410 | bottom: 0; |
| 411 | left: 0; |
| 412 | right: 0; |
| 413 | height: ${greenZoneHeight}px; |
| 414 | background: rgba(34, 197, 94, 0.1); |
| 415 | border-top: 2px dashed rgba(34, 197, 94, 0.5); |
| 416 | pointer-events: none; |
| 417 | z-index: 10000; |
| 418 | `; |
| 419 | overlay.appendChild(greenZone); |
| 420 | |
| 421 | const greenAnnotation = this.createAnnotation( |
| 422 | '🟢 Thumb-Friendly Zone', |
| 423 | 'Primary actions & navigation', |
| 424 | 'bottom-left', |
| 425 | greenZone |
| 426 | ); |
| 427 | greenAnnotation.style.bottom = '20px'; |
| 428 | greenAnnotation.style.left = '20px'; |
| 429 | overlay.appendChild(greenAnnotation); |
| 430 | |
| 431 | return overlay; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Create spacing units overlay |
| 436 | */ |
| 437 | createSpacingOverlay() { |
| 438 | const overlay = document.createElement('div'); |
| 439 | overlay.className = 'standards-overlay spacing-overlay'; |
| 440 | |
| 441 | // Find all major elements and annotate their spacing |
| 442 | const sections = document.querySelectorAll('section, .hero, .features, .pricing, .testimonials, [class*="section"]'); |
| 443 | |
| 444 | sections.forEach((section, index) => { |
| 445 | if (index === 0) return; // Skip first section |
| 446 | |
| 447 | const prevSection = sections[index - 1]; |
| 448 | const spacing = section.offsetTop - (prevSection.offsetTop + prevSection.offsetHeight); |
| 449 | |
| 450 | if (spacing > 0) { |
| 451 | // Create spacing indicator |
| 452 | const spacingIndicator = document.createElement('div'); |
| 453 | spacingIndicator.className = 'spacing-indicator'; |
| 454 | spacingIndicator.style.cssText = ` |
| 455 | position: absolute; |
| 456 | top: ${prevSection.offsetTop + prevSection.offsetHeight}px; |
| 457 | left: 20px; |
| 458 | width: 40px; |
| 459 | height: ${spacing}px; |
| 460 | border-left: 2px solid rgba(168, 85, 247, 0.6); |
| 461 | border-right: 2px solid rgba(168, 85, 247, 0.6); |
| 462 | border-top: 2px solid rgba(168, 85, 247, 0.6); |
| 463 | border-bottom: 2px solid rgba(168, 85, 247, 0.6); |
| 464 | pointer-events: none; |
| 465 | z-index: 10000; |
| 466 | `; |
| 467 | |
| 468 | const label = document.createElement('div'); |
| 469 | label.className = 'spacing-label'; |
| 470 | label.textContent = `${spacing}px`; |
| 471 | label.style.cssText = ` |
| 472 | position: absolute; |
| 473 | top: 50%; |
| 474 | left: 50px; |
| 475 | transform: translateY(-50%); |
| 476 | background: rgba(168, 85, 247, 0.9); |
| 477 | color: white; |
| 478 | padding: 4px 8px; |
| 479 | border-radius: 4px; |
| 480 | font-size: 12px; |
| 481 | font-weight: 600; |
| 482 | white-space: nowrap; |
| 483 | font-family: 'Inter', -apple-system, sans-serif; |
| 484 | `; |
| 485 | spacingIndicator.appendChild(label); |
| 486 | overlay.appendChild(spacingIndicator); |
| 487 | } |
| 488 | }); |
| 489 | |
| 490 | // Add general annotation |
| 491 | const annotation = this.createAnnotation( |
| 492 | '📏 Spacing Units', |
| 493 | 'Measured in 8px increments', |
| 494 | 'top-right' |
| 495 | ); |
| 496 | overlay.appendChild(annotation); |
| 497 | |
| 498 | return overlay; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Create visual flow overlay (F-pattern or Z-pattern) |
| 503 | */ |
| 504 | createVisualFlowOverlay() { |
| 505 | const overlay = document.createElement('div'); |
| 506 | overlay.className = 'standards-overlay visual-flow-overlay'; |
| 507 | |
| 508 | // Detect if this is a hero/marketing section (Z-pattern) or content (F-pattern) |
| 509 | const hero = document.querySelector('.hero, [class*="hero"]'); |
| 510 | |
| 511 | if (hero) { |
| 512 | // Z-pattern for hero section |
| 513 | const canvas = document.createElement('canvas'); |
| 514 | canvas.width = window.innerWidth; |
| 515 | canvas.height = window.innerHeight; |
| 516 | canvas.style.cssText = ` |
| 517 | position: fixed; |
| 518 | top: 0; |
| 519 | left: 0; |
| 520 | pointer-events: none; |
| 521 | z-index: 10000; |
| 522 | `; |
| 523 | |
| 524 | const ctx = canvas.getContext('2d'); |
| 525 | ctx.strokeStyle = 'rgba(99, 102, 241, 0.6)'; |
| 526 | ctx.lineWidth = 3; |
| 527 | ctx.setLineDash([10, 10]); |
| 528 | |
| 529 | // Draw Z pattern |
| 530 | const startX = 100; |
| 531 | const startY = 100; |
| 532 | const endX = canvas.width - 100; |
| 533 | const midY = canvas.height / 3; |
| 534 | const endY = canvas.height / 2; |
| 535 | |
| 536 | ctx.beginPath(); |
| 537 | ctx.moveTo(startX, startY); |
| 538 | ctx.lineTo(endX, startY); // Top horizontal |
| 539 | ctx.lineTo(startX, midY); // Diagonal |
| 540 | ctx.lineTo(endX, midY); // Bottom horizontal |
| 541 | ctx.stroke(); |
| 542 | |
| 543 | // Add arrow heads |
| 544 | this.drawArrow(ctx, endX - 20, startY, 0); |
| 545 | this.drawArrow(ctx, endX - 20, midY, 0); |
| 546 | |
| 547 | overlay.appendChild(canvas); |
| 548 | |
| 549 | const annotation = this.createAnnotation( |
| 550 | '📐 Z-Pattern Flow', |
| 551 | 'Marketing/landing page scan pattern', |
| 552 | 'top-left' |
| 553 | ); |
| 554 | overlay.appendChild(annotation); |
| 555 | } else { |
| 556 | // F-pattern for content |
| 557 | const annotation = this.createAnnotation( |
| 558 | '📐 F-Pattern Flow', |
| 559 | 'Content-heavy page scan pattern', |
| 560 | 'top-left' |
| 561 | ); |
| 562 | overlay.appendChild(annotation); |
| 563 | } |
| 564 | |
| 565 | return overlay; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Create touch targets overlay (shows 44×44px minimum zones) |
| 570 | */ |
| 571 | createTouchTargetsOverlay() { |
| 572 | const overlay = document.createElement('div'); |
| 573 | overlay.className = 'standards-overlay touch-targets-overlay'; |
| 574 | |
| 575 | // Find all interactive elements |
| 576 | const interactiveElements = document.querySelectorAll( |
| 577 | 'button, a, input, [role="button"], [onclick], [tabindex="0"]' |
| 578 | ); |
| 579 | |
| 580 | let violations = 0; |
| 581 | |
| 582 | interactiveElements.forEach(el => { |
| 583 | const rect = el.getBoundingClientRect(); |
| 584 | const width = rect.width; |
| 585 | const height = rect.height; |
| 586 | |
| 587 | const isMobile = window.innerWidth < 768; |
| 588 | const minSize = isMobile ? 44 : 32; |
| 589 | |
| 590 | // Check if touch target is too small |
| 591 | const isTooSmall = width < minSize || height < minSize; |
| 592 | |
| 593 | if (rect.top >= 0 && rect.top < window.innerHeight) { // Only show visible elements |
| 594 | const highlighter = document.createElement('div'); |
| 595 | highlighter.className = 'touch-target-highlighter'; |
| 596 | highlighter.style.cssText = ` |
| 597 | position: absolute; |
| 598 | top: ${window.scrollY + rect.top}px; |
| 599 | left: ${rect.left}px; |
| 600 | width: ${rect.width}px; |
| 601 | height: ${rect.height}px; |
| 602 | border: 2px solid ${isTooSmall ? '#dc2626' : '#22c55e'}; |
| 603 | background: ${isTooSmall ? 'rgba(220, 38, 38, 0.1)' : 'rgba(34, 197, 94, 0.1)'}; |
| 604 | pointer-events: none; |
| 605 | z-index: 10000; |
| 606 | `; |
| 607 | |
| 608 | // Add size label |
| 609 | const label = document.createElement('div'); |
| 610 | label.className = 'size-label'; |
| 611 | label.textContent = `${Math.round(width)}×${Math.round(height)}px`; |
| 612 | label.style.cssText = ` |
| 613 | position: absolute; |
| 614 | top: -24px; |
| 615 | left: 0; |
| 616 | background: ${isTooSmall ? '#dc2626' : '#22c55e'}; |
| 617 | color: white; |
| 618 | padding: 2px 6px; |
| 619 | border-radius: 4px; |
| 620 | font-size: 10px; |
| 621 | font-weight: 600; |
| 622 | white-space: nowrap; |
| 623 | font-family: 'Inter', -apple-system, sans-serif; |
| 624 | `; |
| 625 | highlighter.appendChild(label); |
| 626 | overlay.appendChild(highlighter); |
| 627 | |
| 628 | if (isTooSmall) violations++; |
| 629 | } |
| 630 | }); |
| 631 | |
| 632 | // Add summary annotation |
| 633 | const annotation = this.createAnnotation( |
| 634 | '🎯 Touch Targets', |
| 635 | `Min: ${window.innerWidth < 768 ? '44×44px' : '32×32px'} | Violations: ${violations}`, |
| 636 | 'top-right' |
| 637 | ); |
| 638 | overlay.appendChild(annotation); |
| 639 | |
| 640 | return overlay; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Create content boundaries overlay (max-widths, safe areas) |
| 645 | */ |
| 646 | createContentBoundariesOverlay() { |
| 647 | const overlay = document.createElement('div'); |
| 648 | overlay.className = 'standards-overlay content-boundaries-overlay'; |
| 649 | |
| 650 | // Show max-width containers |
| 651 | const containers = document.querySelectorAll('.container, [class*="container"], main, article'); |
| 652 | |
| 653 | containers.forEach(container => { |
| 654 | const rect = container.getBoundingClientRect(); |
| 655 | const computedStyle = window.getComputedStyle(container); |
| 656 | const maxWidth = computedStyle.maxWidth; |
| 657 | |
| 658 | if (maxWidth && maxWidth !== 'none') { |
| 659 | const boundary = document.createElement('div'); |
| 660 | boundary.className = 'content-boundary'; |
| 661 | boundary.style.cssText = ` |
| 662 | position: absolute; |
| 663 | top: ${window.scrollY + rect.top}px; |
| 664 | left: ${rect.left}px; |
| 665 | width: ${rect.width}px; |
| 666 | height: ${rect.height}px; |
| 667 | border: 2px dashed rgba(124, 58, 237, 0.5); |
| 668 | pointer-events: none; |
| 669 | z-index: 10000; |
| 670 | `; |
| 671 | |
| 672 | const label = document.createElement('div'); |
| 673 | label.textContent = `max-width: ${maxWidth}`; |
| 674 | label.style.cssText = ` |
| 675 | position: absolute; |
| 676 | top: 10px; |
| 677 | right: 10px; |
| 678 | background: rgba(124, 58, 237, 0.9); |
| 679 | color: white; |
| 680 | padding: 4px 8px; |
| 681 | border-radius: 4px; |
| 682 | font-size: 12px; |
| 683 | font-weight: 600; |
| 684 | font-family: 'Inter', -apple-system, sans-serif; |
| 685 | `; |
| 686 | boundary.appendChild(label); |
| 687 | overlay.appendChild(boundary); |
| 688 | } |
| 689 | }); |
| 690 | |
| 691 | const annotation = this.createAnnotation( |
| 692 | '📦 Content Boundaries', |
| 693 | 'Max-width containers for readability', |
| 694 | 'top-right' |
| 695 | ); |
| 696 | overlay.appendChild(annotation); |
| 697 | |
| 698 | return overlay; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Helper: Create annotation label |
| 703 | */ |
| 704 | createAnnotation(title, description, position = 'top-left', parentElement = null) { |
| 705 | const annotation = document.createElement('div'); |
| 706 | annotation.className = `standards-annotation ${position}`; |
| 707 | annotation.innerHTML = ` |
| 708 | <div class="annotation-title">${title}</div> |
| 709 | <div class="annotation-description">${description}</div> |
| 710 | `; |
| 711 | |
| 712 | const positions = { |
| 713 | 'top-left': 'top: 20px; left: 20px;', |
| 714 | 'top-right': 'top: 20px; right: 20px;', |
| 715 | 'bottom-left': 'bottom: 20px; left: 20px;', |
| 716 | 'bottom-right': 'bottom: 20px; right: 20px;' |
| 717 | }; |
| 718 | |
| 719 | annotation.style.cssText = ` |
| 720 | position: fixed; |
| 721 | ${positions[position] || positions['top-left']} |
| 722 | background: rgba(17, 24, 39, 0.95); |
| 723 | color: white; |
| 724 | padding: 12px 16px; |
| 725 | border-radius: 8px; |
| 726 | font-family: 'Inter', -apple-system, sans-serif; |
| 727 | z-index: 10001; |
| 728 | pointer-events: none; |
| 729 | backdrop-filter: blur(10px); |
| 730 | box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2); |
| 731 | `; |
| 732 | |
| 733 | return annotation; |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Helper: Draw arrow on canvas |
| 738 | */ |
| 739 | drawArrow(ctx, x, y, angle) { |
| 740 | const headLength = 15; |
| 741 | ctx.save(); |
| 742 | ctx.translate(x, y); |
| 743 | ctx.rotate(angle); |
| 744 | ctx.beginPath(); |
| 745 | ctx.moveTo(0, 0); |
| 746 | ctx.lineTo(-headLength, -headLength/2); |
| 747 | ctx.lineTo(-headLength, headLength/2); |
| 748 | ctx.closePath(); |
| 749 | ctx.fillStyle = 'rgba(99, 102, 241, 0.6)'; |
| 750 | ctx.fill(); |
| 751 | ctx.restore(); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | // Initialize global instance |
| 756 | window.standardsOverlay = new StandardsOverlay(); |
| 757 | |
| 758 | // Keyboard shortcut: Ctrl+Shift+S to toggle all overlays |
| 759 | document.addEventListener('keydown', (e) => { |
| 760 | if (e.ctrlKey && e.shiftKey && e.key === 'S') { |
| 761 | e.preventDefault(); |
| 762 | if (window.standardsOverlay.activeOverlays.size > 0) { |
| 763 | window.standardsOverlay.hideAll(); |
| 764 | } else { |
| 765 | window.standardsOverlay.showAll(); |
| 766 | } |
| 767 | } |
| 768 | }); |
| 769 | |
| 770 | console.log('📐 DBBasic Standards Overlay loaded. Use Ctrl+Shift+S to toggle all overlays.'); |