Skip to main content

Setup Theme Provider

FlexNative ThemeProvider from FlexNative is a component that makes the theme available throughout your app. It uses React's Context API.

info

If you are not familiar with React's Context we strongly suggest to follow this link before starting.

Installation

You can installing FlexNative ThemeProvider packages using npm:

npm i @flexnative/theme-context

Using ThemeProvider

ThemeProvider it is a Class React Component that let's passing/consuming theme from all components through the App. It accepts a value prop which is the data that will be shared with all components that are consumers of that context.

ThemeProvider wrap the top-level component of your application with a Provider component, thus making the context available to all its descendants.

ThemeProvider it comes with the base structure for implementing multiple themes and supporting dark mode.

CAUTION

ThemeProvider it supports dark mode depending on the device's color scheme. You also can implement you custom dark mode beheiver, for more details read section ThemeProvider.

To change default colors or add new colors fallow this link on how to change default colors or how to add custom colors.

To support dark mode fallow this link on how to implement dark mode using ThemeContext.

Examples

Add ThemeProvider from FlexNative to the root of your app and update App.js as follows examples.

import ThemeContext, { light } from '@flexnative/theme-context';

function App() {
return (
<ThemeContext.Provider value={{colors: themeColors}}>
<YourAPP />
</ThemeContext.Provider>
);
}

ThemeProviderProps

NameTypeRequiredDefault ValueDescription
colorsBaseTheme<TColors>truenullWhere TColors is the object with custom colors.
colorSchemeColorSchemeNamefalsenullValue indicates the current user preferred color scheme.

BaseTheme

NameTypeRequiredLight ValueDark Value
backgroundColorValuetrue #f8f8f8 #000000
defaultColorValuetrue #f5f5f5 #404040
cardColorValuetrue #ffffff #232323
textColorValuetrue #424242 #ffffff
borderColorValuetrue #42424226 #ffffff1A
placeholderColorValuetrue #0000001A #FFFFFF1A
infoColorValuetrue #3e80ed #3e80ed
successColorValuetrue #5ec232 #5ec232
warningColorValuetrue #ffc107 #ffc107
errorColorValuetrue #d51923 #d51923
darkColorValuetrue #424242 #424242
secondaryColorValuetrue #666666 #5C5C5C
lightColorValuetrue #ebebeb #ebebeb
primaryColorValuetrue #ff6358 #ff6358

Using ThemeContext

ThemeContext it expects as value an object of type ThemeProviderProps<TColors>. You can use one of default themes light or dark from @flexnative/theme-context or you can create your own theme as long you implements ThemeBase object.

To change default colors or add new colors fallow this link on how to change default colors or how to add custom colors.

To support dark mode fallow this link on how to implement dark mode using ThemeContext.

ThemeContextProps

NameTypeRequiredDefault ValueDescription
colorsBaseTheme<TColors>trueBaseThemeWhere TColors object with custom colors
colorSchemeColorSchemeNamefalseDevice ColorSchemeColorSchemeName from react-native
setTheme(theme: BaseTheme & TColors) => voidfalsenullvoid function for change all theme colors. It expect as parameter object of BaseTheme<TColors>
setColorScheme(colorScheme: ColorSchemeName) => voidfalsenullvoid function for change schema (light/dark). It expect as parameter type of ColorSchemeName

BaseTheme<TColors>

BaseTheme and TColors custom extendet colors.

Examples

Example of consuming Theme Context.

import { useThemeContext } from '@flexnative/theme-context';
import ThemeContext from '@flexnative/theme-context';

function App() {
const theme = useThemeContext();

return (
<Text style={{ color: theme.colors.text}}>Example of consuming Theme Context.</Text>
);
}