Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
feat: add ability to render label beside the icon (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitdion authored and satya164 committed Aug 18, 2019
1 parent d0e43bd commit 8f70ebb
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions packages/bottom-tabs/src/views/BottomTabBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import { SafeAreaView } from '@react-navigation/native';
import CrossFadeIcon from './CrossFadeIcon';
import withDimensions from '../utils/withDimensions';

type Orientation = 'horizontal' | 'vertical';
type Position = 'beside-icon' | 'below-icon';
type LabelPosition =
| Position
| ((options: { deviceOrientation: Orientation }) => Position);

export type TabBarOptions = {
keyboardHidesTabBar: boolean,
activeTintColor?: string,
Expand All @@ -25,6 +31,7 @@ export type TabBarOptions = {
showIcon: boolean,
labelStyle: any,
tabStyle: any,
labelPosition?: LabelPosition,
adaptive?: boolean,
style: any,
};
Expand Down Expand Up @@ -174,6 +181,7 @@ class TabBarBottom extends React.Component<Props, State> {
}

const label = this.props.getLabelText({ route });
const horizontal = this._shouldUseHorizontalLabels();
const tintColor = focused ? activeTintColor : inactiveTintColor;

if (typeof label === 'string') {
Expand All @@ -183,9 +191,7 @@ class TabBarBottom extends React.Component<Props, State> {
style={[
styles.label,
{ color: tintColor },
showIcon && this._shouldUseHorizontalLabels()
? styles.labelBeside
: styles.labelBeneath,
showIcon && horizontal ? styles.labelBeside : styles.labelBeneath,
labelStyle,
]}
allowFontScaling={allowFontScaling}
Expand All @@ -196,7 +202,12 @@ class TabBarBottom extends React.Component<Props, State> {
}

if (typeof label === 'function') {
return label({ route, focused, tintColor });
return label({
route,
focused,
tintColor,
orientation: horizontal ? 'horizontal' : 'vertical',
});
}

return label;
Expand Down Expand Up @@ -241,7 +252,28 @@ class TabBarBottom extends React.Component<Props, State> {

_shouldUseHorizontalLabels = () => {
const { routes } = this.props.navigation.state;
const { isLandscape, dimensions, adaptive, tabStyle } = this.props;
const {
isLandscape,
dimensions,
adaptive,
tabStyle,
labelPosition,
} = this.props;

if (labelPosition) {
let position;
if (typeof labelPosition === 'string') {
position = labelPosition;
} else {
position = labelPosition({
deviceOrientation: isLandscape ? 'horizontal' : 'vertical',
});
}

if (position) {
return position === 'beside-icon';
}
}

if (!adaptive) {
return false;
Expand Down

0 comments on commit 8f70ebb

Please sign in to comment.