ba-mode
Sets the colour mode for a React Native app. Light mode uses the white theme. Dark mode uses midnight-700
Airframe
Web Components
React
React Native
Parent components
Wrap the app in SafeAreaProvider from react-native-safe-area-context. Put BaModeProvider inside it, once, around every screen.
BaModeProvider and useBaMode are in src/theme/BaModeContext.tsx.
Usage
BaModeProvider sets the colour mode for the app.
Light mode uses the default theme (white). Dark mode uses midnight-700. A missing device colour scheme is treated as light.
<BaScreen> and <BaPageSegment> use that theme when they omit theme. Pass theme on a screen or segment to keep that surface on one palette. theme="default" is the white palette in light and dark mode.
BaText, BaButton, and other children read the nearest theme. A cabin, sale, or club theme stays as you set it. With no BaModeProvider, an omitted theme is default.
preference is system, light, or dark. It defaults to system, which follows the device through useColorScheme. light and dark force a mode.
Omit preference when the provider should hold the choice for this session. It starts at system. Call setPreference from useBaMode to change it.
Pass preference and onPreferenceChange when the app stores the choice across launches. Save that value and pass it back in on the next launch.
useBaMode() returns preference, mode (light or dark), theme, and setPreference. It throws outside BaModeProvider.
Default provider
Omit preference. The provider starts at system and follows the device. Put each screen in <BaScreen>, then <BaPageSegment> and <BaContent>. When the page scrolls, put <BaScroll> between the screen and the segments.
<SafeAreaProvider>
<BaModeProvider>
<BaScreen>
<BaPageSegment>
<BaContent>
<BaText variant="header28">Hello</BaText>
</BaContent>
</BaPageSegment>
</BaScreen>
</BaModeProvider>
</SafeAreaProvider>
Settings screen
Pass preference and onPreferenceChange when the app stores the choice. setPreference calls onPreferenceChange with the next value. Pass that value back through preference.
import { useState } from "react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import {
BaButton,
BaContent,
BaModeProvider,
BaPageSegment,
BaScreen,
BaText,
useBaMode,
type BaColorModePreference,
} from "@britishairways-ent/bagel-react-native";
function Settings() {
const { setPreference } = useBaMode();
return (
<BaScreen>
<BaPageSegment>
<BaContent>
<BaText variant="header28">Appearance</BaText>
<BaButton onPress={() => setPreference("light")}>Light</BaButton>
<BaButton onPress={() => setPreference("dark")}>Dark</BaButton>
<BaButton onPress={() => setPreference("system")}>Match device</BaButton>
</BaContent>
</BaPageSegment>
</BaScreen>
);
}
export default function App() {
const [preference, setPreference] = useState<BaColorModePreference>("system");
return (
<SafeAreaProvider>
<BaModeProvider
preference={preference}
onPreferenceChange={setPreference}
>
<Settings />
</BaModeProvider>
</SafeAreaProvider>
);
}
Expo userInterfaceStyle
Set userInterfaceStyle to automatic in the Expo config. light keeps iOS in light mode, so the provider stays on the white theme.
{
"expo": {
"userInterfaceStyle": "automatic"
}
}
Guidelines
High contrast is separate from colour mode. Increase Contrast on iOS, and High contrast text on Android, leave preference, mode, and the theme as they are.
Light mode stays on default. Dark mode stays on midnight-700. A screen or segment that sets theme keeps that palette.
Greyscale and Invert colours are painted by the system over the whole screen. BaModeProvider does not choose those colours.
A component that has its own high contrast treatment, such as BaButton, still applies it inside the provider.
Keyboard navigation
State: BaModeProvider is not a focusable element
Focus moves through the controls rendered inside the provider