UI: Fix thumb not rendered on initial render

This commit is contained in:
fuma-nama
2024-08-18 18:22:28 +08:00
parent 0794fc3921
commit 7258c4b046
2 changed files with 9 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-ui': patch
---
Fix thumb not rendered on initial render
+4 -4
View File
@@ -1,6 +1,5 @@
import { type RefObject, useState } from 'react';
import { type RefObject, useLayoutEffect, useState } from 'react';
import * as Primitive from 'fumadocs-core/toc';
import { useOnChange } from 'fumadocs-core/utils/use-on-change';
export type TOCThumb = [top: number, height: number];
@@ -8,7 +7,8 @@ export function useTocThumb(containerRef: RefObject<HTMLElement>): TOCThumb {
const active = Primitive.useActiveAnchors();
const [pos, setPos] = useState<TOCThumb>([0, 0]);
useOnChange(active, () => {
// effect is required to render TOC thumb at the correct position
useLayoutEffect(() => {
const container = containerRef.current;
if (active.length === 0 || !container || container.clientHeight === 0) {
setPos([0, 0]);
@@ -38,7 +38,7 @@ export function useTocThumb(containerRef: RefObject<HTMLElement>): TOCThumb {
}
setPos([upper, lower - upper]);
});
}, [active, containerRef]);
return pos;
}