Configuration
To unlock more features and tailor Unistyles to your needs, you can configure it. The Unistyles configuration is divided into three parts:
- Themes
- Breakpoints
- Settings
Themes (Optional)
Section titled “Themes (Optional)”Themes is a JavaScript object where the keys represent unique theme names, and the values are the corresponding theme definitions. For more details, refer to the theming guide.
const lightTheme = { colors: { primary: '#ff1ff4', secondary: '#1ff4ff' // any nesting, spreading, arrays, etc. }, // functions, external imports, etc. gap: (v: number) => v * 8}
const otherTheme = { colors: { primary: '#aa12ff', secondary: 'pink' }, gap: (v: number) => v * 8}
const appThemes = { light: lightTheme, other: otherTheme}Breakpoints (Optional)
Section titled “Breakpoints (Optional)”Breakpoints is a JavaScript object where the keys are unique breakpoint names and the values are the corresponding breakpoint values (numbers). Be sure to register at least one breakpoint with a value of 0, as it’s required to simulate the cascading behavior of CSS media queries.
const breakpoints = { xs: 0, // <-- make sure to register one breakpoint with value 0 sm: 300, md: 500, lg: 800, xl: 1200 // use as many breakpoints as you need}Settings (Optional)
Section titled “Settings (Optional)”The Settings object has been simplified, and in the most recent version, it supports only four properties:
adaptiveThemes– a boolean that enables or disables adaptive themes learn moreinitialTheme– a string or a synchronous function that sets the initial themeCSSVars– a boolean that enables or disables web CSS variables (defaults totrue) learn morenativeBreakpointsMode- iOS/Android only. User preferred mode for breakpoints. Can be eitherpointsorpixels(defaults topixels) learn more
const settings = { initialTheme: 'light'}
// or with a synchronous functionconst settings = { initialTheme: () => { // get preferred theme from user's preferences/MMKV/SQL/StanJS etc.
return storage.getString('preferredTheme') ?? 'light' }}
// or with adaptive themesconst settings = { adaptiveThemes: true}TypeScript Types (Optional)
Section titled “TypeScript Types (Optional)”If your repository is using TypeScript, it is highly recommended to override the library types for optimal autocomplete and type safety regarding your themes and breakpoints:
type AppThemes = typeof appThemestype AppBreakpoints = typeof breakpoints
declare module 'react-native-unistyles' { export interface UnistylesThemes extends AppThemes {} export interface UnistylesBreakpoints extends AppBreakpoints {}}Set configuration
Section titled “Set configuration”The final step in the configuration is to set all the options by calling the StyleSheet.configure function:
import { StyleSheet } from 'react-native-unistyles'
StyleSheet.configure({ themes: appThemes, breakpoints, settings})That’s it! You can now use all the features of Unistyles in your project!
Full example
Section titled “Full example”import { StyleSheet } from 'react-native-unistyles'
const lightTheme = { colors: { primary: '#ff1ff4', secondary: '#1ff4ff' }, gap: (v: number) => v * 8}
const otherTheme = { colors: { primary: '#aa12ff', secondary: 'pink' }, gap: (v: number) => v * 8}
const appThemes = { light: lightTheme, other: otherTheme}
const breakpoints = { xs: 0, sm: 300, md: 500, lg: 800, xl: 1200}
type AppBreakpoints = typeof breakpointstype AppThemes = typeof appThemes
declare module 'react-native-unistyles' { export interface UnistylesThemes extends AppThemes {} export interface UnistylesBreakpoints extends AppBreakpoints {}}
StyleSheet.configure({ settings: { initialTheme: 'light', }, breakpoints, themes: appThemes})