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,307 @@
1
+ ---
2
+ /**
3
+ * DocsToc — "On this page" component built on the shared DocsTree.
4
+ *
5
+ * The <docs-toc> wrapper element owns the scroll-spy + click-pin logic
6
+ * and queries `a[href^="#"]` inside the rendered tree. The shared
7
+ * DocsTree owns the markup + visual language (variant="toc").
8
+ *
9
+ * Active state contract: this component never passes `activeId` to
10
+ * DocsTree — runtime scroll-spy mutates `aria-current="true"` instead.
11
+ */
12
+ import type { MarkdownHeading } from 'astro';
13
+ import DocsTree from '../DocsTree/DocsTree.astro';
14
+ import { headingsToTreeNodes, type TocConfig } from './toTreeNodes';
15
+
16
+ interface Props {
17
+ headings?: MarkdownHeading[];
18
+ tocConfig?: TocConfig;
19
+ label?: string;
20
+ offsetTarget?: string;
21
+ }
22
+
23
+ const {
24
+ headings = [],
25
+ tocConfig,
26
+ label = 'On this page',
27
+ offsetTarget,
28
+ } = Astro.props;
29
+
30
+ const nodes = headingsToTreeNodes(headings, tocConfig);
31
+ ---
32
+
33
+ {nodes.length > 0 && (
34
+ <docs-toc data-offset-target={offsetTarget}>
35
+ <nav aria-labelledby="toc-heading">
36
+ <h2 id="toc-heading">{label}</h2>
37
+ <DocsTree nodes={nodes} variant="toc" />
38
+ </nav>
39
+ </docs-toc>
40
+ )}
41
+
42
+ <script>
43
+ type ExpandableEl = HTMLElement & { expanded: boolean };
44
+
45
+ class DocsToc extends HTMLElement {
46
+ private static readonly DEFAULT_THRESHOLD = 160;
47
+
48
+ private pageController: AbortController | undefined;
49
+ private rafId: number | undefined;
50
+ private headings: HTMLElement[] = [];
51
+ private links: HTMLAnchorElement[] = [];
52
+ private parentItems: HTMLElement[] = [];
53
+ private currentLink: HTMLAnchorElement | undefined;
54
+ private offsetEl: Element | null = null;
55
+ private resizeObserver: ResizeObserver | undefined;
56
+ private pinnedIndex: number | undefined;
57
+
58
+ connectedCallback() {
59
+ this.init();
60
+ }
61
+
62
+ disconnectedCallback() {
63
+ this.teardown();
64
+ }
65
+
66
+ private teardown() {
67
+ this.pageController?.abort();
68
+ this.pageController = undefined;
69
+ this.resizeObserver?.disconnect();
70
+ this.resizeObserver = undefined;
71
+ this.offsetEl = null;
72
+ document.documentElement.style.removeProperty('scroll-padding-top');
73
+ if (this.rafId !== undefined) {
74
+ cancelAnimationFrame(this.rafId);
75
+ this.rafId = undefined;
76
+ }
77
+ this.currentLink = undefined;
78
+ this.pinnedIndex = undefined;
79
+ }
80
+
81
+ init() {
82
+ this.teardown();
83
+
84
+ // Wait for igc-tree to be defined before querying slotted links,
85
+ // since the links live inside igc-tree-item light DOM and igc-tree
86
+ // may not have upgraded yet when init() runs.
87
+ customElements.whenDefined('igc-tree').then(() => requestAnimationFrame(() => this._init()));
88
+ }
89
+
90
+ private _init() {
91
+ // Clear any stale aria-current left over from a previous page.
92
+ this.querySelectorAll('a[aria-current]').forEach(
93
+ (a) => a.removeAttribute('aria-current'),
94
+ );
95
+
96
+ this.links = Array.from(this.querySelectorAll<HTMLAnchorElement>('a[href^="#"]'));
97
+ this.headings = this.links
98
+ .map((link) => document.getElementById(decodeURIComponent(link.hash.slice(1))))
99
+ .filter((el): el is HTMLElement => el !== null);
100
+ this.parentItems = Array.from(
101
+ this.querySelectorAll<HTMLElement>('igc-tree-item[data-has-children]'),
102
+ );
103
+
104
+ if (this.headings.length === 0) return;
105
+
106
+ const schedule = () => {
107
+ if (this.rafId !== undefined) return;
108
+ this.rafId = requestAnimationFrame(() => {
109
+ this.rafId = undefined;
110
+ this.update();
111
+ });
112
+ };
113
+
114
+ this.pageController = new AbortController();
115
+ const opts = { passive: true, signal: this.pageController.signal };
116
+
117
+ window.addEventListener('scroll', schedule, opts);
118
+ window.addEventListener('resize', () => { this.syncScrollPadding(); schedule(); }, opts);
119
+
120
+ // Any user-initiated scroll input releases the click pin so the
121
+ // spy can take over again immediately.
122
+ const releasePin = () => { this.pinnedIndex = undefined; };
123
+ window.addEventListener('wheel', releasePin, opts);
124
+ window.addEventListener('touchstart', releasePin, opts);
125
+ window.addEventListener('keydown', releasePin, opts);
126
+
127
+ this.addEventListener('click', (e) => {
128
+ const target = (e.target as HTMLElement | null)?.closest('a[href^="#"]');
129
+ if (!(target instanceof HTMLAnchorElement)) return;
130
+ const idx = this.links.indexOf(target);
131
+ if (idx === -1) return;
132
+ this.pinnedIndex = idx;
133
+ this.setActive(idx);
134
+ }, { signal: this.pageController.signal });
135
+
136
+ this.bindOffsetTarget();
137
+ this.update();
138
+ }
139
+
140
+ private bindOffsetTarget() {
141
+ const selector = this.dataset.offsetTarget ?? null;
142
+ if (!selector) return;
143
+ this.offsetEl = document.querySelector(selector);
144
+ if (!this.offsetEl) return;
145
+
146
+ this.syncScrollPadding();
147
+
148
+ // Re-measure when the header itself resizes (responsive collapse,
149
+ // font-load reflow, etc.).
150
+ this.resizeObserver = new ResizeObserver(() => this.syncScrollPadding());
151
+ this.resizeObserver.observe(this.offsetEl);
152
+ }
153
+
154
+ private syncScrollPadding() {
155
+ if (!this.offsetEl) return;
156
+ const px = Math.round(this.offsetEl.getBoundingClientRect().bottom + 8);
157
+ document.documentElement.style.scrollPaddingTop = `${px}px`;
158
+ }
159
+
160
+ private update() {
161
+ if (this.pinnedIndex !== undefined) {
162
+ this.setActive(this.pinnedIndex);
163
+ return;
164
+ }
165
+
166
+ const threshold = this.computeThreshold();
167
+ let activeIndex = -1;
168
+ for (let i = 0; i < this.headings.length; i++) {
169
+ if (this.headings[i].getBoundingClientRect().top <= threshold) {
170
+ activeIndex = i;
171
+ } else {
172
+ break;
173
+ }
174
+ }
175
+
176
+ // Favor the closer heading: if the next heading is nearer to
177
+ // the threshold than the current one, jump forward.
178
+ if (activeIndex >= 0 && activeIndex + 1 < this.headings.length) {
179
+ const currentDist = threshold - this.headings[activeIndex].getBoundingClientRect().top;
180
+ const nextDist = this.headings[activeIndex + 1].getBoundingClientRect().top - threshold;
181
+ if (nextDist < currentDist) {
182
+ activeIndex = activeIndex + 1;
183
+ }
184
+ }
185
+
186
+ // Above the first heading: no link is active.
187
+ if (activeIndex === -1) {
188
+ if (this.currentLink) {
189
+ this.currentLink.removeAttribute('aria-current');
190
+ this.currentLink = undefined;
191
+ }
192
+ this.syncExpansion(undefined);
193
+ return;
194
+ }
195
+
196
+ this.setActive(activeIndex);
197
+ }
198
+
199
+ private computeThreshold(): number {
200
+ // The bottom edge of the sticky header is the line below which
201
+ // a heading is considered "passed". Add a small fudge so a
202
+ // heading flush against the header still activates.
203
+ if (this.offsetEl) return this.offsetEl.getBoundingClientRect().bottom + 8;
204
+ return DocsToc.DEFAULT_THRESHOLD;
205
+ }
206
+
207
+ private setActive(index: number) {
208
+ const next = this.links[index];
209
+ if (!next || next === this.currentLink) return;
210
+ this.currentLink?.removeAttribute('aria-current');
211
+ next.setAttribute('aria-current', 'true');
212
+ this.currentLink = next;
213
+ this.syncExpansion(next);
214
+ }
215
+
216
+ /**
217
+ * Drive `expanded` on every parent <igc-tree-item> based on the
218
+ * active link's ancestry chain. The active item itself is included
219
+ * (so when an h2 with h3 children is active, its h3s become
220
+ * visible). Manual click-toggles by the user are intentionally
221
+ * overridden on the next scroll update — scrolling is the source
222
+ * of truth for the open section.
223
+ */
224
+ private syncExpansion(activeLink: HTMLAnchorElement | undefined) {
225
+ const activePath = new Set<Element>();
226
+ if (activeLink) {
227
+ let item: Element | null = activeLink.closest('igc-tree-item');
228
+ while (
229
+ item &&
230
+ item.tagName === 'IGC-TREE-ITEM' &&
231
+ this.contains(item)
232
+ ) {
233
+ activePath.add(item);
234
+ item = item.parentElement?.closest('igc-tree-item') ?? null;
235
+ }
236
+ }
237
+ for (const el of this.parentItems) {
238
+ const wantOpen = activePath.has(el);
239
+ if ((el as ExpandableEl).expanded !== wantOpen) {
240
+ (el as ExpandableEl).expanded = wantOpen;
241
+ }
242
+ }
243
+ }
244
+ }
245
+
246
+ if (!customElements.get('docs-toc')) {
247
+ customElements.define('docs-toc', DocsToc);
248
+ }
249
+
250
+ // Astro View Transitions: bundled module scripts run only once, so
251
+ // connectedCallback alone won't re-fire after a page swap if the
252
+ // element is morphed rather than replaced. This top-level listener
253
+ // fires on both initial load and after every navigation.
254
+ document.addEventListener('astro:page-load', () => {
255
+ document.querySelectorAll('docs-toc').forEach((el) => {
256
+ if (el instanceof DocsToc) el.init();
257
+ });
258
+ });
259
+ </script>
260
+
261
+ <style>
262
+ :global(:root) {
263
+ --ig-toc-width: 24rem;
264
+ }
265
+
266
+ /* .toc-sidebar and .main-frame live in DocsLayout — use :global() */
267
+ :global(.toc-sidebar) {
268
+ display: none;
269
+ }
270
+
271
+ /* Hide the aside entirely when DocsToc has nothing to render */
272
+ :global(.toc-sidebar:not(:has(docs-toc))) {
273
+ display: none !important;
274
+ }
275
+
276
+ @media (min-width: 84rem) {
277
+ :global(.toc-sidebar) {
278
+ display: block;
279
+ position: fixed;
280
+ top: var(--sl-nav-height);
281
+ right: 0;
282
+ width: var(--ig-toc-width);
283
+ height: calc(100vh - var(--sl-nav-height) - var(--sl-sidebar-bottom, 0px));
284
+ overflow-y: auto;
285
+ padding: 1.5rem 1rem 1.5rem 1.25rem;
286
+ border-inline-start: 1px solid var(--sl-color-hairline-shade, #d0d0d0);
287
+ }
288
+
289
+ :global(.page:has(docs-toc) .main-frame) {
290
+ padding-inline-end: var(--ig-toc-width);
291
+ }
292
+ }
293
+
294
+ /* docs-toc layout — tree internals live in docs-tree.css */
295
+ docs-toc {
296
+ display: block;
297
+ }
298
+
299
+ docs-toc h2 {
300
+ margin: 0 0 0.75rem;
301
+ font-size: var(--sl-text-xs, 0.75rem);
302
+ font-weight: 600;
303
+ text-transform: uppercase;
304
+ letter-spacing: 0.06em;
305
+ color: var(--sl-color-gray-3, #888);
306
+ }
307
+ </style>
@@ -0,0 +1,68 @@
1
+ # DocsToc
2
+
3
+ "On this page" table of contents with scroll-spy. Builds a nested list from the page's `MarkdownHeading[]` and highlights the current section as the user scrolls.
4
+
5
+ The component renders a `<docs-toc>` custom element that manages all scroll-spy logic on the client. It supports sticky-header offset compensation, click-pin behaviour, and Astro View Transitions.
6
+
7
+ ## Import
8
+
9
+ ```astro
10
+ import DocsToc from 'igniteui-astro-components/components/DocsToc.astro';
11
+ ```
12
+
13
+ ## Props
14
+
15
+ | Prop | Type | Default | Description |
16
+ |------|------|---------|-------------|
17
+ | `headings` | `MarkdownHeading[]` | `[]` | Array of headings from Astro's `getHeadings()` or MDX frontmatter. |
18
+ | `tocConfig` | `false \| { minHeadingLevel?: number; maxHeadingLevel?: number }` | — | Pass `false` to disable the TOC entirely. |
19
+ | `label` | `string` | `'On this page'` | Heading label rendered above the list. |
20
+ | `offsetTarget` | `string` | — | CSS selector for a sticky element (e.g. a nav bar). Its bottom edge is used as the scroll-spy threshold and sets `scroll-padding-top` automatically. |
21
+
22
+ `tocConfig` defaults:
23
+ - `minHeadingLevel`: `2` (i.e. `##`)
24
+ - `maxHeadingLevel`: `3` (i.e. `###`)
25
+
26
+ ## Example
27
+
28
+ ```astro
29
+ ---
30
+ import DocsToc from 'igniteui-astro-components/components/DocsToc.astro';
31
+
32
+ const { headings } = Astro.props;
33
+ ---
34
+ <DocsToc headings={headings} />
35
+
36
+ <!-- Only h2 -->
37
+ <DocsToc headings={headings} tocConfig={{ minHeadingLevel: 2, maxHeadingLevel: 2 }} />
38
+
39
+ <!-- Disabled -->
40
+ <DocsToc headings={headings} tocConfig={false} />
41
+
42
+ <!-- Compensate for a sticky nav bar -->
43
+ <DocsToc headings={headings} offsetTarget=".site-header" />
44
+ ```
45
+
46
+ Place it in the `toc` slot of `DocsLayout`: `<DocsToc slot="toc" headings={headings} />`.
47
+
48
+ ## Scroll-spy behaviour
49
+
50
+ - The active link is determined by comparing each heading's position to a threshold (the bottom of the `offsetTarget` element + 8 px, or 160 px when no offset target is set).
51
+ - At the bottom of the page the last heading is always highlighted, even if its section is too short to ever cross the threshold.
52
+ - **Click-pin**: clicking a TOC link immediately marks it active and suppresses the scroll-spy until the user interacts with the page via wheel, touch, or keyboard.
53
+ - The component uses `requestAnimationFrame` batching so scroll and resize handlers are cheap.
54
+ - `scroll-padding-top` is set dynamically via a `ResizeObserver` on the offset target element so anchor-jump scrolling always lands below the sticky header.
55
+
56
+ ## Astro View Transitions
57
+
58
+ Because bundled `<script>` modules run only once, the component listens for `astro:page-load` at the document level. After every navigation it calls `init()` on each `<docs-toc>` element to re-attach listeners and re-run the initial highlight pass against the new page's headings.
59
+
60
+ ## CSS
61
+
62
+ The component ships global styles. Place the TOC inside an element with the `.toc-sidebar` class to get the built-in fixed sidebar layout (visible above `84rem`).
63
+
64
+ | CSS custom property | Default | Description |
65
+ |---------------------|---------|-------------|
66
+ | `--ig-toc-width` | `24rem` | Width of the fixed TOC sidebar. |
67
+
68
+ `.toc-sidebar:not(:has(docs-toc))` is set to `display: none` so the sidebar collapses automatically when the TOC has no items to render.
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Adapter: Astro `MarkdownHeading[]` → TreeNode[] for the shared DocsTree.
3
+ *
4
+ * Filters headings by `min`/`max` heading level (matching Starlight's TOC
5
+ * config), nests them by depth, and emits TreeNodes whose `href` points
6
+ * at the heading's anchor (`#slug`).
7
+ *
8
+ * Initial state: every node is collapsed. The DocsToc scroll-spy
9
+ * expands the active link's ancestors at runtime.
10
+ */
11
+
12
+ import type { MarkdownHeading } from 'astro';
13
+ import type { TreeNode } from '../DocsTree/types';
14
+
15
+ export type TocConfig =
16
+ | false
17
+ | { minHeadingLevel?: number; maxHeadingLevel?: number }
18
+ | undefined;
19
+
20
+ interface TocItem {
21
+ depth: number;
22
+ slug: string;
23
+ text: string;
24
+ children: TocItem[];
25
+ }
26
+
27
+ const injectChild = (items: TocItem[], item: TocItem): void => {
28
+ const last = items.at(-1);
29
+ if (!last || last.depth >= item.depth) {
30
+ items.push(item);
31
+ return;
32
+ }
33
+ injectChild(last.children, item);
34
+ };
35
+
36
+ const buildToc = (headings: MarkdownHeading[], config: TocConfig): TocItem[] => {
37
+ if (config === false) return [];
38
+ const min = (typeof config === 'object' ? config.minHeadingLevel : undefined) ?? 2;
39
+ const max = (typeof config === 'object' ? config.maxHeadingLevel : undefined) ?? 3;
40
+ const toc: TocItem[] = [];
41
+ for (const h of headings.filter(({ depth }) => depth >= min && depth <= max)) {
42
+ injectChild(toc, { ...h, children: [] });
43
+ }
44
+ return toc;
45
+ };
46
+
47
+ const toNode = (item: TocItem): TreeNode => ({
48
+ id: item.slug,
49
+ label: item.text,
50
+ href: `#${item.slug}`,
51
+ children: item.children.length > 0 ? item.children.map(toNode) : undefined,
52
+ });
53
+
54
+ export const headingsToTreeNodes = (
55
+ headings: MarkdownHeading[],
56
+ config: TocConfig,
57
+ ): TreeNode[] => buildToc(headings, config).map(toNode);
@@ -0,0 +1,66 @@
1
+ ---
2
+ /**
3
+ * DocsTree — the base tree component shared by DocsSidebar and DocsToc.
4
+ *
5
+ * Wraps <igc-tree>, registers the underlying web components, imports the
6
+ * single styling locus, and recursively renders nodes via DocsTreeItem.
7
+ *
8
+ * Variants are selected with `variant="sidebar" | "toc"` and styled by
9
+ * attribute selectors in `docs-tree.css`. Variant-specific label content
10
+ * (badges, carets) is supplied by the optional `renderLabel` prop.
11
+ *
12
+ * Active-state contract:
13
+ * • Sidebar passes `activeId` and never mutates aria-current at runtime.
14
+ * • Toc never passes `activeId`; its <docs-toc> wrapper owns aria-current
15
+ * after hydration.
16
+ * Passing both would flicker — see ./README.md.
17
+ */
18
+ import type { TreeNode, TreeVariant } from './types';
19
+ import DocsTreeItem from './DocsTreeItem.astro';
20
+ import './docs-tree.css';
21
+
22
+ interface Props {
23
+ nodes: TreeNode[];
24
+ variant: TreeVariant;
25
+ /** SSR-time active id; matched against TreeNode.id. */
26
+ activeId?: string;
27
+ /** Pass-through to <igc-tree>. Sidebar: true. Toc: omit. */
28
+ toggleNodeOnClick?: boolean;
29
+ /** Accessible label for the tree's nav role. Skip when an outer <nav aria-labelledby> already names the region. */
30
+ ariaLabel?: string;
31
+ /**
32
+ * Astro component rendering the label slot's inner content. Receives
33
+ * `{ node, depth, isLeaf, isActive }`. When omitted, the tree falls
34
+ * back to a plain `<span>{node.label}</span>`.
35
+ */
36
+ renderLabel?: any;
37
+ }
38
+
39
+ const { nodes, variant, activeId, toggleNodeOnClick, ariaLabel, renderLabel } = Astro.props;
40
+ ---
41
+
42
+ <igc-tree
43
+ data-variant={variant}
44
+ aria-label={ariaLabel}
45
+ toggle-node-on-click={toggleNodeOnClick ? '' : undefined}
46
+ >
47
+ {nodes.map((node) => (
48
+ <DocsTreeItem
49
+ node={node}
50
+ depth={0}
51
+ variant={variant}
52
+ activeId={activeId}
53
+ renderLabel={renderLabel}
54
+ />
55
+ ))}
56
+ </igc-tree>
57
+
58
+ <script>
59
+ import {
60
+ defineComponents,
61
+ IgcTreeComponent,
62
+ IgcTreeItemComponent,
63
+ } from 'igniteui-webcomponents';
64
+
65
+ defineComponents(IgcTreeComponent, IgcTreeItemComponent);
66
+ </script>
@@ -0,0 +1,84 @@
1
+ ---
2
+ /**
3
+ * Recursive renderer for a single TreeNode.
4
+ *
5
+ * Three shapes a node can take:
6
+ * 1. Group — no `href`, has `children` → label is a <div slot="label">
7
+ * 2. Leaf — has `href`, no `children` → label is wrapped in <a slot="label">
8
+ * 3. Link+kids — has `href` AND `children` → toc h2 with h3 children
9
+ *
10
+ * Sets `data-tree-id`, `data-depth`, and `data-has-children` (when applicable).
11
+ * Any extra `data-*` attributes come from `node.itemData` (kebab-cased keys).
12
+ *
13
+ * The native `active` attribute is set when `activeId` matches `node.id`,
14
+ * which drives the `::part(wrapper active)` row highlight in docs-tree.css.
15
+ * The default expand indicator is hidden globally via `::part(indicator)`.
16
+ */
17
+ import type { TreeNode, TreeVariant } from './types';
18
+
19
+ interface Props {
20
+ node: TreeNode;
21
+ depth: number;
22
+ variant: TreeVariant;
23
+ activeId?: string;
24
+ renderLabel?: any;
25
+ }
26
+
27
+ const { node, depth, variant, activeId, renderLabel: RenderLabel } = Astro.props;
28
+
29
+ const hasChildren = !!(node.children && node.children.length > 0);
30
+ const isLeaf = !hasChildren;
31
+ const isActive = activeId !== undefined && node.id === activeId;
32
+
33
+ // Fold itemData entries into a plain object of `data-*` props so Astro spreads
34
+ // them onto <igc-tree-item>. Keys are camelCase → Astro converts to kebab.
35
+ const dataAttrs: Record<string, string> = {};
36
+ if (node.itemData) {
37
+ for (const [key, value] of Object.entries(node.itemData)) {
38
+ dataAttrs[`data-${key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())}`] = value;
39
+ }
40
+ }
41
+ ---
42
+
43
+ <igc-tree-item
44
+ data-tree-id={node.id}
45
+ data-depth={String(depth)}
46
+ data-has-children={hasChildren ? '' : undefined}
47
+ expanded={node.expanded ? '' : undefined}
48
+ active={isActive ? '' : undefined}
49
+ {...dataAttrs}
50
+ >
51
+ {node.href ? (
52
+ <a
53
+ slot="label"
54
+ href={node.href}
55
+ aria-current={isActive ? 'page' : undefined}
56
+ class="docs-tree-link"
57
+ {...node.linkAttrs}
58
+ >
59
+ {RenderLabel ? (
60
+ <RenderLabel node={node} depth={depth} isLeaf={isLeaf} isActive={isActive} />
61
+ ) : (
62
+ <span class="docs-tree-label">{node.label}</span>
63
+ )}
64
+ </a>
65
+ ) : (
66
+ <div slot="label" class="docs-tree-group">
67
+ {RenderLabel ? (
68
+ <RenderLabel node={node} depth={depth} isLeaf={isLeaf} isActive={isActive} />
69
+ ) : (
70
+ <span class="docs-tree-label">{node.label}</span>
71
+ )}
72
+ </div>
73
+ )}
74
+
75
+ {hasChildren && node.children!.map((child) => (
76
+ <Astro.self
77
+ node={child}
78
+ depth={depth + 1}
79
+ variant={variant}
80
+ activeId={activeId}
81
+ renderLabel={RenderLabel}
82
+ />
83
+ ))}
84
+ </igc-tree-item>
@@ -0,0 +1,105 @@
1
+ # DocsTree
2
+
3
+ Base tree component shared by `DocsSidebar` and `DocsToc`. Wraps
4
+ `<igc-tree>` from `igniteui-webcomponents` and recursively renders
5
+ `<igc-tree-item>`s from a generic `TreeNode[]`.
6
+
7
+ The single styling locus is [`docs-tree.css`](./docs-tree.css). Designers
8
+ tune values via the `--docs-tree-*` CSS custom properties; per-variant
9
+ tweaks live at the bottom of the file under `[data-variant="sidebar"]`
10
+ and `[data-variant="toc"]` selectors.
11
+
12
+ ## Usage
13
+
14
+ ```astro
15
+ ---
16
+ import DocsTree from 'igniteui-astro-components/components/DocsTree/DocsTree.astro';
17
+ import type { TreeNode } from 'igniteui-astro-components/components/DocsTree/types';
18
+ import MyLabel from './MyLabel.astro';
19
+
20
+ const nodes: TreeNode[] = [/* your tree */];
21
+ ---
22
+ <DocsTree
23
+ nodes={nodes}
24
+ variant="sidebar"
25
+ activeId="getting-started"
26
+ toggleNodeOnClick
27
+ renderLabel={MyLabel}
28
+ />
29
+ ```
30
+
31
+ ## Props
32
+
33
+ | Prop | Type | Notes |
34
+ | --- | --- | --- |
35
+ | `nodes` | `TreeNode[]` | Required. Root of the tree. |
36
+ | `variant` | `'sidebar' \| 'toc'` | Required. Drives variant CSS rules. |
37
+ | `activeId` | `string` | Optional. SSR-time match against `TreeNode.id`. **See active-state contract below.** |
38
+ | `toggleNodeOnClick` | `boolean` | Optional. Forwarded to `<igc-tree>`. |
39
+ | `renderLabel` | Astro component | Optional. Receives `{ node, depth, isLeaf, isActive }`. Falls back to a plain `<span>{node.label}</span>` when omitted. |
40
+
41
+ ## TreeNode shape
42
+
43
+ ```ts
44
+ interface TreeNode {
45
+ id: string; // → data-tree-id
46
+ label: string; // plain-text fallback
47
+ href?: string; // when set, label is wrapped in <a href>
48
+ expanded?: boolean; // SSR expand hint
49
+ children?: TreeNode[];
50
+ itemData?: Record<string, string>; // → kebab-case data-* attrs on <igc-tree-item>
51
+ linkAttrs?: Record<string, string | number | boolean | undefined>; // spread on leaf <a>
52
+ meta?: Record<string, unknown>; // free-form, read by renderLabel
53
+ }
54
+ ```
55
+
56
+ A node with both `href` and `children` (e.g. a TOC h2 with h3 subheadings)
57
+ renders as a clickable parent — the `<a>` carries the `href`, and child
58
+ nodes render as nested `<igc-tree-item>`s.
59
+
60
+ ## Active-state contract
61
+
62
+ DocsTree supports two non-overlapping ways to mark the active node:
63
+
64
+ 1. **SSR (sidebar style):** pass `activeId`. The matching node's `<a>`
65
+ gets `aria-current="page"` at render. The component never mutates
66
+ `aria-current` after hydration.
67
+
68
+ 2. **Runtime (toc style):** omit `activeId` and let a wrapper custom
69
+ element (e.g. `<docs-toc>`) set `aria-current="true"` on links it
70
+ selects via scroll-spy.
71
+
72
+ **Do not pass `activeId` AND mutate `aria-current` at runtime in the
73
+ same tree.** The two paths use different attribute *values* and would
74
+ flicker.
75
+
76
+ ## Styling
77
+
78
+ All structural and per-variant rules are in
79
+ [`docs-tree.css`](./docs-tree.css). The file is imported once by
80
+ `DocsTree.astro`; Astro hoists and dedupes it.
81
+
82
+ Overridable tokens (set on `igc-tree`):
83
+
84
+ ```
85
+ --docs-tree-row-pad-y
86
+ --docs-tree-row-pad-x
87
+ --docs-tree-row-radius
88
+ --docs-tree-row-color
89
+ --docs-tree-row-hover-bg
90
+ --docs-tree-row-hover-color
91
+ --docs-tree-row-active-bg
92
+ --docs-tree-row-active-color
93
+ --docs-tree-group-color
94
+ --docs-tree-nested-rule
95
+ --docs-tree-nested-pad-x
96
+ ```
97
+
98
+ ## Adapters
99
+
100
+ Consumers convert their domain types to `TreeNode[]` via small adapters:
101
+
102
+ - [`src/components/DocsSidebar/toTreeNodes.ts`](../DocsSidebar/toTreeNodes.ts)
103
+ for `SidebarEntry[]`.
104
+ - [`src/components/DocsToc/toTreeNodes.ts`](../DocsToc/toTreeNodes.ts) for
105
+ `MarkdownHeading[]`.