Theming
Customize the look and feel of Crossbuild UI components to match your application's brand.
How Theming Works
Crossbuild UI uses a flexible theming system that allows you to override default styles, define custom color palettes, and adjust layout properties.
Projects Created with cbui-cli
If you initialized your project using cbui-cli init
, a theme configuration file named myCustomTheme.ts
is automatically created in your project's styles
folder (or a similar conventional location). You can directly edit this file to apply your custom theme.
Manual Configuration / Existing Projects
For existing projects or manual setups:
- Ensure you have the
@crossbuildui/core
package (or your main UI package) installed. You can install it using$ npm install @crossbuildui/core
- Create a new file, for example,
myCustomTheme.ts
, typically in asrc/styles
orstyles
directory. - In this file, import
AppConfig
anddefaultAppConfig
from@crossbuildui/core
. Define your custom theme by providing a partialAppConfig
object.
Here’s an example of how you can override parts of the default theme:
1
2import { AppConfig, defaultAppConfig } from '@crossbuildui/core';
3
4export const myCustomTheme: Partial<AppConfig> = {
5 themes: {
6 light: {
7 colors: {
8 primary: {
9 // ...defaultAppConfig.themes.light.colors.primary, // Optional: spread default if you only change some shades
10 '50': '#e6f7ff', // Lighter shade
11 '100': '#bae7ff',
12 '200': '#91d5ff',
13 '300': '#69c0ff',
14 '400': '#40a9ff',
15 '500': '#FF5733', // Your custom primary color
16 '600': '#d9482e',
17 '700': '#b33c28',
18 '800': '#8c2f21',
19 '900': '#66221a',
20 'foreground': '#FFFFFF', // Text color on primary background
21 'DEFAULT': '#FF5733', // Default primary color
22 },
23 // You can override other colors like secondary, success, etc.
24 },
25 },
26 dark: {
27 // Similar overrides for dark theme
28 colors: {
29 primary: {
30 '500': '#FF8C66', // A corresponding dark theme primary
31 'DEFAULT': '#FF8C66',
32 }
33 }
34 }
35 },
36 layout: {
37 borderRadius: {
38 sm: 6, // Custom small border radius
39 md: 10, // Custom medium border radius
40 // ... other layout properties
41 }
42 }
43};
44
You can override as little or as much as you need. The structure must follow the AppConfig
type. If you provide completely custom values, ensure they adhere to the expected format for colors (typically a scale from 50 to 900, plus DEFAULT and foreground) and layout properties.
After defining your theme, you'll need to provide it to the Crossbuild UI Provider at the root of your application. Refer to the Provider documentation for specific integration steps.
Usage
To apply your theme (either default or custom) throughout your application, you need to wrap your root component with the ThemeProvider
.
1. Wrap Your Application with ThemeProvider
In your main App.tsx
(or equivalent entry file):
1
2import React from 'react';
3import { ThemeProvider, View, Text, defaultAppConfig } from '@crossbuildui/core';
4// import { myCustomTheme } from './styles/myCustomTheme'; // Assuming your theme is here
5
6const App = () => {
7 // To use a custom theme, merge it with defaultAppConfig or provide it fully:
8 // const themeToUse = myCustomTheme; // If myCustomTheme is a complete AppConfig
9 // Or, for partial overrides:
10 // const themeToUse = { ...defaultAppConfig, ...myCustomTheme };
11
12 return (
13 // <ThemeProvider theme={themeToUse}> {/* With custom theme */}
14 <ThemeProvider> {/* With default theme */}
15 <MainScreen />
16 </ThemeProvider>
17 );
18};
19
20const MainScreen = () => {
21 // Your app's main screen content
22 return (
23 <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
24 <Text>Hello from CrossBuild UI!</Text>
25 </View>
26 );
27};
28
29export default App;
30
2. Using Themed Components and useTheme
Hook
Once the ThemeProvider
is set up, all Crossbuild UI components within its scope will automatically adapt to the active theme. You can also access theme properties and functions directly in your components using the useTheme
hook:
1
2import React from 'react';
3import { View, Text, Button, useTheme } from '@crossbuildui/core';
4
5const MyComponent = () => {
6 const { colors, mode, layout, toggleTheme, isDark } = useTheme();
7
8 return (
9 <View style={{ backgroundColor: colors.background, padding: layout.borderRadius.md }}>
10 <Text style={{ color: colors.primary.DEFAULT }}>Current mode: {mode}</Text>
11 <Text style={{ color: colors.foreground }}>Is Dark Mode: {isDark ? 'Yes' : 'No'}</Text>
12 <Button onPress={toggleTheme} title="Toggle Theme" />
13 </View>
14 );
15};
16
17export default MyComponent;
18
The useTheme
hook provides access to the current theme's colors
, active mode
(e.g., 'light' or 'dark'), layout
configurations, a boolean isDark
flag, and a toggleTheme
function to switch between light and dark modes.