@m3e/toc 1.0.0-rc.1 → 1.0.0-rc.3

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/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@m3e/toc",
3
- "version": "1.0.0-rc.1",
3
+ "version": "1.0.0-rc.3",
4
4
  "description": "Table of Contents for M3E",
5
5
  "author": "matraic <matraic@yahoo.com>",
6
6
  "license": "MIT",
7
- "homepage": "https://matraic.github.io/m3e/",
7
+ "homepage": "https://matraic.github.io/m3e/#/components/toc.html",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/matraic/m3e.git"
@@ -31,7 +31,7 @@
31
31
  "@material/material-color-utilities": "^0.3.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@m3e/core": "1.0.0-rc.1",
34
+ "@m3e/core": "1.0.0-rc.3",
35
35
  "lit": "^3.3.0"
36
36
  },
37
37
  "devDependencies": {
package/cem.config.mjs DELETED
@@ -1,16 +0,0 @@
1
- import { customElementVsCodePlugin } from "custom-element-vs-code-integration";
2
-
3
- export default {
4
- globs: ["src/**/*.ts"],
5
- exclude: ["src/**/*.spec.ts"],
6
- packagejson: true,
7
- outdir: "dist",
8
- litelement: true,
9
- plugins: [
10
- customElementVsCodePlugin({
11
- outdir: "dist",
12
- htmlFileName: "html-custom-data.json",
13
- cssFileName: "css-custom-data.json",
14
- }),
15
- ],
16
- };
package/demo/index.html DELETED
@@ -1,60 +0,0 @@
1
- <!doctype html>
2
- <html lang="en" style="overflow-y: auto">
3
- <head>
4
- <title>Table of Contents for M3E</title>
5
- <meta charset="utf-8" />
6
- <meta name="viewport" content="width=device-width, initial-scale=1" />
7
- <meta name="description" content="Table of Contents for M3E" />
8
- <base href="./" />
9
- <link rel="preconnect" href="https://fonts.googleapis.com" />
10
- <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
11
- <link
12
- href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100..900;1,100..900&display=swap"
13
- rel="stylesheet"
14
- />
15
- <link
16
- href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0..1,0"
17
- rel="stylesheet"
18
- />
19
- <script type="importmap">
20
- {
21
- "imports": {
22
- "lit": "https://cdn.jsdelivr.net/npm/lit@3.3.0/+esm",
23
- "@m3e/core": "../../core/dist/index.min.js",
24
- "@m3e/core/a11y": "../../core/dist/a11y.min.js"
25
- }
26
- }
27
- </script>
28
- <script type="module" src="../../theme/dist/index.min.js"></script>
29
- <script type="module" src="../dist/index.min.js"></script>
30
- <style>
31
- body {
32
- font-family: "Roboto";
33
- }
34
- *:not(:defined) {
35
- display: none;
36
- }
37
- </style>
38
- </head>
39
- <body>
40
- <m3e-theme strong-focus>
41
- <div
42
- id="d1"
43
- style="display: flex; flex-direction: column; overflow-y: auto; position: relative; max-height: 250px"
44
- >
45
- <m3e-toc for="d1" style="position: sticky; top: 0; margin-left: auto">
46
- <span slot="overline">A long overline that should not wrap</span>
47
- <span slot="title">A long title that should wrap to multiple lines</span>
48
- </m3e-toc>
49
-
50
- <div style="position: absolute">
51
- <h2 id="h1" style="margin-bottom: 100px">Heading 1</h2>
52
- <h3 id="h1.1" style="margin-bottom: 100px">Heading 1.1</h3>
53
- <h4 id="h1.1.1" style="margin-bottom: 100px">Heading 1.1.1</h4>
54
- <h3 id="h2" style="margin-bottom: 100px">Heading 1.2</h3>
55
- <h2 id="h2.1" style="margin-bottom: 100px">Heading 2</h2>
56
- </div>
57
- </div>
58
- </m3e-theme>
59
- </body>
60
- </html>
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
- }
@@ -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
- }