@lit-roa/custom-header 0.5.1 → 0.5.2

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.
@@ -1,704 +1,731 @@
1
- import { LitElement, css, html, nothing } from 'lit';
2
- import { customElement, property, query, state } from 'lit/decorators.js';
3
- import {
4
- emitir,
5
- estilosBase,
6
- iconoChevronDown,
7
- iconoMenu2,
8
- iconoShoppingBag,
9
- iconoUser,
10
- tokens,
11
- } from '@lit-roa/base';
12
- import '@lit-roa/buscador';
13
- import '@lit-roa/menu-lateral';
14
- import type { LrMenuLateral, LigaMenu } from '@lit-roa/menu-lateral';
15
- import type { Sugerencia } from '@lit-roa/buscador';
16
-
17
- /** Campana (Tabler `bell`). */
18
- const iconoBell = html`<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M10 5a2 2 0 1 1 4 0a7 7 0 0 1 4 6v3a4 4 0 0 0 2 3h-16a4 4 0 0 0 2 -3v-3a7 7 0 0 1 4 -6" /><path d="M9 17v1a3 3 0 0 0 6 0v-1" /></svg>`;
19
-
20
- export interface LigaHeader {
21
- texto: string;
22
- href?: string;
23
- id?: string;
24
- /** Submenú sencillo. */
25
- hijos?: LigaHeader[];
26
- /** Mega menú: columnas con título. Si hay `columnas` se ignoran `hijos`. */
27
- columnas?: { titulo: string; ligas: LigaHeader[] }[];
28
- /** Tarjeta destacada a la derecha del mega menú. */
29
- promo?: { titulo: string; texto?: string; imagen?: string; href?: string; id?: string };
30
- }
31
-
32
- export interface DetalleNavegar {
33
- liga: LigaHeader;
34
- }
35
-
36
- /**
37
- * Encabezado de la tienda: logo, ligas con submenús o mega menú, buscador,
38
- * notificaciones, cuenta y carrito. En pantallas angostas muestra un menú
39
- * lateral, donde va la cuenta para no llenar el encabezado de botones. Puede
40
- * quedarse pegado arriba y hacerse más bajo al hacer scroll.
41
- *
42
- * Las ligas no navegan solas: el sitio escucha `navegar` y decide.
43
- *
44
- * @fires navegar - Al tocar una liga, el logo o una promoción. `detail`: `{ liga }`.
45
- * @fires abrir-carrito - Al tocar el carrito.
46
- * @fires abrir-cuenta - Al tocar "Entrar" o el nombre de la persona.
47
- * @fires abrir-notificaciones - Al tocar la campana (solo con `con-avisos`).
48
- * @fires buscar-texto - (del buscador) Al escribir. `detail`: `{ texto }`.
49
- * @fires buscar - (del buscador) Al presionar Enter. `detail`: `{ texto }`.
50
- * @fires elegir-sugerencia - (del buscador) `detail`: `{ sugerencia }`.
51
- * @slot anuncio - Arriba de todo, por ejemplo `<lr-barra-anuncio>`.
52
- * @slot carrito - Reemplaza el botón del carrito (por ejemplo con `<lr-mini-carrito>`).
53
- * @slot menu-pie - Contenido extra abajo del menú de celular.
54
- */
55
- @customElement('lr-custom-header')
56
- export class LrCustomHeader extends LitElement {
57
- static override styles = [
58
- tokens,
59
- estilosBase,
60
- css`
61
- :host {
62
- display: block;
63
- --_fondo: var(--lr-header-fondo, #292929);
64
- --_texto: var(--lr-header-texto, #ffffff);
65
- --_alto: var(--lr-header-alto, 4.75rem);
66
- --_alto-compacto: var(--lr-header-alto-compacto, 3.5rem);
67
- position: relative;
68
- z-index: var(--lr-header-capa, 50);
69
- }
70
- :host([pegajoso]) {
71
- position: sticky;
72
- top: 0;
73
- transition: transform 0.25s ease;
74
- }
75
- :host([escondido]) {
76
- transform: translateY(-100%);
77
- }
78
- header {
79
- background: var(--_fondo);
80
- color: var(--_texto);
81
- transition: box-shadow 0.2s;
82
- }
83
- :host([compacto]) header {
84
- box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);
85
- }
86
- :host([compacto]) .anuncio {
87
- display: none;
88
- }
89
- .fila {
90
- display: flex;
91
- align-items: center;
92
- gap: 1.5rem;
93
- max-width: var(--lr-header-ancho, 1200px);
94
- height: var(--_alto);
95
- margin: 0 auto;
96
- padding: 0 1.5rem;
97
- transition: height 0.2s;
98
- }
99
- :host([compacto]) .fila {
100
- height: var(--_alto-compacto);
101
- }
102
- button,
103
- a {
104
- font: inherit;
105
- color: inherit;
106
- }
107
- button {
108
- all: unset;
109
- box-sizing: border-box;
110
- cursor: pointer;
111
- }
112
- a {
113
- text-decoration: none;
114
- }
115
- button:focus-visible,
116
- a:focus-visible {
117
- outline: 2px solid currentColor;
118
- outline-offset: 2px;
119
- border-radius: 8px;
120
- }
121
- .logo {
122
- display: flex;
123
- align-items: center;
124
- gap: 0.625rem;
125
- flex: none;
126
- font-weight: 800;
127
- font-size: 1.1875rem;
128
- white-space: nowrap;
129
- }
130
- .logo img {
131
- height: 2.5rem;
132
- width: auto;
133
- max-width: 10rem;
134
- object-fit: contain;
135
- transition: height 0.2s;
136
- }
137
- :host([compacto]) .logo img {
138
- height: 2rem;
139
- }
140
- .fila nav > ul {
141
- display: flex;
142
- gap: 0.25rem;
143
- margin: 0;
144
- padding: 0;
145
- list-style: none;
146
- }
147
- .liga {
148
- display: flex;
149
- align-items: center;
150
- gap: 0.25rem;
151
- padding: 0.5rem 0.75rem;
152
- border-radius: 8px;
153
- font-weight: 600;
154
- font-size: 0.9375rem;
155
- white-space: nowrap;
156
- opacity: 0.9;
157
- }
158
- .liga:hover,
159
- .liga[aria-expanded='true'] {
160
- opacity: 1;
161
- background: rgb(255 255 255 / 0.1);
162
- }
163
- .liga svg {
164
- transition: transform 0.2s;
165
- }
166
- .liga[aria-expanded='true'] svg {
167
- transform: rotate(180deg);
168
- }
169
- li {
170
- position: relative;
171
- }
172
- .submenu {
173
- position: absolute;
174
- top: calc(100% + 0.5rem);
175
- left: 0;
176
- min-width: 13rem;
177
- margin: 0;
178
- padding: 0.5rem;
179
- list-style: none;
180
- background: #fff;
181
- color: #111827;
182
- border-radius: 12px;
183
- box-shadow: 0 18px 30px rgb(0 0 0 / 0.18);
184
- }
185
- .submenu a,
186
- .mega a {
187
- display: block;
188
- padding: 0.5rem 0.75rem;
189
- border-radius: 8px;
190
- }
191
- .submenu a:hover,
192
- .mega a:hover {
193
- background: #f3f4f6;
194
- }
195
- .mega {
196
- position: absolute;
197
- left: 0;
198
- right: 0;
199
- top: 100%;
200
- background: #fff;
201
- color: #111827;
202
- box-shadow: 0 18px 30px rgb(0 0 0 / 0.15);
203
- }
204
- .mega .dentro {
205
- display: grid;
206
- grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
207
- gap: 2rem;
208
- max-width: var(--lr-header-ancho, 1200px);
209
- margin: 0 auto;
210
- padding: 1.75rem 1.5rem;
211
- }
212
- .mega .dentro.con-promo {
213
- grid-template-columns: repeat(var(--_columnas), minmax(9rem, 1fr)) minmax(14rem, 1.3fr);
214
- }
215
- .mega h3 {
216
- margin: 0 0 0.375rem 0.75rem;
217
- font-size: 0.75rem;
218
- text-transform: uppercase;
219
- letter-spacing: 0.06em;
220
- color: #6b7280;
221
- }
222
- .mega ul {
223
- margin: 0;
224
- padding: 0;
225
- list-style: none;
226
- }
227
- .mega .promo {
228
- display: flex;
229
- flex-direction: column;
230
- justify-content: flex-end;
231
- gap: 0.25rem;
232
- min-height: 10rem;
233
- padding: 1.25rem;
234
- border-radius: 12px;
235
- background: var(--lr-header-promo-fondo, linear-gradient(135deg, #7c2d12, #c2410c)) center / cover;
236
- color: #fff;
237
- }
238
- .promo b {
239
- font-size: 1.1875rem;
240
- }
241
- .promo span {
242
- font-size: 0.8125rem;
243
- opacity: 0.9;
244
- }
245
- lr-buscador {
246
- flex: 1;
247
- min-width: 9rem;
248
- max-width: 22rem;
249
- margin-left: auto;
250
- --lr-color-texto: #111827;
251
- --lr-buscador-borde: transparent;
252
- }
253
- .acciones {
254
- display: flex;
255
- align-items: center;
256
- gap: 0.375rem;
257
- flex: none;
258
- }
259
- .solo-acciones {
260
- margin-left: auto;
261
- }
262
- .icono {
263
- position: relative;
264
- display: grid;
265
- place-items: center;
266
- width: 2.625rem;
267
- height: 2.625rem;
268
- border-radius: 999px;
269
- font-size: 1.375rem;
270
- }
271
- .icono:hover,
272
- .cuenta:hover {
273
- background: rgb(255 255 255 / 0.1);
274
- }
275
- .cuenta {
276
- display: flex;
277
- align-items: center;
278
- gap: 0.5rem;
279
- height: 2.625rem;
280
- padding: 0 0.75rem 0 0.5rem;
281
- border-radius: 999px;
282
- font-size: 0.875rem;
283
- font-weight: 600;
284
- }
285
- .nombre-usuario {
286
- max-width: 9rem;
287
- overflow: hidden;
288
- text-overflow: ellipsis;
289
- white-space: nowrap;
290
- }
291
- .cuenta svg {
292
- font-size: 1.25rem;
293
- }
294
- .avatar {
295
- display: grid;
296
- place-items: center;
297
- width: 1.875rem;
298
- height: 1.875rem;
299
- border-radius: 999px;
300
- background: var(--lr-header-avatar, #fbbf24);
301
- color: #292929;
302
- font-size: 0.75rem;
303
- font-weight: 800;
304
- }
305
- .globo {
306
- position: absolute;
307
- top: 0.1875rem;
308
- right: 0;
309
- min-width: 1.1875rem;
310
- height: 1.1875rem;
311
- padding: 0 0.3125rem;
312
- border-radius: 999px;
313
- border: 2px solid var(--_fondo);
314
- background: var(--lr-header-globo, #ea580c);
315
- color: #fff;
316
- font-size: 0.6875rem;
317
- font-weight: 800;
318
- line-height: 0.8rem;
319
- text-align: center;
320
- }
321
- .solo-lectores {
322
- position: absolute;
323
- width: 1px;
324
- height: 1px;
325
- overflow: hidden;
326
- clip: rect(0 0 0 0);
327
- }
328
-
329
- /* Celular */
330
- .celular .fila {
331
- gap: 0.25rem;
332
- padding: 0 0.5rem;
333
- height: 3.75rem;
334
- }
335
- :host([compacto]) .celular .fila {
336
- height: 3.25rem;
337
- }
338
- .celular .logo {
339
- flex: 1;
340
- justify-content: center;
341
- font-size: 1.0625rem;
342
- min-width: 0;
343
- }
344
- .celular .logo img {
345
- height: 2rem;
346
- }
347
- .celular .acciones {
348
- gap: 0;
349
- }
350
- .celular .barra-buscar {
351
- padding: 0 0.75rem 0.75rem;
352
- }
353
- :host([compacto]) .celular .barra-buscar {
354
- display: none;
355
- }
356
- .celular lr-buscador {
357
- max-width: none;
358
- margin: 0;
359
- }
360
-
361
- /* El menú de celular es lr-menu-lateral: su cabecera ya toma --lr-header-fondo y --lr-header-texto */
362
- .pie {
363
- display: grid;
364
- gap: 0.75rem;
365
- }
366
- .boton-cuenta {
367
- display: flex;
368
- justify-content: center;
369
- align-items: center;
370
- gap: 0.5rem;
371
- height: 2.875rem;
372
- border-radius: 10px;
373
- background: var(--_fondo);
374
- color: var(--_texto);
375
- font-weight: 700;
376
- }
377
- @media (prefers-reduced-motion: reduce) {
378
- :host([pegajoso]),
379
- .fila {
380
- transition: none;
381
- animation: none;
382
- }
383
- }
384
- `,
385
- ];
386
-
387
- /** URL de la imagen del logo. */
388
- @property() logo = '';
389
-
390
- /** Nombre de la tienda (texto junto al logo y texto alternativo). */
391
- @property() nombre = '';
392
-
393
- /** Liga del logo. */
394
- @property({ attribute: 'href-inicio' }) hrefInicio = '/';
395
-
396
- /** Ligas del menú. Como atributo, en JSON. */
397
- @property({ type: Array }) ligas: LigaHeader[] = [];
398
-
399
- /** Piezas en el carrito (globito). */
400
- @property({ type: Number }) piezas = 0;
401
-
402
- /** Muestra la campana de notificaciones (por ejemplo, solo con sesión iniciada). */
403
- @property({ type: Boolean, attribute: 'con-avisos' }) conAvisos = false;
404
-
405
- /** Notificaciones sin leer (globito de la campana). */
406
- @property({ type: Number }) avisos = 0;
407
-
408
- @property({ attribute: 'texto-avisos' }) textoAvisos = 'Notificaciones';
409
-
410
- /** Nombre de la persona con sesión iniciada; vacío muestra "Entrar". */
411
- @property() usuario = '';
412
-
413
- @property({ attribute: 'texto-entrar' }) textoEntrar = 'Entrar';
414
-
415
- @property({ attribute: 'texto-cuenta' }) textoCuenta = 'Mi cuenta y pedidos';
416
-
417
- /** Oculta el buscador. */
418
- @property({ type: Boolean, attribute: 'sin-buscador' }) sinBuscador = false;
419
-
420
- /** Oculta el carrito (por ejemplo, en una tienda que solo muestra su catálogo). */
421
- @property({ type: Boolean, attribute: 'sin-carrito' }) sinCarrito = false;
422
-
423
- /** Oculta el botón de cuenta ("Entrar" o el nombre), también en el menú lateral. */
424
- @property({ type: Boolean, attribute: 'sin-cuenta' }) sinCuenta = false;
425
-
426
- @property({ attribute: 'placeholder-buscador' }) placeholderBuscador = 'Buscar en la tienda';
427
-
428
- /** Sugerencias para el buscador (ver `lr-buscador`). */
429
- @property({ type: Array }) sugerencias: Sugerencia[] = [];
430
-
431
- @property({ type: Boolean, attribute: 'buscando' }) buscando = false;
432
-
433
- /** Se queda pegado arriba y se hace más bajo al hacer scroll. */
434
- @property({ type: Boolean, reflect: true }) pegajoso = false;
435
-
436
- /** Con `pegajoso`, se esconde al bajar y aparece al subir. */
437
- @property({ type: Boolean, attribute: 'ocultar-al-bajar' }) ocultarAlBajar = false;
438
-
439
- /** Ancho (px) del header debajo del cual se usa la vista de celular. */
440
- @property({ type: Number, attribute: 'ancho-celular' }) anchoCelular = 900;
441
-
442
- @property({ type: Boolean, reflect: true }) compacto = false;
443
- @property({ type: Boolean, reflect: true }) escondido = false;
444
-
445
- @state() private celular = false;
446
- @state() private abierta = -1;
447
- @state() private conAnuncio = false;
448
-
449
- @query('lr-menu-lateral') private menu?: LrMenuLateral;
450
-
451
- private observador?: ResizeObserver;
452
- private ultimoScroll = 0;
453
-
454
- override connectedCallback() {
455
- super.connectedCallback();
456
- this.observador = new ResizeObserver(([e]) => (this.celular = e.contentRect.width < this.anchoCelular));
457
- this.observador.observe(this);
458
- window.addEventListener('scroll', this.alScroll, { passive: true });
459
- document.addEventListener('click', this.alClicFuera);
460
- document.addEventListener('keydown', this.alEscape);
461
- }
462
-
463
- override disconnectedCallback() {
464
- super.disconnectedCallback();
465
- this.observador?.disconnect();
466
- window.removeEventListener('scroll', this.alScroll);
467
- document.removeEventListener('click', this.alClicFuera);
468
- document.removeEventListener('keydown', this.alEscape);
469
- }
470
-
471
- override render() {
472
- return html`<header class=${this.celular ? 'celular' : ''} part="header">
473
- <div class="anuncio" ?hidden=${!this.conAnuncio}>
474
- <slot name="anuncio" @slotchange=${this.alCambiarAnuncio}></slot>
475
- </div>
476
- ${this.celular ? this.vistaCelular() : this.vistaEscritorio()}
477
- </header>`;
478
- }
479
-
480
- /** Abre el menú de celular. */
481
- abrirMenu() {
482
- this.menu?.abrir();
483
- }
484
-
485
- /** Cierra el menú de celular y los submenús. */
486
- cerrarMenu() {
487
- this.menu?.cerrar();
488
- this.abierta = -1;
489
- }
490
-
491
- private vistaEscritorio() {
492
- const liga = this.ligas[this.abierta];
493
- return html`<div class="fila">
494
- ${this.logoTpl()}
495
- ${this.ligas.length
496
- ? html`<nav aria-label="Principal">
497
- <ul>
498
- ${this.ligas.map((l, i) => this.ligaEscritorio(l, i))}
499
- </ul>
500
- </nav>`
501
- : nothing}
502
- ${this.buscadorTpl()}
503
- <div class="acciones ${this.sinBuscador ? 'solo-acciones' : ''}">${this.avisosTpl()} ${this.cuentaTpl()} ${this.carritoTpl()}</div>
504
- </div>
505
- ${liga?.columnas?.length ? this.megaTpl(liga) : nothing}`;
506
- }
507
-
508
- private vistaCelular() {
509
- return html`<div class="fila">
510
- <button class="icono" aria-label="Abrir menú" aria-haspopup="dialog" @click=${this.abrirMenu}>${iconoMenu2}</button>
511
- ${this.logoTpl()}
512
- <div class="acciones">${this.avisosTpl()} ${this.carritoTpl()}</div>
513
- </div>
514
- ${this.sinBuscador ? nothing : html`<div class="barra-buscar">${this.buscadorTpl()}</div>`}
515
- <lr-menu-lateral
516
- .ligas=${this.ligasMenu()}
517
- logo=${this.logo}
518
- nombre=${this.nombre}
519
- href-inicio=${this.hrefInicio}
520
- @cerrar=${() => (this.abierta = -1)}
521
- >
522
- <div slot="pie" class="pie">
523
- <slot name="menu-pie"></slot>
524
- ${this.sinCuenta
525
- ? nothing
526
- : html`<button class="boton-cuenta" @click=${this.alCuenta}>
527
- ${this.usuario
528
- ? html`<span class="avatar" aria-hidden="true">${this.iniciales()}</span> ${this.textoCuenta}`
529
- : html`${iconoUser} ${this.textoEntrar}`}
530
- </button>`}
531
- </div>
532
- </lr-menu-lateral>`;
533
- }
534
-
535
- private logoTpl() {
536
- const inicio: LigaHeader = { texto: this.nombre || 'Inicio', href: this.hrefInicio, id: 'inicio' };
537
- return html`<a class="logo" href=${this.hrefInicio} part="logo" @click=${(e: MouseEvent) => this.alNavegar(e, inicio)}>
538
- ${this.logo ? html`<img src=${this.logo} alt=${this.nombre ? '' : 'Inicio'} />` : nothing}
539
- ${this.nombre ? html`<span>${this.nombre}</span>` : nothing}
540
- </a>`;
541
- }
542
-
543
- private buscadorTpl() {
544
- if (this.sinBuscador) return nothing;
545
- return html`<lr-buscador
546
- placeholder=${this.placeholderBuscador}
547
- .sugerencias=${this.sugerencias}
548
- ?cargando=${this.buscando}
549
- ver-todo
550
- ></lr-buscador>`;
551
- }
552
-
553
- /** Cuenta en escritorio; en celular va en el menú lateral. */
554
- private cuentaTpl() {
555
- if (this.sinCuenta) return nothing;
556
- return html`<button class="cuenta" @click=${this.alCuenta}>
557
- ${this.usuario
558
- ? html`<span class="avatar" aria-hidden="true">${this.iniciales()}</span><span class="solo-lectores">${this.textoCuenta}:</span><span class="nombre-usuario">${this.usuario.split(/\s+/)[0]}</span>`
559
- : html`${iconoUser}${this.textoEntrar}`}
560
- </button>`;
561
- }
562
-
563
- private avisosTpl() {
564
- if (!this.conAvisos) return nothing;
565
- const n = Math.max(0, Math.floor(this.avisos));
566
- return html`<button
567
- class="icono"
568
- aria-label=${n > 0 ? `${this.textoAvisos}, ${n} sin leer` : this.textoAvisos}
569
- @click=${this.alAvisos}
570
- >
571
- ${iconoBell}${n > 0 ? html`<span class="globo" aria-hidden="true">${n > 99 ? '99+' : n}</span>` : nothing}
572
- </button>`;
573
- }
574
-
575
- private carritoTpl() {
576
- if (this.sinCarrito) return nothing;
577
- return html`<slot name="carrito">
578
- <button class="icono" aria-label="Carrito, ${this.piezas} ${this.piezas === 1 ? 'pieza' : 'piezas'}" @click=${this.alCarrito}>
579
- ${iconoShoppingBag}${this.piezas > 0 ? html`<span class="globo" aria-hidden="true">${this.piezas > 99 ? '99+' : this.piezas}</span>` : nothing}
580
- </button>
581
- </slot>`;
582
- }
583
-
584
- private ligaEscritorio(liga: LigaHeader, i: number) {
585
- const conSub = !!(liga.columnas?.length || liga.hijos?.length);
586
- if (!conSub) {
587
- return html`<li><a class="liga" href=${liga.href || '#'} @click=${(e: MouseEvent) => this.alNavegar(e, liga)}>${liga.texto}</a></li>`;
588
- }
589
- const abierta = this.abierta === i;
590
- return html`<li @mouseenter=${() => (this.abierta = i)} @mouseleave=${() => this.alSalirLiga(i)}>
591
- <button class="liga" aria-expanded=${abierta} @click=${(e: MouseEvent) => this.alClicLiga(e, i)}>${liga.texto} ${iconoChevronDown}</button>
592
- ${abierta && !liga.columnas?.length
593
- ? html`<ul class="submenu">
594
- ${liga.hijos!.map((h) => html`<li><a href=${h.href || '#'} @click=${(e: MouseEvent) => this.alNavegar(e, h)}>${h.texto}</a></li>`)}
595
- </ul>`
596
- : nothing}
597
- </li>`;
598
- }
599
-
600
- private megaTpl(liga: LigaHeader) {
601
- const promo = liga.promo;
602
- const indice = this.abierta;
603
- return html`<div class="mega" @mouseenter=${() => (this.abierta = indice)} @mouseleave=${() => (this.abierta = -1)}>
604
- <div class="dentro ${promo ? 'con-promo' : ''}" style="--_columnas: ${liga.columnas!.length}">
605
- ${liga.columnas!.map(
606
- (c) => html`<div>
607
- <h3>${c.titulo}</h3>
608
- <ul>
609
- ${c.ligas.map((l) => html`<li><a href=${l.href || '#'} @click=${(e: MouseEvent) => this.alNavegar(e, l)}>${l.texto}</a></li>`)}
610
- </ul>
611
- </div>`,
612
- )}
613
- ${promo
614
- ? html`<a
615
- class="promo"
616
- href=${promo.href || '#'}
617
- style=${promo.imagen ? `background-image: linear-gradient(0deg, rgb(0 0 0 / .55), rgb(0 0 0 / 0) 70%), url(${promo.imagen})` : ''}
618
- @click=${(e: MouseEvent) => this.alNavegar(e, { texto: promo.titulo, href: promo.href, id: promo.id })}
619
- >
620
- <b>${promo.titulo}</b>${promo.texto ? html`<span>${promo.texto}</span>` : nothing}
621
- </a>`
622
- : nothing}
623
- </div>
624
- </div>`;
625
- }
626
-
627
- /** Ligas para el menú de celular: el mega menú se aplana con sus títulos como grupos. */
628
- private ligasMenu(): LigaMenu[] {
629
- return this.ligas.map((l) => ({
630
- texto: l.texto,
631
- href: l.href,
632
- id: l.id,
633
- hijos: l.columnas?.length ? l.columnas.flatMap((c) => [{ grupo: c.titulo }, ...c.ligas]) : l.hijos,
634
- }));
635
- }
636
-
637
- private iniciales() {
638
- return this.usuario
639
- .split(/\s+/)
640
- .filter(Boolean)
641
- .slice(0, 2)
642
- .map((p) => p[0]!.toUpperCase())
643
- .join('');
644
- }
645
-
646
- private alNavegar(evento: MouseEvent, liga: LigaHeader) {
647
- if (liga.href && (evento.ctrlKey || evento.metaKey)) return;
648
- evento.preventDefault();
649
- this.cerrarMenu();
650
- emitir<DetalleNavegar>(this, 'navegar', { liga });
651
- }
652
-
653
- private alCuenta = () => {
654
- this.cerrarMenu();
655
- emitir(this, 'abrir-cuenta');
656
- };
657
-
658
- private alAvisos = () => {
659
- this.cerrarMenu();
660
- emitir(this, 'abrir-notificaciones');
661
- };
662
-
663
- private alCarrito = () => {
664
- this.cerrarMenu();
665
- emitir(this, 'abrir-carrito');
666
- };
667
-
668
- private alClicLiga(evento: MouseEvent, i: number) {
669
- // Con mouse el hover ya lo abrió: el clic no debe cerrarlo. Con teclado (detail 0) alterna.
670
- if (evento.detail === 0) this.abierta = this.abierta === i ? -1 : i;
671
- else this.abierta = i;
672
- }
673
-
674
- private alSalirLiga(i: number) {
675
- // El mega menú vive fuera del <li>; se cierra cuando el mouse sale de él.
676
- if (!this.ligas[i]?.columnas?.length) this.abierta = -1;
677
- }
678
-
679
- private alCambiarAnuncio(e: Event) {
680
- this.conAnuncio = (e.target as HTMLSlotElement).assignedElements().length > 0;
681
- }
682
-
683
- private alClicFuera = (e: MouseEvent) => {
684
- if (this.abierta >= 0 && !e.composedPath().includes(this)) this.abierta = -1;
685
- };
686
-
687
- private alEscape = (e: KeyboardEvent) => {
688
- if (e.key === 'Escape' && this.abierta >= 0) this.abierta = -1;
689
- };
690
-
691
- private alScroll = () => {
692
- if (!this.pegajoso) return;
693
- const y = window.scrollY;
694
- this.compacto = y > 40;
695
- if (this.ocultarAlBajar) this.escondido = y > 200 && y > this.ultimoScroll && this.abierta < 0;
696
- this.ultimoScroll = y;
697
- };
698
- }
699
-
700
- declare global {
701
- interface HTMLElementTagNameMap {
702
- 'lr-custom-header': LrCustomHeader;
703
- }
704
- }
1
+ import { LitElement, css, html, nothing } from 'lit';
2
+ import { customElement, property, query, state } from 'lit/decorators.js';
3
+ import {
4
+ emitir,
5
+ estilosBase,
6
+ iconoChevronDown,
7
+ iconoMenu2,
8
+ iconoShoppingBag,
9
+ iconoUser,
10
+ tokens,
11
+ } from '@lit-roa/base';
12
+ import '@lit-roa/buscador';
13
+ import '@lit-roa/menu-lateral';
14
+ import type { LrMenuLateral, LigaMenu } from '@lit-roa/menu-lateral';
15
+ import type { Sugerencia } from '@lit-roa/buscador';
16
+
17
+ /** Campana (Tabler `bell`). */
18
+ const iconoBell = html`<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M10 5a2 2 0 1 1 4 0a7 7 0 0 1 4 6v3a4 4 0 0 0 2 3h-16a4 4 0 0 0 2 -3v-3a7 7 0 0 1 4 -6" /><path d="M9 17v1a3 3 0 0 0 6 0v-1" /></svg>`;
19
+
20
+ export interface LigaHeader {
21
+ texto: string;
22
+ href?: string;
23
+ id?: string;
24
+ /** Submenú sencillo. */
25
+ hijos?: LigaHeader[];
26
+ /** Mega menú: columnas con título. Si hay `columnas` se ignoran `hijos`. */
27
+ columnas?: { titulo: string; ligas: LigaHeader[] }[];
28
+ /** Tarjeta destacada a la derecha del mega menú. */
29
+ promo?: { titulo: string; texto?: string; imagen?: string; href?: string; id?: string };
30
+ }
31
+
32
+ export interface DetalleNavegar {
33
+ liga: LigaHeader;
34
+ }
35
+
36
+ /**
37
+ * Encabezado de la tienda: logo, ligas con submenús o mega menú, buscador,
38
+ * notificaciones, cuenta y carrito. En pantallas angostas muestra un menú
39
+ * lateral, donde va la cuenta para no llenar el encabezado de botones. Puede
40
+ * quedarse pegado arriba y hacerse más bajo al hacer scroll.
41
+ *
42
+ * Las ligas no navegan solas: el sitio escucha `navegar` y decide.
43
+ *
44
+ * @fires navegar - Al tocar una liga, el logo o una promoción. `detail`: `{ liga }`.
45
+ * @fires abrir-carrito - Al tocar el carrito.
46
+ * @fires abrir-cuenta - Al tocar "Entrar" o el nombre de la persona.
47
+ * @fires abrir-notificaciones - Al tocar la campana (solo con `con-avisos`).
48
+ * @fires buscar-texto - (del buscador) Al escribir. `detail`: `{ texto }`.
49
+ * @fires buscar - (del buscador) Al presionar Enter. `detail`: `{ texto }`.
50
+ * @fires elegir-sugerencia - (del buscador) `detail`: `{ sugerencia }`.
51
+ * @slot anuncio - Arriba de todo, por ejemplo `<lr-barra-anuncio>`.
52
+ * @slot carrito - Reemplaza el botón del carrito (por ejemplo con `<lr-mini-carrito>`).
53
+ * @slot menu-pie - Contenido extra abajo del menú de celular.
54
+ */
55
+ @customElement('lr-custom-header')
56
+ export class LrCustomHeader extends LitElement {
57
+ static override styles = [
58
+ tokens,
59
+ estilosBase,
60
+ css`
61
+ :host {
62
+ display: block;
63
+ --_fondo: var(--lr-header-fondo, #292929);
64
+ --_texto: var(--lr-header-texto, #ffffff);
65
+ --_alto: var(--lr-header-alto, 4.75rem);
66
+ --_alto-compacto: var(--lr-header-alto-compacto, 3.5rem);
67
+ position: relative;
68
+ z-index: var(--lr-header-capa, 50);
69
+ }
70
+ :host([pegajoso]) {
71
+ position: sticky;
72
+ top: 0;
73
+ transition: transform 0.25s ease;
74
+ }
75
+ :host([escondido]) {
76
+ transform: translateY(-100%);
77
+ }
78
+ header {
79
+ background: var(--_fondo);
80
+ color: var(--_texto);
81
+ transition: box-shadow 0.2s;
82
+ }
83
+ :host([compacto]) header {
84
+ box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);
85
+ }
86
+ :host([compacto]) .anuncio {
87
+ display: none;
88
+ }
89
+ .fila {
90
+ display: flex;
91
+ align-items: center;
92
+ gap: 1.5rem;
93
+ max-width: var(--lr-header-ancho, 1200px);
94
+ height: var(--_alto);
95
+ margin: 0 auto;
96
+ padding: 0 1.5rem;
97
+ transition: height 0.2s;
98
+ }
99
+ :host([compacto]) .fila {
100
+ height: var(--_alto-compacto);
101
+ }
102
+ button,
103
+ a {
104
+ font: inherit;
105
+ color: inherit;
106
+ }
107
+ button {
108
+ all: unset;
109
+ box-sizing: border-box;
110
+ cursor: pointer;
111
+ }
112
+ a {
113
+ text-decoration: none;
114
+ }
115
+ button:focus-visible,
116
+ a:focus-visible {
117
+ outline: 2px solid currentColor;
118
+ outline-offset: 2px;
119
+ border-radius: 8px;
120
+ }
121
+ .logo {
122
+ display: flex;
123
+ align-items: center;
124
+ gap: 0.625rem;
125
+ flex: none;
126
+ font-weight: 800;
127
+ font-size: 1.1875rem;
128
+ white-space: nowrap;
129
+ }
130
+ .logo img {
131
+ height: 2.5rem;
132
+ width: auto;
133
+ max-width: 10rem;
134
+ object-fit: contain;
135
+ transition: height 0.2s;
136
+ }
137
+ :host([compacto]) .logo img {
138
+ height: 2rem;
139
+ }
140
+ .fila nav > ul {
141
+ display: flex;
142
+ gap: 0.25rem;
143
+ margin: 0;
144
+ padding: 0;
145
+ list-style: none;
146
+ }
147
+ .liga {
148
+ display: flex;
149
+ align-items: center;
150
+ gap: 0.25rem;
151
+ padding: 0.5rem 0.75rem;
152
+ border-radius: 8px;
153
+ font-weight: 600;
154
+ font-size: 0.9375rem;
155
+ white-space: nowrap;
156
+ opacity: 0.9;
157
+ }
158
+ .liga:hover,
159
+ .liga[aria-expanded='true'] {
160
+ opacity: 1;
161
+ background: rgb(255 255 255 / 0.1);
162
+ }
163
+ .liga svg {
164
+ transition: transform 0.2s;
165
+ }
166
+ .liga[aria-expanded='true'] svg {
167
+ transform: rotate(180deg);
168
+ }
169
+ li {
170
+ position: relative;
171
+ }
172
+ .submenu {
173
+ position: absolute;
174
+ top: calc(100% + 0.5rem);
175
+ left: 0;
176
+ min-width: 13rem;
177
+ margin: 0;
178
+ padding: 0.5rem;
179
+ list-style: none;
180
+ background: #fff;
181
+ color: #111827;
182
+ border-radius: 12px;
183
+ box-shadow: 0 18px 30px rgb(0 0 0 / 0.18);
184
+ }
185
+ /* Puente invisible sobre el hueco: el mouse sigue dentro del <li> al bajar al submenú. */
186
+ .submenu::before {
187
+ content: '';
188
+ position: absolute;
189
+ left: 0;
190
+ right: 0;
191
+ bottom: 100%;
192
+ height: 0.75rem;
193
+ }
194
+ .submenu a,
195
+ .mega a {
196
+ display: block;
197
+ padding: 0.5rem 0.75rem;
198
+ border-radius: 8px;
199
+ }
200
+ .submenu a:hover,
201
+ .mega a:hover {
202
+ background: #f3f4f6;
203
+ }
204
+ .mega {
205
+ position: absolute;
206
+ left: 0;
207
+ right: 0;
208
+ top: 100%;
209
+ background: #fff;
210
+ color: #111827;
211
+ box-shadow: 0 18px 30px rgb(0 0 0 / 0.15);
212
+ }
213
+ .mega .dentro {
214
+ display: grid;
215
+ grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
216
+ gap: 2rem;
217
+ max-width: var(--lr-header-ancho, 1200px);
218
+ margin: 0 auto;
219
+ padding: 1.75rem 1.5rem;
220
+ }
221
+ .mega .dentro.con-promo {
222
+ grid-template-columns: repeat(var(--_columnas), minmax(9rem, 1fr)) minmax(14rem, 1.3fr);
223
+ }
224
+ .mega h3 {
225
+ margin: 0 0 0.375rem 0.75rem;
226
+ font-size: 0.75rem;
227
+ text-transform: uppercase;
228
+ letter-spacing: 0.06em;
229
+ color: #6b7280;
230
+ }
231
+ .mega ul {
232
+ margin: 0;
233
+ padding: 0;
234
+ list-style: none;
235
+ }
236
+ .mega .promo {
237
+ display: flex;
238
+ flex-direction: column;
239
+ justify-content: flex-end;
240
+ gap: 0.25rem;
241
+ min-height: 10rem;
242
+ padding: 1.25rem;
243
+ border-radius: 12px;
244
+ background: var(--lr-header-promo-fondo, linear-gradient(135deg, #7c2d12, #c2410c)) center / cover;
245
+ color: #fff;
246
+ }
247
+ .promo b {
248
+ font-size: 1.1875rem;
249
+ }
250
+ .promo span {
251
+ font-size: 0.8125rem;
252
+ opacity: 0.9;
253
+ }
254
+ lr-buscador {
255
+ flex: 1;
256
+ min-width: 9rem;
257
+ max-width: 22rem;
258
+ margin-left: auto;
259
+ --lr-color-texto: #111827;
260
+ --lr-buscador-borde: transparent;
261
+ }
262
+ .acciones {
263
+ display: flex;
264
+ align-items: center;
265
+ gap: 0.375rem;
266
+ flex: none;
267
+ }
268
+ .solo-acciones {
269
+ margin-left: auto;
270
+ }
271
+ .icono {
272
+ position: relative;
273
+ display: grid;
274
+ place-items: center;
275
+ width: 2.625rem;
276
+ height: 2.625rem;
277
+ border-radius: 999px;
278
+ font-size: 1.375rem;
279
+ }
280
+ .icono:hover,
281
+ .cuenta:hover {
282
+ background: rgb(255 255 255 / 0.1);
283
+ }
284
+ .cuenta {
285
+ display: flex;
286
+ align-items: center;
287
+ gap: 0.5rem;
288
+ height: 2.625rem;
289
+ padding: 0 0.75rem 0 0.5rem;
290
+ border-radius: 999px;
291
+ font-size: 0.875rem;
292
+ font-weight: 600;
293
+ }
294
+ .nombre-usuario {
295
+ max-width: 9rem;
296
+ overflow: hidden;
297
+ text-overflow: ellipsis;
298
+ white-space: nowrap;
299
+ }
300
+ .cuenta svg {
301
+ font-size: 1.25rem;
302
+ }
303
+ .avatar {
304
+ display: grid;
305
+ place-items: center;
306
+ width: 1.875rem;
307
+ height: 1.875rem;
308
+ border-radius: 999px;
309
+ background: var(--lr-header-avatar, #fbbf24);
310
+ color: #292929;
311
+ font-size: 0.75rem;
312
+ font-weight: 800;
313
+ }
314
+ .globo {
315
+ position: absolute;
316
+ top: 0.1875rem;
317
+ right: 0;
318
+ min-width: 1.1875rem;
319
+ height: 1.1875rem;
320
+ padding: 0 0.3125rem;
321
+ border-radius: 999px;
322
+ border: 2px solid var(--_fondo);
323
+ background: var(--lr-header-globo, #ea580c);
324
+ color: #fff;
325
+ font-size: 0.6875rem;
326
+ font-weight: 800;
327
+ line-height: 0.8rem;
328
+ text-align: center;
329
+ }
330
+ .solo-lectores {
331
+ position: absolute;
332
+ width: 1px;
333
+ height: 1px;
334
+ overflow: hidden;
335
+ clip: rect(0 0 0 0);
336
+ }
337
+
338
+ /* Celular */
339
+ .celular .fila {
340
+ gap: 0.25rem;
341
+ padding: 0 0.5rem;
342
+ height: 3.75rem;
343
+ }
344
+ :host([compacto]) .celular .fila {
345
+ height: 3.25rem;
346
+ }
347
+ .celular .logo {
348
+ flex: 1;
349
+ justify-content: center;
350
+ font-size: 1.0625rem;
351
+ min-width: 0;
352
+ }
353
+ .celular .logo img {
354
+ height: 2rem;
355
+ }
356
+ .celular .acciones {
357
+ gap: 0;
358
+ }
359
+ .celular .barra-buscar {
360
+ padding: 0 0.75rem 0.75rem;
361
+ }
362
+ :host([compacto]) .celular .barra-buscar {
363
+ display: none;
364
+ }
365
+ .celular lr-buscador {
366
+ max-width: none;
367
+ margin: 0;
368
+ }
369
+
370
+ /* El menú de celular es lr-menu-lateral: su cabecera ya toma --lr-header-fondo y --lr-header-texto */
371
+ .pie {
372
+ display: grid;
373
+ gap: 0.75rem;
374
+ }
375
+ .boton-cuenta {
376
+ display: flex;
377
+ justify-content: center;
378
+ align-items: center;
379
+ gap: 0.5rem;
380
+ height: 2.875rem;
381
+ border-radius: 10px;
382
+ background: var(--_fondo);
383
+ color: var(--_texto);
384
+ font-weight: 700;
385
+ }
386
+ @media (prefers-reduced-motion: reduce) {
387
+ :host([pegajoso]),
388
+ .fila {
389
+ transition: none;
390
+ animation: none;
391
+ }
392
+ }
393
+ `,
394
+ ];
395
+
396
+ /** URL de la imagen del logo. */
397
+ @property() logo = '';
398
+
399
+ /** Nombre de la tienda (texto junto al logo y texto alternativo). */
400
+ @property() nombre = '';
401
+
402
+ /** Liga del logo. */
403
+ @property({ attribute: 'href-inicio' }) hrefInicio = '/';
404
+
405
+ /** Ligas del menú. Como atributo, en JSON. */
406
+ @property({ type: Array }) ligas: LigaHeader[] = [];
407
+
408
+ /** Piezas en el carrito (globito). */
409
+ @property({ type: Number }) piezas = 0;
410
+
411
+ /** Muestra la campana de notificaciones (por ejemplo, solo con sesión iniciada). */
412
+ @property({ type: Boolean, attribute: 'con-avisos' }) conAvisos = false;
413
+
414
+ /** Notificaciones sin leer (globito de la campana). */
415
+ @property({ type: Number }) avisos = 0;
416
+
417
+ @property({ attribute: 'texto-avisos' }) textoAvisos = 'Notificaciones';
418
+
419
+ /** Nombre de la persona con sesión iniciada; vacío muestra "Entrar". */
420
+ @property() usuario = '';
421
+
422
+ @property({ attribute: 'texto-entrar' }) textoEntrar = 'Entrar';
423
+
424
+ @property({ attribute: 'texto-cuenta' }) textoCuenta = 'Mi cuenta y pedidos';
425
+
426
+ /** Oculta el buscador. */
427
+ @property({ type: Boolean, attribute: 'sin-buscador' }) sinBuscador = false;
428
+
429
+ /** Oculta el carrito (por ejemplo, en una tienda que solo muestra su catálogo). */
430
+ @property({ type: Boolean, attribute: 'sin-carrito' }) sinCarrito = false;
431
+
432
+ /** Oculta el botón de cuenta ("Entrar" o el nombre), también en el menú lateral. */
433
+ @property({ type: Boolean, attribute: 'sin-cuenta' }) sinCuenta = false;
434
+
435
+ @property({ attribute: 'placeholder-buscador' }) placeholderBuscador = 'Buscar en la tienda';
436
+
437
+ /** Sugerencias para el buscador (ver `lr-buscador`). */
438
+ @property({ type: Array }) sugerencias: Sugerencia[] = [];
439
+
440
+ @property({ type: Boolean, attribute: 'buscando' }) buscando = false;
441
+
442
+ /** Se queda pegado arriba y se hace más bajo al hacer scroll. */
443
+ @property({ type: Boolean, reflect: true }) pegajoso = false;
444
+
445
+ /** Con `pegajoso`, se esconde al bajar y aparece al subir. */
446
+ @property({ type: Boolean, attribute: 'ocultar-al-bajar' }) ocultarAlBajar = false;
447
+
448
+ /** Ancho (px) del header debajo del cual se usa la vista de celular. */
449
+ @property({ type: Number, attribute: 'ancho-celular' }) anchoCelular = 900;
450
+
451
+ @property({ type: Boolean, reflect: true }) compacto = false;
452
+ @property({ type: Boolean, reflect: true }) escondido = false;
453
+
454
+ @state() private celular = false;
455
+ @state() private abierta = -1;
456
+ @state() private conAnuncio = false;
457
+
458
+ @query('lr-menu-lateral') private menu?: LrMenuLateral;
459
+
460
+ private observador?: ResizeObserver;
461
+ private ultimoScroll = 0;
462
+ private cierre?: ReturnType<typeof setTimeout>;
463
+
464
+ override connectedCallback() {
465
+ super.connectedCallback();
466
+ this.observador = new ResizeObserver(([e]) => (this.celular = e.contentRect.width < this.anchoCelular));
467
+ this.observador.observe(this);
468
+ window.addEventListener('scroll', this.alScroll, { passive: true });
469
+ document.addEventListener('click', this.alClicFuera);
470
+ document.addEventListener('keydown', this.alEscape);
471
+ }
472
+
473
+ override disconnectedCallback() {
474
+ super.disconnectedCallback();
475
+ this.observador?.disconnect();
476
+ window.removeEventListener('scroll', this.alScroll);
477
+ document.removeEventListener('click', this.alClicFuera);
478
+ document.removeEventListener('keydown', this.alEscape);
479
+ clearTimeout(this.cierre);
480
+ }
481
+
482
+ override render() {
483
+ return html`<header class=${this.celular ? 'celular' : ''} part="header">
484
+ <div class="anuncio" ?hidden=${!this.conAnuncio}>
485
+ <slot name="anuncio" @slotchange=${this.alCambiarAnuncio}></slot>
486
+ </div>
487
+ ${this.celular ? this.vistaCelular() : this.vistaEscritorio()}
488
+ </header>`;
489
+ }
490
+
491
+ /** Abre el menú de celular. */
492
+ abrirMenu() {
493
+ this.menu?.abrir();
494
+ }
495
+
496
+ /** Cierra el menú de celular y los submenús. */
497
+ cerrarMenu() {
498
+ this.menu?.cerrar();
499
+ this.cerrarSubmenu();
500
+ }
501
+
502
+ private vistaEscritorio() {
503
+ const liga = this.ligas[this.abierta];
504
+ return html`<div class="fila">
505
+ ${this.logoTpl()}
506
+ ${this.ligas.length
507
+ ? html`<nav aria-label="Principal">
508
+ <ul>
509
+ ${this.ligas.map((l, i) => this.ligaEscritorio(l, i))}
510
+ </ul>
511
+ </nav>`
512
+ : nothing}
513
+ ${this.buscadorTpl()}
514
+ <div class="acciones ${this.sinBuscador ? 'solo-acciones' : ''}">${this.avisosTpl()} ${this.cuentaTpl()} ${this.carritoTpl()}</div>
515
+ </div>
516
+ ${liga?.columnas?.length ? this.megaTpl(liga) : nothing}`;
517
+ }
518
+
519
+ private vistaCelular() {
520
+ return html`<div class="fila">
521
+ <button class="icono" aria-label="Abrir menú" aria-haspopup="dialog" @click=${this.abrirMenu}>${iconoMenu2}</button>
522
+ ${this.logoTpl()}
523
+ <div class="acciones">${this.avisosTpl()} ${this.carritoTpl()}</div>
524
+ </div>
525
+ ${this.sinBuscador ? nothing : html`<div class="barra-buscar">${this.buscadorTpl()}</div>`}
526
+ <lr-menu-lateral
527
+ .ligas=${this.ligasMenu()}
528
+ logo=${this.logo}
529
+ nombre=${this.nombre}
530
+ href-inicio=${this.hrefInicio}
531
+ @cerrar=${() => (this.abierta = -1)}
532
+ >
533
+ <div slot="pie" class="pie">
534
+ <slot name="menu-pie"></slot>
535
+ ${this.sinCuenta
536
+ ? nothing
537
+ : html`<button class="boton-cuenta" @click=${this.alCuenta}>
538
+ ${this.usuario
539
+ ? html`<span class="avatar" aria-hidden="true">${this.iniciales()}</span> ${this.textoCuenta}`
540
+ : html`${iconoUser} ${this.textoEntrar}`}
541
+ </button>`}
542
+ </div>
543
+ </lr-menu-lateral>`;
544
+ }
545
+
546
+ private logoTpl() {
547
+ const inicio: LigaHeader = { texto: this.nombre || 'Inicio', href: this.hrefInicio, id: 'inicio' };
548
+ return html`<a class="logo" href=${this.hrefInicio} part="logo" @click=${(e: MouseEvent) => this.alNavegar(e, inicio)}>
549
+ ${this.logo ? html`<img src=${this.logo} alt=${this.nombre ? '' : 'Inicio'} />` : nothing}
550
+ ${this.nombre ? html`<span>${this.nombre}</span>` : nothing}
551
+ </a>`;
552
+ }
553
+
554
+ private buscadorTpl() {
555
+ if (this.sinBuscador) return nothing;
556
+ return html`<lr-buscador
557
+ placeholder=${this.placeholderBuscador}
558
+ .sugerencias=${this.sugerencias}
559
+ ?cargando=${this.buscando}
560
+ ver-todo
561
+ ></lr-buscador>`;
562
+ }
563
+
564
+ /** Cuenta en escritorio; en celular va en el menú lateral. */
565
+ private cuentaTpl() {
566
+ if (this.sinCuenta) return nothing;
567
+ return html`<button class="cuenta" @click=${this.alCuenta}>
568
+ ${this.usuario
569
+ ? html`<span class="avatar" aria-hidden="true">${this.iniciales()}</span><span class="solo-lectores">${this.textoCuenta}:</span><span class="nombre-usuario">${this.usuario.split(/\s+/)[0]}</span>`
570
+ : html`${iconoUser}${this.textoEntrar}`}
571
+ </button>`;
572
+ }
573
+
574
+ private avisosTpl() {
575
+ if (!this.conAvisos) return nothing;
576
+ const n = Math.max(0, Math.floor(this.avisos));
577
+ return html`<button
578
+ class="icono"
579
+ aria-label=${n > 0 ? `${this.textoAvisos}, ${n} sin leer` : this.textoAvisos}
580
+ @click=${this.alAvisos}
581
+ >
582
+ ${iconoBell}${n > 0 ? html`<span class="globo" aria-hidden="true">${n > 99 ? '99+' : n}</span>` : nothing}
583
+ </button>`;
584
+ }
585
+
586
+ private carritoTpl() {
587
+ if (this.sinCarrito) return nothing;
588
+ return html`<slot name="carrito">
589
+ <button class="icono" aria-label="Carrito, ${this.piezas} ${this.piezas === 1 ? 'pieza' : 'piezas'}" @click=${this.alCarrito}>
590
+ ${iconoShoppingBag}${this.piezas > 0 ? html`<span class="globo" aria-hidden="true">${this.piezas > 99 ? '99+' : this.piezas}</span>` : nothing}
591
+ </button>
592
+ </slot>`;
593
+ }
594
+
595
+ private ligaEscritorio(liga: LigaHeader, i: number) {
596
+ const conSub = !!(liga.columnas?.length || liga.hijos?.length);
597
+ if (!conSub) {
598
+ return html`<li><a class="liga" href=${liga.href || '#'} @click=${(e: MouseEvent) => this.alNavegar(e, liga)}>${liga.texto}</a></li>`;
599
+ }
600
+ const abierta = this.abierta === i;
601
+ return html`<li @mouseenter=${() => this.abrirSubmenu(i)} @mouseleave=${() => this.alSalirLiga(i)}>
602
+ <button class="liga" aria-expanded=${abierta} @click=${(e: MouseEvent) => this.alClicLiga(e, i)}>${liga.texto} ${iconoChevronDown}</button>
603
+ ${abierta && !liga.columnas?.length
604
+ ? html`<ul class="submenu">
605
+ ${liga.hijos!.map((h) => html`<li><a href=${h.href || '#'} @click=${(e: MouseEvent) => this.alNavegar(e, h)}>${h.texto}</a></li>`)}
606
+ </ul>`
607
+ : nothing}
608
+ </li>`;
609
+ }
610
+
611
+ private megaTpl(liga: LigaHeader) {
612
+ const promo = liga.promo;
613
+ const indice = this.abierta;
614
+ return html`<div class="mega" @mouseenter=${() => this.abrirSubmenu(indice)} @mouseleave=${() => this.cerrarConRetraso()}>
615
+ <div class="dentro ${promo ? 'con-promo' : ''}" style="--_columnas: ${liga.columnas!.length}">
616
+ ${liga.columnas!.map(
617
+ (c) => html`<div>
618
+ <h3>${c.titulo}</h3>
619
+ <ul>
620
+ ${c.ligas.map((l) => html`<li><a href=${l.href || '#'} @click=${(e: MouseEvent) => this.alNavegar(e, l)}>${l.texto}</a></li>`)}
621
+ </ul>
622
+ </div>`,
623
+ )}
624
+ ${promo
625
+ ? html`<a
626
+ class="promo"
627
+ href=${promo.href || '#'}
628
+ style=${promo.imagen ? `background-image: linear-gradient(0deg, rgb(0 0 0 / .55), rgb(0 0 0 / 0) 70%), url(${promo.imagen})` : ''}
629
+ @click=${(e: MouseEvent) => this.alNavegar(e, { texto: promo.titulo, href: promo.href, id: promo.id })}
630
+ >
631
+ <b>${promo.titulo}</b>${promo.texto ? html`<span>${promo.texto}</span>` : nothing}
632
+ </a>`
633
+ : nothing}
634
+ </div>
635
+ </div>`;
636
+ }
637
+
638
+ /** Ligas para el menú de celular: el mega menú se aplana con sus títulos como grupos. */
639
+ private ligasMenu(): LigaMenu[] {
640
+ return this.ligas.map((l) => ({
641
+ texto: l.texto,
642
+ href: l.href,
643
+ id: l.id,
644
+ hijos: l.columnas?.length ? l.columnas.flatMap((c) => [{ grupo: c.titulo }, ...c.ligas]) : l.hijos,
645
+ }));
646
+ }
647
+
648
+ private iniciales() {
649
+ return this.usuario
650
+ .split(/\s+/)
651
+ .filter(Boolean)
652
+ .slice(0, 2)
653
+ .map((p) => p[0]!.toUpperCase())
654
+ .join('');
655
+ }
656
+
657
+ private alNavegar(evento: MouseEvent, liga: LigaHeader) {
658
+ if (liga.href && (evento.ctrlKey || evento.metaKey)) return;
659
+ evento.preventDefault();
660
+ this.cerrarMenu();
661
+ emitir<DetalleNavegar>(this, 'navegar', { liga });
662
+ }
663
+
664
+ private alCuenta = () => {
665
+ this.cerrarMenu();
666
+ emitir(this, 'abrir-cuenta');
667
+ };
668
+
669
+ private alAvisos = () => {
670
+ this.cerrarMenu();
671
+ emitir(this, 'abrir-notificaciones');
672
+ };
673
+
674
+ private alCarrito = () => {
675
+ this.cerrarMenu();
676
+ emitir(this, 'abrir-carrito');
677
+ };
678
+
679
+ private alClicLiga(evento: MouseEvent, i: number) {
680
+ // Con mouse el hover ya lo abrió: el clic no debe cerrarlo. Con teclado (detail 0) alterna.
681
+ if (evento.detail === 0) this.abierta === i ? this.cerrarSubmenu() : this.abrirSubmenu(i);
682
+ else this.abrirSubmenu(i);
683
+ }
684
+
685
+ private alSalirLiga(i: number) {
686
+ // El mega menú vive fuera del <li>; se cierra cuando el mouse sale de él.
687
+ if (!this.ligas[i]?.columnas?.length) this.cerrarConRetraso();
688
+ }
689
+
690
+ private abrirSubmenu(i: number) {
691
+ clearTimeout(this.cierre);
692
+ this.abierta = i;
693
+ }
694
+
695
+ private cerrarSubmenu() {
696
+ clearTimeout(this.cierre);
697
+ this.abierta = -1;
698
+ }
699
+
700
+ /** Cierra tras un momento, para que el mouse pueda cruzar en diagonal o por un hueco sin perder el submenú. */
701
+ private cerrarConRetraso() {
702
+ clearTimeout(this.cierre);
703
+ this.cierre = setTimeout(() => (this.abierta = -1), 250);
704
+ }
705
+
706
+ private alCambiarAnuncio(e: Event) {
707
+ this.conAnuncio = (e.target as HTMLSlotElement).assignedElements().length > 0;
708
+ }
709
+
710
+ private alClicFuera = (e: MouseEvent) => {
711
+ if (this.abierta >= 0 && !e.composedPath().includes(this)) this.cerrarSubmenu();
712
+ };
713
+
714
+ private alEscape = (e: KeyboardEvent) => {
715
+ if (e.key === 'Escape' && this.abierta >= 0) this.cerrarSubmenu();
716
+ };
717
+
718
+ private alScroll = () => {
719
+ if (!this.pegajoso) return;
720
+ const y = window.scrollY;
721
+ this.compacto = y > 40;
722
+ if (this.ocultarAlBajar) this.escondido = y > 200 && y > this.ultimoScroll && this.abierta < 0;
723
+ this.ultimoScroll = y;
724
+ };
725
+ }
726
+
727
+ declare global {
728
+ interface HTMLElementTagNameMap {
729
+ 'lr-custom-header': LrCustomHeader;
730
+ }
731
+ }