@amboss/design-system 0.27.2 → 1.0.0-canary-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,96 +27,88 @@ To build storybook run `npm run build-storybook`
27
27
  - Add `skip-release` label to skip the release
28
28
  - If you want your commit to skip ci, add "[skip ci]" to your commit message.
29
29
 
30
- ## Overview
30
+ ### Codebase Overview
31
31
 
32
- 1. [Tech stack](#tech-stack)
33
- 2. [Folder structure](#folder-structure)
34
- 3. [Build](#build)
35
- 4. [\*.d.ts for styles](#dts-for-styles)
36
- 5. [Tokens, styles and theming](#tokens-styles-and-theming)
37
- 6. [Assets](#assets)
38
- 7. [Media queries](#media-queries)
32
+ **1. [Tech stack](#tech-stack)**<br/>
33
+ **2. [Linting](#linting)**<br/>
34
+ **3. [Folder structure](#folder-structure)**<br/>
35
+ **4. [Build](#build)**<br/>
36
+ **5. [Tokens, styles and theming](#tokens-styles-and-theming)**<br/>
37
+ **6. [Assets](#assets)**<br/>
38
+ **7. [Media queries](#media-queries)**<br/>
39
39
 
40
- ### Tech stack
40
+ ## Tech stack
41
41
 
42
- Code: **typescript + scss**
42
+ - Code: **typescript**
43
+ - Styles: **emotion.js**
44
+ - Framework: **react**
45
+ - Bundler: **webpack**
46
+ - Environment: **storybook**
47
+ - Tokens: **[style-dictionary](https://github.com/amzn/style-dictionary)**
48
+ - Testing: **jest + react testing library**
43
49
 
44
- Framework: **react**
50
+ ## Linting
45
51
 
46
- Bundler: **webpack**
52
+ As for linting and formatting, you can configure your editor to automatically lint and format your code on save. For this purpose, we're using [Prettier](https://prettier.io/) and [ESLint](https://eslint.org/). If you need to do it manually, you can run:
47
53
 
48
- Environment: **storybook**
49
-
50
- Tokens: **[style-dictionary](https://github.com/amzn/style-dictionary)**
54
+ ```sh
55
+ npm run lint
56
+ ```
51
57
 
52
- Testing: **jest + react testing library**
58
+ > This command is also run as a **pre-push hook** and in the pipeline.
53
59
 
54
- ### Folder structure
60
+ ## Folder structure
55
61
 
56
- <pre>
62
+ ```
57
63
  |-- .storybook
58
- |-- components
59
- |-- ColorGrid.tsx
60
- |-- SizeGrid.tsx
61
- |-- main.js
62
- |-- preview.js
64
+     |-- components
65
+         |-- ColorGrid.tsx
66
+         |-- SizeGrid.tsx
67
+     |-- main.js
68
+     |-- preview.js
63
69
  |-- assets
64
- |-- fonts
65
- |-- Lato.woff
66
- |-- build (autogenerated)
67
- |-- build-tokens (autogenerated)
68
- |-- _assets_fonts.scss
69
- |-- _variables.scss
70
- |-- _theme.scss
71
- |-- _colors.json
72
- |-- _theme.json
70
+     |-- fonts
71
+         |-- Lato.woff
72
+ |-- build (autogenerated)
73
+ |-- build-tokens (autogenerated)
74
+ |-- assets
75
+ |-- icons.json
76
+ |-- icons16.json
77
+     |-- _colors.json
78
+     |-- _sizes.json
79
+ |-- visualConfig.ts
73
80
  |-- docs
74
- |-- Design-principles.mdx
81
+     |-- Design-principles.mdx
82
+ |-- static
83
+     |-- image-for-storybook.png
75
84
  |-- tokens
76
- |-- themes
77
- |-- dark.json
78
- |-- light.json
79
- |-- assets.json
80
- |-- colors.json
85
+     |-- themes
86
+         |-- dark.json
87
+         |-- light.json
88
+     |-- assets.json
89
+     |-- colors.json
81
90
  |-- src
82
- |-- components
83
- |-- Button
84
- |-- Button.tsx
85
- |-- Button.scss
86
- |-- Button.scss.d.ts (autogenerated)
87
- |-- Button.stories.tsx
88
- |-- Button.test.tsx
89
- |-- shared
90
- |-- _fonts.scss
91
- |-- types
92
- |-- index.ts
93
- |-- index.ts
94
-
95
- </pre>
96
-
97
- ### Build
98
-
99
- There are 3 stages of a build.
100
-
101
- 1. Generate type definitions for styles
102
- 2. Generate variables and mixins for tokens
103
- 3. Bundle all exported components
104
-
105
- #### Generate type definitions for styles
106
-
107
- Files like `Button.scss.d.ts` are autogenerated, and they are added to .gitignore.
108
-
109
- Type definitions are generating automatically:
91
+     |-- index.ts
92
+     |-- components
93
+         |-- Button
94
+             |-- Button.tsx
95
+             |-- Button.stories.tsx
96
+             |-- Button.test.tsx
97
+     |-- shared
98
+ -- mediaQueries.ts
99
+     |-- types
100
+         |-- index.ts
101
+ ```
110
102
 
111
- - in watch mode while `npm run start` is running
112
- - after npm install
113
- - before build
103
+ ## Build
114
104
 
115
- You can generate \*d.ts manually using `npm run type-styles` and `npm run type-styles:watch`
105
+ There are 3 stages of a build.
116
106
 
117
- [Why we need \*.d.ts for styles?](#dts-for-styles)
107
+ 1. Generate variables, theme values and assets from tokens
108
+ 2. Build all exported components with babel (preset-react, preset-typescript)
109
+ 2. Bundle all built components with webpack
118
110
 
119
- #### Generate variables and mixins from tokens
111
+ ### Generate variables, theme values and assets from tokens
120
112
 
121
113
  In order to structure our tokens we use **[style-dictionary](https://github.com/amzn/style-dictionary)**.
122
114
  This tool is used for creation a content of `./build-tokens` folder where we keep all the scss variables and mixins.
@@ -130,155 +122,121 @@ New variables and mixins gets generated automaticaly:
130
122
  You can generate tokens manually using `npm run tokens` and `npm run tokens:watch`
131
123
 
132
124
  We use tokens to control the most atomic values in styles and themes.
125
+
133
126
  How does it work?
127
+
134
128
  We have a set of .json files that are located in `./tokens` folder, and we have a `style-dictionary` as a tool for converting .json in to various formats.
129
+
135
130
  For example:
136
- We have a file - `./tokens/colors.json` that gets converted into `./build-tokens/_variables.scss` and `./build-tokens/_colors.json`.
137
- `_variables.scss` is directly imported in other `.scss` files.
131
+
132
+ We have a file - `./tokens/colors.json` that gets converted into part of `./build-tokens/visualConfig.ts` and `./build-tokens/_colors.json`.
133
+
134
+ `visualConfig.ts` is used later as a source for emotion.js theme.
135
+
138
136
  `_colors.json` is imported inside `./.storybook/components/ColorGrid.tsx` in order to present in storybook all the colors we currently have.
139
137
 
140
- The configuration for `style-dictionary` is inside `./style-dictionary.config.json`. In this config we use 2 custom formatters and 1 custom filter, all of them defined in `./style-dictionary.build.js`.
138
+ The configuration for `style-dictionary` is inside `./style-dictionary/style-dictionary.config.json`. In this config we use some custom transforms, filters and formatters, all of them defined in `./style-dictionary/style-dictionary.build.js`.
141
139
 
142
- **custom/format/scss** - a formatter that is used for generation a .json files for tokens like colors, size, weight and so on. These .json files are imported in storybook for presentation.
143
- **custom/format/theme-scss** - a formatter that is used for generation a `_theme.scss` that contains a `@theme` mixin.
144
- **custom/filter/only-vars** - a filter for excluding theme and assets from processing them as variables.
140
+ **custom/format/emotion-visual-config** - a formatter that generates visualConfig.ts that contains all variables and themed values.
141
+
142
+ **custom/filter/emotion-build-tokens** - a filter for all token categories that should go to visutal config.
143
+
144
+ **custom/format/json** - a formatter for generation .json files per category. This formatter also add `isUsed` flag to according token.
145
+
146
+ **custom/format/sub-theme-types** - a formatter that generates type definitions for sub-themes.
147
+
148
+ **custom/assets/svg** - a transformer that adds a path name to dictionary of svg icons.
145
149
 
146
150
  [More about tokens, themes and styles.](#tokens-styles-and-theming)
147
151
 
148
- #### Bundle all exported components
152
+ ### Bundle all exported components
149
153
 
150
154
  Before publish the design system for npm we run `npm run build` that bundles everything that is exported from `./src/index.ts`.
151
155
 
152
- We use webpack + typescript for bundling. For all `/\.ts(x?)$/` files webpack uses a ts-loader as a rule which is implicitly using `tsconfig.json`.
156
+ We use webpack + babel for bundling. For all `/\.ts(x?)$/` files webpack uses a babel-loader as a rule which is implicitly using `tsconfig.json` and `.babelrc`.
153
157
 
154
- For styles we use sass-loader + css-loader with enabled modules for namespacing all css selectors.
158
+ For styles we use emotion.js.
155
159
 
156
160
  What in the bundle?
157
161
 
158
- - JS code that is compiled from typescript
159
- - css rules that are namespaced using css-loader
162
+ - JS code that is compiled from .tsx files (includes common components and emotion.js styled components)
160
163
  - I svg icons that are inlined
161
164
 
162
165
  fonts that are base64-ed currently taken off the build
163
166
 
164
- ### .d.ts for styles
165
-
166
- We use type definitions for style in order to improve quality of a product overall. One example:
167
+ ## Tokens, styles and theming
167
168
 
168
- ```js
169
- import styles from "./button.scss";
169
+ Currently, we have 4 types of tokens:
170
170
 
171
- type ButtonProps = {
172
- size: "s" | "m" | "l",
173
- };
171
+ - atomic values, like colors, sizes, shadows.
172
+ - font assets, that are basically base64 of font files. (not used in production)
173
+ - theme values, that are referencing atomic different atomic values
174
+ - svg icon assets, that are inlined to a json file
174
175
 
175
- export const Button = ({ size }: ButtonProps) => (
176
- <button className={styles[size]} />
177
- );
178
- ```
176
+ As we use Emotion.js all tokenized values are accessible via theme provider. <br />
179
177
 
180
- Here with type definitions we are sure that `button.scss` contain all 3 classes (.s, .m, and .l). Otherwise, it won't compile.
178
+ Basically during the build phase we generate big objects of variables and themed values for each theme (currently light and dark). <br />
181
179
 
182
- ### Tokens, styles and theming
180
+ We call this object `ambossVisualConfiguration`.
183
181
 
184
- Currently, we have 3 types of tokens:
182
+ Particular themes from ambossVisualConfiguration has to be imported and passed to the theme provider at the consumer side.<br />
185
183
 
186
- - atomic values, like colors, sizes, shadows.
187
- - font assets, that are basically base64 of font files.
188
- - theme values, that are referencing atomic different atomic values
189
- - svg icon assets, that are inlined to a json file
184
+ <br />
185
+ Atomic values are straight forward and simply converted to `variables`.
190
186
 
191
- Atomic values and font assets are straight forward and simply converted to scss variables.
192
- After build all atomic values can't be imported as `@import "build-tokens/variables";`
193
- Fonts are imported inside `./src/shared/_fonts` and never used directly (it doesn't make sense).
194
- In the application simply `@import "src/shared/fonts";`.
187
+ After build all atomic values are passed to `theme.variables` by emotion.
195
188
 
196
- Theme values are little more complex. When theme token is precessed we generate several entities. All of them are located in `./build-tokens/_theme.scss`
189
+ Example:
190
+ ```
191
+ const StyledContainer - styled.div(
192
+ ({ theme }) => ({
193
+ marginTop: theme.variables.size.spacing.s
194
+ })
195
+ );
196
+ ```
197
197
 
198
- 1. A number of scss-maps with all the theme variables for each theme.
199
- 2. A `@theme` mixin, that include all themes.
200
- 3. A list of scss variables with the name of entries in the maps (see point 1). This is just for IDE auto-complition.
198
+ Theme tokens are little more complex. We generate separate VisualConfig for each theme, but together with that we generate type definition that describe any of the themes. This enables developers not care about current theme and how many themes are there.
201
199
 
202
- As the result developers are free to use a `@theme` mixin for particular css properties with no worries about active theme and amount of themes.
200
+ After build all themes values are passed to `theme.values` by emotion.
203
201
 
204
202
  Example:
205
203
 
206
- ```scss
207
- @import "build-tokens/theme";
208
- @import "build-tokens/variables";
209
- @import "src/shared/fonts";
210
-
211
- .primary {
212
- @include theme("color", $theme-color-text-primary);
213
- font-family: $Lato;
214
- margin: 0;
215
- }
216
-
217
- .secondary {
218
- @include theme("color", $theme-color-text-secondary);
219
- }
220
-
221
- .s {
222
- font-size: $size-font-text-s;
223
- line-height: $size-line-height-text-s;
224
- }
225
-
226
- .m {
227
- font-size: $size-font-text-m;
228
- line-height: $size-line-height-text-m;
229
- }
230
-
231
- .normal {
232
- font-weight: $weight-normal;
233
- }
234
-
235
- .bold {
236
- font-weight: $weight-bold;
237
- }
238
204
  ```
205
+ const StyledContainer - styled.div(
206
+ ({ theme }) => ({
207
+ backgroundColor: theme.values.color.background.layer_2
208
+ })
209
+ );
210
+ ```
211
+
212
+ Sub-themes are folowing the same concept as themes. Developer can fully rely on `theme.values` object. As long as component in runtime is wrapped in `SubThemeProvider` and it's tokens listed in "tokens/themes/sub-themes/...", all values will be set correctly and automaticaly.
213
+
214
+ > Note that not all tokens are enabled in sub-themes. Make sure to add missing ones to "tokens/themes/sub-themes/...".
215
+
216
+ > Components that has "Sub-themed" badge are already in.
239
217
 
240
- ### Assets
218
+ ## Assets
241
219
 
242
220
  - assets are defined in `tokens/assets.json`
243
221
  - with the help from style-dictionary we create a json file with tokens (`/build-tokens/assets/`)
244
222
  - those json files supposed to be imported in the ts module (see Icons.tsx)
245
223
  - in order to secure `d.ts` filed in the build we "manually" copy asset jsons on the `postbuild` phase (see package.json)
246
224
 
247
- ### Media queries
225
+ ## Media queries
248
226
 
249
227
  There is an API for media-query related props: an array of 3 elements [small, medium, large], for respective screen sizes.
250
- for example `<Box space=["xs", "xl", "xxl"] />` will set `xs` for small screens, `xl` for medium screens and `xxl` for large.
228
+
229
+ For example `<Box space=["xs", "xl", "xxl"] />` will set `xs` for small screens, `xl` for medium screens and `xxl` for large.
251
230
 
252
231
  Basically, if you see a type of `MQ<Whatever>` you can provide an array of 3 `Whatever`s.
253
232
 
254
233
  In order to add a media query support to your components:
255
234
 
256
- > mq mixin from media-queries module will create a set of media-query selectors.
257
-
258
- ```scss
259
- @import "src/shared/media-queries";
260
-
261
- @include mq(".align-left") {
262
- text-align: left;
263
- }
264
-
265
- @include mq(".align-right") {
266
- text-align: right;
267
- }
268
-
269
- @include mq(".align-center") {
270
- text-align: center;
271
- }
272
- ```
273
-
274
- > import getMediaQueryClasses from mediaQueries module to create classnames compatible with mixin above.
275
- > note that third parameter is a prefix to classname.
276
-
277
235
  ```ts
278
- import cn from "classnames";
236
+ import React from "react";
279
237
 
280
- import { getMediaQueryClasses } from "../../shared/mediaQueries";
281
- import styles from "./Box.scss";
238
+ import styled from "@emotion/styled";
239
+ import { mq } from "../../shared/mediaQueries";
282
240
 
283
241
  type TextAlignment = "left" | "center" | "right";
284
242
 
@@ -286,9 +244,14 @@ type BoxProps = {
286
244
  alignText: TextAlignment | MQ<TextAlignment>;
287
245
  };
288
246
 
289
- export const Box = ({ alignText }: BoxProps) => (
290
- <button
291
- className={cn(...getMediaQueryClasses(alignText, styles, "align-"))}
292
- />
247
+ export const Box = styled.div<BoxProps>(
248
+ ({ alignText = "left" }) =>
249
+ mq({
250
+ textAlign: [
251
+ alignText,
252
+ { left: "left", right: "right", center: "center" },
253
+ ],
254
+ })
293
255
  );
294
256
  ```
257
+