A starting point for building your own design system on top of Designsystemet.
Designsystemet gives you accessible components, design tokens and a theme generated from your own colors. This repo shows a good way to add what is specific to your organisation: your own theme, and components that extend or add to the ones from Designsystemet.
It is a working design system, published as one npm package. The design system itself is CSS and class names, and works with any framework. React components and Angular directives are thin wrappers on top, and both are optional. Clone it, rename it, and replace the examples with your own.
- Web: the stylesheet and the class names, written as CSS Modules and compiled with PostCSS
- React and Angular: wrappers that set the class names for you
- A theme generated by the Designsystemet CLI from
designsystemet.config.json - Storybook for development and documentation, with accessibility checks in the Accessibility panel
- Vite to build the library, the Angular compiler to build the directives, TypeScript for types, Biome for linting and formatting
You need Node.js 22.12 or newer and pnpm.
On GitHub, select Use this template to make your own repository. Then:
pnpm installpnpm devStorybook opens at http://localhost:6006.
| Script | What it does |
|---|---|
pnpm dev |
Start Storybook |
pnpm build |
Build the library to dist |
pnpm build:theme |
Generate the theme from designsystemet.config.json |
pnpm build:storybook |
Build Storybook as a static site to storybook-static |
pnpm types |
Check types |
pnpm lint / pnpm lint:fix |
Lint and format |
The starter is called acme throughout. Replace it with a short name of your own:
- Search for
acmein the whole repo and replace it. That renames the npm package (package.json), the theme (designsystemet.config.json), the CSS layer (@layer acme) and the prefix of the Angular directives (acmeButton). The Angular directives and thetsconfigfiles refer to the package by its full name, so search for that too if you change more of it thanacme. - Set your colors, border radius and font in
designsystemet.config.json. The theme builder helps you pick them and gives you the config to paste in. - Run
pnpm build:theme, and delete the olddesignsystemet/acme.css. - Replace
LICENSEand this README with your own.
├── designsystemet.config.json Your theme: colors, border radius, font
├── designsystemet/ Generated theme. Do not edit, run `pnpm build:theme`
├── src/
│ ├── index.ts Web: exports `styles`, the class names
│ ├── react.ts React: exports the components
│ ├── angular.ts Angular: exports the directives
│ ├── styles.module.css The stylesheet: layers, theme and all components
│ ├── base.css Global styles
│ ├── alert/ A Designsystemet component as it is
│ ├── button/ Extends a Designsystemet component
│ └── search/ A component of your own
│ ├── search.module.css The component: all of the styling
│ ├── search.react.tsx Wrapper for React
│ ├── search.angular.ts Wrapper for Angular
│ ├── search.web.stories.tsx Stories with plain HTML
│ └── search.react.stories.tsx Stories with the React components
├── .storybook/ Storybook configuration
└── vite.config.ts Library build
Every component has a folder of its own, with the same files as search.
The CSS is the design system. It is built to one stylesheet, and src/index.ts exports the class names in it as styles:
import '@acme/design/style.css';
import { styles } from '@acme/design';
const html = `<button class="${styles.button}" data-variant="secondary">Click me</button>`;That works in anything that can import JavaScript: Vue, Svelte, Astro or no framework at all. Variants are set with data attributes. Behaviour, like popovers, tabs and tooltips, comes from @digdir/designsystemet-web, which src/index.ts imports.
The wrappers are a convenience. They put the class names on the HTML for you, turn variants into typed props, and do nothing else. For React they are components:
import { Button } from '@acme/design/react';
<Button variant='secondary'>Click me</Button>;For Angular they are directives that you put on the HTML element:
import { Button } from '@acme/design/angular';
@Component({
imports: [Button],
template: `<button acmeButton variant="secondary">Click me</button>`,
})
export class App {}Compare button.react.tsx and button.angular.ts: a few lines each, next to the CSS they wrap. For another framework, add <name>.<framework> files in the same way, and an entry for it in src.
To remove a framework you do not use:
- React: delete the
*.react.tsxand*.react.stories.tsxfiles,src/react.ts, thereactentry invite.config.ts, and./reactand the React peer dependencies inpackage.json. Keep React as a dev dependency, since Storybook renders the stories with it. - Angular: delete the
*.angular.tsfiles,src/angular.ts,tsconfig.angular.json, and inpackage.json./angular,ngcin the build script and everything that starts with@angular. Then removepathsfromtsconfig.json.
Start at the top: the further down you go, the more of the accessibility, testing and maintenance is yours.
- Use as is.
alert.module.csstakesds-alertfrom Designsystemet without changing it. - Extend.
button.module.csstakesds-button, sets component tokens (--dsc-button-*) and adds a variant. - Create.
search.module.cssis a search field that Designsystemet does not have. It puts Designsystemet's input and button together, and adds the layout and the icons.
All three use composes from CSS Modules:
.button {
composes: ds-button from '@digdir/designsystemet-css';
font-weight: var(--ds-font-weight-semibold);
}styles.button is then two class names: your own, and a copy of ds-button with a unique name.
If you create something that others could use too, consider contributing it to Designsystemet instead.
All CSS from Designsystemet is in the layer ds. Everything this design system adds is in the layer acme, which is declared after ds in src/styles.module.css:
@layer ds, acme.base, acme.components;A later layer always wins over an earlier one, no matter how specific the selectors are. So your styles override Designsystemet without !important or long selectors. Unlayered CSS wins over all layers, so applications can in turn override the design system.
Wrap the CSS of every component in @layer acme.components { … }.
In CSS, the tokens are variables. You will meet three kinds:
| Kind | Example | Where it comes from |
|---|---|---|
| Theme | --ds-color-brand1-surface-tinted, --ds-size-4, --ds-border-radius-md |
Generated from designsystemet.config.json. Change them there, for single colors with overrides. |
| Current color | --ds-color-surface-tinted, --ds-color-text-subtle |
The color tokens without the name of a color. They point to the color set with data-color. |
| Component | --dsc-button-background |
The variables each Designsystemet component is built with. Set them in the CSS of your component to restyle it. |
Use theme tokens instead of fixed values, and the color tokens without a color name when you can. Then your components follow data-color, data-color-scheme and data-size, and get dark mode and the size modes for free.
The colors of the theme are generated so that they have enough contrast with each other. If you override a color, check the contrast yourself.
If you need tokens of your own, give them the prefix --acme- and build them from --ds-* tokens, so they follow the theme, the color scheme and the size mode.
An application using your design system can end up on the same page as something else built with Designsystemet, for example an application on another version of your design system. If both had the class ds-button, the stylesheet loaded last would change how the other looks.
The starter avoids that with CSS Modules. composes: ds-button from '@digdir/designsystemet-css' copies Designsystemet's CSS into your stylesheet and gives every class a unique name. ds-button is not in your stylesheet at all, and the names change when the CSS changes, so two versions never share a class.
The price is that every class from Designsystemet you want to offer must go through a CSS Module, and that @digdir/designsystemet-react cannot be used, since its components set the class names ds-button and so on.
Some things are still shared by everything on the page:
- The CSS variables of the theme (
--ds-*). Two versions of the same theme work together. Two different themes do not: the one loaded last sets the colors for both. data-color,data-sizeanddata-color-scheme.- Selectors without a class in Designsystemet's CSS, like
body. @digdir/designsystemet-webregisters its custom elements once. The version that is loaded first is used by everyone.
When you know that your design system is alone on the page, you can use Designsystemet's own class names:
- In every
*.module.css, changefrom '@digdir/designsystemet-css'tofrom global. - Add
import '@digdir/designsystemet-css';above the other imports insrc/index.ts.
styles.button is then your class name and ds-button. Classes from Designsystemet work without going through a CSS Module, and you can build your React components on @digdir/designsystemet-react.
- Make
src/<name>/<name>.module.css. All files end up in one CSS Module, so start the class names with the name of the component:.search,.searchInput. - Import it at the bottom of
src/styles.module.css. - Add the class names to
stylesinsrc/index.ts. - Write stories with plain HTML in
<name>.web.stories.tsx, one for each variant. - For React: make
<name>.react.tsx, export it fromsrc/react.ts, and write stories in<name>.react.stories.tsx. - For Angular: make
<name>.angular.ts, and export it fromsrc/angular.ts.
CONTRIBUTING.md has the conventions.
npm install @acme/designImport the stylesheet once. It includes Designsystemet's CSS and your theme, so applications should not import @digdir/designsystemet-css themselves.
import '@acme/design/style.css';Then use the class names from @acme/design, the components from @acme/design/react or the directives from @acme/design/angular, as shown under Web and the wrappers.
Applications must load the font of the theme themselves. See fonts in the Designsystemet setup guide.
The package is marked "private": true, so it cannot be published by accident. When you are ready, remove that line, set the version, and run:
pnpm buildpnpm publishWhen several people work on the design system, Changesets is a good way to handle versions and changelogs. Designsystemet uses it too.
- Vite prints
A PostCSS plugin did not pass the from option to postcss.parsewhen it builds and when Storybook starts. It comes fromcomposesreading Designsystemet's CSS, and does no harm. - Storybook renders the stories with React, also the ones in
*.web.stories.tsx. The markup in those is still plain HTML. - The Angular directives have no stories, since a Storybook can only render one framework. They put the same class names on the same HTML as the web stories show.
- Angular code must go through Angular's own compiler, so
pnpm buildrunsngcon the directives after Vite has built the rest. That is why they importstylesfrom@acme/designand not from../index.