Skip to content
designsystemetPublic

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Designsystemet starter

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.

What is in it

  • 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

Get started

You need Node.js 22.12 or newer and pnpm.

On GitHub, select Use this template to make your own repository. Then:

pnpm install
pnpm dev

Storybook 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

Make it yours

The starter is called acme throughout. Replace it with a short name of your own:

  1. Search for acme in 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 the tsconfig files refer to the package by its full name, so search for that too if you change more of it than acme.
  2. 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.
  3. Run pnpm build:theme, and delete the old designsystemet/acme.css.
  4. Replace LICENSE and this README with your own.

Structure

├── 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.

How it fits together

Web and the wrappers

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.tsx and *.react.stories.tsx files, src/react.ts, the react entry in vite.config.ts, and ./react and the React peer dependencies in package.json. Keep React as a dev dependency, since Storybook renders the stories with it.
  • Angular: delete the *.angular.ts files, src/angular.ts, tsconfig.angular.json, and in package.json ./angular, ngc in the build script and everything that starts with @angular. Then remove paths from tsconfig.json.

Three ways to get a component

Start at the top: the further down you go, the more of the accessibility, testing and maintenance is yours.

  1. Use as is. alert.module.css takes ds-alert from Designsystemet without changing it.
  2. Extend. button.module.css takes ds-button, sets component tokens (--dsc-button-*) and adds a variant.
  3. Create. search.module.css is 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.

Layers

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 { … }.

Tokens

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.

Sharing a page with other versions of Designsystemet

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-size and data-color-scheme.
  • Selectors without a class in Designsystemet's CSS, like body.
  • @digdir/designsystemet-web registers its custom elements once. The version that is loaded first is used by everyone.

If you do not need it

When you know that your design system is alone on the page, you can use Designsystemet's own class names:

  1. In every *.module.css, change from '@digdir/designsystemet-css' to from global.
  2. Add import '@digdir/designsystemet-css'; above the other imports in src/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.

Add a component

  1. 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.
  2. Import it at the bottom of src/styles.module.css.
  3. Add the class names to styles in src/index.ts.
  4. Write stories with plain HTML in <name>.web.stories.tsx, one for each variant.
  5. For React: make <name>.react.tsx, export it from src/react.ts, and write stories in <name>.react.stories.tsx.
  6. For Angular: make <name>.angular.ts, and export it from src/angular.ts.

CONTRIBUTING.md has the conventions.

Use the design system in an application

npm install @acme/design

Import 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.

Publish

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 build
pnpm publish

When several people work on the design system, Changesets is a good way to handle versions and changelogs. Designsystemet uses it too.

Good to know

  • Vite prints A PostCSS plugin did not pass the from option to postcss.parse when it builds and when Storybook starts. It comes from composes reading 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 build runs ngc on the directives after Vite has built the rest. That is why they import styles from @acme/design and not from ../index.

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages