Setup
RC
This project is currently in its Release Candidate phase. While it hasn’t reached version 1.0.0 yet, it’s been tested and proven in a large-scale application, performing flawlessly across hundreds screens and components.
I’m looking for testers to check the typings, scalability and overall usability for your monorepo projects.
Suggestions, ideas, and potential improvements are always welcome!
Install library yarn add react - native - unistyles@rc
Define your theme You don’t have to follow a specific format. Just make an object and add any keys/values you like.
// theme.ts
export const theme = {
colors : {
blood : '#eb4d4b' ,
barbie : '#e056fd' ,
pumpkin : '#f0932b' ,
background : '#ffffff'
} ,
margins : {
sm : 2 ,
md : 4 ,
lg : 8 ,
xl : 12
}
}
or something more advanced with nested objects / functions:
// theme.ts
export const theme = {
colors : {
blood : '#eb4d4b' ,
barbie : '#e056fd' ,
pumpkin : '#f0932b' ,
background : '#ffffff'
} ,
components : {
typography : {
bold : {
fontWeight : 'bold'
} ,
thin : {
fontWeight : '300'
}
}
} ,
margins : {
sm : 2 ,
md : 4 ,
lg : 8 ,
xl : 12
} ,
utils : {
hexToRGBA : (hex : string , opacity : number ) => {
const rgb = hex
.replace ( '#' , '' )
.split ( /(?=(?:..) *$ )/ )
.map (x => parseInt (x , 16 ))
return `rgba( ${ rgb .at ( 0 ) } , ${ rgb .at ( 1 ) } , ${ rgb .at ( 2 ) } , ${ opacity } )`
}
}
}
Create breakpoints There are no predefined breakpoints. You can name them anything. Just make an object with string keys and number values.
// breakpoints.ts
export const breakpoints = {
xs : 0 ,
sm : 576 ,
md : 768 ,
lg : 992 ,
xl : 1200 ,
superLarge : 2000 ,
tvLike : 4000
}
Wrap your app with UnistylesTheme to inject theme import React from 'react'
import { UnistylesTheme } from 'react-native-unistyles'
import { theme } from './theme'
export const App : React . FunctionComponent = () => (
< UnistylesTheme theme = {theme}>
// Your App
</ UnistylesTheme >
)
Access createStyleSheet and useStyles with a factory // styles.ts
// import library factory
import { createUnistyles } from 'react-native-unistyles'
// import your breakpoints, add whatever keys and numeric values you want
import { breakpoints } from './breakpoints'
// import your app's theme TypeScript type, or simply use 'typeof theme'
import { theme } from './theme'
export const {
createStyleSheet ,
useStyles ,
} = createUnistyles < typeof breakpoints , typeof theme>(breakpoints)