@cngx/ui 0.1.0-rc.6 → 0.1.0-rc.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -1
- package/a11y/README.md +87 -0
- package/accordion/README.md +4 -2
- package/breadcrumb/README.md +262 -0
- package/collection/README.md +6 -0
- package/command-palette/README.md +224 -0
- package/context-menu/README.md +98 -0
- package/data-grid-accordion/README.md +103 -2
- package/feedback/README.md +42 -0
- package/fesm2022/cngx-ui-a11y.mjs +452 -0
- package/fesm2022/cngx-ui-a11y.mjs.map +1 -0
- package/fesm2022/cngx-ui-accordion.mjs +116 -38
- package/fesm2022/cngx-ui-accordion.mjs.map +1 -1
- package/fesm2022/cngx-ui-action-button.mjs +115 -50
- package/fesm2022/cngx-ui-action-button.mjs.map +1 -1
- package/fesm2022/cngx-ui-breadcrumb.mjs +181 -96
- package/fesm2022/cngx-ui-breadcrumb.mjs.map +1 -1
- package/fesm2022/cngx-ui-chart-panel.mjs +586 -0
- package/fesm2022/cngx-ui-chart-panel.mjs.map +1 -0
- package/fesm2022/cngx-ui-collection.mjs +207 -66
- package/fesm2022/cngx-ui-collection.mjs.map +1 -1
- package/fesm2022/cngx-ui-command-palette.mjs +1281 -0
- package/fesm2022/cngx-ui-command-palette.mjs.map +1 -0
- package/fesm2022/cngx-ui-context-menu.mjs +857 -0
- package/fesm2022/cngx-ui-context-menu.mjs.map +1 -0
- package/fesm2022/cngx-ui-data-grid-accordion.mjs +498 -111
- package/fesm2022/cngx-ui-data-grid-accordion.mjs.map +1 -1
- package/fesm2022/cngx-ui-empty-state.mjs +16 -6
- package/fesm2022/cngx-ui-empty-state.mjs.map +1 -1
- package/fesm2022/cngx-ui-feedback.mjs +1095 -521
- package/fesm2022/cngx-ui-feedback.mjs.map +1 -1
- package/fesm2022/cngx-ui-layout.mjs +18 -9
- package/fesm2022/cngx-ui-layout.mjs.map +1 -1
- package/fesm2022/cngx-ui-mat-accordion.mjs +84 -16
- package/fesm2022/cngx-ui-mat-accordion.mjs.map +1 -1
- package/fesm2022/cngx-ui-mat-paginator.mjs +67 -24
- package/fesm2022/cngx-ui-mat-paginator.mjs.map +1 -1
- package/fesm2022/cngx-ui-mat-stepper.mjs +72 -47
- package/fesm2022/cngx-ui-mat-stepper.mjs.map +1 -1
- package/fesm2022/cngx-ui-mat-tabs.mjs +370 -203
- package/fesm2022/cngx-ui-mat-tabs.mjs.map +1 -1
- package/fesm2022/cngx-ui-overlay.mjs +69 -13
- package/fesm2022/cngx-ui-overlay.mjs.map +1 -1
- package/fesm2022/cngx-ui-paginator.mjs +501 -182
- package/fesm2022/cngx-ui-paginator.mjs.map +1 -1
- package/fesm2022/cngx-ui-sidenav.mjs +327 -106
- package/fesm2022/cngx-ui-sidenav.mjs.map +1 -1
- package/fesm2022/cngx-ui-skeleton.mjs +22 -21
- package/fesm2022/cngx-ui-skeleton.mjs.map +1 -1
- package/fesm2022/cngx-ui-speak.mjs +107 -8
- package/fesm2022/cngx-ui-speak.mjs.map +1 -1
- package/fesm2022/cngx-ui-stat-card.mjs +718 -0
- package/fesm2022/cngx-ui-stat-card.mjs.map +1 -0
- package/fesm2022/cngx-ui-stepper.mjs +217 -76
- package/fesm2022/cngx-ui-stepper.mjs.map +1 -1
- package/fesm2022/cngx-ui-tabs.mjs +40 -19
- package/fesm2022/cngx-ui-tabs.mjs.map +1 -1
- package/fesm2022/cngx-ui-timeline.mjs +548 -0
- package/fesm2022/cngx-ui-timeline.mjs.map +1 -0
- package/fesm2022/cngx-ui-toc.mjs +748 -0
- package/fesm2022/cngx-ui-toc.mjs.map +1 -0
- package/fesm2022/cngx-ui.mjs +16 -21
- package/fesm2022/cngx-ui.mjs.map +1 -1
- package/mat-accordion/README.md +1 -1
- package/mat-paginator/README.md +9 -2
- package/mat-tabs/styles/mat-tabs.css +29 -27
- package/overlay/README.md +9 -4
- package/package.json +32 -1
- package/paginator/README.md +8 -4
- package/sidenav/README.md +33 -11
- package/skeleton/README.md +2 -0
- package/speak/README.md +3 -2
- package/stepper/README.md +15 -3
- package/timeline/README.md +350 -0
- package/toc/README.md +186 -0
- package/types/cngx-ui-a11y.d.ts +394 -0
- package/types/cngx-ui-accordion.d.ts +100 -30
- package/types/cngx-ui-action-button.d.ts +47 -3
- package/types/cngx-ui-breadcrumb.d.ts +159 -58
- package/types/cngx-ui-chart-panel.d.ts +382 -0
- package/types/cngx-ui-collection.d.ts +159 -58
- package/types/cngx-ui-command-palette.d.ts +565 -0
- package/types/cngx-ui-context-menu.d.ts +559 -0
- package/types/cngx-ui-data-grid-accordion.d.ts +344 -51
- package/types/cngx-ui-empty-state.d.ts +9 -2
- package/types/cngx-ui-feedback.d.ts +664 -73
- package/types/cngx-ui-layout.d.ts +8 -3
- package/types/cngx-ui-mat-accordion.d.ts +32 -4
- package/types/cngx-ui-mat-paginator.d.ts +37 -8
- package/types/cngx-ui-mat-stepper.d.ts +23 -11
- package/types/cngx-ui-mat-tabs.d.ts +218 -23
- package/types/cngx-ui-overlay.d.ts +56 -10
- package/types/cngx-ui-paginator.d.ts +416 -132
- package/types/cngx-ui-sidenav.d.ts +248 -74
- package/types/cngx-ui-skeleton.d.ts +18 -7
- package/types/cngx-ui-speak.d.ts +118 -5
- package/types/cngx-ui-stat-card.d.ts +411 -0
- package/types/cngx-ui-stepper.d.ts +81 -29
- package/types/cngx-ui-tabs.d.ts +22 -6
- package/types/cngx-ui-timeline.d.ts +495 -0
- package/types/cngx-ui-toc.d.ts +581 -0
- package/types/cngx-ui.d.ts +8 -5
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
import { DOCUMENT, isPlatformBrowser, NgTemplateOutlet } from '@angular/common';
|
|
2
|
+
import * as i0 from '@angular/core';
|
|
3
|
+
import { InjectionToken, inject, TemplateRef, Directive, PLATFORM_ID, input, booleanAttribute, output, viewChild, contentChild, signal, computed, DestroyRef, effect, ViewEncapsulation, ChangeDetectionStrategy, Component, afterNextRender, makeEnvironmentProviders } from '@angular/core';
|
|
4
|
+
import { arrayEqual } from '@cngx/utils';
|
|
5
|
+
import { injectMediaQuery, CngxScrollSpy } from '@cngx/common/layout';
|
|
6
|
+
import { createFilledOverrideMerge, createOverrideMerge } from '@cngx/core/utils';
|
|
7
|
+
import { createLanguageSection } from '@cngx/core/i18n';
|
|
8
|
+
import { outputToObservable, takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
|
|
9
|
+
import { Router, ActivatedRoute } from '@angular/router';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The English toc section: the single source of the toc's English copy. The
|
|
13
|
+
* `CNGX_TOC_CONFIG` labels default to it.
|
|
14
|
+
*
|
|
15
|
+
* @category ui/toc/i18n
|
|
16
|
+
* @since 0.1.0
|
|
17
|
+
* @relatedTo CNGX_TOC_CONFIG
|
|
18
|
+
*/
|
|
19
|
+
const CNGX_TOC_LANGUAGE_EN = {
|
|
20
|
+
nav: 'On this page',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @internal The toc section of the active pack over the English section,
|
|
25
|
+
* shared app-wide. Consumers override copy through `withTocAriaLabels`.
|
|
26
|
+
*/
|
|
27
|
+
const injectTocSiteCopy = createLanguageSection('toc', CNGX_TOC_LANGUAGE_EN);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Library defaults for the toc configuration cascade. Carries no copy: the
|
|
31
|
+
* `ariaLabels` defaults are the `toc` section of the language pack; single
|
|
32
|
+
* keys ride the `withTocAriaLabels` feature. The spy defaults (`rootMargin` / `threshold`) mirror
|
|
33
|
+
* `CngxScrollSpy`'s own input defaults so an un-configured toc tracks
|
|
34
|
+
* scrolling identically to a hand-wired spy.
|
|
35
|
+
*
|
|
36
|
+
* Exported for intra-lib consumers (`provideTocConfig` deep-merges with this
|
|
37
|
+
* base) but **NOT** re-exported from `public-api.ts` - downstream consumers
|
|
38
|
+
* reach the defaults via `inject(CNGX_TOC_CONFIG)` so `provideTocConfig` /
|
|
39
|
+
* `provideTocConfigAt` overrides take precedence.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
const CNGX_TOC_DEFAULTS = {
|
|
44
|
+
scrollBehavior: 'smooth',
|
|
45
|
+
spy: {
|
|
46
|
+
rootMargin: '0px',
|
|
47
|
+
threshold: 0.3,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* App-wide configuration cascade for the table-of-contents organism. Resolves
|
|
52
|
+
* in priority order:
|
|
53
|
+
*
|
|
54
|
+
* 1. Per-instance Input binding.
|
|
55
|
+
* 2. `provideTocConfigAt(...)` in a parent component's `viewProviders`.
|
|
56
|
+
* 3. `provideTocConfig(...)` at the application root.
|
|
57
|
+
* 4. Library defaults (this token's `factory`).
|
|
58
|
+
*
|
|
59
|
+
* `providedIn: 'root'` with a default factory means consumers never need to
|
|
60
|
+
* provide the token explicitly - `inject(CNGX_TOC_CONFIG)` always resolves.
|
|
61
|
+
* Consumers who want overrides call `provideTocConfig(...)` in
|
|
62
|
+
* `bootstrapApplication` providers (root cascade) or `provideTocConfigAt(...)`
|
|
63
|
+
* in a component's `viewProviders` (sub-tree cascade).
|
|
64
|
+
*
|
|
65
|
+
* @category ui/toc
|
|
66
|
+
* @github https://github.com/cngxjs/cngx/blob/main/projects/ui/toc/config/toc.config.defaults.ts
|
|
67
|
+
* @since 0.1.0
|
|
68
|
+
*/
|
|
69
|
+
const CNGX_TOC_CONFIG = new InjectionToken('CNGX_TOC_CONFIG', {
|
|
70
|
+
providedIn: 'root',
|
|
71
|
+
factory: () => CNGX_TOC_DEFAULTS,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Convenience accessor for the toc configuration cascade. Runs in injection
|
|
76
|
+
* context; resolves through the priority chain (per-instance Input ->
|
|
77
|
+
* `provideTocConfigAt` -> `provideTocConfig` -> library defaults). Equivalent
|
|
78
|
+
* to `inject(CNGX_TOC_CONFIG)` - the helper exists so consumers don't import
|
|
79
|
+
* the token directly. Mirrors `injectBreadcrumbConfig`.
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* export class CngxToc {
|
|
83
|
+
* private readonly cfg = injectTocConfig();
|
|
84
|
+
* protected readonly navLabel = computed(() => this.cfg.ariaLabels?.nav ?? 'On this page');
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @category ui/toc
|
|
89
|
+
* @since 0.1.0
|
|
90
|
+
*/
|
|
91
|
+
function injectTocConfig() {
|
|
92
|
+
return inject(CNGX_TOC_CONFIG);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The resolved accessible names of the toc config in scope, every key the
|
|
96
|
+
* config leaves unset (also a key an override sets to `undefined`) filled from
|
|
97
|
+
* the `toc` section of the active pack, as a Signal that follows a runtime
|
|
98
|
+
* language switch. Runs in injection context; read it inside a `computed()`, a template
|
|
99
|
+
* or a handler.
|
|
100
|
+
*
|
|
101
|
+
* @category ui/toc
|
|
102
|
+
* @since 0.1.0
|
|
103
|
+
*/
|
|
104
|
+
function injectTocAriaLabels() {
|
|
105
|
+
return createFilledOverrideMerge(injectTocSiteCopy(), injectTocConfig().ariaLabels);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Per-item template slot for `CngxToc`. Project an `<ng-template cngxTocItem>`
|
|
110
|
+
* inside `<cngx-toc>` to replace the built-in plain-label rendering with
|
|
111
|
+
* custom markup - the `let-` variables come out typed via
|
|
112
|
+
* `ngTemplateContextGuard`:
|
|
113
|
+
*
|
|
114
|
+
* ```html
|
|
115
|
+
* <cngx-toc [items]="toc" contentRoot="#article">
|
|
116
|
+
* <ng-template cngxTocItem let-item let-active="active" let-depth="depth">
|
|
117
|
+
* <span [class.is-active]="active">{{ item.label }}</span>
|
|
118
|
+
* @if (depth === 0) { <cngx-badge>{{ item.children?.length }}</cngx-badge> }
|
|
119
|
+
* </ng-template>
|
|
120
|
+
* </cngx-toc>
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* Named `CngxTocItemSlot` (not `*Directive`) per the repo's no-artifact-suffix
|
|
124
|
+
* rule; the selector stays `cngxTocItem`, which is what consumers write.
|
|
125
|
+
* Resolves ahead of `CNGX_TOC_CONFIG.templates.item` and the built-in label.
|
|
126
|
+
*
|
|
127
|
+
* @category ui/toc
|
|
128
|
+
* @github https://github.com/cngxjs/cngx/blob/main/projects/ui/toc/toc-item-slot.ts
|
|
129
|
+
* @since 0.1.0
|
|
130
|
+
* @relatedTo CngxToc
|
|
131
|
+
* <example-url>http://localhost:4200/#/ui/toc/slots/custom-item-template</example-url>
|
|
132
|
+
*/
|
|
133
|
+
class CngxTocItemSlot {
|
|
134
|
+
templateRef = inject(TemplateRef);
|
|
135
|
+
static ngTemplateContextGuard(_dir, ctx) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.4", ngImport: i0, type: CngxTocItemSlot, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
139
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.4", type: CngxTocItemSlot, isStandalone: true, selector: "ng-template[cngxTocItem]", exportAs: ["cngxTocItem"], ngImport: i0 });
|
|
140
|
+
}
|
|
141
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.4", ngImport: i0, type: CngxTocItemSlot, decorators: [{
|
|
142
|
+
type: Directive,
|
|
143
|
+
args: [{
|
|
144
|
+
selector: 'ng-template[cngxTocItem]',
|
|
145
|
+
standalone: true,
|
|
146
|
+
exportAs: 'cngxTocItem',
|
|
147
|
+
}]
|
|
148
|
+
}] });
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Contract token for a toc rail. `CngxToc` provides it via
|
|
152
|
+
* `{ provide: CNGX_TOC, useExisting: CngxToc }`, so a collaborator injects the
|
|
153
|
+
* narrow {@link CngxTocContract} instead of the concrete class.
|
|
154
|
+
*
|
|
155
|
+
* @category ui/toc
|
|
156
|
+
* @since 0.1.0
|
|
157
|
+
* @relatedTo CngxToc, CngxTocRouterSync
|
|
158
|
+
*/
|
|
159
|
+
const CNGX_TOC = new InjectionToken('CNGX_TOC');
|
|
160
|
+
|
|
161
|
+
/** Shared empty result so the no-active-section trail is reference-stable. */
|
|
162
|
+
const EMPTY_IDS = [];
|
|
163
|
+
/** Shared empty outline so the pre-scan / no-headings state is reference-stable. */
|
|
164
|
+
const EMPTY_ITEMS = [];
|
|
165
|
+
/**
|
|
166
|
+
* Structural compare of two discovered outlines (id + label + nesting). Guards
|
|
167
|
+
* the `discovered` signal so a re-scan of unchanged headings keeps the same
|
|
168
|
+
* reference and never re-stamps the outline outlet.
|
|
169
|
+
*/
|
|
170
|
+
function itemsEqual(a, b) {
|
|
171
|
+
if (a.length !== b.length) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
for (let i = 0; i < a.length; i++) {
|
|
175
|
+
const x = a[i];
|
|
176
|
+
const y = b[i];
|
|
177
|
+
if (x.id !== y.id || x.label !== y.label) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (!itemsEqual(x.children ?? EMPTY_ITEMS, y.children ?? EMPTY_ITEMS)) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* "On this page" navigation rail. Renders a `<nav>` of anchor links from a
|
|
188
|
+
* {@link CngxTocItem} outline, tracks the most-visible section through an
|
|
189
|
+
* internal {@link CngxScrollSpy}, and communicates the active link via
|
|
190
|
+
* `aria-current` inside the `computed()` graph. Nested items render as
|
|
191
|
+
* indented sub-lists; native link semantics and tab order stay intact (a toc
|
|
192
|
+
* is a navigation landmark of links, not a treeview).
|
|
193
|
+
*
|
|
194
|
+
* Composition over configuration: the spy is bound in this component's own
|
|
195
|
+
* template rather than via `hostDirectives`, because the organism - not the
|
|
196
|
+
* consumer - owns the flat id list the spy observes, and `[cngxScrollSpy]` is
|
|
197
|
+
* a required input. The spy resolves its targets through `getElementById`, so
|
|
198
|
+
* its host placement is irrelevant to its function.
|
|
199
|
+
*
|
|
200
|
+
* @category ui/toc
|
|
201
|
+
* @docsKind primary
|
|
202
|
+
* @wcag AA
|
|
203
|
+
* @github https://github.com/cngxjs/cngx/blob/main/projects/ui/toc/toc.component.ts
|
|
204
|
+
* @since 0.1.0
|
|
205
|
+
* @relatedTo CngxScrollSpy, CngxTocRouterSync, CngxSidenav
|
|
206
|
+
* <example-url>http://localhost:4200/#/ui/toc/basic/on-this-page-rail</example-url>
|
|
207
|
+
* <example-url>http://localhost:4200/#/ui/toc/auto/heading-auto-discovery</example-url>
|
|
208
|
+
* @slot cngxTocItem Replaces the plain label inside each item's link at every depth, ahead of CNGX_TOC_CONFIG.templates.item; gets the item as $implicit, plus active and the zero-based depth.
|
|
209
|
+
*/
|
|
210
|
+
class CngxToc {
|
|
211
|
+
doc = inject(DOCUMENT);
|
|
212
|
+
cfg = injectTocConfig();
|
|
213
|
+
ariaLabels = injectTocAriaLabels();
|
|
214
|
+
reducedMotion = injectMediaQuery('(prefers-reduced-motion: reduce)');
|
|
215
|
+
isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
|
|
216
|
+
/**
|
|
217
|
+
* The outline to render. Tree-shaped; nested `children` become sub-lists.
|
|
218
|
+
* Ignored when `[autoDiscover]` is set - the headings under `contentRoot`
|
|
219
|
+
* become the outline instead.
|
|
220
|
+
*/
|
|
221
|
+
items = input(EMPTY_ITEMS, ...(ngDevMode ? [{ debugName: "items" }] : /* istanbul ignore next */ []));
|
|
222
|
+
/**
|
|
223
|
+
* Derive the outline from the heading elements under `contentRoot` instead
|
|
224
|
+
* of a hand-maintained `[items]` array. Headings nest by their level
|
|
225
|
+
* (`h2 > h3`), and a heading with no `id` gets a slugified one written onto
|
|
226
|
+
* it so the link can target it. Scans once after the first render; call
|
|
227
|
+
* {@link refresh} after you inject or remove sections at runtime.
|
|
228
|
+
*/
|
|
229
|
+
autoDiscover = input(false, { ...(ngDevMode ? { debugName: "autoDiscover" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
|
|
230
|
+
/** Which headings `[autoDiscover]` collects, in CSS-selector form. */
|
|
231
|
+
headingSelector = input('h2, h3', ...(ngDevMode ? [{ debugName: "headingSelector" }] : /* istanbul ignore next */ []));
|
|
232
|
+
/**
|
|
233
|
+
* CSS selector for the scroll container the spy observes. `null` (default)
|
|
234
|
+
* uses the viewport. Forwarded to the spy's `[root]`; also the root the
|
|
235
|
+
* `[autoDiscover]` scan walks for headings.
|
|
236
|
+
*/
|
|
237
|
+
contentRoot = input(null, ...(ngDevMode ? [{ debugName: "contentRoot" }] : /* istanbul ignore next */ []));
|
|
238
|
+
/** Root margin forwarded to the spy. Defaults to the config cascade. */
|
|
239
|
+
rootMargin = input(this.cfg.spy?.rootMargin ?? '0px', ...(ngDevMode ? [{ debugName: "rootMargin" }] : /* istanbul ignore next */ []));
|
|
240
|
+
/** Minimum visibility ratio forwarded to the spy. Defaults to the config cascade. */
|
|
241
|
+
threshold = input(this.cfg.spy?.threshold ?? 0.3, ...(ngDevMode ? [{ debugName: "threshold" }] : /* istanbul ignore next */ []));
|
|
242
|
+
/** Fires when a link is activated (click / Enter). */
|
|
243
|
+
activated = output();
|
|
244
|
+
spy = viewChild(CngxScrollSpy, ...(ngDevMode ? [{ debugName: "spy" }] : /* istanbul ignore next */ []));
|
|
245
|
+
itemSlot = contentChild(CngxTocItemSlot, ...(ngDevMode ? [{ debugName: "itemSlot" }] : /* istanbul ignore next */ []));
|
|
246
|
+
/** Headings discovered by the last `[autoDiscover]` scan. */
|
|
247
|
+
discovered = signal(EMPTY_ITEMS, { ...(ngDevMode ? { debugName: "discovered" } : /* istanbul ignore next */ {}), equal: itemsEqual });
|
|
248
|
+
/** The outline actually rendered: the discovered headings when `[autoDiscover]` is on, else `[items]`. */
|
|
249
|
+
resolvedItems = computed(() => this.autoDiscover() ? this.discovered() : this.items(), ...(ngDevMode ? [{ debugName: "resolvedItems" }] : /* istanbul ignore next */ []));
|
|
250
|
+
/** Accessible name of the `nav` landmark, from the config cascade; follows a language switch. */
|
|
251
|
+
navLabel = computed(() => this.ariaLabels().nav, ...(ngDevMode ? [{ debugName: "navLabel" }] : /* istanbul ignore next */ []));
|
|
252
|
+
/**
|
|
253
|
+
* Depth-first flat id list feeding `[cngxScrollSpy]`. `equal: arrayEqual`
|
|
254
|
+
* keeps the reference stable across same-shape re-sets so a re-render does
|
|
255
|
+
* not re-create the observer.
|
|
256
|
+
*/
|
|
257
|
+
flatIds = computed(() => this.collectIds(this.resolvedItems()), { ...(ngDevMode ? { debugName: "flatIds" } : /* istanbul ignore next */ {}), equal: arrayEqual });
|
|
258
|
+
constructor() {
|
|
259
|
+
// Re-scan whenever the discovery inputs change. The DOM read + discovered
|
|
260
|
+
// write are deferred to a microtask so no signal is written during effect
|
|
261
|
+
// execution (Pillar 1) and the just-rendered content has settled - the same
|
|
262
|
+
// queueMicrotask discipline the panel-lifecycle focus restore uses. The
|
|
263
|
+
// destroyed guard keeps a microtask queued right before teardown from
|
|
264
|
+
// scanning a detached DOM.
|
|
265
|
+
let destroyed = false;
|
|
266
|
+
inject(DestroyRef).onDestroy(() => {
|
|
267
|
+
destroyed = true;
|
|
268
|
+
});
|
|
269
|
+
effect(() => {
|
|
270
|
+
if (!this.autoDiscover()) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
this.contentRoot();
|
|
274
|
+
this.headingSelector();
|
|
275
|
+
queueMicrotask(() => {
|
|
276
|
+
if (!destroyed) {
|
|
277
|
+
this.refresh();
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* The active section, or `null`. `viewChild` is a view query, so `spy()` is
|
|
284
|
+
* `undefined` during the creation pass; `?? null` makes that an explicit
|
|
285
|
+
* value every downstream computed is defined at.
|
|
286
|
+
*/
|
|
287
|
+
activeId = computed(() => this.spy()?.activeId() ?? null, ...(ngDevMode ? [{ debugName: "activeId" }] : /* istanbul ignore next */ []));
|
|
288
|
+
/**
|
|
289
|
+
* Ancestor-id chain of the active leaf - the parent links that get
|
|
290
|
+
* `data-active-trail`. Same `equal` as `flatIds`: an array-valued computed
|
|
291
|
+
* feeding an attribute binding must not allocate a fresh array every spy
|
|
292
|
+
* tick (reference_signal_architecture Equality Rule).
|
|
293
|
+
*/
|
|
294
|
+
activeTrail = computed(() => this.computeTrail(), { ...(ngDevMode ? { debugName: "activeTrail" } : /* istanbul ignore next */ {}), equal: arrayEqual });
|
|
295
|
+
/** Resolved item template: instance slot -> config default -> built-in label. */
|
|
296
|
+
resolvedItemTpl = computed(() => this.itemSlot()?.templateRef ?? this.cfg.templates?.item ?? null, ...(ngDevMode ? [{ debugName: "resolvedItemTpl" }] : /* istanbul ignore next */ []));
|
|
297
|
+
/**
|
|
298
|
+
* Scroll the section with this id into view and move focus to it. Honours
|
|
299
|
+
* `prefers-reduced-motion` - the configured `scrollBehavior` is swapped for
|
|
300
|
+
* `'auto'` (instant) whenever reduced motion is requested. A visual-only
|
|
301
|
+
* jump is a silent state change for screen-reader users, so focus follows
|
|
302
|
+
* the scroll (Pillar 2).
|
|
303
|
+
*/
|
|
304
|
+
scrollTo(id) {
|
|
305
|
+
const target = this.doc.getElementById(id);
|
|
306
|
+
if (target === null) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const behavior = this.reducedMotion() ? 'auto' : (this.cfg.scrollBehavior ?? 'smooth');
|
|
310
|
+
target.scrollIntoView({ behavior });
|
|
311
|
+
this.focusTarget(target);
|
|
312
|
+
}
|
|
313
|
+
/** Click / Enter handler: takes over the native anchor jump, scrolls, and announces the activation. */
|
|
314
|
+
handleActivate(item, event) {
|
|
315
|
+
event?.preventDefault();
|
|
316
|
+
this.scrollTo(item.id);
|
|
317
|
+
this.activated.emit(item);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Move focus to the scrolled-to section. `preventScroll` keeps `focus()`
|
|
321
|
+
* from fighting the smooth `scrollIntoView` above with a second instant
|
|
322
|
+
* jump. Section elements are consumer-owned DOM, so `tabindex="-1"` is set
|
|
323
|
+
* imperatively only when the element is not focusable by default and the
|
|
324
|
+
* consumer set no `tabindex` - an author-set value is never overwritten.
|
|
325
|
+
* That persistent attribute on borrowed DOM is the entry's tracked debt.
|
|
326
|
+
*/
|
|
327
|
+
focusTarget(target) {
|
|
328
|
+
const needsTabindex = !target.hasAttribute('tabindex') && !this.isFocusableByDefault(target);
|
|
329
|
+
if (needsTabindex) {
|
|
330
|
+
target.setAttribute('tabindex', '-1');
|
|
331
|
+
}
|
|
332
|
+
target.focus({ preventScroll: true });
|
|
333
|
+
}
|
|
334
|
+
/** Elements that take focus without an author `tabindex`. */
|
|
335
|
+
isFocusableByDefault(el) {
|
|
336
|
+
return /^(a|button|input|select|textarea|summary|iframe)$/i.test(el.tagName);
|
|
337
|
+
}
|
|
338
|
+
/** Depth-first collect of every id in the outline. */
|
|
339
|
+
collectIds(items, acc = []) {
|
|
340
|
+
for (const item of items) {
|
|
341
|
+
acc.push(item.id);
|
|
342
|
+
if (item.children?.length) {
|
|
343
|
+
this.collectIds(item.children, acc);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return acc;
|
|
347
|
+
}
|
|
348
|
+
/** Ancestor ids of the active leaf (path from root, excluding the leaf itself). */
|
|
349
|
+
computeTrail() {
|
|
350
|
+
const id = this.activeId();
|
|
351
|
+
if (id === null) {
|
|
352
|
+
return EMPTY_IDS;
|
|
353
|
+
}
|
|
354
|
+
const path = this.pathTo(id, this.resolvedItems());
|
|
355
|
+
return path ? path.slice(0, -1) : EMPTY_IDS;
|
|
356
|
+
}
|
|
357
|
+
/** Root-to-target id path, or `null` when the id is not in the outline. */
|
|
358
|
+
pathTo(target, items) {
|
|
359
|
+
for (const item of items) {
|
|
360
|
+
if (item.id === target) {
|
|
361
|
+
return [item.id];
|
|
362
|
+
}
|
|
363
|
+
const childPath = item.children?.length ? this.pathTo(target, item.children) : null;
|
|
364
|
+
if (childPath) {
|
|
365
|
+
return [item.id, ...childPath];
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Re-run the `[autoDiscover]` heading scan. No-op unless `[autoDiscover]` is
|
|
372
|
+
* set and we are on the browser. Call it after injecting or removing sections
|
|
373
|
+
* at runtime - discovery is one-shot by design, not a live observer.
|
|
374
|
+
*/
|
|
375
|
+
refresh() {
|
|
376
|
+
if (!this.autoDiscover() || !this.isBrowser) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
this.discovered.set(this.scanHeadings());
|
|
380
|
+
}
|
|
381
|
+
/** Walk the headings under `contentRoot` into a level-nested outline. */
|
|
382
|
+
scanHeadings() {
|
|
383
|
+
const rootSelector = this.contentRoot();
|
|
384
|
+
let root = this.doc;
|
|
385
|
+
if (rootSelector) {
|
|
386
|
+
const matched = this.doc.querySelector(rootSelector);
|
|
387
|
+
// A set-but-unmatched root scans nothing, not the whole document -
|
|
388
|
+
// falling back to `doc` would sweep site chrome into the outline.
|
|
389
|
+
if (matched === null) {
|
|
390
|
+
return EMPTY_ITEMS;
|
|
391
|
+
}
|
|
392
|
+
root = matched;
|
|
393
|
+
}
|
|
394
|
+
const headings = Array.from(root.querySelectorAll(this.headingSelector()));
|
|
395
|
+
if (headings.length === 0) {
|
|
396
|
+
return EMPTY_ITEMS;
|
|
397
|
+
}
|
|
398
|
+
const roots = [];
|
|
399
|
+
const stack = [];
|
|
400
|
+
const used = new Set();
|
|
401
|
+
for (const el of headings) {
|
|
402
|
+
const level = this.headingLevel(el);
|
|
403
|
+
const node = { id: this.ensureId(el, used), label: this.headingLabel(el), children: [] };
|
|
404
|
+
while (stack.length > 0 && stack[stack.length - 1].level >= level) {
|
|
405
|
+
stack.pop();
|
|
406
|
+
}
|
|
407
|
+
const parent = stack[stack.length - 1];
|
|
408
|
+
if (parent) {
|
|
409
|
+
parent.node.children.push(node);
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
roots.push(node);
|
|
413
|
+
}
|
|
414
|
+
stack.push({ level, node });
|
|
415
|
+
}
|
|
416
|
+
return roots.map((node) => this.freeze(node));
|
|
417
|
+
}
|
|
418
|
+
/** Numeric level of a heading element (`h2` -> 2). Non-heading matches read as level 1. */
|
|
419
|
+
headingLevel(el) {
|
|
420
|
+
const match = /^h([1-6])$/i.exec(el.tagName);
|
|
421
|
+
return match ? Number(match[1]) : 1;
|
|
422
|
+
}
|
|
423
|
+
/** Visible text of a heading, collapsed to a single spaced line. */
|
|
424
|
+
headingLabel(el) {
|
|
425
|
+
return (el.textContent ?? '').replace(/\s+/g, ' ').trim();
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Return the heading's id, generating and writing a slug when it has none.
|
|
429
|
+
* The write mutates consumer-owned DOM (tracked debt); an author-set id is
|
|
430
|
+
* kept verbatim. Collisions against prior scan ids and existing document ids
|
|
431
|
+
* get a `-2`, `-3`, ... suffix.
|
|
432
|
+
*/
|
|
433
|
+
ensureId(el, used) {
|
|
434
|
+
if (el.id) {
|
|
435
|
+
used.add(el.id);
|
|
436
|
+
return el.id;
|
|
437
|
+
}
|
|
438
|
+
const base = this.slugify(this.headingLabel(el)) || 'section';
|
|
439
|
+
let candidate = base;
|
|
440
|
+
let n = 2;
|
|
441
|
+
while (used.has(candidate) || this.doc.getElementById(candidate) !== null) {
|
|
442
|
+
candidate = `${base}-${n++}`;
|
|
443
|
+
}
|
|
444
|
+
used.add(candidate);
|
|
445
|
+
el.setAttribute('id', candidate);
|
|
446
|
+
return candidate;
|
|
447
|
+
}
|
|
448
|
+
/** Lowercase, strip diacritics-free non-alphanumerics to hyphens, collapse and trim. */
|
|
449
|
+
slugify(text) {
|
|
450
|
+
return text
|
|
451
|
+
.toLowerCase()
|
|
452
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
453
|
+
.replace(/^-+|-+$/g, '');
|
|
454
|
+
}
|
|
455
|
+
/** Freeze a mutable node into a CngxTocItem, dropping empty children arrays. */
|
|
456
|
+
freeze(node) {
|
|
457
|
+
if (node.children.length === 0) {
|
|
458
|
+
return { id: node.id, label: node.label };
|
|
459
|
+
}
|
|
460
|
+
return { id: node.id, label: node.label, children: node.children.map((child) => this.freeze(child)) };
|
|
461
|
+
}
|
|
462
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.4", ngImport: i0, type: CngxToc, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
463
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.4", type: CngxToc, isStandalone: true, selector: "cngx-toc", inputs: { items: { classPropertyName: "items", publicName: "items", isSignal: true, isRequired: false, transformFunction: null }, autoDiscover: { classPropertyName: "autoDiscover", publicName: "autoDiscover", isSignal: true, isRequired: false, transformFunction: null }, headingSelector: { classPropertyName: "headingSelector", publicName: "headingSelector", isSignal: true, isRequired: false, transformFunction: null }, contentRoot: { classPropertyName: "contentRoot", publicName: "contentRoot", isSignal: true, isRequired: false, transformFunction: null }, rootMargin: { classPropertyName: "rootMargin", publicName: "rootMargin", isSignal: true, isRequired: false, transformFunction: null }, threshold: { classPropertyName: "threshold", publicName: "threshold", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { activated: "activated" }, providers: [{ provide: CNGX_TOC, useExisting: CngxToc }], queries: [{ propertyName: "itemSlot", first: true, predicate: CngxTocItemSlot, descendants: true, isSignal: true }], viewQueries: [{ propertyName: "spy", first: true, predicate: CngxScrollSpy, descendants: true, isSignal: true }], ngImport: i0, template: "<nav\n class=\"cngx-toc__nav\"\n [attr.aria-label]=\"navLabel()\"\n [cngxScrollSpy]=\"flatIds()\"\n [root]=\"contentRoot()\"\n [rootMargin]=\"rootMargin()\"\n [threshold]=\"threshold()\"\n>\n <ng-container *ngTemplateOutlet=\"list; context: { $implicit: resolvedItems(), depth: 0 }\" />\n</nav>\n\n<ng-template #list let-nodes let-depth=\"depth\">\n <ul class=\"cngx-toc__list\" [style.--cngx-toc-depth]=\"depth\">\n @for (item of nodes; track item.id) {\n <li class=\"cngx-toc__item\" [attr.data-active-trail]=\"activeTrail().includes(item.id) ? '' : null\">\n <a\n class=\"cngx-toc__link\"\n [href]=\"'#' + item.id\"\n [attr.aria-current]=\"activeId() === item.id ? 'location' : null\"\n (click)=\"handleActivate(item, $event)\"\n >\n @if (resolvedItemTpl(); as tpl) {\n <ng-container\n *ngTemplateOutlet=\"\n tpl;\n context: { $implicit: item, active: activeId() === item.id, depth: depth }\n \"\n />\n } @else {\n {{ item.label }}\n }\n </a>\n @if (item.children?.length) {\n <ng-container\n *ngTemplateOutlet=\"list; context: { $implicit: item.children, depth: depth + 1 }\"\n />\n }\n </li>\n }\n </ul>\n</ng-template>\n", styles: ["cngx-toc{--cngx-toc-indent: var(--cngx-space-lg, 1rem);--cngx-toc-gap: var(--cngx-space-xs, .25rem);--cngx-toc-link-pad-block: var(--cngx-space-xs, .25rem);--cngx-toc-link-pad-inline: var(--cngx-space-sm, .5rem);display:block}.cngx-toc__nav{display:block}.cngx-toc__list{display:flex;flex-direction:column;gap:var(--cngx-toc-gap);margin:0;padding:0;list-style:none}.cngx-toc__item .cngx-toc__list{margin-block-start:var(--cngx-toc-gap);padding-inline-start:var(--cngx-toc-indent)}.cngx-toc__link{display:flex;align-items:center;min-block-size:var(--cngx-target-min, 0px);padding-block:var(--cngx-toc-link-pad-block);padding-inline:var(--cngx-toc-link-pad-inline);border-inline-start:var(--cngx-toc-rail-width, 2px) solid transparent;color:var(--cngx-toc-color, var(--cngx-color-text-muted, oklch(.5 .01 250)));line-height:1.4;text-decoration:none}.cngx-toc__link:hover{color:var(--cngx-toc-color-hover, var(--cngx-color-text, oklch(.2 .01 250)))}.cngx-toc__link:focus-visible{outline:var(--cngx-toc-focus-ring-width, 2px) solid var(--cngx-toc-focus-ring, var(--cngx-color-primary, oklch(.66 .19 50)));outline-offset:2px}.cngx-toc__link[aria-current]{border-inline-start-color:var(--cngx-toc-rail-active, var(--cngx-color-primary, oklch(.66 .19 50)));color:var(--cngx-toc-color-active, var(--cngx-color-text, oklch(.2 .01 250)));font-weight:var(--cngx-toc-weight-active, 600)}.cngx-toc__item[data-active-trail]>.cngx-toc__link{color:var(--cngx-toc-color-trail, var(--cngx-color-text, oklch(.2 .01 250)))}@media(forced-colors:active){.cngx-toc__link{border-inline-start-color:Canvas}.cngx-toc__link[aria-current]{border-inline-start-color:Highlight}}\n"], dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: CngxScrollSpy, selector: "[cngxScrollSpy]", inputs: ["cngxScrollSpy", "threshold", "root", "rootMargin"], outputs: ["activeIdChange"], exportAs: ["cngxScrollSpy"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
|
|
464
|
+
}
|
|
465
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.4", ngImport: i0, type: CngxToc, decorators: [{
|
|
466
|
+
type: Component,
|
|
467
|
+
args: [{ selector: 'cngx-toc', standalone: true, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, imports: [NgTemplateOutlet, CngxScrollSpy], providers: [{ provide: CNGX_TOC, useExisting: CngxToc }], template: "<nav\n class=\"cngx-toc__nav\"\n [attr.aria-label]=\"navLabel()\"\n [cngxScrollSpy]=\"flatIds()\"\n [root]=\"contentRoot()\"\n [rootMargin]=\"rootMargin()\"\n [threshold]=\"threshold()\"\n>\n <ng-container *ngTemplateOutlet=\"list; context: { $implicit: resolvedItems(), depth: 0 }\" />\n</nav>\n\n<ng-template #list let-nodes let-depth=\"depth\">\n <ul class=\"cngx-toc__list\" [style.--cngx-toc-depth]=\"depth\">\n @for (item of nodes; track item.id) {\n <li class=\"cngx-toc__item\" [attr.data-active-trail]=\"activeTrail().includes(item.id) ? '' : null\">\n <a\n class=\"cngx-toc__link\"\n [href]=\"'#' + item.id\"\n [attr.aria-current]=\"activeId() === item.id ? 'location' : null\"\n (click)=\"handleActivate(item, $event)\"\n >\n @if (resolvedItemTpl(); as tpl) {\n <ng-container\n *ngTemplateOutlet=\"\n tpl;\n context: { $implicit: item, active: activeId() === item.id, depth: depth }\n \"\n />\n } @else {\n {{ item.label }}\n }\n </a>\n @if (item.children?.length) {\n <ng-container\n *ngTemplateOutlet=\"list; context: { $implicit: item.children, depth: depth + 1 }\"\n />\n }\n </li>\n }\n </ul>\n</ng-template>\n", styles: ["cngx-toc{--cngx-toc-indent: var(--cngx-space-lg, 1rem);--cngx-toc-gap: var(--cngx-space-xs, .25rem);--cngx-toc-link-pad-block: var(--cngx-space-xs, .25rem);--cngx-toc-link-pad-inline: var(--cngx-space-sm, .5rem);display:block}.cngx-toc__nav{display:block}.cngx-toc__list{display:flex;flex-direction:column;gap:var(--cngx-toc-gap);margin:0;padding:0;list-style:none}.cngx-toc__item .cngx-toc__list{margin-block-start:var(--cngx-toc-gap);padding-inline-start:var(--cngx-toc-indent)}.cngx-toc__link{display:flex;align-items:center;min-block-size:var(--cngx-target-min, 0px);padding-block:var(--cngx-toc-link-pad-block);padding-inline:var(--cngx-toc-link-pad-inline);border-inline-start:var(--cngx-toc-rail-width, 2px) solid transparent;color:var(--cngx-toc-color, var(--cngx-color-text-muted, oklch(.5 .01 250)));line-height:1.4;text-decoration:none}.cngx-toc__link:hover{color:var(--cngx-toc-color-hover, var(--cngx-color-text, oklch(.2 .01 250)))}.cngx-toc__link:focus-visible{outline:var(--cngx-toc-focus-ring-width, 2px) solid var(--cngx-toc-focus-ring, var(--cngx-color-primary, oklch(.66 .19 50)));outline-offset:2px}.cngx-toc__link[aria-current]{border-inline-start-color:var(--cngx-toc-rail-active, var(--cngx-color-primary, oklch(.66 .19 50)));color:var(--cngx-toc-color-active, var(--cngx-color-text, oklch(.2 .01 250)));font-weight:var(--cngx-toc-weight-active, 600)}.cngx-toc__item[data-active-trail]>.cngx-toc__link{color:var(--cngx-toc-color-trail, var(--cngx-color-text, oklch(.2 .01 250)))}@media(forced-colors:active){.cngx-toc__link{border-inline-start-color:Canvas}.cngx-toc__link[aria-current]{border-inline-start-color:Highlight}}\n"] }]
|
|
468
|
+
}], ctorParameters: () => [], propDecorators: { items: [{ type: i0.Input, args: [{ isSignal: true, alias: "items", required: false }] }], autoDiscover: [{ type: i0.Input, args: [{ isSignal: true, alias: "autoDiscover", required: false }] }], headingSelector: [{ type: i0.Input, args: [{ isSignal: true, alias: "headingSelector", required: false }] }], contentRoot: [{ type: i0.Input, args: [{ isSignal: true, alias: "contentRoot", required: false }] }], rootMargin: [{ type: i0.Input, args: [{ isSignal: true, alias: "rootMargin", required: false }] }], threshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "threshold", required: false }] }], activated: [{ type: i0.Output, args: ["activated"] }], spy: [{ type: i0.ViewChild, args: [i0.forwardRef(() => CngxScrollSpy), { isSignal: true }] }], itemSlot: [{ type: i0.ContentChild, args: [i0.forwardRef(() => CngxTocItemSlot), { isSignal: true }] }] } });
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Dev-warning emitted when `@angular/router` is absent. A deliberate small
|
|
472
|
+
* local copy of the `@cngx/common/layout` kernel's warning rather than a
|
|
473
|
+
* shared import - sharing would force the helper onto a `public-api.ts`, and
|
|
474
|
+
* `warn*` matches no approved public-API prefix (same call the tabs and
|
|
475
|
+
* sidenav sync directives make).
|
|
476
|
+
*
|
|
477
|
+
* @internal
|
|
478
|
+
*/
|
|
479
|
+
function warnRouterAbsent(directive, enables) {
|
|
480
|
+
console.warn(`${directive}: no Router available - directive is a no-op. ` +
|
|
481
|
+
`Provide @angular/router via provideRouter(...) to enable ${enables}.`);
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Deep-links a `CngxToc` into the URL fragment. Opt-in via
|
|
485
|
+
* `[cngxTocRouterSync]` on the same host as `<cngx-toc>`: activating a link
|
|
486
|
+
* writes `#<section-id>` (replacing the history entry, so a scroll-driven rail
|
|
487
|
+
* never floods the back stack), and a deep link like `/guide#pricing` scrolls
|
|
488
|
+
* to that section once on load.
|
|
489
|
+
*
|
|
490
|
+
* Reaches the rail through the {@link CNGX_TOC} contract token, never the
|
|
491
|
+
* concrete `CngxToc` class. The activation subscription is torn down with the
|
|
492
|
+
* directive via `takeUntilDestroyed` - an `OutputRef` subscription would
|
|
493
|
+
* otherwise outlive route changes. Without `@angular/router` the directive
|
|
494
|
+
* dev-warns once and no-ops.
|
|
495
|
+
*
|
|
496
|
+
* @category ui/toc
|
|
497
|
+
* @docsKind primary
|
|
498
|
+
* @github https://github.com/cngxjs/cngx/blob/main/projects/ui/toc/toc-router-sync.directive.ts
|
|
499
|
+
* @since 0.1.0
|
|
500
|
+
* @relatedTo CngxToc, CNGX_TOC
|
|
501
|
+
* <example-url>http://localhost:4200/#/ui/toc/router/fragment-deep-link</example-url>
|
|
502
|
+
*/
|
|
503
|
+
class CngxTocRouterSync {
|
|
504
|
+
toc = inject(CNGX_TOC, { host: true });
|
|
505
|
+
router = inject(Router, { optional: true });
|
|
506
|
+
route = inject(ActivatedRoute, { optional: true });
|
|
507
|
+
constructor() {
|
|
508
|
+
if (!this.router || !this.route) {
|
|
509
|
+
afterNextRender(() => warnRouterAbsent('CngxTocRouterSync', 'fragment deep-linking'));
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const router = this.router;
|
|
513
|
+
// Write the fragment on every activation. replaceUrl keeps a scroll-driven
|
|
514
|
+
// rail from pushing a history entry per section; 'merge' keeps the query
|
|
515
|
+
// params a bare fragment navigate would otherwise wipe.
|
|
516
|
+
outputToObservable(this.toc.activated)
|
|
517
|
+
.pipe(takeUntilDestroyed())
|
|
518
|
+
.subscribe((item) => {
|
|
519
|
+
router
|
|
520
|
+
.navigate([], { fragment: item.id, replaceUrl: true, queryParamsHandling: 'merge' })
|
|
521
|
+
.catch(() => undefined);
|
|
522
|
+
});
|
|
523
|
+
// One-shot deep-link scroll: read the fragment present at load and bring
|
|
524
|
+
// that section into view after the first render.
|
|
525
|
+
const fragment = toSignal(this.route.fragment, {
|
|
526
|
+
initialValue: this.route.snapshot.fragment,
|
|
527
|
+
});
|
|
528
|
+
afterNextRender(() => {
|
|
529
|
+
const initial = fragment();
|
|
530
|
+
if (initial) {
|
|
531
|
+
this.toc.scrollTo(initial);
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.4", ngImport: i0, type: CngxTocRouterSync, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
536
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.4", type: CngxTocRouterSync, isStandalone: true, selector: "[cngxTocRouterSync]", exportAs: ["cngxTocRouterSync"], ngImport: i0 });
|
|
537
|
+
}
|
|
538
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.4", ngImport: i0, type: CngxTocRouterSync, decorators: [{
|
|
539
|
+
type: Directive,
|
|
540
|
+
args: [{
|
|
541
|
+
selector: '[cngxTocRouterSync]',
|
|
542
|
+
exportAs: 'cngxTocRouterSync',
|
|
543
|
+
standalone: true,
|
|
544
|
+
}]
|
|
545
|
+
}], ctorParameters: () => [] });
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Override the toc's ARIA-string fallback - the accessible name of the `nav`
|
|
549
|
+
* landmark that wraps the link list (default `'On this page'`). Pass a
|
|
550
|
+
* `Signal` to switch the language at runtime.
|
|
551
|
+
*
|
|
552
|
+
* ```ts
|
|
553
|
+
* provideTocConfig(withTocAriaLabels({ nav: 'Auf dieser Seite' }));
|
|
554
|
+
* ```
|
|
555
|
+
*
|
|
556
|
+
* @category ui/toc
|
|
557
|
+
* @since 0.1.0
|
|
558
|
+
*/
|
|
559
|
+
function withTocAriaLabels(payload) {
|
|
560
|
+
return { kind: 'ariaLabels', payload };
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Set the app-wide default scroll behaviour used when a link is activated.
|
|
564
|
+
* `'smooth'` by default; the organism still swaps to `'auto'` at runtime
|
|
565
|
+
* whenever `prefers-reduced-motion: reduce` matches, so this only chooses the
|
|
566
|
+
* motion-allowed behaviour.
|
|
567
|
+
*
|
|
568
|
+
* ```ts
|
|
569
|
+
* provideTocConfig(withTocScrollBehavior('auto'));
|
|
570
|
+
* ```
|
|
571
|
+
*
|
|
572
|
+
* @category ui/toc
|
|
573
|
+
* @since 0.1.0
|
|
574
|
+
*/
|
|
575
|
+
function withTocScrollBehavior(scrollBehavior) {
|
|
576
|
+
return { kind: 'scrollBehavior', payload: { scrollBehavior } };
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Move the app-wide `CngxScrollSpy` defaults - the `rootMargin` and
|
|
580
|
+
* `threshold` the rail forwards to its internal spy. Per-instance
|
|
581
|
+
* `[rootMargin]` / `[threshold]` bindings still win; this only sets the
|
|
582
|
+
* cascade default (e.g. a fixed-header offset every toc should share).
|
|
583
|
+
*
|
|
584
|
+
* ```ts
|
|
585
|
+
* provideTocConfig(withTocSpy({ rootMargin: '-80px 0px 0px 0px' }));
|
|
586
|
+
* ```
|
|
587
|
+
*
|
|
588
|
+
* @category ui/toc
|
|
589
|
+
* @since 0.1.0
|
|
590
|
+
*/
|
|
591
|
+
function withTocSpy(payload) {
|
|
592
|
+
return { kind: 'spy', payload };
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Register a global default item template. Resolves after a per-instance
|
|
596
|
+
* `*cngxTocItem` slot and before the built-in plain-label rendering, so every
|
|
597
|
+
* un-slotted toc renders the same custom item markup.
|
|
598
|
+
*
|
|
599
|
+
* ```ts
|
|
600
|
+
* @Component({
|
|
601
|
+
* viewProviders: [provideTocConfigAt(withTocTemplates({ item: myItemTpl }))],
|
|
602
|
+
* })
|
|
603
|
+
* ```
|
|
604
|
+
*
|
|
605
|
+
* @category ui/toc
|
|
606
|
+
* @since 0.1.0
|
|
607
|
+
*/
|
|
608
|
+
function withTocTemplates(payload) {
|
|
609
|
+
return { kind: 'templates', payload };
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const NO_ARIA_LABELS = {};
|
|
613
|
+
/**
|
|
614
|
+
* Reduces a list of feature objects into a partial config - last write wins
|
|
615
|
+
* per sub-tree. Inner objects are spread-merged so partial overrides compose
|
|
616
|
+
* cleanly with prior writes.
|
|
617
|
+
*
|
|
618
|
+
* @internal
|
|
619
|
+
*/
|
|
620
|
+
function reduceFeatures(features) {
|
|
621
|
+
const out = {};
|
|
622
|
+
for (const f of features) {
|
|
623
|
+
switch (f.kind) {
|
|
624
|
+
case 'ariaLabels':
|
|
625
|
+
out.ariaLabels = createOverrideMerge(out.ariaLabels ?? NO_ARIA_LABELS, f.payload);
|
|
626
|
+
break;
|
|
627
|
+
case 'scrollBehavior':
|
|
628
|
+
out.scrollBehavior = f.payload.scrollBehavior;
|
|
629
|
+
break;
|
|
630
|
+
case 'spy':
|
|
631
|
+
out.spy = { ...out.spy, ...f.payload };
|
|
632
|
+
break;
|
|
633
|
+
case 'templates':
|
|
634
|
+
out.templates = { ...out.templates, ...f.payload };
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return out;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Deep merge: the nested sub-trees (`ariaLabels` / `spy` / `templates`) are
|
|
642
|
+
* spread-merged so a partial fills in missing keys without nuking unrelated
|
|
643
|
+
* sub-trees; the flat scalar `scrollBehavior` takes the partial when present,
|
|
644
|
+
* else the base. Inner objects are flat (one level), so a single spread per
|
|
645
|
+
* key suffices.
|
|
646
|
+
*
|
|
647
|
+
* @internal
|
|
648
|
+
*/
|
|
649
|
+
function mergeConfig(base, partial) {
|
|
650
|
+
return {
|
|
651
|
+
ariaLabels: createOverrideMerge(base.ariaLabels ?? NO_ARIA_LABELS, partial.ariaLabels),
|
|
652
|
+
scrollBehavior: partial.scrollBehavior ?? base.scrollBehavior,
|
|
653
|
+
spy: { ...base.spy, ...partial.spy },
|
|
654
|
+
templates: { ...base.templates, ...partial.templates },
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Application-root configuration cascade for the toc organism. Pass any
|
|
659
|
+
* combination of `withTocAriaLabels`, `withTocScrollBehavior`, `withTocSpy`,
|
|
660
|
+
* and `withTocTemplates` features in `bootstrapApplication`'s providers array.
|
|
661
|
+
*
|
|
662
|
+
* Resolution priority (high -> low):
|
|
663
|
+
* 1. Per-instance Input binding.
|
|
664
|
+
* 2. `provideTocConfigAt(...)` in a parent component's `viewProviders`.
|
|
665
|
+
* 3. `provideTocConfig(...)` at the application root.
|
|
666
|
+
* 4. Library defaults (`CNGX_TOC_DEFAULTS`).
|
|
667
|
+
*
|
|
668
|
+
* The provider deep-merges supplied features with the library defaults so
|
|
669
|
+
* consumers only declare keys they want to override.
|
|
670
|
+
*
|
|
671
|
+
* ```ts
|
|
672
|
+
* bootstrapApplication(AppComponent, {
|
|
673
|
+
* providers: [
|
|
674
|
+
* provideTocConfig(
|
|
675
|
+
* withTocAriaLabels({ nav: 'On this page' }),
|
|
676
|
+
* withTocScrollBehavior('smooth'),
|
|
677
|
+
* ),
|
|
678
|
+
* ],
|
|
679
|
+
* });
|
|
680
|
+
* ```
|
|
681
|
+
*
|
|
682
|
+
* @category ui/toc
|
|
683
|
+
* @since 0.1.0
|
|
684
|
+
*/
|
|
685
|
+
function provideTocConfig(...features) {
|
|
686
|
+
// Empty-features: skip the override so the root factory's CNGX_TOC_DEFAULTS
|
|
687
|
+
// reference flows through untouched. A fresh mergeConfig(...) would allocate
|
|
688
|
+
// identical content under a new reference and bust identity comparisons.
|
|
689
|
+
if (features.length === 0) {
|
|
690
|
+
return makeEnvironmentProviders([]);
|
|
691
|
+
}
|
|
692
|
+
const partial = reduceFeatures(features);
|
|
693
|
+
return makeEnvironmentProviders([
|
|
694
|
+
{
|
|
695
|
+
provide: CNGX_TOC_CONFIG,
|
|
696
|
+
useValue: mergeConfig(CNGX_TOC_DEFAULTS, partial),
|
|
697
|
+
},
|
|
698
|
+
]);
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Component-scoped configuration cascade for the toc organism. Pass any
|
|
702
|
+
* combination of feature factories in a parent component's `viewProviders`
|
|
703
|
+
* array.
|
|
704
|
+
*
|
|
705
|
+
* Unlike `provideTocConfig` (root-only), `provideTocConfigAt` injects the
|
|
706
|
+
* parent injector's `CNGX_TOC_CONFIG` value (resolves through the priority
|
|
707
|
+
* chain) and deep-merges the supplied features on top. Descendant toc
|
|
708
|
+
* instances see the merged config; sibling sub-trees keep the inherited value.
|
|
709
|
+
*
|
|
710
|
+
* ```ts
|
|
711
|
+
* @Component({
|
|
712
|
+
* viewProviders: [provideTocConfigAt(withTocScrollBehavior('auto'))],
|
|
713
|
+
* template: '<cngx-toc [items]="toc" contentRoot="#article" />',
|
|
714
|
+
* })
|
|
715
|
+
* class Guide {}
|
|
716
|
+
* ```
|
|
717
|
+
*
|
|
718
|
+
* @category ui/toc
|
|
719
|
+
* @since 0.1.0
|
|
720
|
+
*/
|
|
721
|
+
function provideTocConfigAt(...features) {
|
|
722
|
+
// Empty-features call: parent value flows through untouched. Skipping the
|
|
723
|
+
// factory preserves reference identity through the sub-tree.
|
|
724
|
+
if (features.length === 0) {
|
|
725
|
+
return [];
|
|
726
|
+
}
|
|
727
|
+
const partial = reduceFeatures(features);
|
|
728
|
+
return [
|
|
729
|
+
{
|
|
730
|
+
provide: CNGX_TOC_CONFIG,
|
|
731
|
+
useFactory: () => {
|
|
732
|
+
const parent = inject(CNGX_TOC_CONFIG, { skipSelf: true });
|
|
733
|
+
return mergeConfig(parent, partial);
|
|
734
|
+
},
|
|
735
|
+
},
|
|
736
|
+
];
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* @module @cngx/ui/toc
|
|
741
|
+
*/
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Generated bundle index. Do not edit.
|
|
745
|
+
*/
|
|
746
|
+
|
|
747
|
+
export { CNGX_TOC, CNGX_TOC_CONFIG, CNGX_TOC_DEFAULTS, CNGX_TOC_LANGUAGE_EN, CngxToc, CngxTocItemSlot, CngxTocRouterSync, injectTocAriaLabels, injectTocConfig, provideTocConfig, provideTocConfigAt, withTocAriaLabels, withTocScrollBehavior, withTocSpy, withTocTemplates };
|
|
748
|
+
//# sourceMappingURL=cngx-ui-toc.mjs.map
|