Skip to content

Commit 80769a3

Browse files
committed
refactor and add comment for readability
1 parent e87d66f commit 80769a3

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

packages/components/tabs/src/tabs.tsx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ const Tabs = forwardRef(function Tabs<T extends object>(
7474
[variant],
7575
);
7676

77+
const withCursorReset = useCallback(
78+
(callback: () => void) => {
79+
// check if cursor should be reset, e.g. to disable animation during callback
80+
if (variant !== previousVariant.current || isVertical !== previousIsVertical.current) {
81+
cursorRef.current?.removeAttribute("data-initialized");
82+
}
83+
callback();
84+
previousVariant.current = variant;
85+
previousIsVertical.current = isVertical;
86+
requestAnimationFrame(() => cursorRef.current?.setAttribute("data-initialized", "true"));
87+
},
88+
[isVertical, variant],
89+
);
90+
7791
const updateCursorPosition = useCallback(
7892
(selectedTab: HTMLElement) => {
7993
if (!cursorRef.current) return;
@@ -87,19 +101,14 @@ const Tabs = forwardRef(function Tabs<T extends object>(
87101

88102
const styles = getCursorStyles(tabRect);
89103

90-
if (variant !== previousVariant.current || isVertical !== previousIsVertical.current) {
91-
cursorRef.current.removeAttribute("data-initialized");
92-
}
93-
cursorRef.current.style.left = styles.left;
94-
cursorRef.current.style.top = styles.top;
95-
cursorRef.current.style.width = styles.width;
96-
cursorRef.current.style.height = styles.height;
97-
previousVariant.current = variant;
98-
previousIsVertical.current = isVertical;
99-
100-
requestAnimationFrame(() => cursorRef.current?.setAttribute("data-initialized", "true"));
104+
withCursorReset(() => {
105+
cursorRef.current!.style.left = styles.left;
106+
cursorRef.current!.style.top = styles.top;
107+
cursorRef.current!.style.width = styles.width;
108+
cursorRef.current!.style.height = styles.height;
109+
});
101110
},
102-
[cursorRef.current, getCursorStyles, isVertical, variant],
111+
[cursorRef.current, getCursorStyles, withCursorReset],
103112
);
104113

105114
const onResize = useCallback(

0 commit comments

Comments
 (0)