Skip to content
Open
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
5 changes: 5 additions & 0 deletions apps/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SafeAreaProvider } from 'react-native-safe-area-context';
import JSBottomTabs from './Examples/JSBottomTabs';
import ThreeTabs from './Examples/ThreeTabs';
import FourTabs from './Examples/FourTabs';
import FourTabsRTL from './Examples/FourTabsRTL';
import MaterialBottomTabs from './Examples/MaterialBottomTabs';
import SFSymbols from './Examples/SFSymbols';
import LabeledTabs from './Examples/Labeled';
Expand Down Expand Up @@ -72,6 +73,9 @@ const FourTabsActiveIndicatorColor = () => {
const UnlabeledTabs = () => {
return <LabeledTabs showLabels={false} />;
};
const FourTabsRightToLeft = () => {
return <FourTabsRTL layoutDirection={'rtl'} />;
};

const examples = [
{
Expand Down Expand Up @@ -161,6 +165,7 @@ const examples = [
name: 'Bottom Accessory View',
screenOptions: { headerShown: false },
},
{ component: FourTabsRightToLeft, name: 'Four Tabs - RTL' },
];

function App() {
Expand Down
95 changes: 95 additions & 0 deletions apps/example/src/Examples/FourTabsRTL.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import TabView, { SceneMap } from 'react-native-bottom-tabs';
import React from 'react';
import { Article } from '../Screens/Article';
import { Albums } from '../Screens/Albums';
import { Contacts } from '../Screens/Contacts';
import { Chat } from '../Screens/Chat';
import { I18nManager, type ColorValue } from 'react-native';
import type { LayoutDirection } from 'react-native-bottom-tabs/src/types';

interface Props {
disablePageAnimations?: boolean;
scrollEdgeAppearance?: 'default' | 'opaque' | 'transparent';
backgroundColor?: ColorValue;
translucent?: boolean;
hideOneTab?: boolean;
rippleColor?: ColorValue;
activeIndicatorColor?: ColorValue;
layoutDirection?: LayoutDirection;
}

const renderScene = SceneMap({
article: Article,
albums: Albums,
contacts: Contacts,
chat: Chat,
});

export default function FourTabsRTL({
disablePageAnimations = false,
scrollEdgeAppearance = 'default',
backgroundColor,
translucent = true,
hideOneTab = false,
rippleColor,
activeIndicatorColor,
layoutDirection = 'locale',
}: Props) {
React.useLayoutEffect(() => {
if (layoutDirection === 'rtl') {
I18nManager.allowRTL(true);
I18nManager.forceRTL(true);
}
return () => {
if (layoutDirection === 'rtl') {
I18nManager.allowRTL(false);
I18nManager.forceRTL(false);
}
};
}, [layoutDirection]);
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{
key: 'article',
title: 'المقالات',
focusedIcon: require('../../assets/icons/article_dark.png'),
unfocusedIcon: require('../../assets/icons/chat_dark.png'),
badge: '!',
},
{
key: 'albums',
title: 'البومات',
focusedIcon: require('../../assets/icons/grid_dark.png'),
badge: '5',
hidden: hideOneTab,
},
{
key: 'contacts',
focusedIcon: require('../../assets/icons/person_dark.png'),
title: 'المتراسلين',
badge: ' ',
},
{
key: 'chat',
focusedIcon: require('../../assets/icons/chat_dark.png'),
title: 'المحادثات',
role: 'search',
},
]);

return (
<TabView
sidebarAdaptable
disablePageAnimations={disablePageAnimations}
scrollEdgeAppearance={scrollEdgeAppearance}
navigationState={{ index, routes }}
onIndexChange={setIndex}
renderScene={renderScene}
tabBarStyle={{ backgroundColor }}
translucent={translucent}
rippleColor={rippleColor}
activeIndicatorColor={activeIndicatorColor}
layoutDirection={layoutDirection}
/>
);
}
1 change: 1 addition & 0 deletions apps/example/src/Examples/NativeBottomTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function NativeBottomTabs() {
initialRouteName="Chat"
labeled={true}
hapticFeedbackEnabled={false}
layoutDirection="leftToRight"
tabBarInactiveTintColor="#C57B57"
tabBarActiveTintColor="#F7DBA7"
tabBarStyle={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class ReactBottomNavigationView(context: Context) : LinearLayout(context) {
layout(left, top, right, bottom)
}

fun applyDirection(dir: Int) {
bottomNavigation.layoutDirection = dir
}

override fun requestLayout() {
super.requestLayout()
@Suppress("SENSELESS_COMPARISON") // layoutCallback can be null here since this method can be called in init
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ class RCTTabViewManager(context: ReactApplicationContext) :
view.isHapticFeedbackEnabled = value
}

override fun setLayoutDirection(view: ReactBottomNavigationView, value: String?) {
val direction = when (value) {
"rtl" -> View.LAYOUT_DIRECTION_RTL
"ltr" -> View.LAYOUT_DIRECTION_LTR
else -> View.LAYOUT_DIRECTION_LOCALE
}
view.applyDirection(direction)
}

override fun setFontFamily(view: ReactBottomNavigationView?, value: String?) {
view?.setFontFamily(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
_tabViewProvider.hapticFeedbackEnabled = newViewProps.hapticFeedbackEnabled;
}

if (oldViewProps.layoutDirection != newViewProps.layoutDirection) {
_tabViewProvider.layoutDirection = RCTNSStringFromStringNilIfEmpty(newViewProps.layoutDirection);
}

if (oldViewProps.fontSize != newViewProps.fontSize) {
_tabViewProvider.fontSize = [NSNumber numberWithInt:newViewProps.fontSize];
}
Expand Down
11 changes: 11 additions & 0 deletions packages/react-native-bottom-tabs/ios/TabView/NewTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ struct NewTabView: AnyTabView {

@ViewBuilder
var body: some View {
var effectiveLayoutDirection: LayoutDirection {
let dir = props.layoutDirection ?? "locale"
if let mapped = ["rtl": LayoutDirection.rightToLeft,
"ltr": LayoutDirection.leftToRight][dir] {
return mapped
}
let system = UIView.userInterfaceLayoutDirection(for: .unspecified)
return system == .rightToLeft ? .rightToLeft : .leftToRight
}

TabView(selection: $props.selectedPage) {
ForEach(props.children) { child in
if let index = props.children.firstIndex(of: child),
Expand Down Expand Up @@ -49,6 +59,7 @@ struct NewTabView: AnyTabView {
}
}
}
.environment(\.layoutDirection, effectiveLayoutDirection)
.measureView { size in
onLayout(size)
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-bottom-tabs/ios/TabViewProps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class TabViewProps: ObservableObject {
@Published var translucent: Bool = true
@Published var disablePageAnimations: Bool = false
@Published var hapticFeedbackEnabled: Bool = false
@Published var layoutDirection: String?
@Published var fontSize: Int?
@Published var fontFamily: String?
@Published var fontWeight: String?
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-bottom-tabs/ios/TabViewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public final class TabInfo: NSObject {
}
}

@objc public var layoutDirection: NSString? {
didSet {
props.layoutDirection = layoutDirection as? String
}
}
@objc public var scrollEdgeAppearance: NSString? {
didSet {
props.scrollEdgeAppearance = scrollEdgeAppearance as? String
Expand Down
15 changes: 14 additions & 1 deletion packages/react-native-bottom-tabs/src/TabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ import { BottomTabBarHeightContext } from './utils/BottomTabBarHeightContext';
import type { ImageSource } from 'react-native/Libraries/Image/ImageSource';
import NativeTabView from './TabViewNativeComponent';
import useLatestCallback from 'use-latest-callback';
import type { AppleIcon, BaseRoute, NavigationState, TabRole } from './types';
import type {
AppleIcon,
BaseRoute,
LayoutDirection,
NavigationState,
TabRole,
} from './types';
import DelayedFreeze from './DelayedFreeze';
import {
BottomAccessoryView,
Expand Down Expand Up @@ -201,6 +207,11 @@ interface Props<Route extends BaseRoute> {
* @platform ios
*/
renderBottomAccessoryView?: BottomAccessoryViewProps['renderBottomAccessoryView'];
/**
* The direction of the layout.
* @default 'locale'
*/
layoutDirection?: LayoutDirection;
}

const ANDROID_MAX_TABS = 100;
Expand Down Expand Up @@ -239,6 +250,7 @@ const TabView = <Route extends BaseRoute>({
tabBarStyle,
tabLabelStyle,
renderBottomAccessoryView,
layoutDirection = 'locale',
...props
}: Props<Route>) => {
// @ts-ignore
Expand Down Expand Up @@ -398,6 +410,7 @@ const TabView = <Route extends BaseRoute>({
onTabBarMeasured={handleTabBarMeasured}
onNativeLayout={handleNativeLayout}
hapticFeedbackEnabled={hapticFeedbackEnabled}
layoutDirection={layoutDirection}
activeTintColor={activeTintColor}
inactiveTintColor={inactiveTintColor}
barTintColor={tabBarStyle?.backgroundColor}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface TabViewProps extends ViewProps {
disablePageAnimations?: boolean;
activeIndicatorColor?: ColorValue;
hapticFeedbackEnabled?: boolean;
layoutDirection?: string;
minimizeBehavior?: string;
fontFamily?: string;
fontWeight?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-bottom-tabs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type AppleIcon = { sfSymbol: SFSymbol };

export type TabRole = 'search';

export type LayoutDirection = 'ltr' | 'rtl' | 'locale';

export type BaseRoute = {
key: string;
title?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function NativeBottomTabNavigator({
screenOptions,
tabBarActiveTintColor: customActiveTintColor,
tabBarInactiveTintColor: customInactiveTintColor,
layoutDirection = 'locale',
...rest
}: NativeBottomTabNavigatorProps) {
const { colors } = useTheme();
Expand Down Expand Up @@ -77,6 +78,7 @@ function NativeBottomTabNavigator({
<NavigationContent>
<NativeBottomTabView
{...rest}
layoutDirection={layoutDirection}
tabBarActiveTintColor={activeTintColor}
tabBarInactiveTintColor={inactiveTintColor}
state={state}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-navigation/src/views/NativeBottomTabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Props = NativeBottomTabNavigationConfig & {

export default function NativeBottomTabView({
state,
layoutDirection,
navigation,
descriptors,
tabBar,
Expand Down Expand Up @@ -114,6 +115,7 @@ export default function NativeBottomTabView({
});
}
}}
layoutDirection={layoutDirection}
/>
);
}