@m3e/toc 1.0.0-rc.1 → 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/dist/custom-elements.json +2726 -6
- package/dist/html-custom-data.json +7 -2
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +8 -7
- package/dist/index.min.js.map +1 -1
- package/package.json +3 -3
- package/cem.config.mjs +0 -16
- package/demo/index.html +0 -60
- package/dist/src/TocElement.d.ts +0 -87
- package/dist/src/TocElement.d.ts.map +0 -1
- package/dist/src/TocGenerator.d.ts +0 -25
- package/dist/src/TocGenerator.d.ts.map +0 -1
- package/dist/src/TocItemElement.d.ts +0 -47
- package/dist/src/TocItemElement.d.ts.map +0 -1
- package/dist/src/index.d.ts +0 -4
- package/dist/src/index.d.ts.map +0 -1
- package/eslint.config.mjs +0 -13
- package/rollup.config.js +0 -32
- package/src/TocElement.ts +0 -370
- package/src/TocGenerator.ts +0 -74
- package/src/TocItemElement.ts +0 -121
- package/src/index.ts +0 -3
- package/tsconfig.json +0 -9
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { CSSResultGroup, LitElement, PropertyValues } from "lit";
|
|
2
|
-
import { TocNode } from "./TocGenerator";
|
|
3
|
-
declare const M3eTocItemElement_base: import("node_modules/@m3e/core/dist/src/shared/mixins/Constructor").Constructor<import("@m3e/core").SelectedMixin> & import("node_modules/@m3e/core/dist/src/shared/mixins/Constructor").Constructor<import("@m3e/core").DisabledMixin> & import("node_modules/@m3e/core/dist/src/shared/mixins/Constructor").Constructor<import("@m3e/core").AttachInternalsMixin> & import("node_modules/@m3e/core/dist/src/shared/mixins/Constructor").Constructor & typeof LitElement;
|
|
4
|
-
/**
|
|
5
|
-
* An item in a table of contents.
|
|
6
|
-
* @tag m3e-toc-item
|
|
7
|
-
*
|
|
8
|
-
* @slot - Renders the label of the item.
|
|
9
|
-
*
|
|
10
|
-
* @attr disabled - A value indicating whether the element is disabled.
|
|
11
|
-
*
|
|
12
|
-
* @cssprop --m3e-toc-item-shape - Border radius of the TOC item.
|
|
13
|
-
* @cssprop --m3e-toc-item-padding-block - Block padding for the TOC item.
|
|
14
|
-
* @cssprop --m3e-toc-item-padding - Inline padding for the TOC item.
|
|
15
|
-
* @cssprop --m3e-toc-item-inset - Indentation per level for the TOC item.
|
|
16
|
-
* @cssprop --m3e-toc-active-indicator-animation-duration - Animation duration for the active indicator.
|
|
17
|
-
* @cssprop --m3e-toc-item-font-size - Font size for unselected items.
|
|
18
|
-
* @cssprop --m3e-toc-item-font-weight - Font weight for unselected items.
|
|
19
|
-
* @cssprop --m3e-toc-item-line-height - Line height for unselected items.
|
|
20
|
-
* @cssprop --m3e-toc-item-tracking - Letter spacing for unselected items.
|
|
21
|
-
* @cssprop --m3e-toc-item-color - Text color for unselected items.
|
|
22
|
-
* @cssprop --m3e-toc-item-selected-font-size - Font size for selected items.
|
|
23
|
-
* @cssprop --m3e-toc-item-selected-font-weight - Font weight for selected items.
|
|
24
|
-
* @cssprop --m3e-toc-item-selected-line-height - Line height for selected items.
|
|
25
|
-
* @cssprop --m3e-toc-item-selected-tracking - Letter spacing for selected items.
|
|
26
|
-
* @cssprop --m3e-toc-item-selected-color - Text color for selected items.
|
|
27
|
-
*/
|
|
28
|
-
export declare class M3eTocItemElement extends M3eTocItemElement_base {
|
|
29
|
-
/** The styles of the element. */
|
|
30
|
-
static styles: CSSResultGroup;
|
|
31
|
-
/** @private */ private readonly _base?;
|
|
32
|
-
/** @private */ private readonly _stateLayer?;
|
|
33
|
-
/** @internal */ node?: TocNode;
|
|
34
|
-
/** @internal */
|
|
35
|
-
protected update(changedProperties: PropertyValues<this>): void;
|
|
36
|
-
/** @inheritdoc */
|
|
37
|
-
protected firstUpdated(_changedProperties: PropertyValues<this>): void;
|
|
38
|
-
/** @inheritdoc */
|
|
39
|
-
render(): unknown;
|
|
40
|
-
}
|
|
41
|
-
declare global {
|
|
42
|
-
interface HTMLElementTagNameMap {
|
|
43
|
-
"m3e-toc-item": M3eTocItemElement;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
export {};
|
|
47
|
-
//# sourceMappingURL=TocItemElement.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TocItemElement.d.ts","sourceRoot":"","sources":["../../src/TocItemElement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,cAAc,EAAQ,UAAU,EAAE,cAAc,EAAa,MAAM,KAAK,CAAC;AAKvF,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;;AAEzC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBACa,iBAAkB,SAAQ,sBAA6D;IAClG,iCAAiC;IACjC,OAAgB,MAAM,EAAE,cAAc,CA6CpC;IAEF,eAAe,CAAiB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAc;IACrE,eAAe,CAAwB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAuB;IAC3F,gBAAgB,CAAU,IAAI,CAAC,EAAE,OAAO,CAAC;IAEzC,gBAAgB;cACG,MAAM,CAAC,iBAAiB,EAAE,cAAc,CAAC,IAAI,CAAC,GAAG,IAAI;IAiBxE,kBAAkB;cACC,YAAY,CAAC,kBAAkB,EAAE,cAAc,CAAC,IAAI,CAAC,GAAG,IAAI;IAK/E,kBAAkB;IACT,MAAM,IAAI,OAAO;CAI3B;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,cAAc,EAAE,iBAAiB,CAAC;KACnC;CACF"}
|
package/dist/src/index.d.ts
DELETED
package/dist/src/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
|
package/eslint.config.mjs
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import eslint from "@eslint/js";
|
|
2
|
-
import tseslint from "typescript-eslint";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
import { dirname } from "path";
|
|
5
|
-
|
|
6
|
-
export default tseslint.config(eslint.configs.recommended, tseslint.configs.recommended, {
|
|
7
|
-
languageOptions: {
|
|
8
|
-
parserOptions: {
|
|
9
|
-
project: true,
|
|
10
|
-
tsconfigRootDir: dirname(fileURLToPath(import.meta.url)),
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
});
|
package/rollup.config.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import resolve from "@rollup/plugin-node-resolve";
|
|
2
|
-
import terser from "@rollup/plugin-terser";
|
|
3
|
-
import typescript from "@rollup/plugin-typescript";
|
|
4
|
-
|
|
5
|
-
const banner = `/**
|
|
6
|
-
* @license MIT
|
|
7
|
-
* Copyright (c) 2025 matraic
|
|
8
|
-
* See LICENSE file in the project root for full license text.
|
|
9
|
-
*/`;
|
|
10
|
-
|
|
11
|
-
export default [
|
|
12
|
-
{
|
|
13
|
-
input: "src/index.ts",
|
|
14
|
-
output: [
|
|
15
|
-
{
|
|
16
|
-
file: "dist/index.js",
|
|
17
|
-
format: "esm",
|
|
18
|
-
sourcemap: true,
|
|
19
|
-
banner: banner,
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
file: "dist/index.min.js",
|
|
23
|
-
format: "esm",
|
|
24
|
-
sourcemap: true,
|
|
25
|
-
banner: banner,
|
|
26
|
-
plugins: [terser({ mangle: true })],
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
external: ["@m3e/core", "@m3e/core/a11y", "lit"],
|
|
30
|
-
plugins: [resolve(), typescript()],
|
|
31
|
-
},
|
|
32
|
-
];
|
package/src/TocElement.ts
DELETED
|
@@ -1,370 +0,0 @@
|
|
|
1
|
-
import { css, CSSResultGroup, html, LitElement, nothing, PropertyValues, unsafeCSS } from "lit";
|
|
2
|
-
import { customElement, property, query, state } from "lit/decorators.js";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
AttachInternals,
|
|
6
|
-
debounce,
|
|
7
|
-
DesignToken,
|
|
8
|
-
hasAssignedNodes,
|
|
9
|
-
HtmlFor,
|
|
10
|
-
IntersectionController,
|
|
11
|
-
MutationController,
|
|
12
|
-
Role,
|
|
13
|
-
ScrollController,
|
|
14
|
-
} from "@m3e/core";
|
|
15
|
-
|
|
16
|
-
import { SelectionManager } from "@m3e/core/a11y";
|
|
17
|
-
|
|
18
|
-
import { M3eTocItemElement } from "./TocItemElement";
|
|
19
|
-
import { TocGenerator, TocNode } from "./TocGenerator";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @summary
|
|
23
|
-
* A table of contents that provides in-page scroll navigation.
|
|
24
|
-
*
|
|
25
|
-
* @description
|
|
26
|
-
* The `m3e-toc` component generates a hierarchical table of contents for in-page navigation.
|
|
27
|
-
* It automatically detects headings or sections in a target element, builds a navigable list,
|
|
28
|
-
* and highlights the active section as the user scrolls. The component supports custom header
|
|
29
|
-
* slots, depth limiting, smooth scrolling, and extensive theming via CSS custom properties.
|
|
30
|
-
*
|
|
31
|
-
* To exclude a heading from the generated table of contents, add the `m3e-toc-ignore` attribute
|
|
32
|
-
* to that heading element.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```html
|
|
36
|
-
* <m3e-toc for="content" max-depth="3">
|
|
37
|
-
* <span slot="overline">Contents</span>
|
|
38
|
-
* <span slot="title">Documentation</span>
|
|
39
|
-
* </m3e-toc>
|
|
40
|
-
* <div id="content">
|
|
41
|
-
* <h2>Introduction</h2>
|
|
42
|
-
* <h2>Getting Started</h2>
|
|
43
|
-
* <h3>Installation</h3>
|
|
44
|
-
* <h3>Usage</h3>
|
|
45
|
-
* <h2>API Reference</h2>
|
|
46
|
-
* </div>
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @tag m3e-toc
|
|
50
|
-
*
|
|
51
|
-
* @slot - Renders content between the header and items.
|
|
52
|
-
* @slot overline - Renders the overline of the table of contents.
|
|
53
|
-
* @slot title - Renders the title of the table of contents.
|
|
54
|
-
*
|
|
55
|
-
* @attr for - The query selector used to specify the element related to this element.
|
|
56
|
-
* @attr max-depth - The maximum depth of the table of contents.
|
|
57
|
-
*
|
|
58
|
-
* @cssprop --m3e-toc-width - Width of the table of contents.
|
|
59
|
-
* @cssprop --m3e-toc-item-shape - Border radius of TOC items and active indicator.
|
|
60
|
-
* @cssprop --m3e-toc-active-indicator-color - Border color of the active indicator.
|
|
61
|
-
* @cssprop --m3e-toc-active-indicator-animation-duration - Animation duration for the active indicator.
|
|
62
|
-
* @cssprop --m3e-toc-item-padding - Inline padding for TOC items and header.
|
|
63
|
-
* @cssprop --m3e-toc-header-space - Block space below and between header elements.
|
|
64
|
-
* @cssprop --m3e-toc-overline-font-size - Font size for the overline slot.
|
|
65
|
-
* @cssprop --m3e-toc-overline-font-weight - Font weight for the overline slot.
|
|
66
|
-
* @cssprop --m3e-toc-overline-line-height - Line height for the overline slot.
|
|
67
|
-
* @cssprop --m3e-toc-overline-tracking - Letter spacing for the overline slot.
|
|
68
|
-
* @cssprop --m3e-toc-overline-color - Text color for the overline slot.
|
|
69
|
-
* @cssprop --m3e-toc-title-font-size - Font size for the title slot.
|
|
70
|
-
* @cssprop --m3e-toc-title-font-weight - Font weight for the title slot.
|
|
71
|
-
* @cssprop --m3e-toc-title-line-height - Line height for the title slot.
|
|
72
|
-
* @cssprop --m3e-toc-title-tracking - Letter spacing for the title slot.
|
|
73
|
-
* @cssprop --m3e-toc-title-color - Text color for the title slot.
|
|
74
|
-
*/
|
|
75
|
-
@customElement("m3e-toc")
|
|
76
|
-
export class M3eTocElement extends HtmlFor(AttachInternals(Role(LitElement, "navigation"))) {
|
|
77
|
-
/** The styles of the element. */
|
|
78
|
-
static override styles: CSSResultGroup = css`
|
|
79
|
-
:host {
|
|
80
|
-
display: inline-block;
|
|
81
|
-
position: relative;
|
|
82
|
-
overflow-y: auto;
|
|
83
|
-
scrollbar-width: ${DesignToken.scrollbar.thinWidth};
|
|
84
|
-
scrollbar-color: ${DesignToken.scrollbar.color};
|
|
85
|
-
width: var(--m3e-toc-width, 9.75rem);
|
|
86
|
-
}
|
|
87
|
-
ul {
|
|
88
|
-
list-style: none;
|
|
89
|
-
padding-inline-start: unset;
|
|
90
|
-
margin-block-start: unset;
|
|
91
|
-
margin-block-end: unset;
|
|
92
|
-
}
|
|
93
|
-
ul,
|
|
94
|
-
li {
|
|
95
|
-
display: flex;
|
|
96
|
-
flex-direction: column;
|
|
97
|
-
align-items: stretch;
|
|
98
|
-
}
|
|
99
|
-
m3e-toc-item {
|
|
100
|
-
flex: none;
|
|
101
|
-
}
|
|
102
|
-
.active-indicator {
|
|
103
|
-
position: absolute;
|
|
104
|
-
pointer-events: none;
|
|
105
|
-
box-sizing: border-box;
|
|
106
|
-
left: 0;
|
|
107
|
-
right: 0;
|
|
108
|
-
|
|
109
|
-
border-radius: var(--m3e-toc-item-shape, ${DesignToken.shape.corner.largeIncreased});
|
|
110
|
-
border: 1px solid var(--m3e-toc-active-indicator-color, ${DesignToken.color.outline});
|
|
111
|
-
transition: ${unsafeCSS(`visibility var(--m3e-toc-active-indicator-animation-duration, ${DesignToken.motion.duration.long1})
|
|
112
|
-
${DesignToken.motion.easing.standard},
|
|
113
|
-
height var(--m3e-toc-active-indicator-animation-duration, ${DesignToken.motion.duration.long1})
|
|
114
|
-
${DesignToken.motion.easing.standard},
|
|
115
|
-
top var(--m3e-toc-active-indicator-animation-duration, ${DesignToken.motion.duration.long1})
|
|
116
|
-
${DesignToken.motion.easing.standard}`)};
|
|
117
|
-
}
|
|
118
|
-
.header {
|
|
119
|
-
display: flex;
|
|
120
|
-
flex-direction: column;
|
|
121
|
-
align-items: stretch;
|
|
122
|
-
padding-inline-start: var(--m3e-toc-item-padding, 1rem);
|
|
123
|
-
padding-block-end: var(--m3e-toc-header-space, 0.5rem);
|
|
124
|
-
row-gap: var(--m3e-toc-header-space, 0.5rem);
|
|
125
|
-
}
|
|
126
|
-
.overline {
|
|
127
|
-
white-space: nowrap;
|
|
128
|
-
overflow: hidden;
|
|
129
|
-
text-overflow: ellipsis;
|
|
130
|
-
}
|
|
131
|
-
.title {
|
|
132
|
-
display: -webkit-box;
|
|
133
|
-
-webkit-line-clamp: 2;
|
|
134
|
-
-webkit-box-orient: vertical;
|
|
135
|
-
overflow: hidden;
|
|
136
|
-
line-clamp: 2;
|
|
137
|
-
}
|
|
138
|
-
:host(:not(.-with-overline)) .overline,
|
|
139
|
-
:host(:not(.-with-title)) .title,
|
|
140
|
-
:host(:not(.-with-overline):not(.-with-title)) .header {
|
|
141
|
-
display: none;
|
|
142
|
-
}
|
|
143
|
-
::slotted([slot="overline"]) {
|
|
144
|
-
font-size: var(--m3e-toc-overline-font-size, ${DesignToken.typescale.standard.label.small.fontSize});
|
|
145
|
-
font-weight: var(--m3e-toc-overline-font-weight, ${DesignToken.typescale.standard.label.small.fontWeight});
|
|
146
|
-
line-height: var(--m3e-toc-overline-line-height, ${DesignToken.typescale.standard.label.small.lineHeight});
|
|
147
|
-
letter-spacing: var(--m3e-toc-overline-tracking, ${DesignToken.typescale.standard.label.small.tracking});
|
|
148
|
-
color: var(--m3e-toc-overline-color, ${DesignToken.color.onSurfaceVariant});
|
|
149
|
-
}
|
|
150
|
-
::slotted([slot="title"]) {
|
|
151
|
-
font-size: var(--m3e-toc-title-font-size, ${DesignToken.typescale.standard.headline.small.fontSize});
|
|
152
|
-
font-weight: var(--m3e-toc-title-font-weight, ${DesignToken.typescale.standard.headline.small.fontWeight});
|
|
153
|
-
line-height: var(--m3e-toc-title-line-height, ${DesignToken.typescale.standard.headline.small.lineHeight});
|
|
154
|
-
letter-spacing: var(--m3e-toc-title-tracking, ${DesignToken.typescale.standard.headline.small.tracking});
|
|
155
|
-
color: var(--m3e-toc-title-color, ${DesignToken.color.onSurface});
|
|
156
|
-
}
|
|
157
|
-
:host(.-no-animate) .active-indicator {
|
|
158
|
-
transition: none;
|
|
159
|
-
}
|
|
160
|
-
@media (prefers-reduced-motion) {
|
|
161
|
-
.active-indicator {
|
|
162
|
-
transition: none;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
`;
|
|
166
|
-
|
|
167
|
-
/** @private */ @state() private _toc: Array<TocNode> = [];
|
|
168
|
-
/** @private */ @query(".active-indicator") private readonly _activeIndicator!: HTMLElement;
|
|
169
|
-
/** @private */ #ignoreScroll = false;
|
|
170
|
-
|
|
171
|
-
/** @private */ readonly #selectionManager = new SelectionManager<M3eTocItemElement>()
|
|
172
|
-
.withHomeAndEnd()
|
|
173
|
-
.withVerticalOrientation()
|
|
174
|
-
.disableRovingTabIndex()
|
|
175
|
-
.onSelectedItemsChange(() => {
|
|
176
|
-
if (this._activeIndicator) {
|
|
177
|
-
const item = this.#selectionManager.selectedItems[0];
|
|
178
|
-
if (!item) {
|
|
179
|
-
this.classList.toggle("-no-animate", true);
|
|
180
|
-
this._activeIndicator.style.top = `0px`;
|
|
181
|
-
this._activeIndicator.style.height = `0px`;
|
|
182
|
-
this._activeIndicator.style.visibility = "hidden";
|
|
183
|
-
} else {
|
|
184
|
-
this._activeIndicator.style.top = `${item.offsetTop}px`;
|
|
185
|
-
this._activeIndicator.style.height = `${item.clientHeight}px`;
|
|
186
|
-
this._activeIndicator.style.visibility = "";
|
|
187
|
-
|
|
188
|
-
if (this.classList.contains("-no-animate")) {
|
|
189
|
-
setTimeout(() => this.classList.toggle("-no-animate", false), 40);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
/** @private */
|
|
196
|
-
readonly #intersectionController = new IntersectionController(this, {
|
|
197
|
-
target: null,
|
|
198
|
-
callback: (entries) => {
|
|
199
|
-
if (!this.control || this.#ignoreScroll) return;
|
|
200
|
-
|
|
201
|
-
const targetOffset = this.control.scrollTop;
|
|
202
|
-
let closestElement: HTMLElement | null = null;
|
|
203
|
-
let closestDistance = Number.POSITIVE_INFINITY;
|
|
204
|
-
|
|
205
|
-
entries
|
|
206
|
-
.filter((x) => x.isIntersecting)
|
|
207
|
-
.map((x) => <HTMLElement>x.target)
|
|
208
|
-
.forEach((item) => {
|
|
209
|
-
const offsetTop = item.offsetTop;
|
|
210
|
-
const distance = Math.abs(offsetTop - targetOffset);
|
|
211
|
-
if (distance < closestDistance) {
|
|
212
|
-
closestDistance = distance;
|
|
213
|
-
closestElement = item;
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
if (closestElement) {
|
|
218
|
-
const item = this.#selectionManager.items.find((x) => x.node?.element === closestElement);
|
|
219
|
-
if (item) {
|
|
220
|
-
this.#selectionManager.select(item);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
},
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
/** @private */
|
|
227
|
-
readonly #scrollController = new ScrollController(this, {
|
|
228
|
-
target: null,
|
|
229
|
-
callback: () => (this.#ignoreScroll = false),
|
|
230
|
-
debounce: true,
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
/** @private */
|
|
234
|
-
readonly #mutationController = new MutationController(this, {
|
|
235
|
-
target: null,
|
|
236
|
-
config: {
|
|
237
|
-
childList: true,
|
|
238
|
-
subtree: true,
|
|
239
|
-
},
|
|
240
|
-
callback: () => this._updateToc(),
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* The maximum depth of the table of contents.
|
|
245
|
-
* @default 2
|
|
246
|
-
*/
|
|
247
|
-
@property({ attribute: "max-depth", type: Number }) maxDepth = 2;
|
|
248
|
-
|
|
249
|
-
/** @inheritdoc */
|
|
250
|
-
override attach(control: HTMLElement): void {
|
|
251
|
-
super.attach(control);
|
|
252
|
-
this.#mutationController.observe(control);
|
|
253
|
-
this.#scrollController.observe(control);
|
|
254
|
-
this.#generateToc();
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/** @inheritdoc */
|
|
258
|
-
override detach(): void {
|
|
259
|
-
if (this.control) {
|
|
260
|
-
this.#mutationController.unobserve(this.control);
|
|
261
|
-
this.#scrollController.unobserve(this.control);
|
|
262
|
-
}
|
|
263
|
-
super.detach();
|
|
264
|
-
this.#generateToc();
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/** @inheritdoc */
|
|
268
|
-
protected override update(changedProperties: PropertyValues<this>): void {
|
|
269
|
-
super.update(changedProperties);
|
|
270
|
-
|
|
271
|
-
if (changedProperties.has("maxDepth")) {
|
|
272
|
-
this.#generateToc();
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/** @inheritdoc */
|
|
277
|
-
protected override updated(_changedProperties: PropertyValues): void {
|
|
278
|
-
super.updated(_changedProperties);
|
|
279
|
-
|
|
280
|
-
if (_changedProperties.has("_toc")) {
|
|
281
|
-
const { added, removed } = this.#selectionManager.setItems([
|
|
282
|
-
...(this.shadowRoot?.querySelectorAll("m3e-toc-item") ?? []),
|
|
283
|
-
]);
|
|
284
|
-
|
|
285
|
-
if (!this.#selectionManager.activeItem) {
|
|
286
|
-
this.classList.toggle("-no-animate", true);
|
|
287
|
-
this.#selectionManager.updateActiveItem(added.find((x) => !x.disabled));
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
for (const item of added) {
|
|
291
|
-
if (item.node) {
|
|
292
|
-
this.#intersectionController.observe(item.node.element);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
for (const item of removed) {
|
|
297
|
-
if (item.node) {
|
|
298
|
-
this.#intersectionController.unobserve(item.node.element);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/** @inheritdoc */
|
|
305
|
-
protected override render(): unknown {
|
|
306
|
-
return html`<div class="header">
|
|
307
|
-
<div class="overline">
|
|
308
|
-
<slot name="overline" @slotchange="${this.#handleOverlineSlotChange}"></slot>
|
|
309
|
-
</div>
|
|
310
|
-
<div class="title">
|
|
311
|
-
<slot name="title" @slotchange="${this.#handleTitleSlotChange}"></slot>
|
|
312
|
-
</div>
|
|
313
|
-
</div>
|
|
314
|
-
<slot></slot>
|
|
315
|
-
<ul class="list">
|
|
316
|
-
${this._toc.map((x) => this.#renderNode(x))}
|
|
317
|
-
</ul>
|
|
318
|
-
<div class="active-indicator" aria-hidden="true"></div>`;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/** @private */
|
|
322
|
-
#renderNode(node: TocNode): unknown {
|
|
323
|
-
return html`<li>
|
|
324
|
-
<m3e-toc-item tabindex="-1" .node="${node}" @click="${this.#handleClick}">${node.label}</m3e-toc-item>
|
|
325
|
-
${node.nodes.length == 0
|
|
326
|
-
? nothing
|
|
327
|
-
: html`<ul>
|
|
328
|
-
${node.nodes.map((x) => this.#renderNode(x))}
|
|
329
|
-
</ul>`}
|
|
330
|
-
</li>`;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/** @private */
|
|
334
|
-
#handleOverlineSlotChange(e: Event): void {
|
|
335
|
-
this.classList.toggle("-with-overline", hasAssignedNodes(<HTMLSlotElement>e.target));
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/** @private */
|
|
339
|
-
#handleTitleSlotChange(e: Event): void {
|
|
340
|
-
this.classList.toggle("-with-title", hasAssignedNodes(<HTMLSlotElement>e.target));
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/** @private */
|
|
344
|
-
#handleClick(e: Event): void {
|
|
345
|
-
if (e.target instanceof M3eTocItemElement && !e.target.disabled && e.target.node?.element) {
|
|
346
|
-
this.#ignoreScroll = true;
|
|
347
|
-
e.target.node.element.scrollIntoView({ block: "start", inline: "start", behavior: "smooth" });
|
|
348
|
-
this.#selectionManager.updateActiveItem(e.target);
|
|
349
|
-
this.#selectionManager.select(e.target);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/** @private */
|
|
354
|
-
#generateToc(): void {
|
|
355
|
-
this._toc = this.control ? TocGenerator.generate(this.control, Math.max(1, Math.min(this.maxDepth, 6))) : [];
|
|
356
|
-
this.requestUpdate();
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/** @private */
|
|
360
|
-
@debounce(40)
|
|
361
|
-
private _updateToc(): void {
|
|
362
|
-
this.#generateToc();
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
declare global {
|
|
367
|
-
interface HTMLElementTagNameMap {
|
|
368
|
-
"m3e-toc": M3eTocElement;
|
|
369
|
-
}
|
|
370
|
-
}
|
package/src/TocGenerator.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { getTextContent, guid } from "@m3e/core";
|
|
2
|
-
|
|
3
|
-
/** A node in a table of contents. */
|
|
4
|
-
export interface TocNode {
|
|
5
|
-
/** An opaque identifier that uniquely identifies the node. */
|
|
6
|
-
id: string;
|
|
7
|
-
|
|
8
|
-
/** The level of the node. */
|
|
9
|
-
level: number;
|
|
10
|
-
|
|
11
|
-
/** The text to display for the node. */
|
|
12
|
-
label: string;
|
|
13
|
-
|
|
14
|
-
/** The element of the node. */
|
|
15
|
-
element: HTMLElement;
|
|
16
|
-
|
|
17
|
-
/** The child nodes. */
|
|
18
|
-
nodes: TocNode[];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Provides functionality used to generate a table of contents used for in-page navigation. */
|
|
22
|
-
export class TocGenerator {
|
|
23
|
-
/**
|
|
24
|
-
* Generates nodes from which to construct a table of contents for in-page navigation.
|
|
25
|
-
* @param {HTMLElement} element The element for which to generate a table of contents.
|
|
26
|
-
* @param {number} [maxDepth=6] The maximum depth of the table of contents.
|
|
27
|
-
* @returns {Array<TocNode>} The top-level nodes of the table of contents.
|
|
28
|
-
*/
|
|
29
|
-
static generate(element: HTMLElement, maxDepth: number = 6): Array<TocNode> {
|
|
30
|
-
const maxLevel = 6;
|
|
31
|
-
let topLevel = maxLevel;
|
|
32
|
-
const nodes = new Array<TocNode>();
|
|
33
|
-
element
|
|
34
|
-
.querySelectorAll<HTMLElement>(
|
|
35
|
-
"h1:not([m3e-toc-ignore]),h2:not([m3e-toc-ignore]),h3:not([m3e-toc-ignore]),h4:not([m3e-toc-ignore]),h5:not([m3e-toc-ignore]),h6:not([m3e-toc-ignore]),m3e-heading[level]:not([m3e-toc-ignore])"
|
|
36
|
-
)
|
|
37
|
-
.forEach((element) => {
|
|
38
|
-
const level = TocGenerator.#getHeaderLevel(element);
|
|
39
|
-
topLevel = Math.min(level, topLevel);
|
|
40
|
-
nodes.push({
|
|
41
|
-
id: element.id || guid(),
|
|
42
|
-
element,
|
|
43
|
-
level,
|
|
44
|
-
label: getTextContent(element, true),
|
|
45
|
-
nodes: new Array<TocNode>(),
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
for (let level = topLevel + maxDepth - 1; level > topLevel; level--) {
|
|
50
|
-
for (let i = 0; i < nodes.length; i++) {
|
|
51
|
-
const node = nodes[i];
|
|
52
|
-
if (node.level === level) {
|
|
53
|
-
for (let j = i; j >= 0; j--) {
|
|
54
|
-
const prev = nodes[j];
|
|
55
|
-
if (prev.level < level) {
|
|
56
|
-
prev.nodes.push(node);
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
nodes.forEach((x) => (x.level -= topLevel - 1));
|
|
65
|
-
return nodes.filter((x) => x.level === 1);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** @internal */
|
|
69
|
-
static #getHeaderLevel(element: HTMLElement): number {
|
|
70
|
-
return element.tagName.startsWith("H")
|
|
71
|
-
? parseInt(element.tagName.substring(1))
|
|
72
|
-
: parseInt(element.getAttribute("level") ?? "0");
|
|
73
|
-
}
|
|
74
|
-
}
|
package/src/TocItemElement.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { css, CSSResultGroup, html, LitElement, PropertyValues, unsafeCSS } from "lit";
|
|
2
|
-
import { customElement, query, state } from "lit/decorators.js";
|
|
3
|
-
|
|
4
|
-
import { AttachInternals, DesignToken, Disabled, M3eStateLayerElement, Role, Selected } from "@m3e/core";
|
|
5
|
-
|
|
6
|
-
import { TocNode } from "./TocGenerator";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* An item in a table of contents.
|
|
10
|
-
* @tag m3e-toc-item
|
|
11
|
-
*
|
|
12
|
-
* @slot - Renders the label of the item.
|
|
13
|
-
*
|
|
14
|
-
* @attr disabled - A value indicating whether the element is disabled.
|
|
15
|
-
*
|
|
16
|
-
* @cssprop --m3e-toc-item-shape - Border radius of the TOC item.
|
|
17
|
-
* @cssprop --m3e-toc-item-padding-block - Block padding for the TOC item.
|
|
18
|
-
* @cssprop --m3e-toc-item-padding - Inline padding for the TOC item.
|
|
19
|
-
* @cssprop --m3e-toc-item-inset - Indentation per level for the TOC item.
|
|
20
|
-
* @cssprop --m3e-toc-active-indicator-animation-duration - Animation duration for the active indicator.
|
|
21
|
-
* @cssprop --m3e-toc-item-font-size - Font size for unselected items.
|
|
22
|
-
* @cssprop --m3e-toc-item-font-weight - Font weight for unselected items.
|
|
23
|
-
* @cssprop --m3e-toc-item-line-height - Line height for unselected items.
|
|
24
|
-
* @cssprop --m3e-toc-item-tracking - Letter spacing for unselected items.
|
|
25
|
-
* @cssprop --m3e-toc-item-color - Text color for unselected items.
|
|
26
|
-
* @cssprop --m3e-toc-item-selected-font-size - Font size for selected items.
|
|
27
|
-
* @cssprop --m3e-toc-item-selected-font-weight - Font weight for selected items.
|
|
28
|
-
* @cssprop --m3e-toc-item-selected-line-height - Line height for selected items.
|
|
29
|
-
* @cssprop --m3e-toc-item-selected-tracking - Letter spacing for selected items.
|
|
30
|
-
* @cssprop --m3e-toc-item-selected-color - Text color for selected items.
|
|
31
|
-
*/
|
|
32
|
-
@customElement("m3e-toc-item")
|
|
33
|
-
export class M3eTocItemElement extends Selected(Disabled(AttachInternals(Role(LitElement, "link")))) {
|
|
34
|
-
/** The styles of the element. */
|
|
35
|
-
static override styles: CSSResultGroup = css`
|
|
36
|
-
:host {
|
|
37
|
-
display: inline-block;
|
|
38
|
-
position: relative;
|
|
39
|
-
user-select: none;
|
|
40
|
-
border-radius: var(--m3e-toc-item-shape, ${DesignToken.shape.corner.largeIncreased});
|
|
41
|
-
padding-block: var(--m3e-toc-item-padding-block, 0.5rem);
|
|
42
|
-
}
|
|
43
|
-
:host(:not(:disabled)) {
|
|
44
|
-
cursor: pointer;
|
|
45
|
-
}
|
|
46
|
-
.base {
|
|
47
|
-
padding-inline-start: calc(
|
|
48
|
-
var(--m3e-toc-item-padding, 1rem) + calc(var(--m3e-toc-item-inset, 0.75rem) * var(--_level, 0))
|
|
49
|
-
);
|
|
50
|
-
padding-inline-end: var(--m3e-toc-item-padding, 1rem);
|
|
51
|
-
transition: ${unsafeCSS(
|
|
52
|
-
`color var(--m3e-toc-active-indicator-animation-duration, ${DesignToken.motion.duration.long1}) ${DesignToken.motion.easing.standard}`
|
|
53
|
-
)};
|
|
54
|
-
}
|
|
55
|
-
:host(:not([selected])) {
|
|
56
|
-
font-size: var(--m3e-toc-item-font-size, ${DesignToken.typescale.standard.body.large.fontSize});
|
|
57
|
-
font-weight: var(--m3e-toc-item-font-weight, ${DesignToken.typescale.standard.body.large.fontWeight});
|
|
58
|
-
line-height: var(--m3e-toc-item-line-height, ${DesignToken.typescale.standard.body.large.lineHeight});
|
|
59
|
-
letter-spacing: var(--m3e-toc-item-tracking, ${DesignToken.typescale.standard.body.large.tracking});
|
|
60
|
-
color: var(--m3e-toc-item-color, ${DesignToken.color.onSurfaceVariant});
|
|
61
|
-
}
|
|
62
|
-
:host([selected]) {
|
|
63
|
-
font-size: var(--m3e-toc-item-selected-font-size, ${DesignToken.typescale.emphasized.body.large.fontSize});
|
|
64
|
-
font-weight: var(--m3e-toc-item-selected-font-weight, ${DesignToken.typescale.emphasized.body.large.fontWeight});
|
|
65
|
-
line-height: var(--m3e-toc-item-selected-line-height, ${DesignToken.typescale.emphasized.body.large.lineHeight});
|
|
66
|
-
letter-spacing: var(--m3e-toc-item-selected-tracking, ${DesignToken.typescale.emphasized.body.large.tracking});
|
|
67
|
-
color: var(--m3e-toc-item-selected-color, ${DesignToken.color.onSecondaryContainer});
|
|
68
|
-
}
|
|
69
|
-
.base {
|
|
70
|
-
justify-content: unset;
|
|
71
|
-
}
|
|
72
|
-
.state-layer {
|
|
73
|
-
--m3e-state-layer-focus-opacity: 0;
|
|
74
|
-
}
|
|
75
|
-
@media (prefers-reduced-motion) {
|
|
76
|
-
.base {
|
|
77
|
-
transition: none;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
`;
|
|
81
|
-
|
|
82
|
-
/** @private */ @query(".base") private readonly _base?: HTMLElement;
|
|
83
|
-
/** @private */ @query(".state-layer") private readonly _stateLayer?: M3eStateLayerElement;
|
|
84
|
-
/** @internal */ @state() node?: TocNode;
|
|
85
|
-
|
|
86
|
-
/** @internal */
|
|
87
|
-
protected override update(changedProperties: PropertyValues<this>): void {
|
|
88
|
-
super.update(changedProperties);
|
|
89
|
-
|
|
90
|
-
if (changedProperties.has("selected")) {
|
|
91
|
-
this.ariaSelected = null;
|
|
92
|
-
this.ariaCurrent = this.selected ? "true" : null;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (changedProperties.has("node")) {
|
|
96
|
-
if (this.node) {
|
|
97
|
-
this._base?.style.setProperty("--_level", `${this.node.level - 1}`);
|
|
98
|
-
} else {
|
|
99
|
-
this._base?.style.removeProperty("--_level");
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** @inheritdoc */
|
|
105
|
-
protected override firstUpdated(_changedProperties: PropertyValues<this>): void {
|
|
106
|
-
super.firstUpdated(_changedProperties);
|
|
107
|
-
this._stateLayer?.attach(this);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** @inheritdoc */
|
|
111
|
-
override render(): unknown {
|
|
112
|
-
return html`<m3e-state-layer class="state-layer"></m3e-state-layer>
|
|
113
|
-
<div class="base"><slot></slot></div>`;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
declare global {
|
|
118
|
-
interface HTMLElementTagNameMap {
|
|
119
|
-
"m3e-toc-item": M3eTocItemElement;
|
|
120
|
-
}
|
|
121
|
-
}
|
package/src/index.ts
DELETED