Build your first button recipe
This tutorial creates a reusable button using recipe and createRecipeResolver.
1. Define the recipe
Section titled “1. Define the recipe”import { recipe } from "@opsydyn/crumbs-css/style"
export const button = recipe({ base: { alignItems: "center", borderRadius: 8, padding: 12, }, defaultVariants: { pressed: false, tone: "primary", }, variants: { pressed: { false: {}, true: { opacity: 0.82 }, }, tone: { primary: { backgroundColor: "#c8aa6e" }, danger: { backgroundColor: "#9f2f25" }, }, },})2. Resolve the themed recipe once
Section titled “2. Resolve the themed recipe once”import { createRecipeResolver } from "@opsydyn/crumbs-css/theme"import { useThemedStyles } from "@opsydyn/crumbs-css/theme/core"import { useMemo } from "react"import * as s from "./button.css"
export function Button() { const styles = useThemedStyles(s) const resolveButtonStyle = useMemo(() => createRecipeResolver(styles.button), [styles.button])
return null}3. Select state from component code
Section titled “3. Select state from component code”import { Pressable } from "react-native"
export function Button() { const styles = useThemedStyles(s) const resolveButtonStyle = useMemo(() => createRecipeResolver(styles.button), [styles.button])
return <Pressable style={({ pressed }) => resolveButtonStyle({ pressed, tone: "primary" })} />}Why this pattern matters
Section titled “Why this pattern matters”React Native has no CSS pseudo-class selector matching. recipe keeps the state model explicit and makes merge order predictable:
- base
- selected variants in author-defined order
- matching compound variants
Next: