Skip to content

Fix/tab controller safe area and indicator width #1923

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

Merged
merged 4 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions generatedTypes/src/components/tabController/TabBar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export interface TabControllerBarProps {
* The indicator insets (default: Spacings.s4, set to 0 to make it wide as the item)
*/
indicatorInsets?: number;
/**
* Send to get a constant width of the indicator (overrides indicatorInsets)
*/
indicatorWidth?: number;
/**
* Additional styles for the container
*/
Expand Down
6 changes: 5 additions & 1 deletion generatedTypes/src/components/tabController/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface TabControllerProps {
* Pass for custom carousel page width
*/
carouselPageWidth?: number;
/**
* Send if a SafeView is used in the context of the TabController.
*/
useSafeArea?: boolean;
}
/**
* @description: A performant solution for a tab controller with lazy load mechanism
Expand All @@ -34,7 +38,7 @@ export interface TabControllerProps {
* @important: On Android, if using react-native-navigation, make sure to wrap your screen with gestureHandlerRootHOC
* @importantLink: https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html#with-wix-react-native-navigation-https-githubcom-wix-react-native-navigation
*/
declare function TabController({ initialIndex, selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, children }: PropsWithChildren<TabControllerProps>): JSX.Element;
declare function TabController({ initialIndex, selectedIndex, asCarousel, items, onChangeIndex, carouselPageWidth, useSafeArea, children }: PropsWithChildren<TabControllerProps>): JSX.Element;
declare namespace TabController {
var TabBar: React.ComponentClass<import("./TabBar").TabControllerBarProps & {
useCustomTheme?: boolean | undefined;
Expand Down
7 changes: 6 additions & 1 deletion src/components/tabController/PageCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const FIX_RTL = Constants.isRTL && Constants.isAndroid;
* @notes: You must pass `asCarousel` flag to TabController and render your TabPages inside a PageCarousel
*/
function PageCarousel(props: ScrollViewProps) {
const {onMomentumScrollEnd, ...others} = props;
const {onMomentumScrollEnd, style, ...others} = props;
const carousel = useAnimatedRef<Reanimated.ScrollView>();
const {
itemsCount,
Expand Down Expand Up @@ -96,9 +96,14 @@ function PageCarousel(props: ScrollViewProps) {
onMomentumScrollEnd?.(event);
}, [onMomentumScrollEnd]);

const _style = useMemo(() => {
return [{width: pageWidth}, style];
}, [pageWidth, style]);

return (
<Reanimated.ScrollView
{...others}
style={_style}
ref={carousel}
horizontal
pagingEnabled
Expand Down
22 changes: 18 additions & 4 deletions src/components/tabController/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export interface TabControllerBarProps {
* The indicator insets (default: Spacings.s4, set to 0 to make it wide as the item)
*/
indicatorInsets?: number;
/**
* Send to get a constant width of the indicator (overrides indicatorInsets)
*/
indicatorWidth?: number;
/**
* Additional styles for the container
*/
Expand Down Expand Up @@ -143,6 +147,7 @@ const TabBar = (props: Props) => {
centerSelected,
spreadItems,
indicatorInsets = Spacings.s4,
indicatorWidth,
containerStyle,
testID
} = props;
Expand Down Expand Up @@ -230,16 +235,25 @@ const TabBar = (props: Props) => {

const _indicatorTransitionStyle = useAnimatedStyle(() => {
const value = targetPage.value;
const width = interpolate(value,
itemsWidthsAnimated.value.map((_v: number, i: number) => i),
itemsWidthsAnimated.value.map((v: number) => v - 2 * indicatorInsets));
let width, marginHorizontal;
if (indicatorWidth) {
width = indicatorWidth;
marginHorizontal = interpolate(value,
itemsWidthsAnimated.value.map((_v: number, i: number) => i),
itemsWidthsAnimated.value.map((v: number) => (v - indicatorWidth) / 2));
} else {
marginHorizontal = indicatorInsets;
width = interpolate(value,
itemsWidthsAnimated.value.map((_v: number, i: number) => i),
itemsWidthsAnimated.value.map((v: number) => v - 2 * indicatorInsets));
}

const left = interpolate(value,
itemsOffsetsAnimated.value.map((_v: any, i: number) => i),
itemsOffsetsAnimated.value);

return {
marginHorizontal: indicatorInsets,
marginHorizontal,
width,
left
};
Expand Down
15 changes: 13 additions & 2 deletions src/components/tabController/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export interface TabControllerProps {
* Pass for custom carousel page width
*/
carouselPageWidth?: number;
/**
* Send if a SafeView is used in the context of the TabController.
*/
useSafeArea?: boolean;
}

/**
Expand All @@ -54,17 +58,24 @@ function TabController({
items,
onChangeIndex = _.noop,
carouselPageWidth,
useSafeArea,
children
}: PropsWithChildren<TabControllerProps>) {
const [screenWidth, setScreenWidth] = useState<number>(Constants.windowWidth);

const getScreenWidth = () => {
const {left, right} = Constants.getSafeAreaInsets();
return Constants.windowWidth - (useSafeArea && Constants.isIphoneX ? left + right : 0);
};

const [screenWidth, setScreenWidth] = useState<number>(getScreenWidth());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird the method getScreenWidth is above the useState.
I suggest moving the function out of the component code, nothing fancy, it can be top/bottom of the file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


if (items?.length < 2) {
console.warn('TabController component expect a minimum of 2 items');
}

useOrientation({
onOrientationChange: () => {
setScreenWidth(Constants.windowWidth);
setScreenWidth(getScreenWidth());
}
});

Expand Down