-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdefer-teleport-demo.stx
More file actions
325 lines (299 loc) · 9.24 KB
/
Copy pathdefer-teleport-demo.stx
File metadata and controls
325 lines (299 loc) · 9.24 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<script server>
const title = "STX Defer & Teleport Demo"
const heavyContent = "This content was lazily loaded!"
const modalTitle = "Teleported Modal"
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: system-ui, -apple-system, sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #e4e4e7;
min-height: 100vh;
padding: 2rem;
}
.container { max-width: 800px; margin: 0 auto; }
h1 {
font-size: 2.5rem;
margin-bottom: 2rem;
background: linear-gradient(90deg, #a78bfa, #60a5fa);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
h2 {
font-size: 1.5rem;
margin: 2rem 0 1rem;
color: #a78bfa;
}
.card {
background: rgba(255,255,255,0.05);
border: 1px solid rgba(255,255,255,0.1);
border-radius: 12px;
padding: 1.5rem;
margin-bottom: 1.5rem;
}
.skeleton {
background: linear-gradient(90deg, #2a2a3e 25%, #3a3a4e 50%, #2a2a3e 75%);
background-size: 200% 100%;
animation: shimmer 1.5s infinite;
border-radius: 8px;
height: 100px;
}
@keyframes shimmer {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
.spinner {
width: 40px;
height: 40px;
border: 3px solid rgba(255,255,255,0.1);
border-top-color: #a78bfa;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 1rem auto;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.loaded-content {
background: linear-gradient(135deg, #4c1d95 0%, #1e40af 100%);
padding: 1.5rem;
border-radius: 8px;
text-align: center;
}
.btn {
background: linear-gradient(135deg, #7c3aed 0%, #2563eb 100%);
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 8px;
cursor: pointer;
font-size: 1rem;
transition: transform 0.2s, box-shadow 0.2s;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(124, 58, 237, 0.3);
}
/* Modal styles */
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.7);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: opacity 0.3s, visibility 0.3s;
}
.modal-overlay.active {
opacity: 1;
visibility: visible;
}
.modal {
background: #1a1a2e;
border: 1px solid rgba(255,255,255,0.1);
border-radius: 16px;
padding: 2rem;
max-width: 500px;
width: 90%;
transform: scale(0.9);
transition: transform 0.3s;
}
.modal-overlay.active .modal {
transform: scale(1);
}
.modal h3 {
font-size: 1.5rem;
margin-bottom: 1rem;
color: #a78bfa;
}
.modal p { margin-bottom: 1.5rem; color: #a1a1aa; }
#modals-container { position: fixed; z-index: 1000; }
</style>
</head>
<body>
<div class="container">
<h1>{{ title }}</h1>
<!-- ===================== -->
<!-- @defer Examples -->
<!-- ===================== -->
<h2>@defer - Lazy Loading</h2>
<!-- Defer on Visible (Intersection Observer) -->
<div class="card">
<h3 style="margin-bottom: 1rem;">Load on Scroll (visible)</h3>
<p style="color: #a1a1aa; margin-bottom: 1rem;">
Scroll down to load this content. Uses IntersectionObserver.
</p>
@defer(on: 'visible')
<div class="loaded-content">
<p>{{ heavyContent }}</p>
<p style="font-size: 0.875rem; opacity: 0.7; margin-top: 0.5rem;">
Loaded when scrolled into view
</p>
</div>
@placeholder
<div class="skeleton"></div>
@loading
<div class="spinner"></div>
@enddefer
</div>
<!-- Defer on Hover -->
<div class="card">
<h3 style="margin-bottom: 1rem;">Load on Hover</h3>
<p style="color: #a1a1aa; margin-bottom: 1rem;">
Hover over this area to load the content.
</p>
@defer(on: 'hover')
<div class="loaded-content">
<p>Loaded on hover!</p>
</div>
@placeholder
<div style="padding: 2rem; text-align: center; border: 2px dashed rgba(255,255,255,0.2); border-radius: 8px;">
Hover me to load content
</div>
@enddefer
</div>
<!-- Defer on Interaction -->
<div class="card">
<h3 style="margin-bottom: 1rem;">Load on Interaction</h3>
<p style="color: #a1a1aa; margin-bottom: 1rem;">
Click or tap this area to load the content.
</p>
@defer(on: 'interaction')
<div class="loaded-content">
<p>Loaded on click/tap!</p>
</div>
@placeholder
<div style="padding: 2rem; text-align: center; border: 2px dashed rgba(255,255,255,0.2); border-radius: 8px; cursor: pointer;">
Click me to load content
</div>
@enddefer
</div>
<!-- Defer with Timer -->
<div class="card">
<h3 style="margin-bottom: 1rem;">Load after 2 seconds</h3>
<p style="color: #a1a1aa; margin-bottom: 1rem;">
This content will load automatically after 2 seconds.
</p>
@defer(on: 'timer(2000)')
<div class="loaded-content">
<p>Loaded after 2 second delay!</p>
</div>
@placeholder
<div style="padding: 2rem; text-align: center; border: 2px dashed rgba(255,255,255,0.2); border-radius: 8px;">
Loading in 2 seconds...
</div>
@loading
<div class="spinner"></div>
@enddefer
</div>
<!-- Defer on Idle -->
<div class="card">
<h3 style="margin-bottom: 1rem;">Load when Idle</h3>
<p style="color: #a1a1aa; margin-bottom: 1rem;">
Uses requestIdleCallback to load when browser is idle.
</p>
@defer(on: 'idle')
<div class="loaded-content">
<p>Loaded when browser was idle!</p>
</div>
@placeholder
<div class="skeleton" style="height: 60px;"></div>
@enddefer
</div>
<!-- ===================== -->
<!-- @teleport Examples -->
<!-- ===================== -->
<h2>@teleport - DOM Portals</h2>
<div class="card">
<h3 style="margin-bottom: 1rem;">Modal with Teleport</h3>
<p style="color: #a1a1aa; margin-bottom: 1rem;">
The modal content is teleported to #modals-container at the end of body,
escaping this card's stacking context.
</p>
<button class="btn" onclick="toggleModal()">Open Modal</button>
@teleport('#modals-container')
<div class="modal-overlay" id="demo-modal">
<div class="modal">
<h3>{{ modalTitle }}</h3>
<p>
This modal was teleported from inside a card component to
#modals-container at the end of the body. This ensures proper
z-index stacking regardless of parent elements.
</p>
<button class="btn" onclick="toggleModal()">Close</button>
</div>
</div>
@endteleport
</div>
<div class="card">
<h3 style="margin-bottom: 1rem;">Notification Teleport</h3>
<p style="color: #a1a1aa; margin-bottom: 1rem;">
Notifications are teleported to a fixed container.
</p>
<button class="btn" onclick="showNotification()">Show Notification</button>
@teleport('#notifications-container')
<div class="notification" id="demo-notification" style="
position: fixed;
top: 1rem;
right: 1rem;
background: linear-gradient(135deg, #059669 0%, #0d9488 100%);
color: white;
padding: 1rem 1.5rem;
border-radius: 8px;
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
transform: translateX(150%);
transition: transform 0.3s ease;
z-index: 1001;
">
Notification teleported to top-right!
</div>
@endteleport
</div>
</div>
<!-- Teleport containers -->
<div id="modals-container"></div>
<div id="notifications-container"></div>
<script client>
function toggleModal() {
const modal = document.getElementById('demo-modal');
if (modal) {
modal.classList.toggle('active');
}
}
function showNotification() {
const notification = document.getElementById('demo-notification');
if (notification) {
notification.style.transform = 'translateX(0)';
setTimeout(() => {
notification.style.transform = 'translateX(150%)';
}, 3000);
}
}
// Close modal on overlay click
document.addEventListener('click', (e) => {
if (e.target.classList.contains('modal-overlay')) {
toggleModal();
}
});
// Close modal on Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
const modal = document.getElementById('demo-modal');
if (modal && modal.classList.contains('active')) {
toggleModal();
}
}
});
</script>
</body>
</html>