|
1 | 1 | import { html, LitElement } from 'lit'; |
2 | 2 | import { customElement } from 'lit/decorators.js'; |
| 3 | +// unsafeHTML is needed to render dynamic custom-element tag names; |
| 4 | +// Lit's html`` tag cannot render variable tag names directly. |
| 5 | +import { unsafeHTML } from 'lit/directives/unsafe-html.js'; |
3 | 6 |
|
4 | | -import '@src/elements/ia-button/ia-button-story'; |
5 | | -import '@src/labs/ia-snow/ia-snow-story'; |
6 | | -import '@src/elements/ia-combo-box/ia-combo-box-story'; |
7 | | -import '@src/elements/ia-status-indicator/ia-status-indicator-story'; |
| 7 | +const storyModules = import.meta.glob( |
| 8 | + ['../src/elements/**/*-story.ts', '../src/labs/**/*-story.ts'], |
| 9 | + { eager: true } |
| 10 | +); |
| 11 | + |
| 12 | +const storyEntries = Object.keys(storyModules) |
| 13 | + .map(path => { |
| 14 | + const labs = path.includes('/src/labs/'); |
| 15 | + const parts = path.split('/'); |
| 16 | + const filename = parts[parts.length - 1]; // e.g. "ia-button-story.ts" |
| 17 | + const tag = filename.replace(/-story\.ts$/, ''); |
| 18 | + return { tag, storyTag: `${tag}-story`, id: `elem-${tag}`, labs }; |
| 19 | + }) |
| 20 | + .sort((a, b) => a.tag.localeCompare(b.tag)); |
| 21 | + |
| 22 | +const productionEntries = storyEntries.filter(e => !e.labs); |
| 23 | +const labsEntries = storyEntries.filter(e => e.labs); |
| 24 | +const ALL_ENTRIES = [...productionEntries, ...labsEntries]; |
8 | 25 |
|
9 | 26 | @customElement('app-root') |
10 | 27 | export class AppRoot extends LitElement { |
| 28 | + createRenderRoot() { return this; } |
| 29 | + |
| 30 | + private _observer?: IntersectionObserver; |
| 31 | + private _abortController = new AbortController(); |
| 32 | + |
11 | 33 | render() { |
12 | 34 | return html` |
13 | | - <h1>🏛️ Internet Archive Elements ⚛️</h1> |
| 35 | + <nav id="ia-sidebar"> |
| 36 | + <h2>Production-Ready</h2> |
| 37 | + ${productionEntries.map(e => html`<a href="#${e.id}"><${e.tag}></a>`)} |
| 38 | + <h2>Labs 🧪</h2> |
| 39 | + ${labsEntries.map(e => html`<a href="#${e.id}"><${e.tag}></a>`)} |
| 40 | + </nav> |
| 41 | + <div id="ia-content"> |
| 42 | + <h1>Internet Archive Elements</h1> |
| 43 | + <h2>Production-Ready Elements</h2> |
| 44 | + ${productionEntries.map(e => html` |
| 45 | + <div id="${e.id}" class="ia-anchor"> |
| 46 | + ${unsafeHTML(`<${e.storyTag}></${e.storyTag}>`)} |
| 47 | + </div> |
| 48 | + `)} |
| 49 | + <h2>Labs Elements</h2> |
| 50 | + ${labsEntries.map(e => html` |
| 51 | + <div id="${e.id}" class="ia-anchor"> |
| 52 | + ${unsafeHTML(`<${e.storyTag}></${e.storyTag}>`)} |
| 53 | + </div> |
| 54 | + `)} |
| 55 | + </div> |
| 56 | + `; |
| 57 | + } |
14 | 58 |
|
15 | | - <h2>🚀 Production-Ready Elements</h2> |
| 59 | + firstUpdated() { |
| 60 | + const allIds = ALL_ENTRIES.map(e => e.id); |
16 | 61 |
|
17 | | - <ia-status-indicator-story></ia-status-indicator-story> |
18 | | - <ia-combo-box-story></ia-combo-box-story> |
| 62 | + const links = Object.fromEntries( |
| 63 | + allIds.map(id => [id, this.querySelector(`#ia-sidebar a[href="#${id}"]`)]) |
| 64 | + ); |
19 | 65 |
|
20 | | - <h2>🧪 Labs Elements</h2> |
| 66 | + const visible = new Set<string>(); |
21 | 67 |
|
22 | | - <ia-snow-story></ia-snow-story> |
23 | | - <ia-button-story></ia-button-story> |
24 | | - `; |
| 68 | + // Only anchors in the top 30% of the viewport count as "active". |
| 69 | + // The first (topmost) visible anchor wins. |
| 70 | + this._observer = new IntersectionObserver( |
| 71 | + entries => { |
| 72 | + for (const entry of entries) { |
| 73 | + if (entry.isIntersecting) visible.add(entry.target.id); |
| 74 | + else visible.delete(entry.target.id); |
| 75 | + } |
| 76 | + const activeId = allIds.find(id => visible.has(id)) ?? allIds[0]; |
| 77 | + allIds.forEach(id => links[id]?.classList.toggle('active', id === activeId)); |
| 78 | + }, |
| 79 | + { rootMargin: '0px 0px -70% 0px' }, |
| 80 | + ); |
| 81 | + |
| 82 | + allIds.forEach(id => { |
| 83 | + const el = document.getElementById(id); |
| 84 | + if (el) this._observer!.observe(el); |
| 85 | + }); |
| 86 | + |
| 87 | + allIds.forEach(id => { |
| 88 | + links[id]?.addEventListener('click', (e: Event) => { |
| 89 | + e.preventDefault(); |
| 90 | + const el = document.getElementById(id); |
| 91 | + if (el) { |
| 92 | + const top = el.getBoundingClientRect().top + window.scrollY; |
| 93 | + window.scrollTo({ top: Math.max(0, top - 16), behavior: 'smooth' }); |
| 94 | + } |
| 95 | + }, { signal: this._abortController.signal }); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + disconnectedCallback() { |
| 100 | + super.disconnectedCallback(); |
| 101 | + this._observer?.disconnect(); |
| 102 | + this._abortController.abort(); |
25 | 103 | } |
26 | 104 | } |
0 commit comments