Slot Recipes
Learn how to style multiple parts components with slot recipes.
Overview
Slot Recipes come in handy when you need to apply style variations to multiple parts of a component.
A slot recipe consists of these properties:
className
: The className prefix to attach to the component slotslots
: An array of component parts to stylejsx
: An optional array of elements that will consume the recipebase
: The base styles per slotvariants
: The different visual styles for each slotdefaultVariants
: The default variant for the componentcompoundVariants
: The compound variant combination and style overrides for each slot.
Slot recipes use the same syntax as PandaCSS recipes, but they are specifically designed to style multiple parts of a component.
Defining the recipe
Use the defineSlotRecipe
identity function to create a slot recipe.
1import { defineSlotRecipe } from "@pandacss/dev"2
3export const checkboxSlotRecipe = defineSlotRecipe({4 className: "checkbox",5 slots: ["root", "control", "label"],6 base: {7 root: { display: "flex", alignItems: "center", gap: "2" },8 control: { borderWidth: "1px", borderRadius: "sm" },9 label: { marginStart: "2" },10 },11 variants: {12 size: {13 sm: {14 control: { width: "8", height: "8" },15 label: { fontSize: "sm" },16 },17 md: {18 control: { width: "10", height: "10" },19 label: { fontSize: "md" },20 },21 },22 },23})
Using the recipe
After defining recipes, we recommend using the Panda CLI to generate theme typings for your recipe.
1npm panda codegen
There are two ways to use the recipe in a component:
- Directly in the component
- Creating a component (recommended) with the
Cerberus
factory
With the Cerberus Factory
Import the checkbox
recipe from your local styled-system/recipes
folder and use it within your component.
1import { cerberus, createCerberusPrimitive } from "@cerberus/react"2import { checkboxRecipe, type CheckboxVariantProps } from "./checkbox.recipe"3
4const { withSlotRecipe } = createCerberusPrimitive<CheckboxVariantProps>(checkboxRecipe)5
6export const CheckboxRoot = withSlotRecipe(cerberus.div, "root")7export const CheckboxControl = withSlotRecipe(cerberus.input, "control")8export const CheckboxLabel = withSlotRecipe(cerberus.label, "label")
Directly in the Component
splitVariantProps
You can also use the [recipe].splitVariantProps
function to split the recipe
props from the component props. This is useful if you are not using the factory to create components.
1'use client';2
3import { checkboxRecipe, type CheckboxVariantProps } from "./button.recipe"4
5interface CustomCheckboxControlProps extends CheckboxVariantProps {6 onChange: () => void7}8
9export function MyCustomCheckboxControl(props: CustomCheckboxControlProps) {10 const [recipeProps, restProps] = checkboxRecipe.splitVariantProps(props)11 const styles = checkboxRecipe(recipeProps)12 return(13 <div className={styles.root}>14 <label className={styles.label}>{restProps.label}</label>15 <input16 className={styles.control}17 onChange={restProps.onChange}18 />19 </div>20 )21}
TypeScript
To infer the recipe variant prop types, use the *VariantProps
type.
1import { checkboxRecipe, type CheckboxVariantProps } from "./checkbox.recipe"2
3export interface CheckboxRootProps extends CheckboxVariantProps {}
Compound Variants
Use the compoundVariants
property to define a set of variants that are applied
based on a combination of other variants.
1import { defineSlotRecipe } from "@pandacss/dev"2
3export const checkboxRecipe = defineSlotRecipe({4 slots: ["root", "control", "label"],5 base: {},6 variants: {7 size: {8 sm: {},9 md: {},10 },11 visual: {12 contained: {},13 outline: {},14 },15 },16 compoundVariants: [17 {18 size: "sm",19 visual: "outline",20 css: {21 control: { borderWidth: "1px" },22 label: { color: "green.500" },23 },24 },25 ],26})
Targeting a slot
In some cases, targeting a slot by className might be needed.
- Set the
className
property in the config - The naming convention is
${className}__${slot}
1import { defineSlotRecipe } from "@pandacss/dev"2
3export const checkboxRecipe = defineSlotRecipe({4 className: "checkbox",5 slots: ["root", "control", "label"],6 base: {7 root: {8 bg: "blue.500",9 _hover: {10 "& .checkbox__label": { color: "white" },11 },12 },13 },14})
TypeScript
Use the PandaCSS CLI to generate the types for the recipe.
1npm panda codegen
This will register the recipe and generate the types for the slots.
On this page