|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Shadow DOM Test</title> |
| 5 | + <style> |
| 6 | + body { font-family: Arial, sans-serif; padding: 20px; } |
| 7 | + .result { margin-top: 20px; padding: 10px; background: #f5f5f5; border-radius: 4px; } |
| 8 | + </style> |
| 9 | +</head> |
| 10 | +<body> |
| 11 | + <h1>Shadow DOM Test Page</h1> |
| 12 | +
|
| 13 | + <p>This page contains Web Components with Shadow DOM for testing.</p> |
| 14 | +
|
| 15 | + <!-- Simple shadow DOM component --> |
| 16 | + <my-button id="simple-component"></my-button> |
| 17 | +
|
| 18 | + <!-- Nested shadow DOM components --> |
| 19 | + <my-app id="nested-component"></my-app> |
| 20 | +
|
| 21 | + <div class="result"> |
| 22 | + <p>Clicked: <span id="clicked-element">none</span></p> |
| 23 | + <p>Input value: <span id="input-value">empty</span></p> |
| 24 | + </div> |
| 25 | +
|
| 26 | + <script> |
| 27 | + // Simple button component |
| 28 | + class MyButton extends HTMLElement { |
| 29 | + constructor() { |
| 30 | + super(); |
| 31 | + const shadow = this.attachShadow({ mode: 'open' }); |
| 32 | + shadow.innerHTML = ` |
| 33 | + <style> |
| 34 | + button { |
| 35 | + padding: 10px 20px; |
| 36 | + background: #007bff; |
| 37 | + color: white; |
| 38 | + border: none; |
| 39 | + border-radius: 4px; |
| 40 | + cursor: pointer; |
| 41 | + } |
| 42 | + button:hover { background: #0056b3; } |
| 43 | + </style> |
| 44 | + <button class="shadow-button">Click Me (Shadow)</button> |
| 45 | + `; |
| 46 | +
|
| 47 | + shadow.querySelector('button').addEventListener('click', () => { |
| 48 | + document.getElementById('clicked-element').textContent = 'my-button > button'; |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + customElements.define('my-button', MyButton); |
| 53 | +
|
| 54 | + // Container component |
| 55 | + class MyApp extends HTMLElement { |
| 56 | + constructor() { |
| 57 | + super(); |
| 58 | + const shadow = this.attachShadow({ mode: 'open' }); |
| 59 | + shadow.innerHTML = ` |
| 60 | + <style> |
| 61 | + .app-container { |
| 62 | + padding: 20px; |
| 63 | + border: 2px solid #333; |
| 64 | + border-radius: 8px; |
| 65 | + margin: 10px 0; |
| 66 | + } |
| 67 | + h2 { margin-top: 0; } |
| 68 | + </style> |
| 69 | + <div class="app-container"> |
| 70 | + <h2>My App (Shadow Root)</h2> |
| 71 | + <my-form></my-form> |
| 72 | + </div> |
| 73 | + `; |
| 74 | + } |
| 75 | + } |
| 76 | + customElements.define('my-app', MyApp); |
| 77 | +
|
| 78 | + // Form component (nested inside my-app) |
| 79 | + class MyForm extends HTMLElement { |
| 80 | + constructor() { |
| 81 | + super(); |
| 82 | + const shadow = this.attachShadow({ mode: 'open' }); |
| 83 | + shadow.innerHTML = ` |
| 84 | + <style> |
| 85 | + .form-group { margin: 10px 0; } |
| 86 | + label { display: block; margin-bottom: 5px; font-weight: bold; } |
| 87 | + input { |
| 88 | + padding: 8px; |
| 89 | + border: 1px solid #ccc; |
| 90 | + border-radius: 4px; |
| 91 | + width: 200px; |
| 92 | + } |
| 93 | + button { |
| 94 | + padding: 8px 16px; |
| 95 | + background: #28a745; |
| 96 | + color: white; |
| 97 | + border: none; |
| 98 | + border-radius: 4px; |
| 99 | + cursor: pointer; |
| 100 | + margin-top: 10px; |
| 101 | + } |
| 102 | + </style> |
| 103 | + <div class="form-group"> |
| 104 | + <label for="shadow-input">Name</label> |
| 105 | + <input type="text" id="shadow-input" class="shadow-input" placeholder="Enter name"> |
| 106 | + </div> |
| 107 | + <button class="submit-btn">Submit</button> |
| 108 | + `; |
| 109 | +
|
| 110 | + const input = shadow.querySelector('input'); |
| 111 | + const submitBtn = shadow.querySelector('button'); |
| 112 | +
|
| 113 | + input.addEventListener('input', () => { |
| 114 | + document.getElementById('input-value').textContent = input.value || 'empty'; |
| 115 | + }); |
| 116 | +
|
| 117 | + submitBtn.addEventListener('click', () => { |
| 118 | + document.getElementById('clicked-element').textContent = 'my-app > my-form > button'; |
| 119 | + }); |
| 120 | + } |
| 121 | + } |
| 122 | + customElements.define('my-form', MyForm); |
| 123 | + </script> |
| 124 | +</body> |
| 125 | +</html> |
0 commit comments