|
| 1 | +--- |
| 2 | +title: class-recipe cookbook |
| 3 | +description: "Ready-to-adapt class-recipe recipes for common components, written with Tailwind CSS and free of conflicting classes: button, badge, card, alert, dialog, and form field." |
| 4 | +sidebar: |
| 5 | + label: Cookbook |
| 6 | +--- |
| 7 | + |
| 8 | +import RecipePlayground from "../../../components/RecipePlayground.astro"; |
| 9 | + |
| 10 | +Recipes for common components, written with Tailwind CSS. Each one is |
| 11 | +conflict-free: it sets each CSS property of an element in one place, so it |
| 12 | +works with the default join, without tailwind-merge (see |
| 13 | +[Writing conflict-free recipes](/recipe/class-recipe/conflict-free-recipes/)). |
| 14 | +Copy one, then change its classes to match your design. |
| 15 | + |
| 16 | +## Button |
| 17 | + |
| 18 | +`state` replaces separate `loading` and `disabled` variants, which would |
| 19 | +both set the cursor and the opacity: |
| 20 | + |
| 21 | +<RecipePlayground name="class-recipe/cookbook/button" /> |
| 22 | + |
| 23 | +The component derives `state` from its props: |
| 24 | + |
| 25 | +```tsx |
| 26 | +import type { ComponentProps } from "react"; |
| 27 | +import type { VariantsOf } from "@lynstack/class-recipe"; |
| 28 | + |
| 29 | +type ButtonProps = ComponentProps<"button"> & |
| 30 | + Omit<VariantsOf<typeof button>, "state"> & { readonly loading?: boolean }; |
| 31 | + |
| 32 | +export function Button({ |
| 33 | + tone, |
| 34 | + size, |
| 35 | + loading = false, |
| 36 | + disabled = false, |
| 37 | + className, |
| 38 | + ...props |
| 39 | +}: ButtonProps) { |
| 40 | + const state = disabled ? "disabled" : loading ? "loading" : "idle"; |
| 41 | + return ( |
| 42 | + <button |
| 43 | + className={button({ tone, size, state, className })} |
| 44 | + disabled={disabled || loading} |
| 45 | + aria-busy={loading} |
| 46 | + {...props} |
| 47 | + /> |
| 48 | + ); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Badge |
| 53 | + |
| 54 | +The text color depends on both `tone` and `outlined`, so only the |
| 55 | +compound variants set it, one for each combination: |
| 56 | + |
| 57 | +<RecipePlayground name="class-recipe/cookbook/badge" /> |
| 58 | + |
| 59 | +## Card |
| 60 | + |
| 61 | +<RecipePlayground name="class-recipe/cookbook/card" /> |
| 62 | + |
| 63 | +```tsx |
| 64 | +const classes = card({ size: "sm" }); |
| 65 | + |
| 66 | +<section className={classes.root}> |
| 67 | + <header className={classes.header}> |
| 68 | + <h2 className={classes.title}>Usage</h2> |
| 69 | + <p className={classes.description}>This month</p> |
| 70 | + </header> |
| 71 | + <div className={classes.body}>…</div> |
| 72 | + <footer className={classes.footer}>…</footer> |
| 73 | +</section>; |
| 74 | +``` |
| 75 | + |
| 76 | +## Alert |
| 77 | + |
| 78 | +One choice of `tone` styles the root, the icon, the title, and the |
| 79 | +description together: |
| 80 | + |
| 81 | +<RecipePlayground name="class-recipe/cookbook/alert" /> |
| 82 | + |
| 83 | +## Dialog |
| 84 | + |
| 85 | +The recipe styles the overlay and the content of a dialog. Use it with |
| 86 | +the dialog primitive of your choice, such as the `<dialog>` element or a |
| 87 | +headless component library: |
| 88 | + |
| 89 | +<RecipePlayground name="class-recipe/cookbook/dialog" /> |
| 90 | + |
| 91 | +## Form field |
| 92 | + |
| 93 | +A label, an input, and a message, whose colors follow `invalid`: |
| 94 | + |
| 95 | +<RecipePlayground name="class-recipe/cookbook/field" /> |
| 96 | + |
| 97 | +```tsx |
| 98 | +import { useId, type ComponentProps } from "react"; |
| 99 | +import type { VariantsOf } from "@lynstack/class-recipe"; |
| 100 | + |
| 101 | +type FieldProps = Omit<ComponentProps<"input">, "size"> & |
| 102 | + VariantsOf<typeof field> & { |
| 103 | + readonly label: string; |
| 104 | + readonly message?: string; |
| 105 | + }; |
| 106 | + |
| 107 | +export function Field({ label, message, size, invalid, ...props }: FieldProps) { |
| 108 | + const id = useId(); |
| 109 | + const classes = field({ size, invalid }); |
| 110 | + return ( |
| 111 | + <div className={classes.root}> |
| 112 | + <label htmlFor={id} className={classes.label}> |
| 113 | + {label} |
| 114 | + </label> |
| 115 | + <input |
| 116 | + id={id} |
| 117 | + className={classes.input} |
| 118 | + aria-invalid={invalid} |
| 119 | + aria-describedby={message === undefined ? undefined : `${id}-message`} |
| 120 | + {...props} |
| 121 | + /> |
| 122 | + {message !== undefined && ( |
| 123 | + <p id={`${id}-message`} className={classes.message}> |
| 124 | + {message} |
| 125 | + </p> |
| 126 | + )} |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +`size` is also an attribute of `<input>`, so the props omit it (see |
| 133 | +[Building components](/recipe/class-recipe/building-components/#variant-names-that-html-also-uses)). |
| 134 | + |
| 135 | +## Next steps |
| 136 | + |
| 137 | +- [Building components](/recipe/class-recipe/building-components/) shows |
| 138 | + how to type and split the props of these components. |
| 139 | +- [Composing recipes](/recipe/class-recipe/composing/) shares classes |
| 140 | + between recipes, such as the border of every form control. |
| 141 | +- [Using with shadcn/ui](/recipe/class-recipe/shadcn-ui/) converts the |
| 142 | + components of shadcn/ui. |
0 commit comments