ShtukbaseStandard
package page→ /design/shadcn.md

pages/design/shadcn.md

67 lines, 5.0 KB. As a page.

  1. ---
  2. type: Page
  3. title: "shadcn/ui"
  4. 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."
  5. icon: "🧱"
  6. components: [button, card, input, label, badge, separator, dialog, tabs, tooltip, dropdown-menu]
  7. aliasRoot: ""
  8. ---
  9. # shadcn/ui
  10. ## What this approach is
  11. 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.
  12. What it stands on:
  13. - **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.
  14. - **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`.
  15. - **`cn()`**: class merging: it removes empty ones and resolves Tailwind conflicts, so that a `className` from outside overrides a class inside.
  16. - **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.
  17. - **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.
  18. - **`components.json`**: the project's settings for shadcn: the style, where the theme file is, which import aliases (`@/components/ui`, `@/lib/utils`).
  19. ## What is placed in this project
  20. The `shadcn` package places the code itself after install and update (the `place` action):
  21. - 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`);
  22. - `cn()` goes to the file of the `utils` alias (`src/lib/utils.ts`);
  23. - 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;
  24. - `components.json`, if there was none;
  25. - `.shadcn.json`: which components are placed and the hash of each file.
  26. Where `@/` points is taken from the `aliasRoot` field of this page, otherwise from `paths` in `tsconfig.json`, otherwise `src`.
  27. The [theme showcase](shadcn/showcase.html) shows the components in the light and dark schemes and the contrast of color pairs.
  28. ## How to add a component
  29. ```sh
  30. shtuk run shadcn place select table # place two more (and their dependencies)
  31. shtuk run shadcn place --force # replace edited files too with the package version
  32. ```
  33. 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`.
  34. A file the project edited after `place` is not overwritten: `place` leaves it and names it. Components removed from the list are not deleted.
  35. ## What the project needs
  36. 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.
  37. ## Norms
  38. | Norm | What | Check |
  39. |---|---|---|
  40. | [SHD-R01](/norms/rules/shadcn/SHD-R01.md) | `components.json` is valid | `config` |
  41. | [SHD-R02](/norms/rules/shadcn/SHD-R02.md) | Components in one folder, Radix only there | `folder` |
  42. | [SHD-R03](/norms/rules/shadcn/SHD-R03.md) | Colors only from the theme | `colors` |
  43. | [SHD-R04](/norms/rules/shadcn/SHD-R04.md) | The theme has both schemes and all required variables | `theme` |
  44. The contrast of theme pairs is checked by the `design-a11y` package (norm A11Y-R01), if it is installed.
  45. ## Where the code comes from
  46. 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.