native-recipe cookbook
Each recipe below is ready to copy into your app. Change the colors and
sizes to match your design. In an app with a theme, write each one with
the creators of
createThemedRecipes, and
replace the literal values with tokens read from themeToken (see
Theming with design tokens).
One element, so a style recipe. React Native has no inline box with text,
so the badge is a Text with a background. overflow: "hidden" lets
iOS round its corners.
import { createStyleRecipe } from "@lynstack/native-recipe";
// One element: a Text with a background.export const badge = createStyleRecipe({ base: { alignSelf: "flex-start", borderRadius: 999, fontWeight: "600", overflow: "hidden", }, variants: { tone: { neutral: { backgroundColor: "#f3f4f6", color: "#374151" }, success: { backgroundColor: "#dcfce7", color: "#166534" }, warning: { backgroundColor: "#fef9c3", color: "#854d0e" }, danger: { backgroundColor: "#fee2e2", color: "#991b1b" }, }, size: { sm: { fontSize: 11, paddingHorizontal: 6, paddingVertical: 1 }, md: { fontSize: 13, paddingHorizontal: 8, paddingVertical: 2 }, }, }, defaultVariants: { tone: "neutral", size: "md" },});<Text style={badge({ tone: "success" })}>Paid</Text>Button
Section titled “Button”A pressable and its label: two slots. disabled and fullWidth are
boolean variants. A compound variant gives a disabled ghost button a
border, so it stays visible.
import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const button = createSlotStyleRecipe({ slots: ["root", "label"], base: { root: { alignItems: "center", borderRadius: 8, flexDirection: "row", gap: 8, justifyContent: "center", }, label: { fontWeight: "600" }, }, variants: { tone: { primary: { root: { backgroundColor: "#2563eb" }, label: { color: "#ffffff" }, }, secondary: { root: { backgroundColor: "#f3f4f6" }, label: { color: "#111827" }, }, ghost: { root: { backgroundColor: "transparent" }, label: { color: "#2563eb" }, }, }, size: { sm: { root: { height: 32, paddingHorizontal: 12 }, label: { fontSize: 14 }, }, md: { root: { height: 40, paddingHorizontal: 16 }, label: { fontSize: 16 }, }, }, disabled: { true: { root: { opacity: 0.5 } } }, fullWidth: { true: { root: { alignSelf: "stretch" } } }, }, compoundVariants: [ // A ghost button gets a border when it is disabled, so it stays visible. { variants: { tone: "ghost", disabled: true }, styles: { root: { borderColor: "#d1d5db", borderWidth: 1 } }, }, ], defaultVariants: { tone: "primary", size: "md" },});import type { VariantsOf } from "@lynstack/native-recipe";import { Pressable, Text } from "react-native";
type ButtonProps = VariantsOf<typeof button> & { title: string; onPress: () => void; disabled?: boolean;};
export function Button({ title, onPress, ...variants }: ButtonProps) { const styles = button(variants); return ( <Pressable style={styles.root} onPress={onPress} disabled={variants.disabled} > <Text style={styles.label}>{title}</Text> </Pressable> );}disabled is both a variant of the recipe and a prop of Pressable, so
the component passes it to both. The variant also accepts the strings
"true" and "false", so the props type it as a boolean only:
disabled?: boolean.
A container, a title, a body, and a footer for actions. The slot names are your own; these follow the parts of the card.
import { createSlotStyleRecipe } from "@lynstack/native-recipe";
// The slot names are your own: name them after the parts of your component.export const card = createSlotStyleRecipe({ slots: ["container", "title", "body", "footer"], base: { container: { borderRadius: 12, gap: 8 }, title: { fontSize: 18, fontWeight: "600" }, body: { fontSize: 14, lineHeight: 20 }, footer: { flexDirection: "row", gap: 8, justifyContent: "flex-end" }, }, variants: { tone: { plain: { container: { backgroundColor: "#ffffff" }, title: { color: "#111827" }, body: { color: "#4b5563" }, }, muted: { container: { backgroundColor: "#f3f4f6" }, title: { color: "#111827" }, body: { color: "#6b7280" }, }, }, padding: { sm: { container: { padding: 12 } }, md: { container: { padding: 16 } }, }, elevated: { true: { container: { boxShadow: "0 1px 3px rgba(0, 0, 0, 0.12)", }, }, }, }, defaultVariants: { tone: "plain", padding: "md" },});const styles = card({ elevated: true });
<View style={styles.container}> <Text style={styles.title}>{title}</Text> <Text style={styles.body}>{body}</Text> <View style={styles.footer}>{actions}</View></View>;A label, a text input, and a hint or error message. A compound variant hides the error colors when the field is disabled, since the user cannot fix it.
import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const field = createSlotStyleRecipe({ slots: ["root", "label", "input", "hint"], base: { root: { gap: 4 }, label: { color: "#374151", fontSize: 14, fontWeight: "500" }, input: { borderColor: "#d1d5db", borderRadius: 6, borderWidth: 1, color: "#111827", paddingHorizontal: 12, }, hint: { color: "#6b7280", fontSize: 12 }, }, variants: { size: { sm: { input: { fontSize: 14, height: 32 } }, md: { input: { fontSize: 16, height: 40 } }, }, invalid: { true: { input: { borderColor: "#dc2626" }, hint: { color: "#dc2626" } }, }, disabled: { true: { input: { backgroundColor: "#f3f4f6", color: "#9ca3af" } }, }, }, compoundVariants: [ // A disabled field shows no error: the user cannot fix it. { variants: { invalid: true, disabled: true }, styles: { input: { borderColor: "#d1d5db" }, hint: { color: "#6b7280" } }, }, ], defaultVariants: { size: "md" },});import type { VariantsOf } from "@lynstack/native-recipe";import { Text, TextInput, View } from "react-native";
type FieldProps = VariantsOf<typeof field> & { disabled?: boolean; label: string; hint?: string; value: string; onChangeText: (text: string) => void;};
export function Field({ label, hint, value, onChangeText, ...variants}: FieldProps) { const styles = field(variants); return ( <View style={styles.root}> <Text style={styles.label}>{label}</Text> <TextInput style={styles.input} value={value} onChangeText={onChangeText} editable={!variants.disabled} /> {hint ? <Text style={styles.hint}>{hint}</Text> : null} </View> );}Dialog
Section titled “Dialog”An overlay, the content box, a title, a description, and the actions.
placement shows it in the center or as a bottom sheet. The compound
variant takes a list of sizes, so a bottom sheet spans the screen at
every size.
import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const dialog = createSlotStyleRecipe({ slots: ["overlay", "content", "title", "description", "actions"], base: { overlay: { backgroundColor: "rgba(0, 0, 0, 0.5)", flex: 1, padding: 16 }, content: { backgroundColor: "#ffffff", gap: 12, padding: 20 }, title: { color: "#111827", fontSize: 18, fontWeight: "600" }, description: { color: "#4b5563", fontSize: 14, lineHeight: 20 }, actions: { flexDirection: "row", gap: 8, justifyContent: "flex-end" }, }, variants: { placement: { center: { overlay: { justifyContent: "center" }, content: { borderRadius: 16 }, }, bottom: { overlay: { justifyContent: "flex-end", padding: 0 }, content: { borderTopLeftRadius: 16, borderTopRightRadius: 16 }, }, }, size: { sm: { content: { maxWidth: 360 } }, md: { content: { maxWidth: 480 } }, }, }, compoundVariants: [ // A bottom sheet spans the screen, whatever its size. { variants: { placement: "bottom", size: ["sm", "md"] }, styles: { content: { maxWidth: "100%" } }, }, ], defaultVariants: { placement: "center", size: "md" },});import { Modal, Text, View } from "react-native";
const styles = dialog({ placement: "bottom" });
<Modal transparent visible={visible} onRequestClose={onClose}> <View style={styles.overlay}> <View style={styles.content}> <Text style={styles.title}>Delete this file?</Text> <Text style={styles.description}>You cannot undo this.</Text> <View style={styles.actions}>{actions}</View> </View> </View></Modal>;Next steps
Section titled “Next steps”- Composing recipes shares the sizes of the button and the field in one recipe.
- Building components lets users override a style.
- Variants explains boolean and compound variants.