Skip to content

Quick start with native-recipe

This page styles a badge, a button, and a card. Install the package first (see Installation).

A recipe is a function made from your style config. createStyleRecipe takes the styles of one element:

  • base: the style it always has.
  • variants: for each variant, a prop such as tone, the style of each option, a value such as "success".
  • compoundVariants: styles for combinations of options.
  • defaultVariants: the option each variant uses when you leave it out.

Create it once, at the top level of a module, as you would call StyleSheet.create:

import { createStyleRecipe } from "@lynstack/native-recipe";
export const badge = createStyleRecipe({
base: { alignSelf: "flex-start", borderRadius: 999, paddingHorizontal: 8 },
variants: {
tone: {
neutral: { backgroundColor: "#f3f4f6" },
success: { backgroundColor: "#dcfce7" },
danger: { backgroundColor: "#fee2e2" },
},
size: {
sm: { height: 20 },
md: { height: 24 },
},
outlined: {
true: { borderColor: "#d1d5db", borderWidth: 1 },
},
},
compoundVariants: [
{
variants: { tone: "danger", outlined: true },
style: { borderColor: "#dc2626" },
},
],
defaultVariants: { tone: "neutral", size: "md" },
});

Call the recipe with a selection, the variants you choose. It returns the style:

badge()→ {    alignSelf: "flex-start",    borderRadius: 999,    paddingHorizontal: 8,    backgroundColor: "#f3f4f6",    height: 24,  }badge({ tone: "success", size: "sm" })→ {    alignSelf: "flex-start",    borderRadius: 999,    paddingHorizontal: 8,    backgroundColor: "#dcfce7",    height: 20,  }badge({ tone: "danger", outlined: true })→ {    alignSelf: "flex-start",    borderRadius: 999,    paddingHorizontal: 8,    backgroundColor: "#fee2e2",    height: 24,    borderColor: "#dc2626",    borderWidth: 1,  }
  • tone and size have defaults, so you can leave them out.
  • outlined has only a true option. That makes it a boolean variant. You can leave it out, and then it is false.
  • The compound variant adds its style when tone is danger and outlined is true. It applies after the styles of the variants, so its border color wins.

Calling it again with the same variants returns the same frozen object:

badge({ tone: "success" }) === badge({ tone: "success" }); // => true

VariantsOf types a component’s props from its recipe. Pass the style to the style prop:

import type { ReactNode } from "react";
import type { VariantsOf } from "@lynstack/native-recipe";
import { View } from "react-native";
type BadgeProps = VariantsOf<typeof badge> & { children: ReactNode };
export function Badge({ children, ...variants }: BadgeProps) {
return <View style={badge(variants)}>{children}</View>;
}

A recipe reads only its variants, so it can take every prop of the component at once. See Building components for style overrides and memoized components.

4. Style several elements with a slot recipe

Section titled “4. Style several elements with a slot recipe”

React Native styles do not cascade, so a component of several elements, such as a button and its label, needs a style for each. A slot recipe returns one style for each element, its slots:

import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const button = createSlotStyleRecipe({
slots: ["root", "label"],
base: {
root: { alignItems: "center", borderRadius: 8, justifyContent: "center" },
label: { fontWeight: "600" },
},
variants: {
tone: {
primary: {
root: { backgroundColor: "#2563eb" },
label: { color: "#ffffff" },
},
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 },
},
},
},
defaultVariants: { tone: "primary", size: "md" },
});
button({ size: "sm" })→ {    root: {      alignItems: "center",      borderRadius: 8,      justifyContent: "center",      backgroundColor: "#2563eb",      height: 32,      paddingHorizontal: 12,    },    label: { fontWeight: "600", color: "#ffffff", fontSize: 14 },  }
import type { VariantsOf } from "@lynstack/native-recipe";
import { Pressable, Text } from "react-native";
type ButtonProps = VariantsOf<typeof button> & {
title: string;
onPress: () => void;
};
export function Button({ title, onPress, ...variants }: ButtonProps) {
const styles = button(variants);
return (
<Pressable style={styles.root} onPress={onPress}>
<Text style={styles.label}>{title}</Text>
</Pressable>
);
}

The slot names are your own. root and label are only names: name the slots after the parts of your component. A card can have a container, a title, and a body:

import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const card = createSlotStyleRecipe({
// Your own names, one for each element of the card.
slots: ["container", "title", "body"],
base: {
container: { borderRadius: 12, gap: 4, padding: 16 },
title: { fontSize: 18, fontWeight: "600" },
body: { fontSize: 14 },
},
variants: {
tone: {
light: {
container: { backgroundColor: "#ffffff" },
title: { color: "#111827" },
body: { color: "#4b5563" },
},
inverted: {
container: { backgroundColor: "#111827" },
title: { color: "#ffffff" },
body: { color: "#d1d5db" },
},
},
},
defaultVariants: { tone: "light" },
});
card({ tone: "inverted" })→ {    container: {      borderRadius: 12,      gap: 4,      padding: 16,      backgroundColor: "#111827",    },    title: { fontSize: 18, fontWeight: "600", color: "#ffffff" },    body: { fontSize: 14, color: "#d1d5db" },  }
import type { ReactNode } from "react";
import type { VariantsOf } from "@lynstack/native-recipe";
import { Text, View } from "react-native";
type CardProps = VariantsOf<typeof card> & {
title: string;
children: ReactNode;
};
export function Card({ title, children, ...variants }: CardProps) {
const styles = card(variants);
return (
<View style={styles.container}>
<Text style={styles.title}>{title}</Text>
<Text style={styles.body}>{children}</Text>
</View>
);
}

See createSlotStyleRecipe.

The colors above are written in each recipe. A design system keeps them in a theme instead, with one theme for light and one for dark. createThemedRecipes returns recipe creators and themeToken. A config is a function that reads tokens from themeToken, and the recipe takes the theme first, then the variants:

import { createThemedRecipes } from "@lynstack/native-recipe";
interface Theme {
readonly colors: { readonly primary: string; readonly onPrimary: string };
readonly radius: { readonly md: number };
}
const { createSlotStyleRecipe, themeToken } = createThemedRecipes<Theme>();
export const themedButton = createSlotStyleRecipe(() => ({
slots: ["root", "label"],
base: {
root: {
borderRadius: themeToken.radius.md,
backgroundColor: themeToken.colors.primary,
},
label: { color: themeToken.colors.onPrimary },
},
// Required, even when a recipe has no variants: then write `variants: {}`.
variants: {
size: {
sm: { root: { height: 32 }, label: { fontSize: 14 } },
md: { root: { height: 40 }, label: { fontSize: 16 } },
},
},
defaultVariants: { size: "md" },
}));
const light: Theme = {
colors: { primary: "#2563eb", onPrimary: "#ffffff" },
radius: { md: 8 },
};
// The theme first, then the variants.
const styles = themedButton(light, { size: "sm" });
styles.root; // borderRadius 8, the light primary color, and height 32