Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up presets #57

Merged
merged 2 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions example/src/screens/BubbleStyled.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo } from 'react';
import { StyleProp, ViewStyle } from 'react-native';
import { useSafeArea } from 'react-native-safe-area-context';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import AnimatedTabBar, {
Expand Down Expand Up @@ -83,33 +84,38 @@ const BubbleStyledScreen = () => {
return 20 + bottom + 12 * 2 + 12 * 2 + 12;
}, [bottom]);

const tabBarStyle = useMemo<StyleProp<ViewStyle>>(
() => ({
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
}),
[bottom]
);

const tabBarOptions = useMemo(
() => ({
safeAreaInsets: {
bottom: 0,
},
style: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
},
style: tabBarStyle,
}),
[bottom]
[tabBarStyle]
);

// render
Expand Down
50 changes: 29 additions & 21 deletions example/src/screens/FlashyStyled.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo } from 'react';
import { StyleProp, ViewStyle } from 'react-native';
import { useSafeArea } from 'react-native-safe-area-context';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import AnimatedTabBar, {
Expand Down Expand Up @@ -79,34 +80,41 @@ const FlashyStyledScreen = () => {
return 20 + bottom + 12 * 2 + 12 * 2 + 12;
}, [bottom]);

const tabBarStyle = useMemo<StyleProp<ViewStyle>>(
() => ({
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
}),
[bottom]
);

const tabBarOptions = useMemo(
() => ({
safeAreaInsets: {
bottom: 0,
},
style: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
},
style: tabBarStyle,
}),
[bottom]
[tabBarStyle]
);

