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

Add ability to customize whether label should be rendered beneath or beside icon #103

Merged
merged 2 commits into from
Apr 1, 2019
Merged
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
42 changes: 37 additions & 5 deletions 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 @@ -176,6 +183,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 @@ -185,9 +193,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 @@ -198,7 +204,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 @@ -243,7 +254,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