Skip to content

Commit

Permalink
add stateSettings to customize state animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Yevstakhov committed Feb 27, 2024
1 parent d9105d7 commit 71e0366
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
26 changes: 26 additions & 0 deletions native/example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ const Login = ({state}) => {
title="go to LoginConfirmation"
onPress={() => goTo('LoginConfirmation', {phone: '2398723987'})}
/>
<Button
title="go to Stats #1"
onPress={() => goTo('Stats', {animation: 'slide'})}
/>
<Button
title="go to Stats #2"
onPress={() => goTo('Stats', {animation: 'rotation'})}
/>
<Text>{'login '.repeat(100)}</Text>
<Button
title="change name"
Expand All @@ -111,6 +119,17 @@ const Login = ({state}) => {
);
};

/** @type {GouterScreen<'Stats'>} */
const Stats = ({state}) => {
return (
<View style={styles.container}>
<Text>Stats</Text>
<Text>animation: {state.params.animation}</Text>
<Button title="go back" onPress={goBack} />
</View>
);
};

/** @type {GouterScreen<'LoginConfirmation'>} */
const LoginConfirmation = ({state, animationProps: {parentIndexes}}) => {
const [parentIndex] = parentIndexes;
Expand Down Expand Up @@ -419,6 +438,13 @@ const screenConfigs = {
Login: {
component: Login,
},
Stats: {
component: Stats,
stateSettings: state => ({
animation:
state.params.animation === 'rotation' ? defaultAnimation : iOSAnimation,
}),
},
LoginConfirmationStack: {
component: LoginConfirmationStack,
stackSettings: drawerSettings,
Expand Down
3 changes: 3 additions & 0 deletions native/example/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ type Config = {
name: string;
};
LoginModal: {};
Stats: {
animation?: 'slide' | 'rotation';
};
LoginConfirmationStack: {};
LoginConfirmation: {
phone: string;
Expand Down
3 changes: 2 additions & 1 deletion native/example/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {getLinking} from 'gouter/linking';
export const routes = {
App: {
navigator: newStackNavigator({}),
allowed: ['LoginStack', 'LoginConfirmationStack', 'Tabs'],
allowed: ['LoginStack', 'LoginConfirmationStack', 'Tabs', 'Stats'],
builder: (_, create) => [create('LoginStack', {})],
},
LoginStack: {
Expand All @@ -18,6 +18,7 @@ export const routes = {
redirector: (_, goTo) => goTo('LoginStack', {}),
},
LoginModal: {},
Stats: {},
LoginConfirmationStack: {
navigator: newStackNavigator({}),
allowed: ['LoginConfirmation', 'LoginDrawer'],
Expand Down
28 changes: 22 additions & 6 deletions native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ import { PanResponder, Animated, StyleSheet, Dimensions } from 'react-native';
*/

/**
* @typedef {{
* animation?: Animation
* }} StateSettings
*/

/**
* @template S
* @template {import('..').Config} T
* @template {keyof T} N
* @typedef {(state: import('../state').GouterState<T, N>) => StackSettings} ComputableStackSettings
* @typedef {(state: import('../state').GouterState<T, N>) => S} Computable
*/

/**
* @template {import('..').Config} T
* @template {keyof T} N
* @typedef {{
* component: React.ComponentType<ScreenProps<T, N>>
* stackSettings?: StackSettings | ComputableStackSettings<T, N>
* stackSettings?: StackSettings | Computable<StackSettings, T, N>
* stateSettings?: StateSettings | Computable<StateSettings, T, N>
* }} ScreenConfig
*/

Expand Down Expand Up @@ -291,7 +299,18 @@ const GouterNativeStack = memo(
],
);

const { animation } = stackSettingsRef.current;
const thisScreenConfig = screenConfigs[state.name] || defaultScreenConfig;

const { stackSettings, stateSettings } = thisScreenConfig;

const thisStateSettings =
typeof stateSettings === 'function' ? stateSettings(state) : stateSettings;

const { animation: stackAnimation } = stackSettingsRef.current;

const animation = thisStateSettings
? thisStateSettings.animation || stackAnimation
: stackAnimation;

const animatedStyleOrStyles = useMemo(
() => (animation ? animation(animationProps) : null),
Expand All @@ -311,9 +330,6 @@ const GouterNativeStack = memo(
const prevFocusedFreshIndex = focusedFreshIndexRef.current;
focusedFreshIndexRef.current = focusedFreshIndex;

const thisScreenConfig = screenConfigs[state.name] || defaultScreenConfig;

const { stackSettings } = thisScreenConfig;
const thisStackSettings =
typeof stackSettings === 'function'
? stackSettings(state)
Expand Down

0 comments on commit 71e0366

Please sign in to comment.