Skip to content

createSlotStyleRecipe

createSlotStyleRecipe creates a slot style recipe, which returns the style of each element of a component, its slots, for a selection of variants. React Native styles do not cascade, so a component of several elements, such as a button with a label, needs a style for each. The slot names are your own: name them after the parts of your component.

import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const card = createSlotStyleRecipe({
slots: ["root", "title", "body"],
base: {
root: { borderRadius: 12, padding: 16 },
title: { fontSize: 18, fontWeight: "600" },
body: { fontSize: 14 },
},
variants: {
tone: {
plain: {
root: { backgroundColor: "#ffffff" },
title: { color: "#111827" },
body: { color: "#4b5563" },
},
inverted: {
root: { backgroundColor: "#111827" },
title: { color: "#ffffff" },
body: { color: "#d1d5db" },
},
},
compact: {
true: { root: { padding: 8 }, title: { fontSize: 16 } },
},
},
defaultVariants: { tone: "plain" },
});

PlaygroundChoose the variants. The properties a choice adds or changes light up.

tone
compact
card()→ {    root: { borderRadius: 12, padding: 16, backgroundColor: "#ffffff" },    title: { fontSize: 18, fontWeight: "600", color: "#111827" },    body: { fontSize: 14, color: "#4b5563" },  }
<View style={styles.root}>
<Text style={styles.title}>{title}</Text>
<Text style={styles.body}>{children}</Text>
</View>
// Simplified: the type parameters are left out.
function createSlotStyleRecipe(config: SlotStyleRecipeConfig): SlotStyleRecipe;

TypeScript infers the slots, variants, options, and style types from config, so you never write type arguments.

The config is the config of a style recipe, with slots. Wherever a style recipe takes a style, a slot recipe takes an object of styles keyed by slot name.

Property Required Description
slots Required The names of the slots, in the order of the result.
variants Required For each variant name, the styles of the slots for each option. Write variants: {} when the recipe has none.
base Optional The style each slot always has.
compoundVariants Optional Styles added to some slots when several variants have particular options at the same time. See Compound variants.
defaultVariants Optional The option each variant uses when a call leaves it out. A variant without a default is required.
composes Optional Slot style recipes whose configs the recipe adds before its own. See Composing slot recipes.
cache Optional Whether the recipe caches its styles. Defaults to true. With false, every call returns a new object, so leave it on in components.

An option or a compound variant gives styles only to the slots it names. A style for a slot that neither slots nor the slot recipes it composes name is a type error; at runtime the recipe ignores that style and warns about it when it is created (see Names the config does not declare). Variants, defaults, boolean variants, and undeclared options work as in a style recipe (see Variants).

A slot named after a property that every object has, such as toString or constructor, is not supported. Every object inherits that property with a function type, so the types reject an object of slot styles that leaves that slot out.

Each item of compoundVariants is an object of type SlotStyleCompoundVariant. It uses styles, with a style for each slot, where a style recipe uses style:

Property Required Description
variants Required The condition: one option, or a list of options, for each variant it names. A variant it leaves out matches any.
styles Required The style to add to each slot it names when the condition matches.
import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const field = createSlotStyleRecipe({
slots: ["label", "input"],
base: {
label: { fontSize: 14 },
input: { borderRadius: 6, borderWidth: 1 },
},
variants: {
invalid: {
true: { label: { color: "#b91c1c" }, input: { borderColor: "#dc2626" } },
},
disabled: { true: { input: { opacity: 0.5 } } },
},
compoundVariants: [
{
variants: { invalid: true, disabled: true },
styles: { label: { opacity: 0.5 } },
},
],
});
field({ invalid: true, disabled: true })→ {    label: { fontSize: 14, color: "#b91c1c", opacity: 0.5 },    input: {      borderRadius: 6,      borderWidth: 1,      borderColor: "#dc2626",      opacity: 0.5,    },  }

A slot recipe returns an object with the style of each slot.

  • Frozen. The object and the style of each slot are frozen.
  • The same object for the same variants. The recipe builds the styles of each selection once, and returns the same object, with the same style for each slot, on every later call with the same variants.
  • In a fixed order. Each slot’s style merges its base style, then its style for each variant’s option, then its style for each matching compound variant, as in a style recipe.
  • Every slot. Every slot is in the result. A slot that gets no style is an empty style, so its element can always receive it.
card({ tone: "inverted", compact: true }) ===
card({ tone: "inverted", compact: true });
// => true
import { createSlotStyleRecipe } from "@lynstack/native-recipe";
export const iconButton = createSlotStyleRecipe({
slots: ["root", "icon"],
variants: { size: { md: { root: { height: 40 } } } },
});
iconButton({ size: "md" })→ { root: { height: 40 }, icon: {} }

A slot recipe has the same variantKeys, variantOptions, and defaultVariants as a style recipe (see createStyleRecipe).

composes lists slot style recipes whose configs the slot recipe adds before its own. The slot recipe has the slots of each, theirs first, and can style any of them. See Composing recipes.