igniteui-astro-components 0.0.1

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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +374 -0
  3. package/package.json +109 -0
  4. package/src/components/DocsSidebar/DocsSidebar.astro +99 -0
  5. package/src/components/DocsSidebar/README.md +166 -0
  6. package/src/components/DocsSidebar/SidebarFilterInput.astro +74 -0
  7. package/src/components/DocsSidebar/SidebarLabel.astro +40 -0
  8. package/src/components/DocsSidebar/sidebar-filter.ts +393 -0
  9. package/src/components/DocsSidebar/toTreeNodes.ts +66 -0
  10. package/src/components/DocsSubHeader/DocsSubHeader.astro +320 -0
  11. package/src/components/DocsSubHeader/README.md +74 -0
  12. package/src/components/DocsToc/DocsToc.astro +307 -0
  13. package/src/components/DocsToc/README.md +68 -0
  14. package/src/components/DocsToc/toTreeNodes.ts +57 -0
  15. package/src/components/DocsTree/DocsTree.astro +66 -0
  16. package/src/components/DocsTree/DocsTreeItem.astro +84 -0
  17. package/src/components/DocsTree/README.md +105 -0
  18. package/src/components/DocsTree/docs-tree.css +227 -0
  19. package/src/components/DocsTree/types.ts +47 -0
  20. package/src/components/GlobalFooter/GlobalFooter.astro +90 -0
  21. package/src/components/GlobalFooter/GlobalFooter.module.scss +23 -0
  22. package/src/components/GlobalFooter/README.md +35 -0
  23. package/src/components/GlobalNavBar/GlobalNavBar.astro +218 -0
  24. package/src/components/GlobalNavBar/GlobalNavBar.module.scss +215 -0
  25. package/src/components/GlobalNavBar/README.md +49 -0
  26. package/src/components/MobileSidebarToggle/MobileSidebarToggle.astro +112 -0
  27. package/src/components/MobileSidebarToggle/README.md +31 -0
  28. package/src/components/Search/README.md +43 -0
  29. package/src/components/Search/Search.astro +222 -0
  30. package/src/components/ThemingWidget/README.md +35 -0
  31. package/src/components/ThemingWidget/ThemingWidget.astro +152 -0
  32. package/src/components/mdx/ApiLink/ApiLink.astro +121 -0
  33. package/src/components/mdx/ApiLink/README.md +68 -0
  34. package/src/components/mdx/ApiRef/ApiRef.astro +92 -0
  35. package/src/components/mdx/ApiRef/README.md +50 -0
  36. package/src/components/mdx/DocsAside/DocsAside.astro +86 -0
  37. package/src/components/mdx/DocsAside/README.md +66 -0
  38. package/src/components/mdx/DocsAside/icons/caution.svg +3 -0
  39. package/src/components/mdx/DocsAside/icons/danger.svg +3 -0
  40. package/src/components/mdx/DocsAside/icons/note.svg +3 -0
  41. package/src/components/mdx/DocsAside/icons/tip.svg +4 -0
  42. package/src/components/mdx/PlatformBlock/PlatformBlock.astro +24 -0
  43. package/src/components/mdx/PlatformBlock/README.md +57 -0
  44. package/src/components/mdx/Sample/README.md +59 -0
  45. package/src/components/mdx/Sample/Sample.astro +142 -0
  46. package/src/content-helper.ts +159 -0
  47. package/src/content.config.ts +17 -0
  48. package/src/index.ts +42 -0
  49. package/src/integration.ts +841 -0
  50. package/src/layouts/DocsLayout/DocsLayout.astro +307 -0
  51. package/src/layouts/DocsLayout/README.md +124 -0
  52. package/src/lib/nav-helpers.ts +155 -0
  53. package/src/lib/platform-context.ts +305 -0
  54. package/src/lib/sidebar/helpers.ts +81 -0
  55. package/src/lib/sidebar/types.ts +36 -0
  56. package/src/llms.ts +315 -0
  57. package/src/platform.ts +281 -0
  58. package/src/plugins/remark-docfx.ts +368 -0
  59. package/src/scripts/sample-widget.ts +516 -0
  60. package/src/sidebar.ts +167 -0
  61. package/src/styles/custom.scss +254 -0
  62. package/src/styles/ig-theme.scss +43 -0
  63. package/src/virtual-modules.d.ts +35 -0
