@robert-stackflow/oneui 0.6.45 → 0.6.46
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/CHANGELOG.md +4 -0
- package/components/TableOfContents.js +52 -38
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -116,46 +116,60 @@ function TableOfContents({
|
|
|
116
116
|
const headings = entries.map((entry) => document.getElementById(entry.id)).filter((node) => Boolean(node));
|
|
117
117
|
if (!headings.length) return;
|
|
118
118
|
const root = scrollParent(headings[0]);
|
|
119
|
-
const intersections = /* @__PURE__ */ new Set();
|
|
120
119
|
let previous = { first: 0, last: 0, up: false };
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
120
|
+
let frame = 0;
|
|
121
|
+
const sync = () => {
|
|
122
|
+
const bounds = root?.getBoundingClientRect();
|
|
123
|
+
const viewTop = (bounds?.top ?? 0) + scrollOffset + 2;
|
|
124
|
+
const viewBottom = bounds ? bounds.top + root.clientHeight - 12 : innerHeight - 12;
|
|
125
|
+
const positions = headings.map((heading) => ({
|
|
126
|
+
heading,
|
|
127
|
+
bounds: heading.getBoundingClientRect()
|
|
128
|
+
}));
|
|
129
|
+
let primary = headings[0].id;
|
|
130
|
+
for (const position of positions)
|
|
131
|
+
if (position.bounds.top <= viewTop) primary = position.heading.id;
|
|
132
|
+
if (root && root.scrollHeight > root.clientHeight + 1 && root.scrollTop + root.clientHeight >= root.scrollHeight - 2)
|
|
133
|
+
primary = headings.at(-1).id;
|
|
134
|
+
const visible2 = positions.filter(
|
|
135
|
+
(position) => position.bounds.bottom > viewTop && position.bounds.top < viewBottom
|
|
136
|
+
).map((position) => position.heading.id);
|
|
137
|
+
const ids = visible2.length ? visible2 : [primary];
|
|
138
|
+
const first2 = entries.findIndex((entry) => entry.id === ids[0]);
|
|
139
|
+
const last2 = entries.findIndex((entry) => entry.id === ids.at(-1));
|
|
140
|
+
const isUp = first2 < previous.first || last2 < previous.last || first2 === previous.first && last2 === previous.last && previous.up;
|
|
141
|
+
previous = { first: first2, last: last2, up: isUp };
|
|
142
|
+
setUp(isUp);
|
|
143
|
+
setVisible((old) => old.join("\0") === ids.join("\0") ? old : ids);
|
|
144
|
+
setInternal(primary);
|
|
145
|
+
if (lastActive.current !== primary) {
|
|
146
|
+
lastActive.current = primary;
|
|
147
|
+
change.current?.(primary);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const schedule = () => {
|
|
151
|
+
if (frame) return;
|
|
152
|
+
frame = requestAnimationFrame(() => {
|
|
153
|
+
frame = 0;
|
|
154
|
+
sync();
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
const observer = new IntersectionObserver(schedule, {
|
|
158
|
+
root,
|
|
159
|
+
rootMargin: `-${scrollOffset}px 0px -12px 0px`,
|
|
160
|
+
threshold: 0
|
|
161
|
+
});
|
|
157
162
|
headings.forEach((heading) => observer.observe(heading));
|
|
158
|
-
|
|
163
|
+
const source = root ?? window;
|
|
164
|
+
source.addEventListener("scroll", schedule, { passive: true });
|
|
165
|
+
window.addEventListener("resize", schedule);
|
|
166
|
+
sync();
|
|
167
|
+
return () => {
|
|
168
|
+
observer.disconnect();
|
|
169
|
+
source.removeEventListener("scroll", schedule);
|
|
170
|
+
window.removeEventListener("resize", schedule);
|
|
171
|
+
cancelAnimationFrame(frame);
|
|
172
|
+
};
|
|
159
173
|
}, [entries, scrollOffset]);
|
|
160
174
|
useLayoutEffect(() => {
|
|
161
175
|
const node = list.current;
|