@ac-base/ac-demo-content 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/src/case-event-bridge.js +39 -0
- package/src/demo-case.js +83 -7
- package/src/demo-content.js +77 -1
- package/src/demo-content.styles.scss +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-base/ac-demo-content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/RenzoCordovaDev/ac-lit.git",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"type": "module",
|
|
12
12
|
"main": "index.js",
|
|
13
13
|
"exports": {
|
|
14
|
-
".": "./index.js"
|
|
14
|
+
".": "./index.js",
|
|
15
|
+
"./case-event-bridge.js": "./src/case-event-bridge.js"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
17
18
|
"/src/",
|
|
@@ -35,7 +36,7 @@
|
|
|
35
36
|
"sass": "^1.79.4",
|
|
36
37
|
"vite": "^5.4.8",
|
|
37
38
|
"@ac-base/ac-core": "^0.1.0",
|
|
38
|
-
"@ac-base/cli": "^0.
|
|
39
|
+
"@ac-base/cli": "^0.5.0",
|
|
39
40
|
"@ac-base/ac-e2e-testing": "^0.1.0"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Puente padre↔iframe para un caso de demo cargado en un <iframe> (ver
|
|
2
|
+
// demo-case.js: camino "documento completo"). Lo importa el <script> del
|
|
3
|
+
// propio caso (junto al resto de imports que ya registra sus componentes).
|
|
4
|
+
// Un iframe es su propio realm de JS: ninguna variable ni estado de módulo
|
|
5
|
+
// del documento padre llega adentro por su cuenta, con lo cual necesita este
|
|
6
|
+
// puente para 2 cosas:
|
|
7
|
+
//
|
|
8
|
+
// 1. Reenviar al padre los eventos que `ac-demo-content` necesita loguear en
|
|
9
|
+
// su panel de eventos (`events="..."`) — un CustomEvent con
|
|
10
|
+
// `composed: true` atraviesa shadow DOM pero NUNCA el límite de un
|
|
11
|
+
// iframe. `ac-demo-content`, al detectar el iframe (`_onCaseLoad` en
|
|
12
|
+
// demo-content.js), le hace un postMessage con los nombres de evento que
|
|
13
|
+
// le interesan; desde ahí este puente escucha exactamente esos nombres en
|
|
14
|
+
// el documento del iframe y reenvía cada uno, también por postMessage.
|
|
15
|
+
// 2. Replicar el idioma global hacia ESTE realm: `setGlobalLocale()` (de
|
|
16
|
+
// `@ac-base/ac-intl-mixin`) solo cambia la variable de módulo de quien la
|
|
17
|
+
// llama — el documento padre tiene su propia copia del módulo, separada
|
|
18
|
+
// de la del iframe, así que cambiar el idioma en el padre no traduce nada
|
|
19
|
+
// acá adentro sin este puente.
|
|
20
|
+
import { setGlobalLocale } from '@ac-base/ac-intl-mixin';
|
|
21
|
+
|
|
22
|
+
if (window.parent !== window) {
|
|
23
|
+
window.addEventListener('message', initEvent => {
|
|
24
|
+
if (!initEvent.data) return;
|
|
25
|
+
if (initEvent.data.__acDemoBridgeInit === true) {
|
|
26
|
+
const eventNames = Array.isArray(initEvent.data.eventNames) ? initEvent.data.eventNames : [];
|
|
27
|
+
for (const name of eventNames) {
|
|
28
|
+
document.addEventListener(name, event => {
|
|
29
|
+
window.parent.postMessage(
|
|
30
|
+
{ __acDemoBridgeEvent: true, name: event.type, detail: event.detail },
|
|
31
|
+
'*',
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
} else if (initEvent.data.__acDemoBridgeSetLocale === true) {
|
|
36
|
+
setGlobalLocale(initEvent.data.locale);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
package/src/demo-case.js
CHANGED
|
@@ -1,11 +1,35 @@
|
|
|
1
|
+
// Un documento completo (<!doctype html>/<html>) en `href` se carga en un
|
|
2
|
+
// <iframe> en vez de por innerHTML — ver el comentario largo más abajo, junto
|
|
3
|
+
// a `_isFullDocument`, sobre por qué existen los dos caminos.
|
|
4
|
+
const FULL_DOCUMENT_RE = /^\s*(<!doctype html|<html[\s>])/i;
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* ac-demo-case: contenedor de un caso de demo cargado desde un archivo HTML
|
|
3
|
-
* externo (`href`).
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
8
|
+
* externo (`href`). Soporta dos formatos de archivo:
|
|
9
|
+
*
|
|
10
|
+
* 1. Fragmento (lo que había hasta ahora): un trozo de HTML suelto, sin
|
|
11
|
+
* `<html>` propio. Se inyecta con `innerHTML` directo en el documento de
|
|
12
|
+
* la página — participa del CSS global del demo (ej. `.row`/`.controls`
|
|
13
|
+
* de demo-base.css) y evita atravesar dos shadow roots anidados
|
|
14
|
+
* (ac-demo-content -> ac-demo-case -> los componentes reales) para que
|
|
15
|
+
* ac-demo-content pueda controlarlos (ej. cambiar su `ambient`). Límite
|
|
16
|
+
* conocido: un `<script>` insertado por `innerHTML` nunca se ejecuta, así
|
|
17
|
+
* que un caso así no puede traer su propia lógica — tiene que vivir en el
|
|
18
|
+
* `demo.js` compartido de todo el demo.
|
|
19
|
+
* 2. Documento completo (`<!doctype html>...<html>...</html>`): se carga en
|
|
20
|
+
* un `<iframe>` con `src` apuntando a `href`, así SÍ puede traer su propio
|
|
21
|
+
* `<script type="module">` (lógica propia por caso, sin acumular todo en
|
|
22
|
+
* el `demo.js` compartido). Efecto colateral deseado: un `<dialog>` nativo
|
|
23
|
+
* (`showModal()`) abierto dentro de ese documento queda acotado al propio
|
|
24
|
+
* iframe (es un browsing context aparte, con su propia capa superior) —
|
|
25
|
+
* sin necesitar `contained`/`getBoundary` ni medir nada por JS, sin
|
|
26
|
+
* importar cuántos niveles de shadow DOM tenga el componente por dentro.
|
|
27
|
+
* Costo: el documento del iframe es un documento aparte, así que
|
|
28
|
+
* ac-demo-content necesita un puente explícito para alcanzarlo (tema
|
|
29
|
+
* claro/oscuro, panel de eventos — ver `_onCaseLoad`/`case-event-bridge.js`
|
|
30
|
+
* en demo-content.js). El archivo debe vivir fuera de `demo/public/` (no
|
|
31
|
+
* como asset estático) para que Vite resuelva sus imports `@ac-base/...`
|
|
32
|
+
* tanto en dev como en build — ver vite.config.js de cada componente.
|
|
9
33
|
*
|
|
10
34
|
* El contenido de `href` se asume de confianza — lo escribe quien desarrolla
|
|
11
35
|
* el componente, igual que el propio demo/index.html. No se sanitiza.
|
|
@@ -23,6 +47,7 @@ export class DemoCase extends HTMLElement {
|
|
|
23
47
|
super();
|
|
24
48
|
this._loaded = false;
|
|
25
49
|
this._source = '';
|
|
50
|
+
this._iframe = null;
|
|
26
51
|
}
|
|
27
52
|
|
|
28
53
|
/** HTML crudo cargado desde `href` — usado por el toggle Visual/Código. */
|
|
@@ -30,6 +55,11 @@ export class DemoCase extends HTMLElement {
|
|
|
30
55
|
return this._source;
|
|
31
56
|
}
|
|
32
57
|
|
|
58
|
+
/** El `<iframe>` del caso, si `href` apuntó a un documento completo (si no, `null`). */
|
|
59
|
+
get iframe() {
|
|
60
|
+
return this._iframe;
|
|
61
|
+
}
|
|
62
|
+
|
|
33
63
|
get header() {
|
|
34
64
|
return this.getAttribute('header') ?? '';
|
|
35
65
|
}
|
|
@@ -51,14 +81,30 @@ export class DemoCase extends HTMLElement {
|
|
|
51
81
|
}
|
|
52
82
|
|
|
53
83
|
connectedCallback() {
|
|
84
|
+
this._syncDisplay();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Elemento indefinido sin shadow DOM: el CSS con scope del shadow root de
|
|
88
|
+
* ac-demo-content (que lo proyecta por <slot>) nunca lo alcanza, ni
|
|
89
|
+
* siquiera vía `::slotted()` para un descendiente como el iframe de abajo
|
|
90
|
+
* — tiene que ser en línea. Pero un `display` en línea fijo le ganaría
|
|
91
|
+
* SIEMPRE a la regla `[hidden] { display: none }` del navegador (una regla
|
|
92
|
+
* de atributo pierde contra un estilo en línea) — así que el propio
|
|
93
|
+
* `display` en línea tiene que reflejar `active`, no ser un valor fijo
|
|
94
|
+
* (si no, el caso inactivo queda renderizado y apilado igual, duplicando
|
|
95
|
+
* el alto real de `.ac-demo-content__viewport` — encontrado vía una
|
|
96
|
+
* violación real de accesibilidad, `scrollable-region-focusable`, en el
|
|
97
|
+
* caso con contenido largo). */
|
|
98
|
+
_syncDisplay() {
|
|
54
99
|
this.hidden = !this.active;
|
|
100
|
+
this.style.display = this.active ? 'block' : 'none';
|
|
55
101
|
}
|
|
56
102
|
|
|
57
103
|
attributeChangedCallback(name, _oldValue, newValue) {
|
|
58
104
|
if (name !== 'active') return;
|
|
59
105
|
|
|
60
106
|
const isActive = newValue !== null;
|
|
61
|
-
this.
|
|
107
|
+
this._syncDisplay();
|
|
62
108
|
|
|
63
109
|
if (isActive && !this._loaded) {
|
|
64
110
|
this._loadContent();
|
|
@@ -73,12 +119,42 @@ export class DemoCase extends HTMLElement {
|
|
|
73
119
|
throw new Error(`HTTP ${response.status}`);
|
|
74
120
|
}
|
|
75
121
|
this._source = await response.text();
|
|
122
|
+
if (FULL_DOCUMENT_RE.test(this._source)) {
|
|
123
|
+
this._loadAsIframe();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
76
126
|
this.innerHTML = this._source;
|
|
77
127
|
} catch (error) {
|
|
78
128
|
this.textContent = `Error cargando "${this.href}": ${error.message}`;
|
|
129
|
+
return;
|
|
79
130
|
}
|
|
80
131
|
// ac-demo-content escucha esto para aplicar `contained`/`getBoundary` a los
|
|
81
132
|
// componentes recién insertados (ver "Superposiciones" en CONVENTIONS.md).
|
|
82
133
|
this.dispatchEvent(new CustomEvent('ac-demo-case-load', { bubbles: true }));
|
|
83
134
|
}
|
|
135
|
+
|
|
136
|
+
/** Camino "documento completo" — ver el comentario largo de la clase. */
|
|
137
|
+
_loadAsIframe() {
|
|
138
|
+
this.style.height = '100%';
|
|
139
|
+
const iframe = document.createElement('iframe');
|
|
140
|
+
iframe.className = 'ac-demo-case__iframe';
|
|
141
|
+
iframe.setAttribute('title', this.header || 'Caso de demo');
|
|
142
|
+
// Mismo motivo que `this.style.display` en connectedCallback: en línea,
|
|
143
|
+
// nunca con CSS con scope de shadow DOM.
|
|
144
|
+
iframe.style.cssText = 'display:block;width:100%;height:100%;border:0;background:transparent;';
|
|
145
|
+
// Sin `{ once: true }` a propósito: en dev, el HMR de Vite puede recargar
|
|
146
|
+
// el documento del iframe (ej. al tocar un `.styles.scss`) sin que
|
|
147
|
+
// ac-demo-case se entere — cada recarga real dispara `load` de nuevo, así
|
|
148
|
+
// que re-disparamos `ac-demo-case-load` cada vez para que
|
|
149
|
+
// ac-demo-content vuelva a sincronizar tema/eventos.
|
|
150
|
+
iframe.addEventListener('load', () => {
|
|
151
|
+
this._iframe = iframe;
|
|
152
|
+
// ac-demo-content escucha esto para sincronizar tema y puentear el
|
|
153
|
+
// panel de eventos a través del iframe — ver `_onCaseLoad` en
|
|
154
|
+
// demo-content.js.
|
|
155
|
+
this.dispatchEvent(new CustomEvent('ac-demo-case-load', { bubbles: true }));
|
|
156
|
+
});
|
|
157
|
+
this.replaceChildren(iframe);
|
|
158
|
+
iframe.src = this.href;
|
|
159
|
+
}
|
|
84
160
|
}
|
package/src/demo-content.js
CHANGED
|
@@ -177,6 +177,7 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
177
177
|
this._onEvent = this._onEvent.bind(this);
|
|
178
178
|
this._onPanelKeydown = this._onPanelKeydown.bind(this);
|
|
179
179
|
this._onCaseLoad = this._onCaseLoad.bind(this);
|
|
180
|
+
this._onBridgeMessage = this._onBridgeMessage.bind(this);
|
|
180
181
|
}
|
|
181
182
|
|
|
182
183
|
get _languages() {
|
|
@@ -221,6 +222,7 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
221
222
|
document.addEventListener(name, this._onEvent);
|
|
222
223
|
}
|
|
223
224
|
this.addEventListener('ac-demo-case-load', this._onCaseLoad);
|
|
225
|
+
window.addEventListener('message', this._onBridgeMessage);
|
|
224
226
|
}
|
|
225
227
|
|
|
226
228
|
disconnectedCallback() {
|
|
@@ -228,6 +230,7 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
228
230
|
document.removeEventListener(name, this._onEvent);
|
|
229
231
|
}
|
|
230
232
|
this.removeEventListener('ac-demo-case-load', this._onCaseLoad);
|
|
233
|
+
window.removeEventListener('message', this._onBridgeMessage);
|
|
231
234
|
document.removeEventListener('keydown', this._onPanelKeydown);
|
|
232
235
|
clearTimeout(this._copiedTimer);
|
|
233
236
|
super.disconnectedCallback();
|
|
@@ -264,6 +267,31 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
264
267
|
caseEl.active = i === index;
|
|
265
268
|
});
|
|
266
269
|
this._activeCaseIndex = index;
|
|
270
|
+
this._syncThemeToActiveCase();
|
|
271
|
+
this._syncLanguageToActiveCase();
|
|
272
|
+
this._syncViewportPadding();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Un caso cargado en iframe (ver demo-case.js) tiene su propio documento:
|
|
276
|
+
* no hereda el `data-theme` del documento padre, hay que replicarlo. */
|
|
277
|
+
_syncThemeToActiveCase() {
|
|
278
|
+
const iframe = this._cases[this._activeCaseIndex]?.iframe;
|
|
279
|
+
iframe?.contentDocument?.documentElement.setAttribute('data-theme', this._theme);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** `.ac-demo-content__viewport` trae 1rem de padding pensado para contenido
|
|
283
|
+
* suelto (fragmento) que necesita respirar sobre el fondo del marco. Un
|
|
284
|
+
* caso "documento completo" (iframe) suele ser una pantalla real de punta a
|
|
285
|
+
* punta (ej. `ac-transition-container` a pantalla completa) — con ese
|
|
286
|
+
* padding de por medio nunca puede llegar a los bordes del marco simulado,
|
|
287
|
+
* aunque internamente esté midiendo/llenando el 100% de su propio iframe.
|
|
288
|
+
* Se anula a mano en línea (mismo motivo que el resto de estilos en línea
|
|
289
|
+
* de demo-case.js: CSS con scope de shadow DOM no alcanza el DOM liviano). */
|
|
290
|
+
_syncViewportPadding() {
|
|
291
|
+
const viewport = this.renderRoot.querySelector('.ac-demo-content__viewport');
|
|
292
|
+
if (!viewport) return;
|
|
293
|
+
const isIframeCase = Boolean(this._cases[this._activeCaseIndex]?.iframe);
|
|
294
|
+
viewport.style.padding = isIframeCase ? '0' : '';
|
|
267
295
|
}
|
|
268
296
|
|
|
269
297
|
selectCase(index) {
|
|
@@ -275,6 +303,20 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
275
303
|
if (this._languages.length !== 2 || (index !== 0 && index !== 1)) return;
|
|
276
304
|
this._langIndex = index;
|
|
277
305
|
setGlobalLocale(this._languages[index]);
|
|
306
|
+
this._syncLanguageToActiveCase();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Mismo motivo que `_syncThemeToActiveCase`: un caso en iframe tiene su
|
|
310
|
+
* propia copia del módulo de `@ac-base/ac-intl-mixin` — `setGlobalLocale()`
|
|
311
|
+
* de arriba solo cambió la del documento padre. Se replica vía
|
|
312
|
+
* case-event-bridge.js (import de cada caso en documento completo). */
|
|
313
|
+
_syncLanguageToActiveCase() {
|
|
314
|
+
if (this._languages.length !== 2) return;
|
|
315
|
+
const iframe = this._cases[this._activeCaseIndex]?.iframe;
|
|
316
|
+
iframe?.contentWindow?.postMessage(
|
|
317
|
+
{ __acDemoBridgeSetLocale: true, locale: this._languages[this._langIndex] },
|
|
318
|
+
'*',
|
|
319
|
+
);
|
|
278
320
|
}
|
|
279
321
|
|
|
280
322
|
selectAmbient(value) {
|
|
@@ -292,6 +334,7 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
292
334
|
if (!THEMES.includes(theme)) return;
|
|
293
335
|
this._theme = theme;
|
|
294
336
|
document.documentElement.setAttribute('data-theme', theme);
|
|
337
|
+
this._syncThemeToActiveCase();
|
|
295
338
|
}
|
|
296
339
|
|
|
297
340
|
selectDevice(key) {
|
|
@@ -338,7 +381,29 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
338
381
|
// viewport simulado por JS. `getBoundary` es una función (no el elemento)
|
|
339
382
|
// porque ese div se reconstruye al alternar Visual/Código.
|
|
340
383
|
_onCaseLoad(event) {
|
|
341
|
-
|
|
384
|
+
const caseEl = event.target;
|
|
385
|
+
const iframe = caseEl.iframe;
|
|
386
|
+
if (iframe) {
|
|
387
|
+
// Camino "documento completo" (demo-case.js): el iframe es su propio
|
|
388
|
+
// browsing context, con su propia capa superior — un <dialog> nativo
|
|
389
|
+
// ya queda acotado a sus límites sin contained/getBoundary. Lo único
|
|
390
|
+
// que hay que cruzar a mano es lo que de verdad depende del documento
|
|
391
|
+
// (tema) y lo que no cruza el límite del iframe por su cuenta (eventos
|
|
392
|
+
// compuestos para el panel de eventos — ver case-event-bridge.js).
|
|
393
|
+
this._syncThemeToActiveCase();
|
|
394
|
+
this._syncLanguageToActiveCase();
|
|
395
|
+
this._syncViewportPadding();
|
|
396
|
+
if (this._eventNames.length) {
|
|
397
|
+
iframe.contentWindow?.postMessage(
|
|
398
|
+
{ __acDemoBridgeInit: true, eventNames: this._eventNames },
|
|
399
|
+
'*',
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
// Camino "fragmento" (innerHTML): mismo documento, se puede aplicar
|
|
405
|
+
// contained/getBoundary a mano a lo que esté en el DOM liviano del caso.
|
|
406
|
+
this._applyContainment(caseEl);
|
|
342
407
|
}
|
|
343
408
|
|
|
344
409
|
_applyContainment(caseEl) {
|
|
@@ -351,6 +416,17 @@ export class DemoContent extends ComponentMixin(LitElement) {
|
|
|
351
416
|
}
|
|
352
417
|
}
|
|
353
418
|
|
|
419
|
+
/** Recibe los eventos que `case-event-bridge.js` reenvía desde el iframe de
|
|
420
|
+
* un caso "documento completo" — el equivalente a `_onEvent` para el
|
|
421
|
+
* camino fragmento, que escucha directo en `document`. */
|
|
422
|
+
_onBridgeMessage(event) {
|
|
423
|
+
if (!event.data || event.data.__acDemoBridgeEvent !== true) return;
|
|
424
|
+
const iframe = this._cases[this._activeCaseIndex]?.iframe;
|
|
425
|
+
if (!iframe || event.source !== iframe.contentWindow) return;
|
|
426
|
+
if (!this._eventNames.includes(event.data.name)) return;
|
|
427
|
+
this._onEvent({ type: event.data.name, detail: event.data.detail });
|
|
428
|
+
}
|
|
429
|
+
|
|
354
430
|
_onThemeSelect(theme) {
|
|
355
431
|
this.selectTheme(theme);
|
|
356
432
|
}
|
|
@@ -431,6 +431,12 @@
|
|
|
431
431
|
border-radius: 0 0 0.5rem 0.5rem;
|
|
432
432
|
}
|
|
433
433
|
|
|
434
|
+
// NOTA: `ac-demo-case`/`.ac-demo-case__iframe` (demo-case.js) son DOM liviano
|
|
435
|
+
// proyectado por <slot> — el CSS con scope de shadow DOM de este componente
|
|
436
|
+
// NUNCA los alcanza (ni siquiera `::slotted()` llega a un descendiente como
|
|
437
|
+
// el <iframe>, solo al propio elemento slotteado). Por eso ese tamaño se fija
|
|
438
|
+
// con estilos en línea desde el propio demo-case.js, no acá.
|
|
439
|
+
|
|
434
440
|
/* Vista de código: editor con barra de título, líneas numeradas y resaltado de
|
|
435
441
|
sintaxis. Paleta FIJA (oscura en ambos temas, como un editor real) — los
|
|
436
442
|
colores no dependen de --color-bg/--color-text, que cambian con el tema. */
|