Guide for Spirit contributors.
This is an example of a typical file structure of a component:
├── src
├── scss
│ └── components
│ └── <ComponentName>
│ ├── _<ComponentName>.scss — component's styling
│ ├── _theme.scss — contains references to design tokens and variant maps
│ ├── _tools.scss — Sass functions and mixins, mostly to generate variants
│ ├── index.scss — component's Sass entry point
│ ├── index.html — component's preview page
│ └── README.md — documentation of the component
└── js
├── <ComponentName>.ts — component's scripts
├── constants.ts — common constants
├── __tests__/<ComponentName>.test.ts — component's test
├── index.esm.ts — component's ESM entry point
└── index.umd.ts — component's UMD entry point
To keep actual stylesheet of the component as clean as possible, definitions of
Sass variables, functions and mixins related to the component always have their
place in standalone Sass partials, namely _theme.scss
(theme) and
_tools.scss
(tools).
Component's theme serves as:
- a centralized storage of component's references to design tokens (see further),
- a place where component's visual variants are defined as Sass maps, so we can iterate over them later on,
- a source of definition of other Sass variables of the component.
Similarly, component's tools serve as a storage of component-scoped:
- Sass mixins, mostly to generate visual variants from theme,
- Sass functions.
Components can (and should!) reuse Spirit design tokens.
Component's theme properties can be linked to design tokens. This way, the appearance of individual components is always unified and coherent: anytime design tokens change, all components are updated consistently.
Deciding which properties are linked to which design tokens is a subject of discussion between Spirit developers and designers. Linking component properties to design tokens needs to be done carefully in order for the design to work as a system.
Default Spirit design tokens live in the spirit-design-tokens
package.
Products may define their own design tokens as long as they preserve the same
structure and naming.
When contributing to Spirit Sass styles, always link Spirit design tokens via
the @tokens
API with
configured load path. This is crucial for the
rebranding functionality of Spirit.
From the implementation point of view, design tokens are Sass variables
organized in Sass modules. Within component styles, Sass modules with design
tokens are referred to just by filename. However, because the modules do not
exist in the place they are called in (the spirit-design-tokens
package is
stored in node_modules
), the correct load path for the desired set of design
tokens must be provided on build time. This is the only
way how Sass modules can be affected from outside, without giving in their
advantages.
At first, let's define a reference to design tokens in component's theme:
// src/components/Button/_theme.scss
@use '@tokens' as tokens;
$border-radius: tokens.$radius-100;
Now use the reference from the theme in component styles:
// src/components/Button/_Button.scss
@use 'theme';
.Button {
border-radius: theme.$border-radius;
}
% cd <your-local-path>/spirit-design-system/packages/web
% yarn examples:build
% cd <your-local-path>/spirit-design-system/packages/web
% yarn test
for test package (lint, format, unit testing, types)% yarn test:unit
for unit tests
Before pushing your components to Spirit, make sure it meets code conventions specified in this document.
To make your code ready for rebranding before moving to Spirit:
-
Remove full path to your
@tokens
whenever it's @used:- @use '../path/to/my/design/@tokens' as tokens; + @use '@tokens' as tokens;
-
Add the previously removed path to load paths of your Sass compiler. See the
spirit-design-tokens
docs to learn how to do that. -
Try using another
@tokens
file with a different brand and see how your code reacts on the change.
What are design tokens?
Design tokens are special variables that define the smallest pieces of a design language, especially colors, typography, or spacing. Design tokens enable adjusting the common parts of visual design.
What are Sass modules?
Sass modules are a new way of organizing Sass source. Aside from new methods of structuring and loading Sass files, Sass modules offer a great portion of encapsulation, traceability, and more.
Does @use
behave the same way as @import
?
In most situations, no. Most importantly, while @import
loads everything into
global context, @use
is scoped and works more like import
in ES modules.
Why are @tokens
prefixed with @
?
By prefixing a Sass file name with @
, we communicate that such file is loaded
in a special way. Read more about it in
spirit-design-tokens
docs.