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
semantic-components.esm.js · 48.7 KB · 1597 lines
| 1 | /** |
| 2 | * Semantic Components v1.0.0 |
| 3 | * semantic-components.js |
| 4 | * |
| 5 | * Intent-based, context-aware, modality-agnostic web components |
| 6 | * https://semantic-components.dev |
| 7 | * |
| 8 | * Copyright (c) 2025 Semantic Components Team |
| 9 | * Released under the MIT License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Semantic Web Components Library |
| 14 | * Version: 1.0.0 |
| 15 | * |
| 16 | * Intent-based, context-aware, modality-agnostic UI components |
| 17 | * for building applications that work across any interaction method. |
| 18 | */ |
| 19 | |
| 20 | // ============================================================================ |
| 21 | // CONTEXT MANAGER |
| 22 | // ============================================================================ |
| 23 | |
| 24 | class ContextManager { |
| 25 | constructor() { |
| 26 | this.context = { |
| 27 | modality: 'screen', |
| 28 | environment: { |
| 29 | noiseLevel: 0, |
| 30 | viewerCount: 1, |
| 31 | isPublic: false, |
| 32 | lighting: 'normal' |
| 33 | }, |
| 34 | user: { |
| 35 | isBusy: false, |
| 36 | inFocusMode: false, |
| 37 | prefersReducedMotion: false |
| 38 | }, |
| 39 | attention: { |
| 40 | maxScreenSpace: 100, |
| 41 | maxAudioTime: 10, |
| 42 | maxCognitiveLoad: 3, |
| 43 | usedScreenSpace: 0, |
| 44 | usedAudioTime: 0, |
| 45 | usedCognitiveLoad: 0 |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | this.components = new Set(); |
| 50 | this.listeners = new Set(); |
| 51 | } |
| 52 | |
| 53 | register(component) { |
| 54 | this.components.add(component); |
| 55 | this.notifyChange(); |
| 56 | } |
| 57 | |
| 58 | unregister(component) { |
| 59 | this.components.delete(component); |
| 60 | this.notifyChange(); |
| 61 | } |
| 62 | |
| 63 | updateContext(updates) { |
| 64 | this.context = { ...this.context, ...updates }; |
| 65 | this.notifyChange(); |
| 66 | } |
| 67 | |
| 68 | setModality(modality) { |
| 69 | this.context.modality = modality; |
| 70 | this.notifyChange(); |
| 71 | } |
| 72 | |
| 73 | notifyChange() { |
| 74 | this.allocateAttention(); |
| 75 | this.listeners.forEach(listener => listener(this.context)); |
| 76 | } |
| 77 | |
| 78 | allocateAttention() { |
| 79 | // Get all components that need attention |
| 80 | const needsAttention = Array.from(this.components) |
| 81 | .filter(c => c.computeAttentionScore) |
| 82 | .map(c => ({ |
| 83 | component: c, |
| 84 | score: c.computeAttentionScore(this.context) |
| 85 | })) |
| 86 | .sort((a, b) => b.score - a.score); |
| 87 | |
| 88 | // Reset attention budget |
| 89 | let screenUsed = 0; |
| 90 | let audioUsed = 0; |
| 91 | let cognitiveUsed = 0; |
| 92 | |
| 93 | // Allocate resources in priority order |
| 94 | needsAttention.forEach(({ component, score }) => { |
| 95 | const needs = component.getAttentionNeeds(); |
| 96 | |
| 97 | const canAllocate = |
| 98 | screenUsed + needs.screen <= this.context.attention.maxScreenSpace && |
| 99 | audioUsed + needs.audio <= this.context.attention.maxAudioTime && |
| 100 | cognitiveUsed + needs.cognitive <= this.context.attention.maxCognitiveLoad; |
| 101 | |
| 102 | if (canAllocate && score > 10) { |
| 103 | component.setAttribute('attention-state', 'allocated'); |
| 104 | screenUsed += needs.screen; |
| 105 | audioUsed += needs.audio; |
| 106 | cognitiveUsed += needs.cognitive; |
| 107 | } else { |
| 108 | component.setAttribute('attention-state', 'deferred'); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | this.context.attention.usedScreenSpace = screenUsed; |
| 113 | this.context.attention.usedAudioTime = audioUsed; |
| 114 | this.context.attention.usedCognitiveLoad = cognitiveUsed; |
| 115 | } |
| 116 | |
| 117 | onChange(callback) { |
| 118 | this.listeners.add(callback); |
| 119 | return () => this.listeners.delete(callback); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Global context manager |
| 124 | window.semanticContext = window.semanticContext || new ContextManager(); |
| 125 | |
| 126 | // ============================================================================ |
| 127 | // BASE SEMANTIC COMPONENT |
| 128 | // ============================================================================ |
| 129 | |
| 130 | class SemanticComponent extends HTMLElement { |
| 131 | constructor() { |
| 132 | super(); |
| 133 | this.attachShadow({ mode: 'open' }); |
| 134 | } |
| 135 | |
| 136 | connectedCallback() { |
| 137 | window.semanticContext.register(this); |
| 138 | this.render(); |
| 139 | this.setupEventListeners(); |
| 140 | } |
| 141 | |
| 142 | disconnectedCallback() { |
| 143 | window.semanticContext.unregister(this); |
| 144 | } |
| 145 | |
| 146 | computeAttentionScore(context) { |
| 147 | const urgency = this.getAttribute('urgency') || 'medium'; |
| 148 | const priority = this.getAttribute('priority') || 'passive-info'; |
| 149 | const weight = parseInt(this.getAttribute('attention-weight') || '50'); |
| 150 | |
| 151 | const urgencyMultiplier = { |
| 152 | 'critical': 2.0, |
| 153 | 'high': 1.5, |
| 154 | 'medium': 1.0, |
| 155 | 'low': 0.5 |
| 156 | }; |
| 157 | |
| 158 | let score = weight * urgencyMultiplier[urgency]; |
| 159 | |
| 160 | // Context penalties |
| 161 | if (context.user.isBusy && this.canDefer()) score *= 0.3; |
| 162 | if (context.user.inFocusMode && urgency !== 'critical') score *= 0.5; |
| 163 | if (context.environment.noiseLevel > 70 && context.modality.includes('voice')) score *= 0.7; |
| 164 | |
| 165 | return Math.round(score); |
| 166 | } |
| 167 | |
| 168 | getAttentionNeeds() { |
| 169 | return { |
| 170 | screen: parseInt(this.getAttribute('screen-space') || '10'), |
| 171 | audio: parseInt(this.getAttribute('audio-time') || '2'), |
| 172 | cognitive: parseFloat(this.getAttribute('cognitive-load') || '0.5') |
| 173 | }; |
| 174 | } |
| 175 | |
| 176 | canDefer() { |
| 177 | return this.getAttribute('can-defer') !== 'false'; |
| 178 | } |
| 179 | |
| 180 | shouldHideInContext(context) { |
| 181 | const sensitivity = this.getAttribute('sensitivity'); |
| 182 | if (sensitivity === 'private' && context.environment.viewerCount > 1) return true; |
| 183 | if (sensitivity === 'private' && context.environment.isPublic) return true; |
| 184 | if (this.getAttribute('urgency') === 'low' && context.user.inFocusMode) return true; |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | emitIntent(intentType, detail = {}) { |
| 189 | this.dispatchEvent(new CustomEvent('intent', { |
| 190 | bubbles: true, |
| 191 | composed: true, |
| 192 | detail: { type: intentType, ...detail } |
| 193 | })); |
| 194 | } |
| 195 | |
| 196 | getStyles() { |
| 197 | return ` |
| 198 | :host { |
| 199 | display: block; |
| 200 | box-sizing: border-box; |
| 201 | } |
| 202 | * { |
| 203 | box-sizing: border-box; |
| 204 | } |
| 205 | `; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // ============================================================================ |
| 210 | // ACTION COMPONENT |
| 211 | // ============================================================================ |
| 212 | |
| 213 | class SemanticAction extends SemanticComponent { |
| 214 | static get observedAttributes() { |
| 215 | return ['intent', 'sentiment', 'disabled']; |
| 216 | } |
| 217 | |
| 218 | render() { |
| 219 | const intent = this.getAttribute('intent') || 'secondary'; |
| 220 | const sentiment = this.getAttribute('sentiment') || 'neutral'; |
| 221 | const disabled = this.hasAttribute('disabled'); |
| 222 | |
| 223 | this.shadowRoot.innerHTML = ` |
| 224 | <style> |
| 225 | ${this.getStyles()} |
| 226 | |
| 227 | button { |
| 228 | min-height: 44px; |
| 229 | min-width: 88px; |
| 230 | padding: 12px 24px; |
| 231 | border: none; |
| 232 | border-radius: 8px; |
| 233 | font-size: 16px; |
| 234 | font-weight: 600; |
| 235 | font-family: inherit; |
| 236 | cursor: pointer; |
| 237 | transition: all 0.2s; |
| 238 | position: relative; |
| 239 | display: inline-flex; |
| 240 | align-items: center; |
| 241 | justify-content: center; |
| 242 | gap: 8px; |
| 243 | } |
| 244 | |
| 245 | button:focus-visible { |
| 246 | outline: 3px solid #4d9fff; |
| 247 | outline-offset: 2px; |
| 248 | } |
| 249 | |
| 250 | /* Intent styles */ |
| 251 | button[data-intent="primary"] { |
| 252 | background: #007aff; |
| 253 | color: white; |
| 254 | } |
| 255 | |
| 256 | button[data-intent="secondary"] { |
| 257 | background: #8e8e93; |
| 258 | color: white; |
| 259 | } |
| 260 | |
| 261 | button[data-intent="tertiary"] { |
| 262 | background: transparent; |
| 263 | color: #007aff; |
| 264 | border: 2px solid #007aff; |
| 265 | } |
| 266 | |
| 267 | /* Sentiment overrides */ |
| 268 | button[data-sentiment="constructive"] { |
| 269 | background: #34c759; |
| 270 | } |
| 271 | |
| 272 | button[data-sentiment="destructive"] { |
| 273 | background: #ff3b30; |
| 274 | } |
| 275 | |
| 276 | /* States */ |
| 277 | button:hover:not(:disabled) { |
| 278 | transform: translateY(-2px); |
| 279 | box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); |
| 280 | } |
| 281 | |
| 282 | button:active:not(:disabled) { |
| 283 | transform: scale(0.96); |
| 284 | } |
| 285 | |
| 286 | button:disabled { |
| 287 | opacity: 0.4; |
| 288 | cursor: not-allowed; |
| 289 | } |
| 290 | |
| 291 | /* Long-press indicator */ |
| 292 | button.inspecting::after { |
| 293 | content: ''; |
| 294 | position: absolute; |
| 295 | inset: -4px; |
| 296 | border: 3px solid #4d9fff; |
| 297 | border-radius: 10px; |
| 298 | animation: pulse 1s infinite; |
| 299 | } |
| 300 | |
| 301 | @keyframes pulse { |
| 302 | 0%, 100% { opacity: 1; } |
| 303 | 50% { opacity: 0.5; } |
| 304 | } |
| 305 | |
| 306 | @media (prefers-reduced-motion: reduce) { |
| 307 | button { |
| 308 | transition: none; |
| 309 | } |
| 310 | button:hover:not(:disabled) { |
| 311 | transform: none; |
| 312 | } |
| 313 | button:active:not(:disabled) { |
| 314 | transform: none; |
| 315 | } |
| 316 | } |
| 317 | </style> |
| 318 | <button |
| 319 | data-intent="${intent}" |
| 320 | data-sentiment="${sentiment}" |
| 321 | ?disabled="${disabled}" |
| 322 | part="button" |
| 323 | > |
| 324 | <slot></slot> |
| 325 | </button> |
| 326 | `; |
| 327 | } |
| 328 | |
| 329 | setupEventListeners() { |
| 330 | const button = this.shadowRoot.querySelector('button'); |
| 331 | let longPressTimer = null; |
| 332 | |
| 333 | // ACTIVATE intent |
| 334 | button.addEventListener('click', (e) => { |
| 335 | if (!this.hasAttribute('disabled')) { |
| 336 | this.emitIntent('activate'); |
| 337 | } |
| 338 | }); |
| 339 | |
| 340 | // INSPECT intent (long-press) |
| 341 | button.addEventListener('mousedown', () => { |
| 342 | longPressTimer = setTimeout(() => { |
| 343 | button.classList.add('inspecting'); |
| 344 | this.emitIntent('inspect'); |
| 345 | }, 500); |
| 346 | }); |
| 347 | |
| 348 | button.addEventListener('mouseup', () => { |
| 349 | clearTimeout(longPressTimer); |
| 350 | button.classList.remove('inspecting'); |
| 351 | }); |
| 352 | |
| 353 | button.addEventListener('mouseleave', () => { |
| 354 | clearTimeout(longPressTimer); |
| 355 | button.classList.remove('inspecting'); |
| 356 | }); |
| 357 | |
| 358 | // Keyboard support |
| 359 | button.addEventListener('keydown', (e) => { |
| 360 | if (e.key === 'Enter' || e.key === ' ') { |
| 361 | e.preventDefault(); |
| 362 | if (!this.hasAttribute('disabled')) { |
| 363 | this.emitIntent('activate'); |
| 364 | } |
| 365 | } |
| 366 | }); |
| 367 | } |
| 368 | |
| 369 | attributeChangedCallback() { |
| 370 | if (this.shadowRoot.innerHTML) { |
| 371 | this.render(); |
| 372 | this.setupEventListeners(); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // ============================================================================ |
| 378 | // CARD COMPONENT |
| 379 | // ============================================================================ |
| 380 | |
| 381 | class SemanticCard extends SemanticComponent { |
| 382 | static get observedAttributes() { |
| 383 | return ['media-url', 'media-alt']; |
| 384 | } |
| 385 | |
| 386 | render() { |
| 387 | const mediaUrl = this.getAttribute('media-url') || ''; |
| 388 | const mediaAlt = this.getAttribute('media-alt') || ''; |
| 389 | |
| 390 | this.shadowRoot.innerHTML = ` |
| 391 | <style> |
| 392 | ${this.getStyles()} |
| 393 | |
| 394 | .card { |
| 395 | background: #1c1c1e; |
| 396 | border: 2px solid #38383a; |
| 397 | border-radius: 12px; |
| 398 | overflow: hidden; |
| 399 | cursor: pointer; |
| 400 | transition: all 0.3s; |
| 401 | position: relative; |
| 402 | } |
| 403 | |
| 404 | .card:hover { |
| 405 | border-color: #007aff; |
| 406 | transform: translateY(-4px); |
| 407 | box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3); |
| 408 | } |
| 409 | |
| 410 | .card.inspecting { |
| 411 | border-color: #4d9fff; |
| 412 | box-shadow: 0 0 0 4px rgba(77, 159, 255, 0.2); |
| 413 | } |
| 414 | |
| 415 | .preview { |
| 416 | position: absolute; |
| 417 | bottom: 100%; |
| 418 | left: 0; |
| 419 | right: 0; |
| 420 | background: #2c2c2e; |
| 421 | border: 2px solid #007aff; |
| 422 | border-radius: 12px; |
| 423 | padding: 16px; |
| 424 | margin-bottom: 8px; |
| 425 | opacity: 0; |
| 426 | transform: translateY(20px); |
| 427 | transition: all 0.3s; |
| 428 | pointer-events: none; |
| 429 | z-index: 10; |
| 430 | } |
| 431 | |
| 432 | .card:hover .preview, |
| 433 | .card.inspecting .preview { |
| 434 | opacity: 1; |
| 435 | transform: translateY(0); |
| 436 | } |
| 437 | |
| 438 | .media { |
| 439 | width: 100%; |
| 440 | height: 200px; |
| 441 | object-fit: cover; |
| 442 | display: block; |
| 443 | } |
| 444 | |
| 445 | .media-placeholder { |
| 446 | width: 100%; |
| 447 | height: 200px; |
| 448 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 449 | display: flex; |
| 450 | align-items: center; |
| 451 | justify-content: center; |
| 452 | font-size: 48px; |
| 453 | } |
| 454 | |
| 455 | .content { |
| 456 | padding: 16px; |
| 457 | } |
| 458 | |
| 459 | .actions { |
| 460 | padding: 12px 16px; |
| 461 | background: #2c2c2e; |
| 462 | border-top: 1px solid #38383a; |
| 463 | display: flex; |
| 464 | gap: 8px; |
| 465 | } |
| 466 | |
| 467 | ::slotted([slot="title"]) { |
| 468 | font-size: 18px; |
| 469 | font-weight: 600; |
| 470 | margin: 0 0 8px 0; |
| 471 | } |
| 472 | |
| 473 | ::slotted([slot="description"]) { |
| 474 | color: #98989d; |
| 475 | font-size: 14px; |
| 476 | margin: 0; |
| 477 | } |
| 478 | |
| 479 | @media (prefers-reduced-motion: reduce) { |
| 480 | .card, .preview { |
| 481 | transition: none; |
| 482 | } |
| 483 | .card:hover { |
| 484 | transform: none; |
| 485 | } |
| 486 | } |
| 487 | </style> |
| 488 | <div class="card" part="card" role="article" tabindex="0"> |
| 489 | <div class="preview" part="preview"> |
| 490 | <slot name="preview">Preview content appears here</slot> |
| 491 | </div> |
| 492 | ${mediaUrl ? |
| 493 | `<img class="media" src="${mediaUrl}" alt="${mediaAlt}" part="media">` : |
| 494 | `<div class="media-placeholder" part="media-placeholder"> |
| 495 | <slot name="media-icon">📄</slot> |
| 496 | </div>` |
| 497 | } |
| 498 | <div class="content" part="content"> |
| 499 | <slot name="title"></slot> |
| 500 | <slot name="description"></slot> |
| 501 | <slot></slot> |
| 502 | </div> |
| 503 | <div class="actions" part="actions"> |
| 504 | <slot name="actions"></slot> |
| 505 | </div> |
| 506 | </div> |
| 507 | `; |
| 508 | } |
| 509 | |
| 510 | setupEventListeners() { |
| 511 | const card = this.shadowRoot.querySelector('.card'); |
| 512 | let longPressTimer = null; |
| 513 | |
| 514 | // ACTIVATE intent (navigate) |
| 515 | card.addEventListener('click', (e) => { |
| 516 | if (!e.target.closest('[slot="actions"]')) { |
| 517 | this.emitIntent('activate', { action: 'navigate' }); |
| 518 | } |
| 519 | }); |
| 520 | |
| 521 | // INSPECT intent (preview) |
| 522 | card.addEventListener('mouseenter', () => { |
| 523 | this.emitIntent('inspect', { action: 'preview' }); |
| 524 | }); |
| 525 | |
| 526 | // Long-press for touch |
| 527 | card.addEventListener('mousedown', (e) => { |
| 528 | if (!e.target.closest('[slot="actions"]')) { |
| 529 | longPressTimer = setTimeout(() => { |
| 530 | card.classList.add('inspecting'); |
| 531 | this.emitIntent('inspect', { action: 'preview', method: 'long-press' }); |
| 532 | }, 500); |
| 533 | } |
| 534 | }); |
| 535 | |
| 536 | card.addEventListener('mouseup', () => { |
| 537 | clearTimeout(longPressTimer); |
| 538 | setTimeout(() => card.classList.remove('inspecting'), 2000); |
| 539 | }); |
| 540 | |
| 541 | card.addEventListener('mouseleave', () => { |
| 542 | clearTimeout(longPressTimer); |
| 543 | card.classList.remove('inspecting'); |
| 544 | }); |
| 545 | |
| 546 | // Keyboard |
| 547 | card.addEventListener('keydown', (e) => { |
| 548 | if (e.key === 'Enter') { |
| 549 | this.emitIntent('activate', { action: 'navigate' }); |
| 550 | } |
| 551 | }); |
| 552 | } |
| 553 | |
| 554 | attributeChangedCallback() { |
| 555 | if (this.shadowRoot.innerHTML) { |
| 556 | this.render(); |
| 557 | this.setupEventListeners(); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // ============================================================================ |
| 563 | // FEEDBACK COMPONENT (Notifications) |
| 564 | // ============================================================================ |
| 565 | |
| 566 | class SemanticFeedback extends SemanticComponent { |
| 567 | static get observedAttributes() { |
| 568 | return ['urgency', 'type', 'auto-dismiss']; |
| 569 | } |
| 570 | |
| 571 | render() { |
| 572 | const urgency = this.getAttribute('urgency') || 'medium'; |
| 573 | const type = this.getAttribute('type') || 'notification'; |
| 574 | const autoDismiss = this.hasAttribute('auto-dismiss'); |
| 575 | |
| 576 | this.shadowRoot.innerHTML = ` |
| 577 | <style> |
| 578 | ${this.getStyles()} |
| 579 | |
| 580 | .feedback { |
| 581 | padding: 16px; |
| 582 | border-radius: 8px; |
| 583 | border-left: 4px solid; |
| 584 | background: #2c2c2e; |
| 585 | display: flex; |
| 586 | align-items: flex-start; |
| 587 | gap: 12px; |
| 588 | animation: slideIn 0.3s; |
| 589 | } |
| 590 | |
| 591 | @keyframes slideIn { |
| 592 | from { |
| 593 | transform: translateX(100%); |
| 594 | opacity: 0; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | .feedback[data-urgency="critical"] { |
| 599 | border-color: #ff3b30; |
| 600 | background: rgba(255, 59, 48, 0.1); |
| 601 | } |
| 602 | |
| 603 | .feedback[data-urgency="high"] { |
| 604 | border-color: #ff9500; |
| 605 | background: rgba(255, 149, 0, 0.1); |
| 606 | } |
| 607 | |
| 608 | .feedback[data-urgency="medium"] { |
| 609 | border-color: #007aff; |
| 610 | background: rgba(0, 122, 255, 0.1); |
| 611 | } |
| 612 | |
| 613 | .feedback[data-urgency="low"] { |
| 614 | border-color: #8e8e93; |
| 615 | } |
| 616 | |
| 617 | .icon { |
| 618 | font-size: 24px; |
| 619 | flex-shrink: 0; |
| 620 | } |
| 621 | |
| 622 | .content { |
| 623 | flex: 1; |
| 624 | } |
| 625 | |
| 626 | .dismiss { |
| 627 | background: none; |
| 628 | border: none; |
| 629 | color: #98989d; |
| 630 | cursor: pointer; |
| 631 | font-size: 20px; |
| 632 | padding: 0; |
| 633 | width: 24px; |
| 634 | height: 24px; |
| 635 | } |
| 636 | |
| 637 | .dismiss:hover { |
| 638 | color: #fff; |
| 639 | } |
| 640 | |
| 641 | :host([attention-state="deferred"]) { |
| 642 | opacity: 0.5; |
| 643 | transform: scale(0.95); |
| 644 | } |
| 645 | |
| 646 | :host([hidden]) { |
| 647 | display: none; |
| 648 | } |
| 649 | |
| 650 | @media (prefers-reduced-motion: reduce) { |
| 651 | .feedback { |
| 652 | animation: none; |
| 653 | } |
| 654 | } |
| 655 | </style> |
| 656 | <div class="feedback" data-urgency="${urgency}" data-type="${type}" role="alert" part="feedback"> |
| 657 | <div class="icon" part="icon"> |
| 658 | <slot name="icon">${this.getIcon(urgency)}</slot> |
| 659 | </div> |
| 660 | <div class="content" part="content"> |
| 661 | <slot></slot> |
| 662 | </div> |
| 663 | <button class="dismiss" aria-label="Dismiss" part="dismiss">×</button> |
| 664 | </div> |
| 665 | `; |
| 666 | |
| 667 | if (autoDismiss) { |
| 668 | setTimeout(() => this.dismiss(), 5000); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | getIcon(urgency) { |
| 673 | const icons = { |
| 674 | 'critical': '⚠️', |
| 675 | 'high': '❗', |
| 676 | 'medium': 'ℹ️', |
| 677 | 'low': '💡' |
| 678 | }; |
| 679 | return icons[urgency] || icons.medium; |
| 680 | } |
| 681 | |
| 682 | setupEventListeners() { |
| 683 | const dismissBtn = this.shadowRoot.querySelector('.dismiss'); |
| 684 | dismissBtn.addEventListener('click', () => this.dismiss()); |
| 685 | } |
| 686 | |
| 687 | dismiss() { |
| 688 | this.emitIntent('dismiss'); |
| 689 | this.remove(); |
| 690 | } |
| 691 | |
| 692 | attributeChangedCallback() { |
| 693 | if (this.shadowRoot.innerHTML) { |
| 694 | this.render(); |
| 695 | this.setupEventListeners(); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | // ============================================================================ |
| 701 | // INPUT ADJUSTER COMPONENT |
| 702 | // ============================================================================ |
| 703 | |
| 704 | class SemanticAdjuster extends SemanticComponent { |
| 705 | static get observedAttributes() { |
| 706 | return ['value', 'min', 'max', 'step', 'label']; |
| 707 | } |
| 708 | |
| 709 | constructor() { |
| 710 | super(); |
| 711 | this._value = 50; |
| 712 | } |
| 713 | |
| 714 | get value() { |
| 715 | return this._value; |
| 716 | } |
| 717 | |
| 718 | set value(val) { |
| 719 | const min = parseFloat(this.getAttribute('min') || '0'); |
| 720 | const max = parseFloat(this.getAttribute('max') || '100'); |
| 721 | this._value = Math.max(min, Math.min(max, parseFloat(val))); |
| 722 | this.render(); |
| 723 | this.emitIntent('adjust', { value: this._value }); |
| 724 | } |
| 725 | |
| 726 | render() { |
| 727 | const label = this.getAttribute('label') || 'Value'; |
| 728 | const min = this.getAttribute('min') || '0'; |
| 729 | const max = this.getAttribute('max') || '100'; |
| 730 | const step = this.getAttribute('step') || '1'; |
| 731 | |
| 732 | this.shadowRoot.innerHTML = ` |
| 733 | <style> |
| 734 | ${this.getStyles()} |
| 735 | |
| 736 | .adjuster { |
| 737 | display: flex; |
| 738 | flex-direction: column; |
| 739 | gap: 12px; |
| 740 | } |
| 741 | |
| 742 | .label { |
| 743 | font-size: 16px; |
| 744 | font-weight: 600; |
| 745 | color: #fff; |
| 746 | } |
| 747 | |
| 748 | .controls { |
| 749 | display: flex; |
| 750 | align-items: center; |
| 751 | gap: 16px; |
| 752 | } |
| 753 | |
| 754 | .value { |
| 755 | font-size: 32px; |
| 756 | font-weight: 700; |
| 757 | color: #007aff; |
| 758 | min-width: 80px; |
| 759 | text-align: center; |
| 760 | } |
| 761 | |
| 762 | button { |
| 763 | width: 48px; |
| 764 | height: 48px; |
| 765 | border-radius: 50%; |
| 766 | background: #2c2c2e; |
| 767 | border: 2px solid #38383a; |
| 768 | color: #fff; |
| 769 | font-size: 24px; |
| 770 | cursor: pointer; |
| 771 | transition: all 0.2s; |
| 772 | display: flex; |
| 773 | align-items: center; |
| 774 | justify-content: center; |
| 775 | } |
| 776 | |
| 777 | button:hover { |
| 778 | background: #007aff; |
| 779 | border-color: #007aff; |
| 780 | transform: scale(1.1); |
| 781 | } |
| 782 | |
| 783 | button:active { |
| 784 | transform: scale(0.95); |
| 785 | } |
| 786 | |
| 787 | .slider { |
| 788 | flex: 1; |
| 789 | height: 8px; |
| 790 | background: #38383a; |
| 791 | border-radius: 4px; |
| 792 | position: relative; |
| 793 | cursor: pointer; |
| 794 | } |
| 795 | |
| 796 | .slider-fill { |
| 797 | height: 100%; |
| 798 | background: #007aff; |
| 799 | border-radius: 4px; |
| 800 | transition: width 0.2s; |
| 801 | } |
| 802 | |
| 803 | .slider-thumb { |
| 804 | position: absolute; |
| 805 | top: 50%; |
| 806 | transform: translate(-50%, -50%); |
| 807 | width: 24px; |
| 808 | height: 24px; |
| 809 | background: white; |
| 810 | border: 3px solid #007aff; |
| 811 | border-radius: 50%; |
| 812 | cursor: grab; |
| 813 | transition: all 0.2s; |
| 814 | } |
| 815 | |
| 816 | .slider-thumb:active { |
| 817 | cursor: grabbing; |
| 818 | transform: translate(-50%, -50%) scale(1.2); |
| 819 | } |
| 820 | |
| 821 | @media (prefers-reduced-motion: reduce) { |
| 822 | button, .slider-fill, .slider-thumb { |
| 823 | transition: none; |
| 824 | } |
| 825 | button:hover { |
| 826 | transform: none; |
| 827 | } |
| 828 | } |
| 829 | </style> |
| 830 | <div class="adjuster" part="adjuster"> |
| 831 | <label class="label" part="label">${label}</label> |
| 832 | <div class="controls"> |
| 833 | <button class="decrease" aria-label="Decrease" part="decrease">−</button> |
| 834 | <div class="value" part="value">${this._value}</div> |
| 835 | <button class="increase" aria-label="Increase" part="increase">+</button> |
| 836 | </div> |
| 837 | <div class="slider" part="slider"> |
| 838 | <div class="slider-fill" style="width: ${this.getPercentage()}%"></div> |
| 839 | <div class="slider-thumb" style="left: ${this.getPercentage()}%"></div> |
| 840 | </div> |
| 841 | </div> |
| 842 | `; |
| 843 | } |
| 844 | |
| 845 | getPercentage() { |
| 846 | const min = parseFloat(this.getAttribute('min') || '0'); |
| 847 | const max = parseFloat(this.getAttribute('max') || '100'); |
| 848 | return ((this._value - min) / (max - min)) * 100; |
| 849 | } |
| 850 | |
| 851 | setupEventListeners() { |
| 852 | const decreaseBtn = this.shadowRoot.querySelector('.decrease'); |
| 853 | const increaseBtn = this.shadowRoot.querySelector('.increase'); |
| 854 | const slider = this.shadowRoot.querySelector('.slider'); |
| 855 | const step = parseFloat(this.getAttribute('step') || '5'); |
| 856 | |
| 857 | decreaseBtn.addEventListener('click', () => { |
| 858 | this.value = this._value - step; |
| 859 | }); |
| 860 | |
| 861 | increaseBtn.addEventListener('click', () => { |
| 862 | this.value = this._value + step; |
| 863 | }); |
| 864 | |
| 865 | let isDragging = false; |
| 866 | |
| 867 | const updateFromPosition = (clientX) => { |
| 868 | const rect = slider.getBoundingClientRect(); |
| 869 | const percent = Math.max(0, Math.min(100, ((clientX - rect.left) / rect.width) * 100)); |
| 870 | const min = parseFloat(this.getAttribute('min') || '0'); |
| 871 | const max = parseFloat(this.getAttribute('max') || '100'); |
| 872 | this.value = min + (percent / 100) * (max - min); |
| 873 | }; |
| 874 | |
| 875 | slider.addEventListener('mousedown', (e) => { |
| 876 | isDragging = true; |
| 877 | updateFromPosition(e.clientX); |
| 878 | }); |
| 879 | |
| 880 | document.addEventListener('mousemove', (e) => { |
| 881 | if (isDragging) updateFromPosition(e.clientX); |
| 882 | }); |
| 883 | |
| 884 | document.addEventListener('mouseup', () => { |
| 885 | isDragging = false; |
| 886 | }); |
| 887 | |
| 888 | // Touch support |
| 889 | slider.addEventListener('touchstart', (e) => { |
| 890 | isDragging = true; |
| 891 | updateFromPosition(e.touches[0].clientX); |
| 892 | }); |
| 893 | |
| 894 | document.addEventListener('touchmove', (e) => { |
| 895 | if (isDragging) updateFromPosition(e.touches[0].clientX); |
| 896 | }); |
| 897 | |
| 898 | document.addEventListener('touchend', () => { |
| 899 | isDragging = false; |
| 900 | }); |
| 901 | } |
| 902 | |
| 903 | attributeChangedCallback(name, oldValue, newValue) { |
| 904 | if (name === 'value' && newValue !== oldValue) { |
| 905 | this._value = parseFloat(newValue); |
| 906 | } |
| 907 | if (this.shadowRoot.innerHTML) { |
| 908 | this.render(); |
| 909 | this.setupEventListeners(); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | // ============================================================================ |
| 915 | // INPUT COMPONENT |
| 916 | // ============================================================================ |
| 917 | |
| 918 | class SemanticInput extends SemanticComponent { |
| 919 | static get observedAttributes() { |
| 920 | return ['type', 'label', 'placeholder', 'value', 'required', 'disabled']; |
| 921 | } |
| 922 | |
| 923 | constructor() { |
| 924 | super(); |
| 925 | this._value = ''; |
| 926 | } |
| 927 | |
| 928 | get value() { |
| 929 | return this._value; |
| 930 | } |
| 931 | |
| 932 | set value(val) { |
| 933 | this._value = val; |
| 934 | const input = this.shadowRoot.querySelector('input, textarea'); |
| 935 | if (input) input.value = val; |
| 936 | this.emitIntent('input', { value: val }); |
| 937 | } |
| 938 | |
| 939 | render() { |
| 940 | const type = this.getAttribute('type') || 'text'; |
| 941 | const label = this.getAttribute('label') || ''; |
| 942 | const placeholder = this.getAttribute('placeholder') || ''; |
| 943 | const required = this.hasAttribute('required'); |
| 944 | const disabled = this.hasAttribute('disabled'); |
| 945 | |
| 946 | const inputTypes = { |
| 947 | 'text': 'text', |
| 948 | 'email': 'email', |
| 949 | 'password': 'password', |
| 950 | 'number': 'number', |
| 951 | 'tel': 'tel', |
| 952 | 'url': 'url', |
| 953 | 'search': 'search' |
| 954 | }; |
| 955 | |
| 956 | const isTextarea = type === 'textarea'; |
| 957 | const inputType = inputTypes[type] || 'text'; |
| 958 | |
| 959 | this.shadowRoot.innerHTML = ` |
| 960 | <style> |
| 961 | ${this.getStyles()} |
| 962 | |
| 963 | .input-wrapper { |
| 964 | display: flex; |
| 965 | flex-direction: column; |
| 966 | gap: 8px; |
| 967 | } |
| 968 | |
| 969 | label { |
| 970 | font-size: 14px; |
| 971 | font-weight: 600; |
| 972 | color: #fff; |
| 973 | } |
| 974 | |
| 975 | input, textarea { |
| 976 | min-height: 44px; |
| 977 | padding: 12px 16px; |
| 978 | background: #1c1c1e; |
| 979 | border: 2px solid #38383a; |
| 980 | border-radius: 8px; |
| 981 | color: #fff; |
| 982 | font-size: 16px; |
| 983 | font-family: inherit; |
| 984 | transition: all 0.2s; |
| 985 | } |
| 986 | |
| 987 | textarea { |
| 988 | min-height: 120px; |
| 989 | resize: vertical; |
| 990 | } |
| 991 | |
| 992 | input:focus, textarea:focus { |
| 993 | outline: none; |
| 994 | border-color: #007aff; |
| 995 | box-shadow: 0 0 0 4px rgba(0, 122, 255, 0.1); |
| 996 | } |
| 997 | |
| 998 | input::placeholder, textarea::placeholder { |
| 999 | color: #98989d; |
| 1000 | } |
| 1001 | |
| 1002 | input:disabled, textarea:disabled { |
| 1003 | opacity: 0.5; |
| 1004 | cursor: not-allowed; |
| 1005 | } |
| 1006 | |
| 1007 | .voice-hint { |
| 1008 | font-size: 12px; |
| 1009 | color: #98989d; |
| 1010 | display: none; |
| 1011 | } |
| 1012 | |
| 1013 | :host([data-modality="voice-screen"]) .voice-hint, |
| 1014 | :host([data-modality="voice-only"]) .voice-hint { |
| 1015 | display: block; |
| 1016 | } |
| 1017 | </style> |
| 1018 | <div class="input-wrapper" part="wrapper"> |
| 1019 | ${label ? `<label part="label">${label}${required ? ' *' : ''}</label>` : ''} |
| 1020 | ${isTextarea ? |
| 1021 | `<textarea |
| 1022 | part="input" |
| 1023 | placeholder="${placeholder}" |
| 1024 | ?required="${required}" |
| 1025 | ?disabled="${disabled}" |
| 1026 | ></textarea>` : |
| 1027 | `<input |
| 1028 | part="input" |
| 1029 | type="${inputType}" |
| 1030 | placeholder="${placeholder}" |
| 1031 | ?required="${required}" |
| 1032 | ?disabled="${disabled}" |
| 1033 | />` |
| 1034 | } |
| 1035 | <div class="voice-hint" part="voice-hint"> |
| 1036 | Say "Type ${label || 'input'}" to dictate |
| 1037 | </div> |
| 1038 | </div> |
| 1039 | `; |
| 1040 | } |
| 1041 | |
| 1042 | setupEventListeners() { |
| 1043 | const input = this.shadowRoot.querySelector('input, textarea'); |
| 1044 | |
| 1045 | input.addEventListener('input', (e) => { |
| 1046 | this._value = e.target.value; |
| 1047 | this.emitIntent('input', { value: this._value }); |
| 1048 | }); |
| 1049 | |
| 1050 | input.addEventListener('change', (e) => { |
| 1051 | this.emitIntent('change', { value: this._value }); |
| 1052 | }); |
| 1053 | |
| 1054 | input.addEventListener('focus', () => { |
| 1055 | this.emitIntent('focus'); |
| 1056 | }); |
| 1057 | |
| 1058 | input.addEventListener('blur', () => { |
| 1059 | this.emitIntent('blur', { value: this._value }); |
| 1060 | }); |
| 1061 | } |
| 1062 | |
| 1063 | attributeChangedCallback() { |
| 1064 | if (this.shadowRoot.innerHTML) { |
| 1065 | this.render(); |
| 1066 | this.setupEventListeners(); |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | // ============================================================================ |
| 1072 | // MODAL COMPONENT |
| 1073 | // ============================================================================ |
| 1074 | |
| 1075 | class SemanticModal extends SemanticComponent { |
| 1076 | static get observedAttributes() { |
| 1077 | return ['open', 'urgency', 'dismissible']; |
| 1078 | } |
| 1079 | |
| 1080 | render() { |
| 1081 | const urgency = this.getAttribute('urgency') || 'medium'; |
| 1082 | const dismissible = this.getAttribute('dismissible') !== 'false'; |
| 1083 | |
| 1084 | this.shadowRoot.innerHTML = ` |
| 1085 | <style> |
| 1086 | ${this.getStyles()} |
| 1087 | |
| 1088 | :host { |
| 1089 | display: none; |
| 1090 | position: fixed; |
| 1091 | top: 0; |
| 1092 | left: 0; |
| 1093 | right: 0; |
| 1094 | bottom: 0; |
| 1095 | z-index: 9999; |
| 1096 | align-items: center; |
| 1097 | justify-content: center; |
| 1098 | } |
| 1099 | |
| 1100 | :host([open]) { |
| 1101 | display: flex; |
| 1102 | } |
| 1103 | |
| 1104 | .backdrop { |
| 1105 | position: absolute; |
| 1106 | top: 0; |
| 1107 | left: 0; |
| 1108 | right: 0; |
| 1109 | bottom: 0; |
| 1110 | background: rgba(0, 0, 0, 0.8); |
| 1111 | animation: fadeIn 0.2s; |
| 1112 | } |
| 1113 | |
| 1114 | @keyframes fadeIn { |
| 1115 | from { opacity: 0; } |
| 1116 | } |
| 1117 | |
| 1118 | .modal { |
| 1119 | position: relative; |
| 1120 | background: #1c1c1e; |
| 1121 | border-radius: 16px; |
| 1122 | max-width: 90vw; |
| 1123 | max-height: 90vh; |
| 1124 | overflow: auto; |
| 1125 | animation: slideUp 0.3s; |
| 1126 | box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); |
| 1127 | } |
| 1128 | |
| 1129 | @keyframes slideUp { |
| 1130 | from { |
| 1131 | transform: translateY(50px); |
| 1132 | opacity: 0; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | .modal[data-urgency="critical"] { |
| 1137 | border: 3px solid #ff3b30; |
| 1138 | } |
| 1139 | |
| 1140 | .header { |
| 1141 | padding: 24px; |
| 1142 | border-bottom: 1px solid #38383a; |
| 1143 | display: flex; |
| 1144 | align-items: center; |
| 1145 | justify-content: space-between; |
| 1146 | } |
| 1147 | |
| 1148 | .close-btn { |
| 1149 | background: none; |
| 1150 | border: none; |
| 1151 | color: #98989d; |
| 1152 | font-size: 32px; |
| 1153 | cursor: pointer; |
| 1154 | padding: 0; |
| 1155 | width: 32px; |
| 1156 | height: 32px; |
| 1157 | display: flex; |
| 1158 | align-items: center; |
| 1159 | justify-content: center; |
| 1160 | border-radius: 50%; |
| 1161 | transition: all 0.2s; |
| 1162 | } |
| 1163 | |
| 1164 | .close-btn:hover { |
| 1165 | background: #38383a; |
| 1166 | color: #fff; |
| 1167 | } |
| 1168 | |
| 1169 | .content { |
| 1170 | padding: 24px; |
| 1171 | } |
| 1172 | |
| 1173 | .actions { |
| 1174 | padding: 24px; |
| 1175 | border-top: 1px solid #38383a; |
| 1176 | display: flex; |
| 1177 | gap: 12px; |
| 1178 | justify-content: flex-end; |
| 1179 | } |
| 1180 | |
| 1181 | @media (prefers-reduced-motion: reduce) { |
| 1182 | .backdrop, .modal { |
| 1183 | animation: none; |
| 1184 | } |
| 1185 | } |
| 1186 | </style> |
| 1187 | <div class="backdrop" part="backdrop"></div> |
| 1188 | <div class="modal" data-urgency="${urgency}" part="modal" role="dialog" aria-modal="true"> |
| 1189 | <div class="header" part="header"> |
| 1190 | <slot name="header"></slot> |
| 1191 | ${dismissible ? '<button class="close-btn" aria-label="Close" part="close">×</button>' : ''} |
| 1192 | </div> |
| 1193 | <div class="content" part="content"> |
| 1194 | <slot></slot> |
| 1195 | </div> |
| 1196 | <div class="actions" part="actions"> |
| 1197 | <slot name="actions"></slot> |
| 1198 | </div> |
| 1199 | </div> |
| 1200 | `; |
| 1201 | } |
| 1202 | |
| 1203 | setupEventListeners() { |
| 1204 | const backdrop = this.shadowRoot.querySelector('.backdrop'); |
| 1205 | const closeBtn = this.shadowRoot.querySelector('.close-btn'); |
| 1206 | const dismissible = this.getAttribute('dismissible') !== 'false'; |
| 1207 | |
| 1208 | if (dismissible) { |
| 1209 | backdrop?.addEventListener('click', () => this.close()); |
| 1210 | closeBtn?.addEventListener('click', () => this.close()); |
| 1211 | |
| 1212 | // ESC key |
| 1213 | document.addEventListener('keydown', (e) => { |
| 1214 | if (e.key === 'Escape' && this.hasAttribute('open')) { |
| 1215 | this.close(); |
| 1216 | } |
| 1217 | }); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | open() { |
| 1222 | this.setAttribute('open', ''); |
| 1223 | this.emitIntent('open'); |
| 1224 | // Trap focus |
| 1225 | const firstFocusable = this.shadowRoot.querySelector('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'); |
| 1226 | firstFocusable?.focus(); |
| 1227 | } |
| 1228 | |
| 1229 | close() { |
| 1230 | this.removeAttribute('open'); |
| 1231 | this.emitIntent('close'); |
| 1232 | } |
| 1233 | |
| 1234 | attributeChangedCallback(name) { |
| 1235 | if (name === 'open' && this.shadowRoot.innerHTML) { |
| 1236 | if (this.hasAttribute('open')) { |
| 1237 | document.body.style.overflow = 'hidden'; |
| 1238 | } else { |
| 1239 | document.body.style.overflow = ''; |
| 1240 | } |
| 1241 | } |
| 1242 | if (this.shadowRoot.innerHTML) { |
| 1243 | this.render(); |
| 1244 | this.setupEventListeners(); |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | // ============================================================================ |
| 1250 | // NAVIGATOR COMPONENT |
| 1251 | // ============================================================================ |
| 1252 | |
| 1253 | class SemanticNavigator extends SemanticComponent { |
| 1254 | static get observedAttributes() { |
| 1255 | return ['type', 'current']; |
| 1256 | } |
| 1257 | |
| 1258 | render() { |
| 1259 | const type = this.getAttribute('type') || 'horizontal'; |
| 1260 | const current = this.getAttribute('current') || ''; |
| 1261 | |
| 1262 | this.shadowRoot.innerHTML = ` |
| 1263 | <style> |
| 1264 | ${this.getStyles()} |
| 1265 | |
| 1266 | .navigator { |
| 1267 | display: flex; |
| 1268 | gap: 4px; |
| 1269 | } |
| 1270 | |
| 1271 | .navigator[data-type="horizontal"] { |
| 1272 | flex-direction: row; |
| 1273 | } |
| 1274 | |
| 1275 | .navigator[data-type="vertical"] { |
| 1276 | flex-direction: column; |
| 1277 | } |
| 1278 | |
| 1279 | ::slotted(*) { |
| 1280 | display: flex; |
| 1281 | align-items: center; |
| 1282 | padding: 12px 20px; |
| 1283 | border-radius: 8px; |
| 1284 | cursor: pointer; |
| 1285 | transition: all 0.2s; |
| 1286 | color: #98989d; |
| 1287 | text-decoration: none; |
| 1288 | user-select: none; |
| 1289 | } |
| 1290 | |
| 1291 | ::slotted(:hover) { |
| 1292 | background: #2c2c2e; |
| 1293 | color: #fff; |
| 1294 | } |
| 1295 | |
| 1296 | ::slotted([aria-current="page"]), |
| 1297 | ::slotted(.current) { |
| 1298 | background: #007aff; |
| 1299 | color: #fff; |
| 1300 | font-weight: 600; |
| 1301 | } |
| 1302 | |
| 1303 | ::slotted(:focus-visible) { |
| 1304 | outline: 3px solid #4d9fff; |
| 1305 | outline-offset: 2px; |
| 1306 | } |
| 1307 | </style> |
| 1308 | <nav class="navigator" data-type="${type}" part="navigator" role="navigation"> |
| 1309 | <slot></slot> |
| 1310 | </nav> |
| 1311 | `; |
| 1312 | } |
| 1313 | |
| 1314 | setupEventListeners() { |
| 1315 | this.addEventListener('click', (e) => { |
| 1316 | const target = e.target; |
| 1317 | if (target.hasAttribute('data-nav-id')) { |
| 1318 | this.emitIntent('navigate', { |
| 1319 | id: target.getAttribute('data-nav-id'), |
| 1320 | href: target.getAttribute('href') |
| 1321 | }); |
| 1322 | } |
| 1323 | }); |
| 1324 | } |
| 1325 | |
| 1326 | attributeChangedCallback() { |
| 1327 | if (this.shadowRoot.innerHTML) { |
| 1328 | this.render(); |
| 1329 | this.setupEventListeners(); |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | // ============================================================================ |
| 1335 | // LIST COMPONENT |
| 1336 | // ============================================================================ |
| 1337 | |
| 1338 | class SemanticList extends SemanticComponent { |
| 1339 | static get observedAttributes() { |
| 1340 | return ['type', 'selectable']; |
| 1341 | } |
| 1342 | |
| 1343 | render() { |
| 1344 | const type = this.getAttribute('type') || 'default'; |
| 1345 | const selectable = this.hasAttribute('selectable'); |
| 1346 | |
| 1347 | this.shadowRoot.innerHTML = ` |
| 1348 | <style> |
| 1349 | ${this.getStyles()} |
| 1350 | |
| 1351 | .list { |
| 1352 | display: flex; |
| 1353 | flex-direction: column; |
| 1354 | gap: 2px; |
| 1355 | } |
| 1356 | |
| 1357 | ::slotted(*) { |
| 1358 | padding: 16px; |
| 1359 | background: #1c1c1e; |
| 1360 | border: 1px solid #38383a; |
| 1361 | border-radius: 8px; |
| 1362 | transition: all 0.2s; |
| 1363 | } |
| 1364 | |
| 1365 | :host([selectable]) ::slotted(*) { |
| 1366 | cursor: pointer; |
| 1367 | } |
| 1368 | |
| 1369 | :host([selectable]) ::slotted(:hover) { |
| 1370 | background: #2c2c2e; |
| 1371 | border-color: #007aff; |
| 1372 | } |
| 1373 | |
| 1374 | :host([selectable]) ::slotted([aria-selected="true"]), |
| 1375 | :host([selectable]) ::slotted(.selected) { |
| 1376 | background: rgba(0, 122, 255, 0.1); |
| 1377 | border-color: #007aff; |
| 1378 | } |
| 1379 | |
| 1380 | ::slotted(:focus-visible) { |
| 1381 | outline: 3px solid #4d9fff; |
| 1382 | outline-offset: 2px; |
| 1383 | } |
| 1384 | |
| 1385 | .list[data-type="compact"] ::slotted(*) { |
| 1386 | padding: 12px; |
| 1387 | } |
| 1388 | |
| 1389 | .list[data-type="dense"] ::slotted(*) { |
| 1390 | padding: 8px 12px; |
| 1391 | border-radius: 4px; |
| 1392 | } |
| 1393 | </style> |
| 1394 | <div class="list" data-type="${type}" part="list" role="list"> |
| 1395 | <slot></slot> |
| 1396 | </div> |
| 1397 | `; |
| 1398 | } |
| 1399 | |
| 1400 | setupEventListeners() { |
| 1401 | if (this.hasAttribute('selectable')) { |
| 1402 | this.addEventListener('click', (e) => { |
| 1403 | const item = e.target.closest('[data-item-id]'); |
| 1404 | if (item) { |
| 1405 | // Toggle selection |
| 1406 | const isSelected = item.getAttribute('aria-selected') === 'true'; |
| 1407 | item.setAttribute('aria-selected', !isSelected); |
| 1408 | |
| 1409 | this.emitIntent('select', { |
| 1410 | id: item.getAttribute('data-item-id'), |
| 1411 | selected: !isSelected |
| 1412 | }); |
| 1413 | } |
| 1414 | }); |
| 1415 | |
| 1416 | // Keyboard navigation |
| 1417 | this.addEventListener('keydown', (e) => { |
| 1418 | const items = Array.from(this.querySelectorAll('[data-item-id]')); |
| 1419 | const current = this.querySelector('[aria-selected="true"]'); |
| 1420 | const currentIndex = items.indexOf(current); |
| 1421 | |
| 1422 | if (e.key === 'ArrowDown' && currentIndex < items.length - 1) { |
| 1423 | e.preventDefault(); |
| 1424 | items[currentIndex + 1].focus(); |
| 1425 | } else if (e.key === 'ArrowUp' && currentIndex > 0) { |
| 1426 | e.preventDefault(); |
| 1427 | items[currentIndex - 1].focus(); |
| 1428 | } else if (e.key === 'Enter' || e.key === ' ') { |
| 1429 | e.preventDefault(); |
| 1430 | e.target.click(); |
| 1431 | } |
| 1432 | }); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | attributeChangedCallback() { |
| 1437 | if (this.shadowRoot.innerHTML) { |
| 1438 | this.render(); |
| 1439 | this.setupEventListeners(); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | // ============================================================================ |
| 1445 | // MENU COMPONENT |
| 1446 | // ============================================================================ |
| 1447 | |
| 1448 | class SemanticMenu extends SemanticComponent { |
| 1449 | static get observedAttributes() { |
| 1450 | return ['open', 'anchor']; |
| 1451 | } |
| 1452 | |
| 1453 | render() { |
| 1454 | this.shadowRoot.innerHTML = ` |
| 1455 | <style> |
| 1456 | ${this.getStyles()} |
| 1457 | |
| 1458 | :host { |
| 1459 | position: absolute; |
| 1460 | display: none; |
| 1461 | z-index: 10000; |
| 1462 | } |
| 1463 | |
| 1464 | :host([open]) { |
| 1465 | display: block; |
| 1466 | } |
| 1467 | |
| 1468 | .menu { |
| 1469 | background: #2c2c2e; |
| 1470 | border: 1px solid #38383a; |
| 1471 | border-radius: 12px; |
| 1472 | padding: 8px; |
| 1473 | min-width: 200px; |
| 1474 | box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5); |
| 1475 | animation: scaleIn 0.15s; |
| 1476 | } |
| 1477 | |
| 1478 | @keyframes scaleIn { |
| 1479 | from { |
| 1480 | transform: scale(0.95); |
| 1481 | opacity: 0; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | ::slotted(*) { |
| 1486 | display: flex; |
| 1487 | align-items: center; |
| 1488 | gap: 12px; |
| 1489 | padding: 10px 12px; |
| 1490 | border-radius: 6px; |
| 1491 | cursor: pointer; |
| 1492 | transition: all 0.15s; |
| 1493 | color: #fff; |
| 1494 | text-decoration: none; |
| 1495 | user-select: none; |
| 1496 | } |
| 1497 | |
| 1498 | ::slotted(:hover) { |
| 1499 | background: #007aff; |
| 1500 | } |
| 1501 | |
| 1502 | ::slotted([disabled]) { |
| 1503 | opacity: 0.4; |
| 1504 | cursor: not-allowed; |
| 1505 | } |
| 1506 | |
| 1507 | ::slotted([disabled]):hover { |
| 1508 | background: transparent; |
| 1509 | } |
| 1510 | |
| 1511 | .divider { |
| 1512 | height: 1px; |
| 1513 | background: #38383a; |
| 1514 | margin: 4px 0; |
| 1515 | } |
| 1516 | |
| 1517 | @media (prefers-reduced-motion: reduce) { |
| 1518 | .menu { |
| 1519 | animation: none; |
| 1520 | } |
| 1521 | } |
| 1522 | </style> |
| 1523 | <div class="menu" part="menu" role="menu"> |
| 1524 | <slot></slot> |
| 1525 | </div> |
| 1526 | `; |
| 1527 | } |
| 1528 | |
| 1529 | setupEventListeners() { |
| 1530 | this.addEventListener('click', (e) => { |
| 1531 | const item = e.target.closest('[data-menu-id]'); |
| 1532 | if (item && !item.hasAttribute('disabled')) { |
| 1533 | this.emitIntent('select', { |
| 1534 | id: item.getAttribute('data-menu-id') |
| 1535 | }); |
| 1536 | this.close(); |
| 1537 | } |
| 1538 | }); |
| 1539 | |
| 1540 | // Close on outside click |
| 1541 | document.addEventListener('click', (e) => { |
| 1542 | if (this.hasAttribute('open') && !this.contains(e.target)) { |
| 1543 | this.close(); |
| 1544 | } |
| 1545 | }); |
| 1546 | |
| 1547 | // Close on ESC |
| 1548 | document.addEventListener('keydown', (e) => { |
| 1549 | if (e.key === 'Escape' && this.hasAttribute('open')) { |
| 1550 | this.close(); |
| 1551 | } |
| 1552 | }); |
| 1553 | } |
| 1554 | |
| 1555 | open(x, y) { |
| 1556 | this.style.left = x + 'px'; |
| 1557 | this.style.top = y + 'px'; |
| 1558 | this.setAttribute('open', ''); |
| 1559 | this.emitIntent('open'); |
| 1560 | |
| 1561 | // Focus first item |
| 1562 | const firstItem = this.querySelector('[data-menu-id]:not([disabled])'); |
| 1563 | firstItem?.focus(); |
| 1564 | } |
| 1565 | |
| 1566 | close() { |
| 1567 | this.removeAttribute('open'); |
| 1568 | this.emitIntent('close'); |
| 1569 | } |
| 1570 | |
| 1571 | attributeChangedCallback() { |
| 1572 | if (this.shadowRoot.innerHTML) { |
| 1573 | this.render(); |
| 1574 | this.setupEventListeners(); |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | // ============================================================================ |
| 1580 | // REGISTER COMPONENTS |
| 1581 | // ============================================================================ |
| 1582 | |
| 1583 | customElements.define('semantic-action', SemanticAction); |
| 1584 | customElements.define('semantic-card', SemanticCard); |
| 1585 | customElements.define('semantic-feedback', SemanticFeedback); |
| 1586 | customElements.define('semantic-adjuster', SemanticAdjuster); |
| 1587 | customElements.define('semantic-input', SemanticInput); |
| 1588 | customElements.define('semantic-modal', SemanticModal); |
| 1589 | customElements.define('semantic-navigator', SemanticNavigator); |
| 1590 | customElements.define('semantic-list', SemanticList); |
| 1591 | customElements.define('semantic-menu', SemanticMenu); |
| 1592 | |
| 1593 | // Export for module usage |
| 1594 | ; |
| 1595 | } |
| 1596 | |
| 1597 | export default window.semanticContext || {}; |