@robert-stackflow/oneui 0.6.44 → 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 CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.6.46
2
+
3
+ - TableOfContents 修复快速滚动跨过多个标题、或处在长章节之间时高亮停留旧章节的问题;滚动事件按动画帧合并,与章节观察器共用同步逻辑。
4
+
5
+ ## 0.6.45
6
+
7
+ - TableOfContents 在外层容器管理滚动时,将当前章节自动滚动到容器可见范围;窗口尺寸变化时重新检查,保留焦点并避免滚动正文,支持减少动态效果偏好。
8
+
1
9
  ## 0.6.44
2
10
 
3
11
  - Lightbox 按图片真实宽高比与可用画布尺寸显示加载骨架;窗口变化时同步适配。未提供有效尺寸时使用紧凑加载指示。
@@ -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
- const observer = new IntersectionObserver(
122
- (changes) => {
123
- for (const entry of changes) {
124
- if (entry.isIntersecting) intersections.add(entry.target.id);
125
- else intersections.delete(entry.target.id);
126
- }
127
- let ids = headings.filter((heading) => intersections.has(heading.id)).map((heading) => heading.id);
128
- if (!ids.length) {
129
- const top = (root?.getBoundingClientRect().top ?? 0) + scrollOffset;
130
- const nearest = headings.reduce(
131
- (best, heading) => Math.abs(heading.getBoundingClientRect().top - top) < Math.abs(best.getBoundingClientRect().top - top) ? heading : best
132
- );
133
- ids = [nearest.id];
134
- }
135
- const first2 = entries.findIndex((entry) => entry.id === ids[0]);
136
- const last2 = entries.findIndex((entry) => entry.id === ids.at(-1));
137
- const isUp = first2 < previous.first || last2 < previous.last || first2 === previous.first && last2 === previous.last && previous.up;
138
- previous = { first: first2, last: last2, up: isUp };
139
- setUp(isUp);
140
- setVisible((old) => old.join("\0") === ids.join("\0") ? old : ids);
141
- const viewTop = (root?.getBoundingClientRect().top ?? 0) + scrollOffset + 2;
142
- let primary = headings[0].id;
143
- for (const heading of headings) {
144
- if (heading.getBoundingClientRect().top <= viewTop)
145
- primary = heading.id;
146
- }
147
- if (root && root.scrollHeight > root.clientHeight + 1 && root.scrollTop + root.clientHeight >= root.scrollHeight - 2)
148
- primary = headings.at(-1).id;
149
- setInternal(primary);
150
- if (lastActive.current !== primary) {
151
- lastActive.current = primary;
152
- change.current?.(primary);
153
- }
154
- },
155
- { root, rootMargin: `-${scrollOffset}px 0px -12px 0px`, threshold: 0 }
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
- return () => observer.disconnect();
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;
@@ -220,19 +234,31 @@ function TableOfContents({
220
234
  }
221
235
  }, [diagram.path, variant, railLayout]);
222
236
  useLayoutEffect(() => {
223
- const node = list.current, viewport = node?.parentElement;
224
- if (!viewport || !first || !last || !open && variant === "rail") return;
225
- if (first.top < viewport.scrollTop)
226
- viewport.scrollTo({
227
- top: Math.max(0, first.top - 12),
228
- behavior: reducedMotion() ? "auto" : "smooth"
229
- });
230
- else if (last.bottom > viewport.scrollTop + viewport.clientHeight)
231
- viewport.scrollTo({
232
- top: last.bottom - viewport.clientHeight + 12,
233
- behavior: reducedMotion() ? "auto" : "smooth"
234
- });
235
- }, [first, last, open, variant]);
237
+ const node = list.current;
238
+ if (!node || !open && variant === "rail") return;
239
+ const viewport = scrollable ? node.parentElement : scrollParent(node);
240
+ const selected = node.querySelector(
241
+ '[aria-current="location"]'
242
+ );
243
+ if (!viewport || !selected) return;
244
+ const reveal = () => {
245
+ if (!viewport.clientHeight || !selected.getClientRects().length) return;
246
+ const bounds = viewport.getBoundingClientRect();
247
+ const item = selected.getBoundingClientRect();
248
+ const top = bounds.top + viewport.clientTop + 12;
249
+ const bottom = bounds.top + viewport.clientTop + viewport.clientHeight - 12;
250
+ const delta = item.top < top ? item.top - top : item.bottom > bottom ? item.bottom - bottom : 0;
251
+ if (delta)
252
+ viewport.scrollTo({
253
+ top: viewport.scrollTop + delta,
254
+ behavior: reducedMotion() ? "auto" : "smooth"
255
+ });
256
+ };
257
+ reveal();
258
+ const resize = new ResizeObserver(reveal);
259
+ resize.observe(viewport);
260
+ return () => resize.disconnect();
261
+ }, [current, diagram.path, open, variant, scrollable]);
236
262
  useLayoutEffect(() => {
237
263
  if (variant !== "rail" || railLayout !== "preview" || !open) return;
238
264
  const node = list.current, viewport = node?.parentElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robert-stackflow/oneui",
3
- "version": "0.6.44",
3
+ "version": "0.6.46",
4
4
  "description": "OneUI React components, application shell and portable visual themes",
5
5
  "type": "module",
6
6
  "sideEffects": [