Skip to content

Commit a43b0d0

Browse files
committed
Update calculatewidth
1 parent e582800 commit a43b0d0

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/theme/Tabs/index.tsx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,38 @@ import { Icon } from "@site/src/components/Icon";
99
function TabList({ className, block, selectedValue, selectValue, tabValues }) {
1010
const tabRefs = [];
1111
const { blockElementScrollPositionUntilNextRender } = useScrollPositionBlocker();
12-
const [open, setOpen] = useState<boolean>(false);
13-
const [overflowing, setOverflowing] = useState<boolean>(false);
14-
const windowWidth = ((document?.body?.getBoundingClientRect().width - 300) * 0.75) - 48;
15-
const [width, setWidth] = useState<number>(windowWidth);
16-
const ulvalues = useRef<any>(null)
12+
const [open, setOpen] = useState(false);
13+
const [overflowing, setOverflowing] = useState(false);
14+
const [width, setWidth] = useState(0);
15+
const ulvalues = useRef(null);
16+
17+
const calculateWidth = () => {
18+
if (typeof document === "undefined") return 0;
19+
return ((document.body.getBoundingClientRect().width - 300) * 0.75) - 48; // 48 is padding, 0.75 is 75% of content
20+
};
21+
22+
useEffect(() => {
23+
setWidth(calculateWidth());
24+
checkForOverflow();
25+
}, [width]);
26+
27+
useEffect(() => {
28+
const handleResize = () => {
29+
const newWidth = calculateWidth();
30+
setWidth(newWidth);
31+
};
32+
window.addEventListener("resize", handleResize);
33+
return () => window.removeEventListener("resize", handleResize);
34+
}, []);
35+
36+
const checkForOverflow = () => {
37+
if (ulvalues.current && width > 0) {
38+
const contentWidth = ulvalues.current.getBoundingClientRect().width;
39+
console.log(width - contentWidth, contentWidth, width);
40+
setOverflowing(contentWidth > width);
41+
}
42+
};
43+
1744

1845
const handleTabChange = (event) => {
1946
const newTab = event.currentTarget;
@@ -25,13 +52,6 @@ function TabList({ className, block, selectedValue, selectValue, tabValues }) {
2552
}
2653
setOpen(false);
2754
};
28-
const checkForOverflow = () => {
29-
if (ulvalues.current.getBoundingClientRect().width > width) setOverflowing(true)
30-
else setOverflowing(false);
31-
}
32-
useEffect(() => {
33-
checkForOverflow();
34-
}, [])
3555
const handleKeydown = (event) => {
3656
let focusElement = null;
3757
switch (event.key) {
@@ -60,16 +80,6 @@ function TabList({ className, block, selectedValue, selectValue, tabValues }) {
6080
blockElementScrollPositionUntilNextRender(list);
6181
}
6282

63-
const checkForResize = () => {
64-
setWidth(((document?.body?.getBoundingClientRect().width - 300) * 0.75) - 48);
65-
}
66-
67-
useEffect(() => {
68-
window.addEventListener("resize", checkForResize);
69-
return () => {
70-
window.removeEventListener("resize", checkForResize);
71-
};
72-
}, []);
7383
return (
7484
<ul
7585
role="tablist"

0 commit comments

Comments
 (0)