Skip to content

Commit e0f1b40

Browse files
committed
Simplify animation prevention
1 parent 9cec7a7 commit e0f1b40

File tree

3 files changed

+9
-26
lines changed

3 files changed

+9
-26
lines changed

packages/react/tabs/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
"@radix-ui/react-presence": "workspace:*",
2626
"@radix-ui/react-primitive": "workspace:*",
2727
"@radix-ui/react-roving-focus": "workspace:*",
28-
"@radix-ui/react-use-controllable-state": "workspace:*",
29-
"@radix-ui/react-use-layout-effect": "workspace:*"
28+
"@radix-ui/react-use-controllable-state": "workspace:*"
3029
},
3130
"peerDependencies": {
3231
"react": "^16.8 || ^17.0 || ^18.0",

packages/react/tabs/src/Tabs.tsx

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { useDirection } from '@radix-ui/react-direction';
99
import { useComposedRefs } from '@radix-ui/react-compose-refs';
1010
import { useControllableState } from '@radix-ui/react-use-controllable-state';
1111
import { useId } from '@radix-ui/react-id';
12-
import { useLayoutEffect } from '@radix-ui/react-use-layout-effect';
1312

1413
import type * as Radix from '@radix-ui/react-primitive';
1514
import type { Scope } from '@radix-ui/react-context';
@@ -231,29 +230,11 @@ const TabsContent = React.forwardRef<TabsContentElement, TabsContentProps>(
231230
const composedRefs = useComposedRefs(forwardedRef, ref);
232231
const isSelected = value === context.value;
233232
const isMountAnimationPreventedRef = React.useRef(isSelected);
234-
const originalStylesRef = React.useRef<Record<string, string>>();
235233

236-
useLayoutEffect(() => {
237-
const node = ref.current;
238-
239-
if (node) {
240-
originalStylesRef.current = originalStylesRef.current || {
241-
animationDuration: node.style.animationDuration,
242-
};
243-
244-
// Prevent animations from running if open when initially mounted
245-
const originalDuration = originalStylesRef.current.animationDuration;
246-
const animationPrevented = isMountAnimationPreventedRef.current;
247-
248-
node.style.animationDuration = animationPrevented ? '0s' : originalDuration;
249-
isMountAnimationPreventedRef.current = false;
250-
}
251-
252-
/**
253-
* Depends on `isSelected` as we want to restore the original
254-
* animation when navigating away from an initially open tab.
255-
*/
256-
}, [isSelected]);
234+
React.useEffect(() => {
235+
const rAF = requestAnimationFrame(() => (isMountAnimationPreventedRef.current = false));
236+
return () => cancelAnimationFrame(rAF);
237+
}, []);
257238

258239
return (
259240
<Presence present={isSelected}>
@@ -268,6 +249,10 @@ const TabsContent = React.forwardRef<TabsContentElement, TabsContentProps>(
268249
tabIndex={0}
269250
{...contentProps}
270251
ref={composedRefs}
252+
style={{
253+
...props.style,
254+
animationDuration: isMountAnimationPreventedRef.current ? '0s' : undefined,
255+
}}
271256
>
272257
{present && children}
273258
</Primitive.div>

yarn.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3675,7 +3675,6 @@ __metadata:
36753675
"@radix-ui/react-primitive": "workspace:*"
36763676
"@radix-ui/react-roving-focus": "workspace:*"
36773677
"@radix-ui/react-use-controllable-state": "workspace:*"
3678-
"@radix-ui/react-use-layout-effect": "workspace:*"
36793678
peerDependencies:
36803679
react: ^16.8 || ^17.0 || ^18.0
36813680
react-dom: ^16.8 || ^17.0 || ^18.0

0 commit comments

Comments
 (0)