-
Use
ThemeV2
andThemeV3
. CurrentlyTheme
is pointing toThemeV2
, and later it will point toThemeV3
. During the v2 -> v3 migration, the more explicit we are, the better. -
Use
useTheme
to get access tocolor
andspace
. We used to usecolor
andspace
imported frompalette
directly. Unforunately, this is not theme-aware, so we need to change our ways. Always useconst { color, space } = useTheme()
orconst color = useColor()
orconst space = useSpace()
, to get the theme-aware functionality we need. -
Use
themeGet
for styled-components, but try to avoid styled-components. If you need to access anything from the theme within a styled-component, you should usethemeGet
like:
import { themeGet } from "@styled-system/theme-get"
const TheComp = styled(Flex)`
background-color: ${themeGet("colors.black100")};
margin-left: ${themeGet("space.2")}px;
border-width: 3;
`
One limitation here is that you cannot use 0.5
or any other value of space
that has a dot in the name. Prefer to use simple components instead, like:
const TheComp = (props) => {
const { color, space } = useTheme()
return (
<Flex
{...props}
style={[
props.style,
{
backgroundColor: color("black100"),
marginLeft: space("0.5"),
borderWidth: 3,
},
]}
/>
)
}
- Use
useThemeConfig
to make components behave correctly in both palette versions. When a component has to be presented in both palette versions but use different styling in the two versions, we can useuseThemeConfig
. For example, if we have a view that needs to have ared
border inv2
and ablue
border inv3
, we do:
const TheView = () => {
const theBorderColor = useThemeConfig({
v2: "red",
v3: "blue",
})
return <View style={{ borderColor: theBorderColor, borderWidth: 1 }} />
}
- Use
ClassTheme
for class components.ClassTheme
exposes the hooks of the theme, so you can use it like:
import { ClassTheme } from "palette"
class TheComp extends React.Component {
render() {
return (
<ClassTheme theme="v2">
{({color, space, theme}) => (
<View style={{ backgroundColor: color("white100") }}>
)}
</ClassTheme>
)
}
}