shadcn/ui
The shadcn/ui approach in a project: component code lives in the project and belongs to it, a theme on CSS variables in a light and a dark scheme. Which components are placed, how to add one, norms.
What this approach is
shadcn/ui is not a library installed as a dependency but a set of sources. The code of a button, a dialog, tabs is copied into the project and from then on belongs to the project: it is read, edited to fit, and updated when you want. There is no "black box" and no fight with someone else's styles.
What it stands on:
- Radix: primitives without a look: a dialog, a menu, tabs, a switch. They already handle the keyboard, focus and roles for a screen reader. shadcn adds the look.
- Tailwind CSS: the look is written as classes right in the component. Variants (a primary button, an outline one, a danger one) are assembled by
cva. cn(): class merging: it removes empty ones and resolves Tailwind conflicts, so that aclassNamefrom outside overrides a class inside.- A theme on CSS variables. Components do not know colors, only role names:
bg-primary,text-muted-foreground,border-border. The values of the roles are the variables--background,--foreground,--primary,--primary-foreground… in:root, and the same variables again in.dark. Change the values and the look of the whole product changes; add the classdarkon<html>and the dark scheme turns on. - Color pairs. A role's background has a text color on it:
--primaryand--primary-foreground,--cardand--card-foreground. Pairs are easy to check for contrast. components.json: the project's settings for shadcn: the style, where the theme file is, which import aliases (@/components/ui,@/lib/utils).
What is placed in this project
The shadcn package places the code itself after install and update (the place action):
- the components from the
components:list of this page and everything they need go to the folder of theuialias fromcomponents.json(by defaultsrc/components/ui); cn()goes to the file of theutilsalias (src/lib/utils.ts);- the theme goes to the
tailwind.cssfile fromcomponents.json(src/styles/globals.css): "neutral" colors in a light and a dark scheme, the radius, thebaselayer; components.json, if there was none;.shadcn.json: which components are placed and the hash of each file.
Where @/ points is taken from the aliasRoot field of this page, otherwise from paths in tsconfig.json, otherwise src.
The theme showcase shows the components in the light and dark schemes and the contrast of color pairs.
How to add a component
shtuk run shadcn place select table # place two more (and their dependencies)
shtuk run shadcn place --force # replace edited files too with the package version
Or add the name to components: above and run shtuk run shadcn place. In the package registry: accordion, alert, alert-dialog, avatar, badge, breadcrumb, button, card, checkbox, dialog, dropdown-menu, empty, field, input, kbd, label, popover, progress, radio-group, select, separator, sheet, skeleton, spinner, switch, table, tabs, textarea, tooltip. Other shadcn components can be added with the ordinary CLI (npx shadcn add …): the package and the CLI share components.json.
A file the project edited after place is not overwritten: place leaves it and names it. Components removed from the list are not deleted.
What the project needs
The package does not install npm dependencies. The project needs: react, radix-ui, class-variance-authority, cn, lucide-react, tailwindcss 4, tw-animate-css (place prints the exact list for the placed components). The theme file is imported from the application's entry point.
Norms
| Norm | What | Check |
|---|---|---|
| SHD-R01 | components.json is valid | config |
| SHD-R02 | Components in one folder, Radix only there | folder |
| SHD-R03 | Colors only from the theme | colors |
| SHD-R04 | The theme has both schemes and all required variables | theme |
The contrast of theme pairs is checked by the design-a11y package (norm A11Y-R01), if it is installed.
Where the code comes from
The components, cn() and the theme come from the open shadcn/ui registry (style new-york-v4, https://ui.shadcn.com), MIT license, © 2023 shadcn. The license text and what was changed are in .shtukbase/packages/shadcn/registry/LICENSE.md and SOURCE.md. One change to the theme: the light --muted-foreground is slightly darker (oklch(0.54 0 0) instead of 0.556), so that the contrast on --muted is 4.5:1.
/design/shadcn.md.Source text