Skip to content

Commit

Permalink
feat: add useCardAnimation (#11568)
Browse files Browse the repository at this point in the history
**Motivation**

Make animation progress available inside a screen.

**Test plan**

Describe the **steps to test this change** so that a reviewer can verify
it. Provide screenshots or videos if the change affects UI.

The change must pass lint, typescript and tests.
  • Loading branch information
adamgrzybowski authored Sep 6, 2023
1 parent ecd0e66 commit 36d19bd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/material-top-tabs/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export { createMaterialTopTabNavigator } from './navigators/createMaterialTopTab
export { MaterialTopTabBar } from './views/MaterialTopTabBar';
export { MaterialTopTabView } from './views/MaterialTopTabView';

/**
* Utilities
*/
export { useTabAnimation } from './utils/useTabAnimation';

/**
* Types
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/material-top-tabs/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from '@react-navigation/native';
import type React from 'react';
import type {
Animated,
PressableAndroidRippleConfig,
StyleProp,
TextStyle,
Expand Down Expand Up @@ -299,3 +300,7 @@ export type MaterialTopTabBarProps = SceneRendererProps & {
>;
descriptors: MaterialTopTabDescriptorMap;
};

export type MaterialTopTabAnimationContext = {
position: Animated.AnimatedInterpolation<number>;
};
7 changes: 7 additions & 0 deletions packages/material-top-tabs/src/utils/TabAnimationContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

import type { MaterialTopTabAnimationContext } from '../types';

export const TabAnimationContext = React.createContext<
MaterialTopTabAnimationContext | undefined
>(undefined);
15 changes: 15 additions & 0 deletions packages/material-top-tabs/src/utils/useTabAnimation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';

import { TabAnimationContext } from './TabAnimationContext';

export function useTabAnimation() {
const animation = React.useContext(TabAnimationContext);

if (animation === undefined) {
throw new Error(
"Couldn't find values for tab animation. Are you inside a screen in Material Top Tab navigator?"
);
}

return animation;
}
7 changes: 6 additions & 1 deletion packages/material-top-tabs/src/views/MaterialTopTabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
MaterialTopTabNavigationConfig,
MaterialTopTabNavigationHelpers,
} from '../types';
import { TabAnimationContext } from '../utils/TabAnimationContext';
import { MaterialTopTabBar } from './MaterialTopTabBar';

type Props = MaterialTopTabNavigationConfig & {
Expand Down Expand Up @@ -56,7 +57,11 @@ export function MaterialTopTabView({
target: state.key,
});
}}
renderScene={({ route }) => descriptors[route.key].render()}
renderScene={({ route, position }) => (
<TabAnimationContext.Provider value={{ position }}>
{descriptors[route.key].render()}
</TabAnimationContext.Provider>
)}
navigationState={state}
renderTabBar={renderTabBar}
renderLazyPlaceholder={({ route }) =>
Expand Down

0 comments on commit 36d19bd

Please sign in to comment.