package page→ /design/shadcn.md
pages/design/shadcn.md
67 lines, 5.0 KB. As a page.
- ---
- type: Page
- title: "shadcn/ui"
- description: "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."
- icon: "🧱"
- components: [button, card, input, label, badge, separator, dialog, tabs, tooltip, dropdown-menu]
- aliasRoot: ""
- ---
- # shadcn/ui
- ## 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 a `className` from 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 class `dark` on `<html>` and the dark scheme turns on.
- - **Color pairs.** A role's background has a text color on it: `--primary` and `--primary-foreground`, `--card` and `--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 the `ui` alias from `components.json` (by default `src/components/ui`);
- - `cn()` goes to the file of the `utils` alias (`src/lib/utils.ts`);
- - the theme goes to the `tailwind.css` file from `components.json` (`src/styles/globals.css`): "neutral" colors in a light and a dark scheme, the radius, the `base` layer;
- - `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](shadcn/showcase.html) shows the components in the light and dark schemes and the contrast of color pairs.
- ## How to add a component
- ```sh
- 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](/norms/rules/shadcn/SHD-R01.md) | `components.json` is valid | `config` |
- | [SHD-R02](/norms/rules/shadcn/SHD-R02.md) | Components in one folder, Radix only there | `folder` |
- | [SHD-R03](/norms/rules/shadcn/SHD-R03.md) | Colors only from the theme | `colors` |
- | [SHD-R04](/norms/rules/shadcn/SHD-R04.md) | 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.