@@ -0,0 +1,166 @@
1
+ # Sidebar suite
2
+
3
+ The sidebar is composed of four focused components and a custom element:
4
+
5
+ | File | Export path | Description |
6
+ |------|-------------|-------------|
7
+ | `DocsSidebar.astro` | `…/components/sidebar/DocsSidebar.astro` | Composition root — filter + scrollable tree |
8
+ | `SidebarFilterInput.astro` | `…/components/sidebar/SidebarFilterInput.astro` | Filter `<input>` markup |
9
+ | `SidebarTree.astro` | `…/components/sidebar/SidebarTree.astro` | Recursive `<ul>` renderer |
10
+ | `SidebarItem.astro` | `…/components/sidebar/SidebarItem.astro` | Single link or `<details>` group |
11
+ | `sidebar-filter.ts` | `…/components/sidebar/sidebar-filter` | `<sidebar-filter>` custom element |
12
+
13
+ ---
14
+
15
+ ## DocsSidebar
16
+
17
+ The only component you need to mount directly.
18
+
19
+ ### Import
20
+
21
+ ```astro
22
+ import DocsSidebar from 'igniteui-astro-components/components/sidebar/DocsSidebar.astro';
23
+ ```
24
+
25
+ ### Props
26
+
27
+ | Prop | Type | Default | Description |
28
+ |------|------|---------|-------------|
29
+ | `currentSlug` | `string` | `''` | Slug of the active page. Highlights the matching link and auto-expands ancestor groups. |
30
+ | `items` | `SidebarEntry[]` | virtual module | Sidebar tree to render. **Overrides** the `virtual:docs-template/site-meta` value when provided. Use this when not running `siteMetaIntegration`. |
31
+
32
+ ### Example
33
+
34
+ #### Prop-driven (no `siteMetaIntegration` required)
35
+
36
+ ```astro
37
+ ---
38
+ import DocsSidebar from 'igniteui-astro-components/components/sidebar/DocsSidebar.astro';
39
+ import type { SidebarEntry } from 'igniteui-astro-components/lib/sidebar/types';
40
+
41
+ // Define the tree inline or import from a shared config file.
42
+ const sidebar: SidebarEntry[] = [
43
+ {
44
+ label: 'Getting Started',
45
+ collapsed: false,
46
+ items: [
47
+ { label: 'Introduction', slug: '' },
48
+ { label: 'Installation', slug: 'installation' },
49
+ ],
50
+ },
51
+ {
52
+ label: 'Components',
53
+ collapsed: false,
54
+ items: [
55
+ { label: 'Toast', slug: 'components/toast' },
56
+ { label: 'Calendar', slug: 'components/calendar', badge: { text: 'New', variant: 'success' } },
57
+ ],
58
+ },
59
+ ];
60
+ ---
61
+ <DocsSidebar items={sidebar} currentSlug="components/toast" />
62
+ ```
63
+
64
+ Place it in the `sidebar` slot of `DocsLayout`:
65
+
66
+ ```astro
67
+ <DocsLayout siteTitle="My Library" hasSidebar currentSlug={slug}>
68
+ <DocsSidebar slot="sidebar" items={sidebar} currentSlug={slug} />
69
+ <slot />
70
+ </DocsLayout>
71
+ ```
72
+
73
+ #### With `siteMetaIntegration` (integration-driven)
74
+
75
+ When `createDocsSite()` / `siteMetaIntegration()` is configured, omit
76
+ `items` — the tree is read from the virtual module automatically.
77
+
78
+ ```astro
79
+ ---
80
+ import DocsSidebar from 'igniteui-astro-components/components/sidebar/DocsSidebar.astro';
81
+ ---
82
+ <DocsSidebar currentSlug="components/toast" />
83
+ ```
84
+
85
+ ### Sidebar data
86
+
87
+ #### Prop-driven
88
+
89
+ Pass the tree directly via the `items` prop (see example above).
90
+ A good pattern is to define the tree in a shared config file:
91
+
92
+ ```ts
93
+ // src/site-config.ts
94
+ import type { SidebarEntry } from 'igniteui-astro-components/lib/sidebar/types';
95
+
96
+ export const SIDEBAR: SidebarEntry[] = [ /* … */ ];
97
+ ```
98
+
99
+ Then import it in every layout/page:
100
+
101
+ ```astro
102
+ import { SIDEBAR } from '../site-config';
103
+ // …
104
+ <DocsSidebar items={SIDEBAR} currentSlug={slug} />
105
+ ```
106
+
107
+ #### Integration-driven
108
+
109
+ Build the tree at config time using `buildSidebarFromToc()` and pass it to
110
+ `siteMetaIntegration` — it is then available automatically via the virtual
111
+ module:
112
+
113
+ ```ts
114
+ // astro.config.ts
115
+ import { buildSidebarFromToc, siteMetaIntegration } from 'igniteui-astro-components/integration';
116
+
117
+ const sidebar = buildSidebarFromToc({ tocPath: './docs/toc.yml', docsDir: './docs/en' });
118
+
119
+ export default defineConfig({
120
+ integrations: [
121
+ siteMetaIntegration({ title: 'My Docs', sidebar }),
122
+ ],
123
+ });
124
+ ```
125
+
126
+ ### SidebarEntry type
127
+
128
+ ```ts
129
+ type SidebarLink = {
130
+ label: string;
131
+ slug: string;
132
+ badge?: { text: string; variant: 'note' | 'tip' | 'caution' | 'danger' | 'success' | 'default' };
133
+ attrs?: Record<string, string | boolean | undefined>;
134
+ };
135
+
136
+ type SidebarGroup = {
137
+ label: string;
138
+ collapsed?: boolean; // false = open by default; true = closed by default
139
+ items: SidebarEntry[];
140
+ };
141
+
142
+ type SidebarEntry = SidebarLink | SidebarGroup;
143
+ ```
144
+
145
+ ### Filter behaviour
146
+
147
+ The `<sidebar-filter>` custom element handles live filtering:
148
+ - Typing in the input adds `data-filtering` to the host element.
149
+ - Items that match get `data-filter-match`; non-matching items are hidden via CSS.
150
+ - "No results" text is set via `data-no-results` on the host element.
151
+ - Filter state persists across Astro View Transitions navigations via `sessionStorage`.
152
+
153
+ ---
154
+
155
+ ## SidebarTree / SidebarItem
156
+
157
+ These are used internally by `DocsSidebar`. Import them directly only when building a fully custom sidebar layout.
158
+
159
+ ```astro
160
+ import SidebarTree from 'igniteui-astro-components/components/sidebar/SidebarTree.astro';
161
+ import SidebarItem from 'igniteui-astro-components/components/sidebar/SidebarItem.astro';
162
+ ```
163
+
164
+ `SidebarTree` props: `items: SidebarEntry[]`, `currentSlug: string`, `ancestors?: string[]`
165
+
166
+ `SidebarItem` props: `item: SidebarEntry`, `currentSlug: string`, `ancestors?: string[]`
@@ -0,0 +1,74 @@
1
+ ---
2
+ /**
3
+ * Filter input UI for the docs sidebar.
4
+ * Markup-only — behavior lives in `./sidebar-filter.ts`.
5
+ * Uses igc-input / igc-button / igc-icon from igniteui-webcomponents.
6
+ * Icons are registered via registerIconFromText in DocsSidebar.astro's script.
7
+ */
8
+ ---
9
+
10
+ <div class="sidebar-filter-wrapper" role="search" aria-label="Filter sidebar topics">
11
+ <igc-input
12
+ id="sidebar-filter-input"
13
+ type="search"
14
+ autocomplete="off"
15
+ spellcheck="false"
16
+ placeholder="Filter topics…"
17
+ aria-label="Filter navigation topics"
18
+ aria-controls="docs-sidebar"
19
+ data-sidebar-filter-input
20
+ >
21
+ <igc-icon slot="prefix" name="sidebar-filter-search" aria-hidden="true"></igc-icon>
22
+ <igc-button
23
+ slot="suffix"
24
+ variant="flat"
25
+ aria-label="Clear filter"
26
+ data-sidebar-filter-clear
27
+ hidden
28
+ ><igc-icon name="sidebar-filter-clear" aria-hidden="true"></igc-icon></igc-button>
29
+ </igc-input>
30
+ <p
31
+ role="status"
32
+ aria-atomic="true"
33
+ class="sidebar-filter-status"
34
+ data-sidebar-filter-status
35
+ ></p>
36
+ </div>
37
+
38
+ <style>
39
+ /* Sits above the scroll container — always visible, never scrolls. */
40
+ .sidebar-filter-wrapper {
41
+ flex-shrink: 0;
42
+ margin-block-end: 0.5rem;
43
+ padding-block-end: 0.25rem;
44
+ background: var(--sl-color-bg-sidebar, var(--sl-color-bg));
45
+ }
46
+
47
+ igc-input {
48
+ display: block;
49
+ width: 100%;
50
+ --ig-size: 1;
51
+ }
52
+
53
+ igc-input::part(container) {
54
+ font-size: var(--sl-text-xs, 0.75rem);
55
+ font-family: inherit;
56
+ }
57
+
58
+ igc-input::part(input) {
59
+ font-size: var(--sl-text-xs, 0.75rem);
60
+ font-family: inherit;
61
+ }
62
+
63
+ igc-button[data-sidebar-filter-clear] {
64
+ --ig-size: 1;
65
+ }
66
+
67
+ .sidebar-filter-status {
68
+ margin: 0.25rem 0 0;
69
+ font-size: 0.7rem;
70
+ color: var(--sl-color-gray-3);
71
+ min-height: 1em;
72
+ padding-inline-start: 0.1rem;
73
+ }
74
+ </style>
@@ -0,0 +1,40 @@
1
+ ---
2
+ /**
3
+ * Label-slot renderer for DocsTree's sidebar variant.
4
+ *
5
+ * Receives `{ node, depth, isLeaf, isActive }` from DocsTreeItem.
6
+ * Branches on isLeaf:
7
+ * • leaf → label text + optional badge (from node.meta.badge)
8
+ * • group → label text + caret SVG that rotates on [expanded]
9
+ *
10
+ * The outer container is the `<a class="docs-tree-link">` (leaves) or
11
+ * `<div class="docs-tree-group">` (groups) provided by DocsTreeItem.
12
+ * SidebarLabel only contributes the inner content.
13
+ */
14
+ import type { TreeNode } from '../DocsTree/types';
15
+ import type { SidebarBadge } from '../../lib/sidebar/types';
16
+
17
+ interface Props {
18
+ node: TreeNode;
19
+ depth: number;
20
+ isLeaf: boolean;
21
+ isActive: boolean;
22
+ }
23
+
24
+ const { node, isLeaf } = Astro.props;
25
+ const badge = (node.meta?.badge ?? null) as SidebarBadge | null;
26
+ ---
27
+
28
+ {isLeaf ? (
29
+ <>
30
+ <span class="docs-tree-label">{node.label}</span>
31
+ {badge && <span class={`sl-badge ${badge.variant}`}>{badge.text}</span>}
32
+ </>
33
+ ) : (
34
+ <>
35
+ <span class="group-label-text">{node.label}</span>
36
+ <svg class="group-caret" viewBox="0 0 16 16" width="10" height="10" aria-hidden="true" focusable="false">
37
+ <path d="M4 2l8 6-8 6V2z" fill="currentColor" />
38
+ </svg>
39
+ </>
40
+ )}
@@ -0,0 +1,393 @@
1
+ /**
2
+ * <sidebar-filter> custom element — drives the filter input + expand/collapse
3
+ * state for the igc-tree-based DocsSidebar.
4
+ *
5
+ * Design:
6
+ * • Filter visibility is CSS-driven:
7
+ * [data-filtering] on the host + [data-filter-match] on each item.
8
+ * • Expand/collapse uses the igc-tree-item *property* API (`el.expanded = bool`)
9
+ * for upgraded elements. The sole exception is `astro:before-swap`, which
10
+ * operates on raw not-yet-upgraded DOM and therefore uses setAttribute.
11
+ * • igcItemExpanded / igcItemCollapsed are dispatched on <igc-tree> with the
12
+ * item in `event.detail` (not e.target, which is the tree itself).
13
+ * • DETAILS_KEY is maintained incrementally:
14
+ * – primed from DOM attributes on the first hard load (connectedCallback)
15
+ * – updated in onItemToggle WITHOUT re-reading the DOM, because Lit
16
+ * reflects the `expanded` property to the attribute asynchronously; the
17
+ * attribute is stale at the time the event fires.
18
+ *
19
+ * sessionStorage keys:
20
+ * sidebar-filter-value → current filter input value
21
+ * sidebar-filter-details → newline-joined list of expanded group keys
22
+ * sidebar-filter-scroll → scrollTop of the scroll container
23
+ */
24
+
25
+ const FILTER_KEY = 'sidebar-filter-value';
26
+ const DETAILS_KEY = 'sidebar-filter-details';
27
+ const SCROLL_KEY = 'sidebar-filter-scroll';
28
+
29
+ const SCROLL_SELECTOR = '[data-sidebar-scroll]';
30
+ const ITEM_SELECTOR = 'igc-tree-item[data-path]';
31
+ const GROUP_SELECTOR = 'igc-tree-item[data-group-key]';
32
+
33
+ let _isClientSideNav = false;
34
+
35
+ // ── Storage helpers ──────────────────────────────────────────────────────────
36
+
37
+ const safeGet = (k: string): string => { try { return sessionStorage.getItem(k) ?? ''; } catch { return ''; } };
38
+ const safeSet = (k: string, v: string) => { try { sessionStorage.setItem(k, v); } catch { /* quota */ } };
39
+ const safeRemove = (k: string) => { try { sessionStorage.removeItem(k); } catch { /* */ } };
40
+
41
+ // ── Helpers ──────────────────────────────────────────────────────────────────
42
+
43
+ /**
44
+ * Read expanded keys by checking the `expanded` ATTRIBUTE.
45
+ * Works on both upgraded custom elements and raw DOM nodes (astro:before-swap).
46
+ * Note: only call this when Lit has had time to reflect properties → attributes
47
+ * (i.e. not synchronously inside event handlers).
48
+ */
49
+ const collectExpandedKeys = (root: ParentNode): string[] => {
50
+ const keys: string[] = [];
51
+ root.querySelectorAll<HTMLElement>(GROUP_SELECTOR).forEach((el) => {
52
+ if (el.hasAttribute('expanded') && el.dataset.groupKey) keys.push(el.dataset.groupKey);
53
+ });
54
+ return keys;
55
+ };
56
+
57
+ /**
58
+ * Apply expanded state using setAttribute / removeAttribute.
59
+ * Used ONLY in astro:before-swap on not-yet-upgraded DOM nodes.
60
+ */
61
+ const applyExpandedAttr = (root: ParentNode, openKeys: Set<string>): void => {
62
+ root.querySelectorAll<HTMLElement>(GROUP_SELECTOR).forEach((el) => {
63
+ // Always keep ancestors of the active page open.
64
+ if (el.querySelector('a[aria-current="page"]')) { el.setAttribute('expanded', ''); return; }
65
+ const key = el.dataset.groupKey;
66
+ if (key && openKeys.has(key)) el.setAttribute('expanded', '');
67
+ else el.removeAttribute('expanded');
68
+ });
69
+ };
70
+
71
+ const tokenize = (q: string): string[] => q.toLowerCase().split(/\s+/).filter(Boolean);
72
+ const itemPath = (el: HTMLElement): string => (el.dataset.path ?? el.dataset.label ?? '').toLowerCase();
73
+
74
+ // ── Cross-navigation hooks ───────────────────────────────────────────────────
75
+
76
+ document.addEventListener('astro:before-preparation', () => {
77
+ _isClientSideNav = true;
78
+
79
+ const sc = document.querySelector<HTMLElement>(SCROLL_SELECTOR);
80
+ if (sc) safeSet(SCROLL_KEY, String(sc.scrollTop));
81
+
82
+ // When filtering is active, override DETAILS_KEY with the filter's openSnapshot
83
+ // (which tracks user-intent expand state including manual collapses during filter).
84
+ // When NOT filtering, DETAILS_KEY was already kept up-to-date by onItemToggle,
85
+ // so we leave it as-is.
86
+ const host = document.querySelector<HTMLElement>('sidebar-filter[data-filtering]');
87
+ const intended = host?.dataset.openSnapshot;
88
+ if (intended !== undefined) safeSet(DETAILS_KEY, intended);
89
+ });
90
+
91
+ document.addEventListener('astro:before-swap', (e) => {
92
+ const newDoc = (e as unknown as { newDocument: Document }).newDocument;
93
+ const newScroll = newDoc.querySelector<HTMLElement>(SCROLL_SELECTOR);
94
+ if (newScroll) newScroll.style.visibility = 'hidden';
95
+
96
+ const raw = safeGet(DETAILS_KEY);
97
+ applyExpandedAttr(newDoc, new Set(raw ? raw.split('\n').filter(Boolean) : []));
98
+ });
99
+
100
+ // ── Custom element ───────────────────────────────────────────────────────────
101
+
102
+ type ExpandableEl = HTMLElement & { expanded: boolean };
103
+ type FilterInputEl = HTMLElement & { value: string; focus(): void };
104
+ type FilterClearEl = HTMLElement & { hidden: boolean };
105
+
106
+ class SidebarFilter extends HTMLElement {
107
+ private input!: FilterInputEl;
108
+ private clearBtn!: FilterClearEl;
109
+ private status!: HTMLElement;
110
+ private scrollEl: HTMLElement | null = null;
111
+ private items: HTMLElement[] = [];
112
+ private groups: HTMLElement[] = [];
113
+
114
+ /** User-intent expand state during active filter. */
115
+ private openSnapshot: Set<string> | null = null;
116
+ /** Pre-filter snapshot; restored on Esc / clear. */
117
+ private preFilterSnapshot: Set<string> | null = null;
118
+ /** Suppress event-driven persistence while programmatically changing state. */
119
+ private suppressPersist = false;
120
+
121
+ connectedCallback(): void {
122
+ const input = this.querySelector<FilterInputEl>('[data-sidebar-filter-input]');
123
+ const clearBtn = this.querySelector<FilterClearEl>('[data-sidebar-filter-clear]');
124
+ const status = this.querySelector<HTMLElement>('[data-sidebar-filter-status]');
125
+ if (!input || !clearBtn || !status) return;
126
+
127
+ this.input = input;
128
+ this.clearBtn = clearBtn;
129
+ this.status = status;
130
+ this.scrollEl = this.querySelector<HTMLElement>(SCROLL_SELECTOR);
131
+ this.items = [...this.querySelectorAll<HTMLElement>(ITEM_SELECTOR)];
132
+ this.groups = [...this.querySelectorAll<HTMLElement>(GROUP_SELECTOR)];
133
+
134
+ // On a hard load (non-client-nav) DETAILS_KEY may be stale or empty.
135
+ // Prime it now from the SSR DOM attributes so onItemToggle can safely
136
+ // do incremental updates against it.
137
+ if (!_isClientSideNav) {
138
+ safeSet(DETAILS_KEY, collectExpandedKeys(this).join('\n'));
139
+ }
140
+
141
+ this.bindEvents();
142
+ this.restoreOnConnect();
143
+ }
144
+
145
+ // ── Events ───────────────────────────────────────────────────────────────
146
+
147
+ private bindEvents(): void {
148
+ // igc-input fires 'igcInput' instead of the native 'input' event.
149
+ this.input.addEventListener('igcInput', this.onFilterInput as EventListener);
150
+ this.input.addEventListener('keydown', this.onFilterKeydown);
151
+ this.clearBtn.addEventListener('click', this.onClearClick);
152
+
153
+ // igcItemExpanded / igcItemCollapsed are dispatched on <igc-tree> with
154
+ // the igc-tree-item instance in event.detail — NOT in e.target.
155
+ this.addEventListener('igcItemExpanded', this.onItemToggle as EventListener);
156
+ this.addEventListener('igcItemCollapsed', this.onItemToggle as EventListener);
157
+ }
158
+
159
+ private onFilterInput = (): void => {
160
+ safeSet(FILTER_KEY, this.input.value);
161
+ this.applyFilter(this.input.value, 'user');
162
+ };
163
+
164
+ private onFilterKeydown = (e: KeyboardEvent): void => {
165
+ if (e.key === 'Escape' && this.input.value) this.resetFilter();
166
+ };
167
+
168
+ private onClearClick = (): void => {
169
+ this.resetFilter();
170
+ this.input.focus();
171
+ };
172
+
173
+ private onItemToggle = (e: Event): void => {
174
+ if (this.suppressPersist) return;
175
+
176
+ // The events are dispatched on <igc-tree>; the actual item is in e.detail.
177
+ const item = (e as CustomEvent<HTMLElement>).detail;
178
+ const key = item?.dataset?.groupKey;
179
+ if (!key) return;
180
+
181
+ if (this.openSnapshot) {
182
+ // Filter active — update user-intent snapshot, not live state.
183
+ if (e.type === 'igcItemExpanded') this.openSnapshot.add(key);
184
+ else this.openSnapshot.delete(key);
185
+ this.syncSnapshotAttr();
186
+ safeSet(DETAILS_KEY, [...this.openSnapshot].join('\n'));
187
+ return;
188
+ }
189
+
190
+ // Not filtering — update DETAILS_KEY incrementally.
191
+ // We CANNOT call collectExpandedKeys here because Lit reflects the
192
+ // `expanded` property to the attribute asynchronously; the attribute
193
+ // is still the old value at the time this synchronous event fires.
194
+ const saved = safeGet(DETAILS_KEY);
195
+ const keys = new Set(saved ? saved.split('\n').filter(Boolean) : []);
196
+ if (e.type === 'igcItemExpanded') keys.add(key);
197
+ else keys.delete(key);
198
+ safeSet(DETAILS_KEY, [...keys].join('\n'));
199
+ };
200
+
201
+ private syncSnapshotAttr(): void {
202
+ if (this.openSnapshot) this.dataset.openSnapshot = [...this.openSnapshot].join('\n');
203
+ else delete this.dataset.openSnapshot;
204
+ }
205
+
206
+ // ── Restore on connect ───────────────────────────────────────────────────
207
+
208
+ private restoreOnConnect(): void {
209
+ const isClientNav = _isClientSideNav;
210
+ _isClientSideNav = false;
211
+
212
+ const saved = isClientNav ? safeGet(FILTER_KEY) : '';
213
+ if (!isClientNav) safeRemove(FILTER_KEY);
214
+
215
+ if (saved) {
216
+ this.input.value = saved;
217
+ this.applyFilter(saved, 'restore');
218
+ } else {
219
+ this.syncClearButton('');
220
+ }
221
+
222
+ this.restoreScroll(isClientNav);
223
+ }
224
+
225
+ private restoreScroll(isClientNav: boolean): void {
226
+ const sc = this.scrollEl;
227
+ if (!sc) return;
228
+ requestAnimationFrame(() => {
229
+ sc.scrollTop = isClientNav
230
+ ? (parseInt(safeGet(SCROLL_KEY) || '0', 10) || 0)
231
+ : this.activeScrollOffset(sc);
232
+ sc.style.visibility = '';
233
+ });
234
+ }
235
+
236
+ private activeScrollOffset(sc: HTMLElement): number {
237
+ const active = this.querySelector<HTMLAnchorElement>('a[aria-current="page"]');
238
+ if (!active) return 0;
239
+ const linkRect = active.getBoundingClientRect();
240
+ const cRect = sc.getBoundingClientRect();
241
+ if (linkRect.top < cRect.top) return sc.scrollTop + (linkRect.top - cRect.top);
242
+ if (linkRect.bottom > cRect.bottom) return sc.scrollTop + (linkRect.bottom - cRect.bottom);
243
+ return sc.scrollTop;
244
+ }
245
+
246
+ // ── Filter ───────────────────────────────────────────────────────────────
247
+
248
+ private applyFilter(rawQuery: string, source: 'user' | 'restore'): void {
249
+ const trimmed = rawQuery.trim();
250
+ this.syncClearButton(trimmed);
251
+
252
+ if (!trimmed) { this.exitFilterMode(); return; }
253
+
254
+ if (!this.openSnapshot) {
255
+ const current = new Set(collectExpandedKeys(this));
256
+ this.openSnapshot = new Set(current);
257
+ this.preFilterSnapshot = source === 'user' ? new Set(current) : null;
258
+ this.syncSnapshotAttr();
259
+ }
260
+
261
+ const matches = this.computeMatches(tokenize(trimmed));
262
+
263
+ // CSS-driven visibility: stamp data-filter-match; stylesheet hides the rest.
264
+ this.markMatches(matches);
265
+ this.dataset.filtering = 'true';
266
+
267
+ if (source === 'user') {
268
+ // Auto-expand groups that contain matching items.
269
+ for (const el of this.groups) {
270
+ const key = el.dataset.groupKey;
271
+ if (key && matches.has(el)) this.openSnapshot.add(key);
272
+ }
273
+ this.syncSnapshotAttr();
274
+ safeSet(DETAILS_KEY, [...this.openSnapshot].join('\n'));
275
+ }
276
+
277
+ // Drive expand state via the igc-tree-item property (WC API).
278
+ this.setExpandedState(this.openSnapshot);
279
+ this.updateStatus(matches.size > 0 ? null : 'no-match');
280
+ }
281
+
282
+ private exitFilterMode(): void {
283
+ delete this.dataset.filtering;
284
+
285
+ // Clear match marks — CSS restores visibility automatically.
286
+ for (const el of this.items) delete el.dataset.filterMatch;
287
+
288
+ const restore = this.preFilterSnapshot ?? this.openSnapshot;
289
+ if (restore) {
290
+ this.withSuppressed(() => this.setExpandedState(restore!));
291
+ safeSet(DETAILS_KEY, [...restore].join('\n'));
292
+ }
293
+
294
+ this.openSnapshot = null;
295
+ this.preFilterSnapshot = null;
296
+ this.syncSnapshotAttr();
297
+ this.ensureActiveAncestorsExpanded();
298
+ this.updateStatus(null);
299
+ }
300
+
301
+ /**
302
+ * Set `el.expanded` on each group via the igc-tree-item property.
303
+ * Ancestors of the active page are always forced open.
304
+ */
305
+ private setExpandedState(openKeys: Set<string>): void {
306
+ this.withSuppressed(() => {
307
+ for (const el of this.groups) {
308
+ if (el.querySelector('a[aria-current="page"]')) {
309
+ (el as ExpandableEl).expanded = true;
310
+ continue;
311
+ }
312
+ const key = el.dataset.groupKey;
313
+ (el as ExpandableEl).expanded = !!key && openKeys.has(key);
314
+ }
315
+ });
316
+ }
317
+
318
+ /** Run fn with event-driven persistence suppressed. */
319
+ private withSuppressed(fn: () => void): void {
320
+ this.suppressPersist = true;
321
+ try { fn(); } finally {
322
+ // igcItem* events may fire after a render cycle; release the suppress
323
+ // flag after the next task to be safe.
324
+ setTimeout(() => { this.suppressPersist = false; }, 0);
325
+ }
326
+ }
327
+
328
+ // ── Match computation ────────────────────────────────────────────────────
329
+
330
+ private computeMatches(tokens: string[]): Set<HTMLElement> {
331
+ const visible = new Set<HTMLElement>();
332
+ for (const el of this.items) {
333
+ if (!tokens.every((t) => itemPath(el).includes(t))) continue;
334
+ visible.add(el);
335
+ this.collectAncestors(el, visible);
336
+ this.collectGroupDescendants(el, visible);
337
+ }
338
+ return visible;
339
+ }
340
+
341
+ private collectAncestors(el: HTMLElement, out: Set<HTMLElement>): void {
342
+ let node: Element | null = el.parentElement;
343
+ while (node && node !== this) {
344
+ if (node instanceof HTMLElement && node.matches(ITEM_SELECTOR)) out.add(node);
345
+ node = node.parentElement;
346
+ }
347
+ }
348
+
349
+ private collectGroupDescendants(el: HTMLElement, out: Set<HTMLElement>): void {
350
+ if (!el.dataset.groupKey) return;
351
+ el.querySelectorAll<HTMLElement>(ITEM_SELECTOR).forEach((d) => out.add(d));
352
+ }
353
+
354
+ /** Stamp data-filter-match on matching items; CSS hides everything else. */
355
+ private markMatches(matches: Set<HTMLElement>): void {
356
+ for (const el of this.items) {
357
+ if (matches.has(el)) el.dataset.filterMatch = 'true';
358
+ else delete el.dataset.filterMatch;
359
+ }
360
+ }
361
+
362
+ private ensureActiveAncestorsExpanded(): void {
363
+ const active = this.querySelector<HTMLAnchorElement>('a[aria-current="page"]');
364
+ if (!active) return;
365
+ let node: Element | null = active.parentElement;
366
+ while (node && node !== this) {
367
+ if (node instanceof HTMLElement && node.matches(GROUP_SELECTOR))
368
+ (node as ExpandableEl).expanded = true;
369
+ node = node.parentElement;
370
+ }
371
+ }
372
+
373
+ // ── UI sync ──────────────────────────────────────────────────────────────
374
+
375
+ private updateStatus(state: 'no-match' | null): void {
376
+ this.status.textContent = state === 'no-match' ? (this.dataset.noResults ?? 'No topics match') : '';
377
+ }
378
+
379
+ private syncClearButton(value: string): void {
380
+ this.clearBtn.hidden = value.length === 0;
381
+ }
382
+
383
+ private resetFilter(): void {
384
+ this.input.value = '';
385
+ this.syncClearButton('');
386
+ safeRemove(FILTER_KEY);
387
+ this.exitFilterMode();
388
+ }
389
+ }
390
+
391
+ if (!customElements.get('sidebar-filter')) {
392
+ customElements.define('sidebar-filter', SidebarFilter);
393
+ }