Build a themed screen
This tutorial adds a real theme contract and uses runtime token resolution in a screen component.
1. Define a theme contract
Section titled “1. Define a theme contract”Save this module as theme.css.ts so Metro emits theme data without bundling authoring code.
import { createTheme, createThemeContract } from "@opsydyn/crumbs-css/style"
export const vars = createThemeContract({ color: { canvas: null, text: null, }, space: { screen: null, },})
export const darkTheme = createTheme(vars, { color: { canvas: "#0a0a0a", text: "#f5f5f5", }, space: { screen: 24, },})2. Use the contract in .css.ts
Section titled “2. Use the contract in .css.ts”import { style } from "@opsydyn/crumbs-css/style"import { vars } from "./theme.css"
export const screen = style({ backgroundColor: vars.color.canvas, flex: 1, justifyContent: "center", padding: vars.space.screen,})
export const title = style({ color: vars.color.text, fontSize: 24, fontWeight: 700,})3. Provide the theme
Section titled “3. Provide the theme”import { ThemeProvider } from "@opsydyn/crumbs-css/theme/core"import { darkTheme } from "./theme.css"
export function App({ children }) { return <ThemeProvider theme={darkTheme}>{children}</ThemeProvider>}4. Resolve the themed styles
Section titled “4. Resolve the themed styles”import { Text, View } from "react-native"import { useThemedStyles } from "@opsydyn/crumbs-css/theme/core"import * as s from "./screen.css"
export function Screen() { const styles = useThemedStyles(s)
return ( <View style={styles.screen}> <Text style={styles.title}>Themed screen</Text> </View> )}What you learned
Section titled “What you learned”createThemeContractdefines the shape and token namescreateThemecreates concrete valuesThemeProviderexposes the active themeuseThemedStylesresolves token references before render
Next: