Skip to content

class-recipe cookbook

Recipes for common components, written with Tailwind CSS. Each one is conflict-free: it sets each CSS property of an element in one place, so it works with the default join, without tailwind-merge (see Writing conflict-free recipes). Copy one, then change its classes to match your design.

state replaces separate loading and disabled variants, which would both set the cursor and the opacity:

import { cva } from "@lynstack/class-recipe";
export const button = cva({
base: "inline-flex items-center justify-center gap-2 rounded-md font-medium transition-colors focus-visible:outline-2 focus-visible:outline-offset-2",
variants: {
tone: {
primary:
"bg-blue-600 text-white hover:bg-blue-700 focus-visible:outline-blue-600",
neutral:
"bg-gray-100 text-gray-900 hover:bg-gray-200 focus-visible:outline-gray-400",
danger:
"bg-red-600 text-white hover:bg-red-700 focus-visible:outline-red-600",
},
size: {
sm: "h-8 px-3 text-sm",
md: "h-10 px-4 text-sm",
lg: "h-12 px-6 text-base",
},
state: {
idle: "cursor-pointer",
loading: "cursor-wait opacity-75",
disabled: "cursor-not-allowed opacity-50",
},
},
defaultVariants: { tone: "primary", size: "md", state: "idle" },
});

PlaygroundChoose the variants. The classes a choice adds light up.

tone
size
state
button()→ "inline-flex items-center justify-center gap-2 rounded-md font-medium transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 bg-blue-600 text-white hover:bg-blue-700 focus-visible:outline-blue-600 h-10 px-4 text-sm cursor-pointer"

The component derives state from its props:

import type { ComponentProps } from "react";
import type { VariantsOf } from "@lynstack/class-recipe";
type ButtonProps = ComponentProps<"button"> &
Omit<VariantsOf<typeof button>, "state"> & { readonly loading?: boolean };
export function Button({
tone,
size,
loading = false,
disabled = false,
className,
...props
}: ButtonProps) {
const state = disabled ? "disabled" : loading ? "loading" : "idle";
return (
<button
className={button({ tone, size, state, className })}
disabled={disabled || loading}
aria-busy={loading}
{...props}
/>
);
}

The text color depends on both tone and outlined, so only the compound variants set it, one for each combination:

import { cva } from "@lynstack/class-recipe";
export const badge = cva({
base: "inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium",
variants: {
tone: {
neutral: "",
success: "",
danger: "",
},
outlined: { true: "ring-1 ring-inset", false: "" },
},
compoundVariants: [
{
variants: { tone: "neutral", outlined: false },
className: "bg-gray-100 text-gray-700",
},
{
variants: { tone: "success", outlined: false },
className: "bg-green-100 text-green-800",
},
{
variants: { tone: "danger", outlined: false },
className: "bg-red-100 text-red-800",
},
{
variants: { tone: "neutral", outlined: true },
className: "text-gray-700 ring-gray-300",
},
{
variants: { tone: "success", outlined: true },
className: "text-green-700 ring-green-600",
},
{
variants: { tone: "danger", outlined: true },
className: "text-red-700 ring-red-600",
},
],
defaultVariants: { tone: "neutral" },
});

PlaygroundChoose the variants. The classes a choice adds light up.

tone
outlined
badge()→ "inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium bg-gray-100 text-gray-700"
import { sva } from "@lynstack/class-recipe";
export const card = sva({
slots: ["root", "header", "title", "description", "body", "footer"],
base: {
root: "flex flex-col rounded-xl border border-gray-200 bg-white",
header: "flex flex-col gap-1",
title: "font-semibold text-gray-900",
description: "text-sm text-gray-600",
body: "text-sm text-gray-700",
footer: "flex items-center justify-end gap-2",
},
variants: {
size: {
sm: {
root: "gap-3 p-4",
title: "text-base",
},
md: {
root: "gap-4 p-6",
title: "text-lg",
},
},
elevated: {
true: { root: "shadow-md" },
false: { root: "shadow-none" },
},
},
defaultVariants: { size: "md" },
});

PlaygroundChoose the variants. The classes a choice adds light up.

size
elevated
card()→ {    root: "flex flex-col rounded-xl border border-gray-200 bg-white gap-4 p-6 shadow-none",    header: "flex flex-col gap-1",    title: "font-semibold text-gray-900 text-lg",    description: "text-sm text-gray-600",    body: "text-sm text-gray-700",    footer: "flex items-center justify-end gap-2",  }
const classes = card({ size: "sm" });
<section className={classes.root}>
<header className={classes.header}>
<h2 className={classes.title}>Usage</h2>
<p className={classes.description}>This month</p>
</header>
<div className={classes.body}>…</div>
<footer className={classes.footer}>…</footer>
</section>;

One choice of tone styles the root, the icon, the title, and the description together:

import { sva } from "@lynstack/class-recipe";
export const alert = sva({
slots: ["root", "icon", "title", "description"],
base: {
root: "flex gap-3 rounded-lg border p-4",
icon: "size-5 shrink-0",
title: "font-semibold",
description: "text-sm",
},
variants: {
tone: {
info: {
root: "border-blue-200 bg-blue-50",
icon: "text-blue-600",
title: "text-blue-900",
description: "text-blue-800",
},
success: {
root: "border-green-200 bg-green-50",
icon: "text-green-600",
title: "text-green-900",
description: "text-green-800",
},
danger: {
root: "border-red-200 bg-red-50",
icon: "text-red-600",
title: "text-red-900",
description: "text-red-800",
},
},
},
defaultVariants: { tone: "info" },
});

PlaygroundChoose the variants. The classes a choice adds light up.

tone
alert()→ {    root: "flex gap-3 rounded-lg border p-4 border-blue-200 bg-blue-50",    icon: "size-5 shrink-0 text-blue-600",    title: "font-semibold text-blue-900",    description: "text-sm text-blue-800",  }

The recipe styles the overlay and the content of a dialog. Use it with the dialog primitive of your choice, such as the <dialog> element or a headless component library:

import { sva } from "@lynstack/class-recipe";
export const dialog = sva({
slots: ["overlay", "content", "header", "title", "description", "footer"],
base: {
overlay: "fixed inset-0 bg-black/50",
content:
"fixed top-1/2 left-1/2 flex w-full -translate-x-1/2 -translate-y-1/2 flex-col gap-4 rounded-xl bg-white p-6 shadow-lg",
header: "flex flex-col gap-1",
title: "text-lg font-semibold text-gray-900",
description: "text-sm text-gray-600",
footer: "flex justify-end gap-2",
},
variants: {
size: {
sm: { content: "max-w-sm" },
md: { content: "max-w-lg" },
lg: { content: "max-w-2xl" },
},
},
defaultVariants: { size: "md" },
});

PlaygroundChoose the variants. The classes a choice adds light up.

size
dialog()→ {    overlay: "fixed inset-0 bg-black/50",    content: "fixed top-1/2 left-1/2 flex w-full -translate-x-1/2 -translate-y-1/2 flex-col gap-4 rounded-xl bg-white p-6 shadow-lg max-w-lg",    header: "flex flex-col gap-1",    title: "text-lg font-semibold text-gray-900",    description: "text-sm text-gray-600",    footer: "flex justify-end gap-2",  }

A label, an input, and a message, whose colors follow invalid:

import { sva } from "@lynstack/class-recipe";
export const field = sva({
slots: ["root", "label", "input", "message"],
base: {
root: "flex flex-col gap-1.5",
label: "text-sm font-medium text-gray-900",
input:
"w-full rounded-md border bg-white outline-none focus-visible:ring-2",
message: "text-xs",
},
variants: {
size: {
sm: { input: "h-8 px-2 text-sm" },
md: { input: "h-10 px-3 text-base" },
},
invalid: {
true: {
input: "border-red-600 focus-visible:ring-red-200",
message: "text-red-600",
},
false: {
input: "border-gray-300 focus-visible:ring-blue-200",
message: "text-gray-500",
},
},
},
defaultVariants: { size: "md" },
});

PlaygroundChoose the variants. The classes a choice adds light up.

size
invalid
field()→ {    root: "flex flex-col gap-1.5",    label: "text-sm font-medium text-gray-900",    input: "w-full rounded-md border bg-white outline-none focus-visible:ring-2 h-10 px-3 text-base border-gray-300 focus-visible:ring-blue-200",    message: "text-xs text-gray-500",  }
import { useId, type ComponentProps } from "react";
import type { VariantsOf } from "@lynstack/class-recipe";
type FieldProps = Omit<ComponentProps<"input">, "size"> &
VariantsOf<typeof field> & {
readonly label: string;
readonly message?: string;
};
export function Field({ label, message, size, invalid, ...props }: FieldProps) {
const id = useId();
const classes = field({ size, invalid });
return (
<div className={classes.root}>
<label htmlFor={id} className={classes.label}>
{label}
</label>
<input
id={id}
className={classes.input}
aria-invalid={invalid}
aria-describedby={message === undefined ? undefined : `${id}-message`}
{...props}
/>
{message !== undefined && (
<p id={`${id}-message`} className={classes.message}>
{message}
</p>
)}
</div>
);
}

size is also an attribute of <input>, so the props omit it (see Building components).