@zag-js/toc 1.37.0
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/LICENSE +21 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +39 -0
- package/dist/index.mjs +10 -0
- package/dist/toc.anatomy.d.mts +6 -0
- package/dist/toc.anatomy.d.ts +6 -0
- package/dist/toc.anatomy.js +34 -0
- package/dist/toc.anatomy.mjs +8 -0
- package/dist/toc.connect.d.mts +7 -0
- package/dist/toc.connect.d.ts +7 -0
- package/dist/toc.connect.js +139 -0
- package/dist/toc.connect.mjs +104 -0
- package/dist/toc.dom.d.mts +19 -0
- package/dist/toc.dom.d.ts +19 -0
- package/dist/toc.dom.js +66 -0
- package/dist/toc.dom.mjs +31 -0
- package/dist/toc.machine.d.mts +7 -0
- package/dist/toc.machine.d.ts +7 -0
- package/dist/toc.machine.js +219 -0
- package/dist/toc.machine.mjs +184 -0
- package/dist/toc.props.d.mts +10 -0
- package/dist/toc.props.d.ts +10 -0
- package/dist/toc.props.js +55 -0
- package/dist/toc.props.mjs +27 -0
- package/dist/toc.types.d.mts +159 -0
- package/dist/toc.types.d.ts +159 -0
- package/dist/toc.types.js +18 -0
- package/dist/toc.types.mjs +0 -0
- package/package.json +72 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/toc.machine.ts
|
|
31
|
+
var toc_machine_exports = {};
|
|
32
|
+
__export(toc_machine_exports, {
|
|
33
|
+
machine: () => machine
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(toc_machine_exports);
|
|
36
|
+
var import_core = require("@zag-js/core");
|
|
37
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
38
|
+
var import_utils = require("@zag-js/utils");
|
|
39
|
+
var dom = __toESM(require("./toc.dom.js"));
|
|
40
|
+
var { createMachine } = (0, import_core.setup)();
|
|
41
|
+
var machine = createMachine({
|
|
42
|
+
props({ props }) {
|
|
43
|
+
return {
|
|
44
|
+
dir: "ltr",
|
|
45
|
+
rootMargin: "-20px 0% -40% 0%",
|
|
46
|
+
threshold: 0,
|
|
47
|
+
autoScroll: true,
|
|
48
|
+
scrollBehavior: "smooth",
|
|
49
|
+
items: [],
|
|
50
|
+
...props
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
initialState() {
|
|
54
|
+
return "idle";
|
|
55
|
+
},
|
|
56
|
+
context({ prop, bindable }) {
|
|
57
|
+
return {
|
|
58
|
+
activeIds: bindable(() => ({
|
|
59
|
+
defaultValue: prop("defaultActiveIds") ?? [],
|
|
60
|
+
value: prop("activeIds")
|
|
61
|
+
})),
|
|
62
|
+
indicatorRect: bindable(() => ({
|
|
63
|
+
defaultValue: null
|
|
64
|
+
}))
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
refs() {
|
|
68
|
+
return {
|
|
69
|
+
visibilityMap: /* @__PURE__ */ new Map(),
|
|
70
|
+
indicatorCleanup: null
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
computed: {
|
|
74
|
+
activeItems({ context, prop }) {
|
|
75
|
+
const ids = context.get("activeIds");
|
|
76
|
+
return prop("items").filter((item) => ids.includes(item.value));
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
watch({ context, track, action }) {
|
|
80
|
+
track([() => context.get("activeIds").join()], () => {
|
|
81
|
+
action(["autoScrollToc", "syncIndicatorRect"]);
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
entry: ["syncIndicatorRect"],
|
|
85
|
+
exit: ["cleanupIndicatorObserver"],
|
|
86
|
+
on: {
|
|
87
|
+
"ACTIVE_IDS.SET": {
|
|
88
|
+
actions: ["setActiveIds"]
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
states: {
|
|
92
|
+
idle: {
|
|
93
|
+
effects: ["trackHeadingVisibility"]
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
implementations: {
|
|
97
|
+
actions: {
|
|
98
|
+
setActiveIds(params) {
|
|
99
|
+
const { context, event } = params;
|
|
100
|
+
context.set("activeIds", event.value);
|
|
101
|
+
invokeOnActiveChange(params);
|
|
102
|
+
},
|
|
103
|
+
autoScrollToc({ context, scope, prop }) {
|
|
104
|
+
if (!prop("autoScroll")) return;
|
|
105
|
+
const tocItemEl = dom.getItemEl(scope, (0, import_utils.first)(context.get("activeIds")));
|
|
106
|
+
tocItemEl?.scrollIntoView({
|
|
107
|
+
behavior: prop("scrollBehavior"),
|
|
108
|
+
block: "nearest"
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
cleanupIndicatorObserver({ refs }) {
|
|
112
|
+
refs.get("indicatorCleanup")?.();
|
|
113
|
+
},
|
|
114
|
+
syncIndicatorRect({ context, refs, scope }) {
|
|
115
|
+
refs.get("indicatorCleanup")?.();
|
|
116
|
+
const indicatorEl = dom.getIndicatorEl(scope);
|
|
117
|
+
if (!indicatorEl) return;
|
|
118
|
+
const activeIds = context.get("activeIds");
|
|
119
|
+
if (activeIds.length === 0) {
|
|
120
|
+
context.set("indicatorRect", null);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const exec = () => {
|
|
124
|
+
const ids = context.get("activeIds");
|
|
125
|
+
if (ids.length === 0) {
|
|
126
|
+
context.set("indicatorRect", null);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const firstEl = dom.getItemEl(scope, (0, import_utils.first)(ids));
|
|
130
|
+
const lastEl = dom.getItemEl(scope, (0, import_utils.last)(ids));
|
|
131
|
+
if (!firstEl) return;
|
|
132
|
+
const listEl = dom.getListEl(scope);
|
|
133
|
+
const listRect = listEl?.getBoundingClientRect();
|
|
134
|
+
const firstRect = firstEl.getBoundingClientRect();
|
|
135
|
+
const offsetY = listRect ? firstRect.top - listRect.top + listEl.scrollTop : firstRect.top;
|
|
136
|
+
const offsetX = listRect ? firstRect.left - listRect.left + listEl.scrollLeft : firstRect.left;
|
|
137
|
+
let height;
|
|
138
|
+
if (lastEl && lastEl !== firstEl) {
|
|
139
|
+
const lastRect = lastEl.getBoundingClientRect();
|
|
140
|
+
height = lastRect.top + lastRect.height - firstRect.top;
|
|
141
|
+
} else {
|
|
142
|
+
height = firstRect.height;
|
|
143
|
+
}
|
|
144
|
+
const nextRect = {
|
|
145
|
+
x: offsetX,
|
|
146
|
+
y: offsetY,
|
|
147
|
+
width: firstRect.width,
|
|
148
|
+
height
|
|
149
|
+
};
|
|
150
|
+
context.set("indicatorRect", (prev) => (0, import_utils.isEqual)(prev, nextRect) ? prev : nextRect);
|
|
151
|
+
};
|
|
152
|
+
exec();
|
|
153
|
+
const cleanups = [];
|
|
154
|
+
for (const id of activeIds) {
|
|
155
|
+
const el = dom.getItemEl(scope, id);
|
|
156
|
+
if (el) {
|
|
157
|
+
cleanups.push(import_dom_query.resizeObserverBorderBox.observe(el, exec));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
refs.set("indicatorCleanup", () => (0, import_utils.callAll)(...cleanups));
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
effects: {
|
|
164
|
+
trackHeadingVisibility(params) {
|
|
165
|
+
const { scope, prop, context, refs } = params;
|
|
166
|
+
const items = prop("items");
|
|
167
|
+
if (items.length === 0) return;
|
|
168
|
+
const visibilityMap = refs.get("visibilityMap");
|
|
169
|
+
const observerOptions = {
|
|
170
|
+
rootMargin: prop("rootMargin"),
|
|
171
|
+
threshold: prop("threshold")
|
|
172
|
+
};
|
|
173
|
+
const scrollEl = prop("getScrollEl")?.();
|
|
174
|
+
if (scrollEl) {
|
|
175
|
+
observerOptions.root = scrollEl;
|
|
176
|
+
}
|
|
177
|
+
const win = scope.getWin();
|
|
178
|
+
const observer = new win.IntersectionObserver((entries) => {
|
|
179
|
+
for (const entry of entries) {
|
|
180
|
+
visibilityMap.set(entry.target.id, entry.isIntersecting);
|
|
181
|
+
}
|
|
182
|
+
const nextActiveIds = [];
|
|
183
|
+
for (const item of items) {
|
|
184
|
+
if (visibilityMap.get(item.value)) {
|
|
185
|
+
nextActiveIds.push(item.value);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (nextActiveIds.length === 0) return;
|
|
189
|
+
const currentActiveIds = context.get("activeIds");
|
|
190
|
+
if (!(0, import_utils.isEqual)(currentActiveIds, nextActiveIds)) {
|
|
191
|
+
context.set("activeIds", nextActiveIds);
|
|
192
|
+
invokeOnActiveChange(params);
|
|
193
|
+
}
|
|
194
|
+
}, observerOptions);
|
|
195
|
+
for (const item of items) {
|
|
196
|
+
const headingEl = dom.getHeadingEl(scope, item.value);
|
|
197
|
+
if (headingEl) {
|
|
198
|
+
observer.observe(headingEl);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return () => {
|
|
202
|
+
observer.disconnect();
|
|
203
|
+
visibilityMap.clear();
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
function invokeOnActiveChange(params) {
|
|
210
|
+
const { context, computed, prop } = params;
|
|
211
|
+
prop("onActiveChange")?.({
|
|
212
|
+
activeIds: context.get("activeIds"),
|
|
213
|
+
activeItems: computed("activeItems")
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
217
|
+
0 && (module.exports = {
|
|
218
|
+
machine
|
|
219
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// src/toc.machine.ts
|
|
2
|
+
import { setup } from "@zag-js/core";
|
|
3
|
+
import { resizeObserverBorderBox } from "@zag-js/dom-query";
|
|
4
|
+
import { callAll, first, isEqual, last } from "@zag-js/utils";
|
|
5
|
+
import * as dom from "./toc.dom.mjs";
|
|
6
|
+
var { createMachine } = setup();
|
|
7
|
+
var machine = createMachine({
|
|
8
|
+
props({ props }) {
|
|
9
|
+
return {
|
|
10
|
+
dir: "ltr",
|
|
11
|
+
rootMargin: "-20px 0% -40% 0%",
|
|
12
|
+
threshold: 0,
|
|
13
|
+
autoScroll: true,
|
|
14
|
+
scrollBehavior: "smooth",
|
|
15
|
+
items: [],
|
|
16
|
+
...props
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
initialState() {
|
|
20
|
+
return "idle";
|
|
21
|
+
},
|
|
22
|
+
context({ prop, bindable }) {
|
|
23
|
+
return {
|
|
24
|
+
activeIds: bindable(() => ({
|
|
25
|
+
defaultValue: prop("defaultActiveIds") ?? [],
|
|
26
|
+
value: prop("activeIds")
|
|
27
|
+
})),
|
|
28
|
+
indicatorRect: bindable(() => ({
|
|
29
|
+
defaultValue: null
|
|
30
|
+
}))
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
refs() {
|
|
34
|
+
return {
|
|
35
|
+
visibilityMap: /* @__PURE__ */ new Map(),
|
|
36
|
+
indicatorCleanup: null
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
computed: {
|
|
40
|
+
activeItems({ context, prop }) {
|
|
41
|
+
const ids = context.get("activeIds");
|
|
42
|
+
return prop("items").filter((item) => ids.includes(item.value));
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
watch({ context, track, action }) {
|
|
46
|
+
track([() => context.get("activeIds").join()], () => {
|
|
47
|
+
action(["autoScrollToc", "syncIndicatorRect"]);
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
entry: ["syncIndicatorRect"],
|
|
51
|
+
exit: ["cleanupIndicatorObserver"],
|
|
52
|
+
on: {
|
|
53
|
+
"ACTIVE_IDS.SET": {
|
|
54
|
+
actions: ["setActiveIds"]
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
states: {
|
|
58
|
+
idle: {
|
|
59
|
+
effects: ["trackHeadingVisibility"]
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
implementations: {
|
|
63
|
+
actions: {
|
|
64
|
+
setActiveIds(params) {
|
|
65
|
+
const { context, event } = params;
|
|
66
|
+
context.set("activeIds", event.value);
|
|
67
|
+
invokeOnActiveChange(params);
|
|
68
|
+
},
|
|
69
|
+
autoScrollToc({ context, scope, prop }) {
|
|
70
|
+
if (!prop("autoScroll")) return;
|
|
71
|
+
const tocItemEl = dom.getItemEl(scope, first(context.get("activeIds")));
|
|
72
|
+
tocItemEl?.scrollIntoView({
|
|
73
|
+
behavior: prop("scrollBehavior"),
|
|
74
|
+
block: "nearest"
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
cleanupIndicatorObserver({ refs }) {
|
|
78
|
+
refs.get("indicatorCleanup")?.();
|
|
79
|
+
},
|
|
80
|
+
syncIndicatorRect({ context, refs, scope }) {
|
|
81
|
+
refs.get("indicatorCleanup")?.();
|
|
82
|
+
const indicatorEl = dom.getIndicatorEl(scope);
|
|
83
|
+
if (!indicatorEl) return;
|
|
84
|
+
const activeIds = context.get("activeIds");
|
|
85
|
+
if (activeIds.length === 0) {
|
|
86
|
+
context.set("indicatorRect", null);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const exec = () => {
|
|
90
|
+
const ids = context.get("activeIds");
|
|
91
|
+
if (ids.length === 0) {
|
|
92
|
+
context.set("indicatorRect", null);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const firstEl = dom.getItemEl(scope, first(ids));
|
|
96
|
+
const lastEl = dom.getItemEl(scope, last(ids));
|
|
97
|
+
if (!firstEl) return;
|
|
98
|
+
const listEl = dom.getListEl(scope);
|
|
99
|
+
const listRect = listEl?.getBoundingClientRect();
|
|
100
|
+
const firstRect = firstEl.getBoundingClientRect();
|
|
101
|
+
const offsetY = listRect ? firstRect.top - listRect.top + listEl.scrollTop : firstRect.top;
|
|
102
|
+
const offsetX = listRect ? firstRect.left - listRect.left + listEl.scrollLeft : firstRect.left;
|
|
103
|
+
let height;
|
|
104
|
+
if (lastEl && lastEl !== firstEl) {
|
|
105
|
+
const lastRect = lastEl.getBoundingClientRect();
|
|
106
|
+
height = lastRect.top + lastRect.height - firstRect.top;
|
|
107
|
+
} else {
|
|
108
|
+
height = firstRect.height;
|
|
109
|
+
}
|
|
110
|
+
const nextRect = {
|
|
111
|
+
x: offsetX,
|
|
112
|
+
y: offsetY,
|
|
113
|
+
width: firstRect.width,
|
|
114
|
+
height
|
|
115
|
+
};
|
|
116
|
+
context.set("indicatorRect", (prev) => isEqual(prev, nextRect) ? prev : nextRect);
|
|
117
|
+
};
|
|
118
|
+
exec();
|
|
119
|
+
const cleanups = [];
|
|
120
|
+
for (const id of activeIds) {
|
|
121
|
+
const el = dom.getItemEl(scope, id);
|
|
122
|
+
if (el) {
|
|
123
|
+
cleanups.push(resizeObserverBorderBox.observe(el, exec));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
refs.set("indicatorCleanup", () => callAll(...cleanups));
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
effects: {
|
|
130
|
+
trackHeadingVisibility(params) {
|
|
131
|
+
const { scope, prop, context, refs } = params;
|
|
132
|
+
const items = prop("items");
|
|
133
|
+
if (items.length === 0) return;
|
|
134
|
+
const visibilityMap = refs.get("visibilityMap");
|
|
135
|
+
const observerOptions = {
|
|
136
|
+
rootMargin: prop("rootMargin"),
|
|
137
|
+
threshold: prop("threshold")
|
|
138
|
+
};
|
|
139
|
+
const scrollEl = prop("getScrollEl")?.();
|
|
140
|
+
if (scrollEl) {
|
|
141
|
+
observerOptions.root = scrollEl;
|
|
142
|
+
}
|
|
143
|
+
const win = scope.getWin();
|
|
144
|
+
const observer = new win.IntersectionObserver((entries) => {
|
|
145
|
+
for (const entry of entries) {
|
|
146
|
+
visibilityMap.set(entry.target.id, entry.isIntersecting);
|
|
147
|
+
}
|
|
148
|
+
const nextActiveIds = [];
|
|
149
|
+
for (const item of items) {
|
|
150
|
+
if (visibilityMap.get(item.value)) {
|
|
151
|
+
nextActiveIds.push(item.value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (nextActiveIds.length === 0) return;
|
|
155
|
+
const currentActiveIds = context.get("activeIds");
|
|
156
|
+
if (!isEqual(currentActiveIds, nextActiveIds)) {
|
|
157
|
+
context.set("activeIds", nextActiveIds);
|
|
158
|
+
invokeOnActiveChange(params);
|
|
159
|
+
}
|
|
160
|
+
}, observerOptions);
|
|
161
|
+
for (const item of items) {
|
|
162
|
+
const headingEl = dom.getHeadingEl(scope, item.value);
|
|
163
|
+
if (headingEl) {
|
|
164
|
+
observer.observe(headingEl);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return () => {
|
|
168
|
+
observer.disconnect();
|
|
169
|
+
visibilityMap.clear();
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
function invokeOnActiveChange(params) {
|
|
176
|
+
const { context, computed, prop } = params;
|
|
177
|
+
prop("onActiveChange")?.({
|
|
178
|
+
activeIds: context.get("activeIds"),
|
|
179
|
+
activeItems: computed("activeItems")
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
export {
|
|
183
|
+
machine
|
|
184
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { TocProps, ItemProps } from './toc.types.mjs';
|
|
2
|
+
import '@zag-js/core';
|
|
3
|
+
import '@zag-js/types';
|
|
4
|
+
|
|
5
|
+
declare const props: (keyof TocProps)[];
|
|
6
|
+
declare const splitProps: <Props extends Partial<TocProps>>(props: Props) => [Partial<TocProps>, Omit<Props, keyof TocProps>];
|
|
7
|
+
declare const itemProps: "item"[];
|
|
8
|
+
declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, "item">];
|
|
9
|
+
|
|
10
|
+
export { itemProps, props, splitItemProps, splitProps };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { TocProps, ItemProps } from './toc.types.js';
|
|
2
|
+
import '@zag-js/core';
|
|
3
|
+
import '@zag-js/types';
|
|
4
|
+
|
|
5
|
+
declare const props: (keyof TocProps)[];
|
|
6
|
+
declare const splitProps: <Props extends Partial<TocProps>>(props: Props) => [Partial<TocProps>, Omit<Props, keyof TocProps>];
|
|
7
|
+
declare const itemProps: "item"[];
|
|
8
|
+
declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, "item">];
|
|
9
|
+
|
|
10
|
+
export { itemProps, props, splitItemProps, splitProps };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/toc.props.ts
|
|
21
|
+
var toc_props_exports = {};
|
|
22
|
+
__export(toc_props_exports, {
|
|
23
|
+
itemProps: () => itemProps,
|
|
24
|
+
props: () => props,
|
|
25
|
+
splitItemProps: () => splitItemProps,
|
|
26
|
+
splitProps: () => splitProps
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(toc_props_exports);
|
|
29
|
+
var import_types = require("@zag-js/types");
|
|
30
|
+
var import_utils = require("@zag-js/utils");
|
|
31
|
+
var props = (0, import_types.createProps)()([
|
|
32
|
+
"activeIds",
|
|
33
|
+
"autoScroll",
|
|
34
|
+
"defaultActiveIds",
|
|
35
|
+
"dir",
|
|
36
|
+
"getRootNode",
|
|
37
|
+
"id",
|
|
38
|
+
"ids",
|
|
39
|
+
"items",
|
|
40
|
+
"onActiveChange",
|
|
41
|
+
"rootMargin",
|
|
42
|
+
"scrollBehavior",
|
|
43
|
+
"getScrollEl",
|
|
44
|
+
"threshold"
|
|
45
|
+
]);
|
|
46
|
+
var splitProps = (0, import_utils.createSplitProps)(props);
|
|
47
|
+
var itemProps = (0, import_types.createProps)()(["item"]);
|
|
48
|
+
var splitItemProps = (0, import_utils.createSplitProps)(itemProps);
|
|
49
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
50
|
+
0 && (module.exports = {
|
|
51
|
+
itemProps,
|
|
52
|
+
props,
|
|
53
|
+
splitItemProps,
|
|
54
|
+
splitProps
|
|
55
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/toc.props.ts
|
|
2
|
+
import { createProps } from "@zag-js/types";
|
|
3
|
+
import { createSplitProps } from "@zag-js/utils";
|
|
4
|
+
var props = createProps()([
|
|
5
|
+
"activeIds",
|
|
6
|
+
"autoScroll",
|
|
7
|
+
"defaultActiveIds",
|
|
8
|
+
"dir",
|
|
9
|
+
"getRootNode",
|
|
10
|
+
"id",
|
|
11
|
+
"ids",
|
|
12
|
+
"items",
|
|
13
|
+
"onActiveChange",
|
|
14
|
+
"rootMargin",
|
|
15
|
+
"scrollBehavior",
|
|
16
|
+
"getScrollEl",
|
|
17
|
+
"threshold"
|
|
18
|
+
]);
|
|
19
|
+
var splitProps = createSplitProps(props);
|
|
20
|
+
var itemProps = createProps()(["item"]);
|
|
21
|
+
var splitItemProps = createSplitProps(itemProps);
|
|
22
|
+
export {
|
|
23
|
+
itemProps,
|
|
24
|
+
props,
|
|
25
|
+
splitItemProps,
|
|
26
|
+
splitProps
|
|
27
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Machine, EventObject, Service } from '@zag-js/core';
|
|
2
|
+
import { PropTypes, RequiredBy, DirectionProperty, CommonProperties, Rect } from '@zag-js/types';
|
|
3
|
+
|
|
4
|
+
interface TocItem {
|
|
5
|
+
/**
|
|
6
|
+
* The slug/id of the heading element in the document
|
|
7
|
+
*/
|
|
8
|
+
value: string;
|
|
9
|
+
/**
|
|
10
|
+
* The nesting depth (e.g., 2 for h2, 3 for h3)
|
|
11
|
+
*/
|
|
12
|
+
depth: number;
|
|
13
|
+
}
|
|
14
|
+
interface ActiveChangeDetails {
|
|
15
|
+
/**
|
|
16
|
+
* All currently active (visible) heading ids
|
|
17
|
+
*/
|
|
18
|
+
activeIds: string[];
|
|
19
|
+
/**
|
|
20
|
+
* The active (visible) TOC items
|
|
21
|
+
*/
|
|
22
|
+
activeItems: TocItem[];
|
|
23
|
+
}
|
|
24
|
+
type ElementIds = Partial<{
|
|
25
|
+
root: string;
|
|
26
|
+
title: string;
|
|
27
|
+
list: string;
|
|
28
|
+
item: (value: string) => string;
|
|
29
|
+
link: (value: string) => string;
|
|
30
|
+
indicator: string;
|
|
31
|
+
}>;
|
|
32
|
+
interface TocProps extends DirectionProperty, CommonProperties {
|
|
33
|
+
/**
|
|
34
|
+
* The ids of the elements in the TOC. Useful for composition.
|
|
35
|
+
*/
|
|
36
|
+
ids?: ElementIds | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The TOC items with `value` (slug/id) and `depth` (heading level).
|
|
39
|
+
*/
|
|
40
|
+
items: TocItem[];
|
|
41
|
+
/**
|
|
42
|
+
* The root margin for the IntersectionObserver.
|
|
43
|
+
* Controls the effective viewport area for determining active headings.
|
|
44
|
+
*
|
|
45
|
+
* @default "-20px 0px -40% 0px"
|
|
46
|
+
*/
|
|
47
|
+
rootMargin?: string | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* The IntersectionObserver threshold. A value of `0` means the heading is
|
|
50
|
+
* active as soon as even one pixel is visible within the root margin area.
|
|
51
|
+
* @default 0
|
|
52
|
+
*/
|
|
53
|
+
threshold?: number | number[] | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Function that returns the scroll container element to observe within.
|
|
56
|
+
* Defaults to the document/viewport.
|
|
57
|
+
*/
|
|
58
|
+
getScrollEl?: (() => HTMLElement | null) | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Whether to auto-scroll the TOC container so the first active item
|
|
61
|
+
* is visible when active headings change.
|
|
62
|
+
* @default true
|
|
63
|
+
*/
|
|
64
|
+
autoScroll?: boolean | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* The scroll behavior for auto-scrolling the TOC container.
|
|
67
|
+
* @default "smooth"
|
|
68
|
+
*/
|
|
69
|
+
scrollBehavior?: ScrollBehavior | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Callback when the active (visible) headings change.
|
|
72
|
+
*/
|
|
73
|
+
onActiveChange?: ((details: ActiveChangeDetails) => void) | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* The controlled active heading ids.
|
|
76
|
+
*/
|
|
77
|
+
activeIds?: string[] | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* The default active heading ids when rendered.
|
|
80
|
+
* Use when you don't need to control the active headings.
|
|
81
|
+
*/
|
|
82
|
+
defaultActiveIds?: string[] | undefined;
|
|
83
|
+
}
|
|
84
|
+
type PropsWithDefault = "rootMargin" | "threshold" | "autoScroll" | "scrollBehavior" | "items";
|
|
85
|
+
interface TocSchema {
|
|
86
|
+
state: "idle";
|
|
87
|
+
props: RequiredBy<TocProps, PropsWithDefault>;
|
|
88
|
+
context: {
|
|
89
|
+
activeIds: string[];
|
|
90
|
+
indicatorRect: Rect | null;
|
|
91
|
+
};
|
|
92
|
+
refs: {
|
|
93
|
+
visibilityMap: Map<string, boolean>;
|
|
94
|
+
indicatorCleanup: VoidFunction | null;
|
|
95
|
+
};
|
|
96
|
+
computed: {
|
|
97
|
+
activeItems: TocItem[];
|
|
98
|
+
};
|
|
99
|
+
action: string;
|
|
100
|
+
guard: string;
|
|
101
|
+
effect: string;
|
|
102
|
+
event: EventObject;
|
|
103
|
+
}
|
|
104
|
+
type TocService = Service<TocSchema>;
|
|
105
|
+
type TocMachine = Machine<TocSchema>;
|
|
106
|
+
interface ItemProps {
|
|
107
|
+
/**
|
|
108
|
+
* The TOC item
|
|
109
|
+
*/
|
|
110
|
+
item: TocItem;
|
|
111
|
+
}
|
|
112
|
+
interface ItemState {
|
|
113
|
+
/**
|
|
114
|
+
* Whether this heading is currently visible/active
|
|
115
|
+
*/
|
|
116
|
+
active: boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Whether this is the first active heading
|
|
119
|
+
*/
|
|
120
|
+
first: boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Whether this is the last active heading
|
|
123
|
+
*/
|
|
124
|
+
last: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* The depth of this heading (for indentation)
|
|
127
|
+
*/
|
|
128
|
+
depth: number;
|
|
129
|
+
}
|
|
130
|
+
interface TocApi<T extends PropTypes = PropTypes> {
|
|
131
|
+
/**
|
|
132
|
+
* All currently active (visible) heading ids
|
|
133
|
+
*/
|
|
134
|
+
activeIds: string[];
|
|
135
|
+
/**
|
|
136
|
+
* The active (visible) TOC items
|
|
137
|
+
*/
|
|
138
|
+
activeItems: TocItem[];
|
|
139
|
+
/**
|
|
140
|
+
* The resolved items list
|
|
141
|
+
*/
|
|
142
|
+
items: TocItem[];
|
|
143
|
+
/**
|
|
144
|
+
* Manually set the active heading ids
|
|
145
|
+
*/
|
|
146
|
+
setActiveIds(value: string[]): void;
|
|
147
|
+
/**
|
|
148
|
+
* Returns the state of a TOC item
|
|
149
|
+
*/
|
|
150
|
+
getItemState(props: ItemProps): ItemState;
|
|
151
|
+
getRootProps(): T["element"];
|
|
152
|
+
getTitleProps(): T["element"];
|
|
153
|
+
getListProps(): T["element"];
|
|
154
|
+
getItemProps(props: ItemProps): T["element"];
|
|
155
|
+
getLinkProps(props: ItemProps): T["element"];
|
|
156
|
+
getIndicatorProps(): T["element"];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export type { ActiveChangeDetails, ElementIds, ItemProps, ItemState, TocApi, TocItem, TocMachine, TocProps, TocSchema, TocService };
|