// render
return (
<Tab.Navigator
tabBarOptions={tabBarOptions}
Expand Down
25 changes: 21 additions & 4 deletions src/presets/bubble/BubbleTabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DEFAULT_ITEM_LAYOUT_DIRECTION,
DEFAULT_ITEM_CONTAINER_WIDTH,
} from './constants';
import { withTransition } from '../../withTransition';
import { noop } from '../../utilities';
import { TabBarViewProps } from '../../types';
import { BubbleTabConfig } from './types';
Expand All @@ -32,7 +33,23 @@ const BubbleTabBarComponent = ({
animatedOnChange,
onLongPress = noop,
}: TabBarViewProps<BubbleTabConfig>) => {
//#region Styles
//#region variables
const animatedFocusValues = useMemo(
() =>
tabs.map((_, index) =>
withTransition({
index,
selectedIndex,
duration,
easing,
})
),
// eslint-disable-next-line react-hooks/exhaustive-deps
[tabs, duration, easing]
);
//#endregion

//#region styles
const containerStyle = useMemo<StyleProp<ViewStyle>>(
() => [
styles.container,
Expand All @@ -44,6 +61,8 @@ const BubbleTabBarComponent = ({
[containerStyleOverride, isRTL]
);
//#endregion

// render
return (
<View style={containerStyle}>
{tabs.map(({ key, title, ...configs }, index) => {
Expand All @@ -58,10 +77,8 @@ const BubbleTabBarComponent = ({
>
<BubbleTabBarItem
index={index}
selectedIndex={selectedIndex}
animatedFocus={animatedFocusValues[index]}
label={title}
duration={duration}
easing={easing}
itemInnerSpace={itemInnerSpace}
itemOuterSpace={itemOuterSpace}
itemContainerWidth={itemContainerWidth}
Expand Down
54 changes: 19 additions & 35 deletions src/presets/bubble/item/BubbleTabBarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useMemo, memo } from 'react';
import { View, Text } from 'react-native';
import { View, Text, LayoutChangeEvent } from 'react-native';
import Animated from 'react-native-reanimated';
import { interpolateColor, useValue } from 'react-native-redash';
// @ts-ignore 😞
import isEqual from 'lodash.isequal';
import { withTransition } from '../../../withTransition';
import {
DEFAULT_ITEM_INNER_SPACE,
DEFAULT_ITEM_OUTER_SPACE,
Expand All @@ -17,23 +16,17 @@ const { add, interpolate } = Animated;

export type BubbleTabBarItemProps = TabBarItemProps & BubbleTabConfig;

const BubbleTabBarItemComponent = (props: BubbleTabBarItemProps) => {
// props
const {
index,
selectedIndex,
label,
icon,
background,
labelStyle: labelStyleOverride,
duration,
easing,
itemInnerSpace,
itemOuterSpace,
iconSize,
isRTL,
} = props;

const BubbleTabBarItemComponent = ({
animatedFocus,
label,
icon,
background,
labelStyle: labelStyleOverride,
itemInnerSpace,
itemOuterSpace,
iconSize,
isRTL,
}: BubbleTabBarItemProps) => {
//#region extract props
const {
itemInnerVerticalSpace,
Expand Down Expand Up @@ -73,16 +66,8 @@ const BubbleTabBarItemComponent = (props: BubbleTabBarItemProps) => {
}, [itemInnerSpace, itemOuterSpace]);
//#endregion

// animations
const animatedFocus = withTransition({
index,
selectedIndex,
duration,
easing,
});

//#region styles
const labelWidth = useValue(0);
//#region variables
const labelWidth = useValue<number>(0);
/**
* @DEV
* min width is calculated by adding outer & inner spaces
Expand All @@ -98,7 +83,9 @@ const BubbleTabBarItemComponent = (props: BubbleTabBarItemProps) => {
* max width is calculated by adding inner space with label width
*/
const maxWidth = add(labelWidth, itemInnerHorizontalSpace, minWidth);
//#endregion

//#region styles
const animatedIconColor = interpolateColor(animatedFocus, {
inputRange: [0, 1],
outputRange: [icon.inactiveColor, icon.activeColor],
Expand Down Expand Up @@ -153,10 +140,10 @@ const BubbleTabBarItemComponent = (props: BubbleTabBarItemProps) => {
// callbacks
const handleTextLayout = ({
nativeEvent: {
// @ts-ignore
layout: { width },
},
}) => requestAnimationFrame(() => labelWidth.setValue(width));
}: LayoutChangeEvent) =>
requestAnimationFrame(() => labelWidth.setValue(width));

// render
const renderIcon = () => {
Expand Down Expand Up @@ -190,9 +177,6 @@ const BubbleTabBarItemComponent = (props: BubbleTabBarItemProps) => {
);
};

const BubbleTabBarItem = memo(
BubbleTabBarItemComponent,
(prevProps, nextProps) => isEqual(prevProps, nextProps)
);
const BubbleTabBarItem = memo(BubbleTabBarItemComponent, isEqual);

export default BubbleTabBarItem;
23 changes: 19 additions & 4 deletions src/presets/flashy/FlashyTabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DEFAULT_ITEM_LAYOUT_DIRECTION,
DEFAULT_ITEM_CONTAINER_WIDTH,
} from './constants';
import { withTransition } from '../../withTransition';
import { noop } from '../../utilities';
import { TabBarViewProps } from '../../types';
import { FlashyTabConfig } from './types';
Expand All @@ -32,7 +33,23 @@ const FlashyTabBarComponent = ({
onLongPress = noop,
animatedOnChange,
}: TabBarViewProps<FlashyTabConfig>) => {
//#region Styles
//#region variables
const animatedFocusValues = useMemo(
() =>
tabs.map((_, index) =>
withTransition({
index,
selectedIndex,
duration,
easing,
})
),
// eslint-disable-next-line react-hooks/exhaustive-deps
[tabs, duration, easing]
);
//#endregion

//#region styles
const containerStyle = useMemo<StyleProp<ViewStyle>>(
() => [
styles.container,
Expand Down Expand Up @@ -65,10 +82,8 @@ const FlashyTabBarComponent = ({
>
<FlashyTabBarItem
index={index}
selectedIndex={selectedIndex}
animatedFocus={animatedFocusValues[index]}
label={title}
duration={duration}
easing={easing}
itemInnerSpace={itemInnerSpace}
itemOuterSpace={itemOuterSpace}
itemContainerWidth={itemContainerWidth}
Expand Down
Loading