@carbon/layout 12.0.0-alpha.1 → 12.0.0-alpha.3

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.
@@ -10,7 +10,7 @@
10
10
  /**
11
11
  * Style Dictionary configuration for @carbon/layout.
12
12
  *
13
- * Reads src/dtcg/layout.json and generates:
13
+ * Reads tokens/layout.tokens.json and generates:
14
14
  * scss/generated/*.scss — Sass variables + maps (one file per token group)
15
15
  * js/generated/layout-tokens.js — ES module named exports
16
16
  * js/generated/layout-tokens.d.ts — TypeScript declarations
@@ -22,13 +22,13 @@ const { default: StyleDictionary } = require('style-dictionary');
22
22
  const scssFormats = require('./formats/scss-layout');
23
23
  const carbonJsLayout = require('./formats/js-layout');
24
24
 
25
- // ── Paths ─────────────────────────────────────────────────────────────────────
25
+ // Paths
26
26
  const ROOT = path.resolve(__dirname, '..');
27
- const DTCG_SOURCE = path.join(ROOT, 'src', 'dtcg', 'layout.json');
27
+ const TOKENS_SOURCE = path.join(ROOT, 'tokens', 'layout.tokens.json');
28
28
  const SCSS_GENERATED = path.join(ROOT, 'scss', 'generated');
29
29
  const JS_GENERATED = path.join(ROOT, 'js', 'generated');
30
30
 
31
- // ── Custom name transform ─────────────────────────────────────────────────────
31
+ // Custom name transform
32
32
  // Uses only the token's own key (last path segment) as the name, discarding
33
33
  // the group prefix. With DTCG groups, path is ["spacing", "spacing-01"] so
34
34
  // joining would produce "spacing-spacing-01" — we want just "spacing-01".
@@ -40,12 +40,34 @@ const carbonNameKebab = {
40
40
  },
41
41
  };
42
42
 
43
- // ── Transform group ───────────────────────────────────────────────────────────
44
- const CARBON_TRANSFORMS = ['attribute/cti', 'carbon/name-kebab'];
43
+ // Custom value transform
44
+ // Converts a token's $value to a CSS value string.
45
+ // dimension: { value, unit } → "0.125rem"
46
+ // number: the number, followed by the `com.ibm.carbon` layout unit when
47
+ // one is set ("2vw"); DTCG dimensions only allow `px` and `rem`
48
+ const carbonCssValue = {
49
+ name: 'carbon/css-value',
50
+ type: 'value',
51
+ transform(token) {
52
+ const value = token.$value;
53
+ if (typeof value === 'object') {
54
+ return `${value.value}${value.unit}`;
55
+ }
56
+ const unit = token.$extensions?.['com.ibm.carbon']?.layout?.unit ?? '';
57
+ return `${value}${unit}`;
58
+ },
59
+ };
60
+
61
+ // Transform group
62
+ const CARBON_TRANSFORMS = [
63
+ 'attribute/cti',
64
+ 'carbon/name-kebab',
65
+ 'carbon/css-value',
66
+ ];
45
67
 
46
- // ── SD config ─────────────────────────────────────────────────────────────────
68
+ // SD config
47
69
  const config = {
48
- source: [DTCG_SOURCE],
70
+ source: [TOKENS_SOURCE],
49
71
  platforms: {
50
72
  scss: {
51
73
  transformGroup: 'carbon',
@@ -85,17 +107,18 @@ const config = {
85
107
  },
86
108
  };
87
109
 
88
- // ── Build a registered SD instance ────────────────────────────────────────────
110
+ // Build a registered SD instance
89
111
  function createInstance() {
90
112
  const sd = new StyleDictionary({});
91
113
  sd.registerTransform(carbonNameKebab);
114
+ sd.registerTransform(carbonCssValue);
92
115
  sd.registerTransformGroup({ name: 'carbon', transforms: CARBON_TRANSFORMS });
93
116
  for (const fmt of scssFormats) sd.registerFormat(fmt);
94
117
  sd.registerFormat(carbonJsLayout);
95
118
  return sd;
96
119
  }
97
120
 
98
- // ── run ───────────────────────────────────────────────────────────────────────
121
+ // run
99
122
  async function run() {
100
123
  const sd = await createInstance().extend(config);
101
124
  await sd.buildAllPlatforms();
@@ -0,0 +1,220 @@
1
+ # Layout Tokens
2
+
3
+ This directory contains the `@carbon/layout` design tokens in the
4
+ [Design Tokens Community Group (DTCG) 2025.10 format](https://www.designtokens.org/tr/2025.10/format/).
5
+ It is the source of truth for the package's generated Sass and JavaScript.
6
+
7
+ ## File
8
+
9
+ ```
10
+ tokens/
11
+ └── layout.tokens.json # All layout tokens organised into token groups
12
+ ```
13
+
14
+ Each top-level key in the JSON (e.g. `"spacing"`, `"border-radius"`) is a
15
+ **token group**, and the tokens live inside it:
16
+
17
+ ```json
18
+ {
19
+ "spacing": {
20
+ "$description": "Spacing scale based on the 8px mini-unit grid.",
21
+ "spacing-05": {
22
+ "$type": "dimension",
23
+ "$value": { "value": 1, "unit": "rem" },
24
+ "$description": "Spacing token — 16px."
25
+ }
26
+ }
27
+ }
28
+ ```
29
+
30
+ The group name is the **authoritative category** for a token. The Style
31
+ Dictionary pipeline reads `token.path[0]` (the group key) to decide which
32
+ generated file the token belongs to — no name-prefix inference, no hidden
33
+ ordering rules.
34
+
35
+ ## Token format
36
+
37
+ Every token has `$type`, `$value`, and `$description`.
38
+
39
+ | Key | Required | Description |
40
+ | -------------- | -------- | --------------------------------------------------------- |
41
+ | `$type` | ✅ | `"dimension"`, or `"number"` for viewport-relative values |
42
+ | `$value` | ✅ | The token value — see **Authoring values** below |
43
+ | `$description` | ✅ | Human-readable description of purpose and usage |
44
+ | `$deprecated` | — | `true` when the token should not be used in new work |
45
+ | `$extensions` | — | Carbon-specific metadata — see **Extensions** below |
46
+
47
+ ---
48
+
49
+ ## Authoring values
50
+
51
+ ### Dimensions — `px` and `rem`
52
+
53
+ A dimension `$value` is an object with a numeric `value` and a `unit`. The
54
+ format allows two units, `px` and `rem`. The build writes them out as
55
+ `<value><unit>`, with no conversion.
56
+
57
+ ```json
58
+ "spacing-07": {
59
+ "$type": "dimension",
60
+ "$value": { "value": 2, "unit": "rem" },
61
+ "$description": "Spacing token — 32px."
62
+ }
63
+ ```
64
+
65
+ Carbon's scales are defined in pixels on an 8px mini-unit grid and published in
66
+ `rem`, with a base font size of 16px. To work out the `rem` value, divide the
67
+ pixel value by 16:
68
+
69
+ | Design value | Calculation | `$value` |
70
+ | --------------------- | ----------- | ----------------------------------- |
71
+ | 4 mini-units (32px) | `32 ÷ 16` | `{ "value": 2, "unit": "rem" }` |
72
+ | 4px | `4 ÷ 16` | `{ "value": 0.25, "unit": "rem" }` |
73
+ | A value that stays px | — | `{ "value": 999999, "unit": "px" }` |
74
+
75
+ ### Numbers — viewport-relative values
76
+
77
+ The DTCG dimension type has no viewport units, so the fluid spacing tokens are
78
+ `number` tokens: the `$value` is the count of viewport-width units, and the unit
79
+ is recorded in `$extensions`.
80
+
81
+ ```json
82
+ "fluid-spacing-02": {
83
+ "$type": "number",
84
+ "$value": 2,
85
+ "$description": "Fluid spacing token — 2vw.",
86
+ "$extensions": {
87
+ "com.ibm.carbon": {
88
+ "layout": { "unit": "vw" }
89
+ }
90
+ }
91
+ }
92
+ ```
93
+
94
+ A `number` token with no unit is written out as the bare number, which is how
95
+ `fluid-spacing-01` produces a unitless `0`.
96
+
97
+ ### Deprecated tokens
98
+
99
+ Mark a deprecated token with the format's own `$deprecated` property. Tools and
100
+ codemods can read it to warn consumers.
101
+
102
+ ```json
103
+ "layout-01": {
104
+ "$type": "dimension",
105
+ "$value": { "value": 1, "unit": "rem" },
106
+ "$description": "Deprecated layout token — 16px. Replaced by spacing scale. Do not use in new work.",
107
+ "$deprecated": true
108
+ }
109
+ ```
110
+
111
+ ---
112
+
113
+ ## Extensions — `com.ibm.carbon`
114
+
115
+ All Carbon-specific metadata lives under the single `com.ibm.carbon` key,
116
+ grouped by concern. Layout uses one property:
117
+
118
+ | Property | Applies to | Description |
119
+ | ------------- | --------------- | ------------------------------------------------------ |
120
+ | `layout.unit` | `number` tokens | CSS unit appended to the number in the output (`"vw"`) |
121
+
122
+ ---
123
+
124
+ ## Token naming and generated file routing
125
+
126
+ Token names in `layout.tokens.json` use **kebab-case**. Routing to a generated
127
+ Sass file is determined by the **group** the token belongs to — `token.path[0]`
128
+ in Style Dictionary terms. There is no name-prefix inference.
129
+
130
+ | Group key | Generated Sass file | Sass map variable |
131
+ | --------------- | ------------------------------------ | --------------------------- |
132
+ | `spacing` | `scss/generated/_spacing.scss` | `$spacing` |
133
+ | `fluid-spacing` | `scss/generated/_fluid-spacing.scss` | `$fluid-spacing` |
134
+ | `container` | `scss/generated/_container.scss` | `$container` |
135
+ | `icon-size` | `scss/generated/_icon-size.scss` | `$icon-size` |
136
+ | `border-radius` | `scss/generated/_border-radius.scss` | `$border-radius` |
137
+ | `layout` | `scss/generated/_layout.scss` | `$layout` |
138
+ | `size` | `scss/generated/_size.scss` | _(no map — bare variables)_ |
139
+
140
+ The JS export name is the camelCase version of the token's own key: `spacing-05`
141
+ → `spacing05`, `border-radius-04` → `borderRadius04`, `fluid-spacing-02` →
142
+ `fluidSpacing02`.
143
+
144
+ ### Adding a token to an existing category
145
+
146
+ Place the token inside the correct group object — it will automatically be
147
+ included in the right file on the next build. For example, to add a new spacing
148
+ step of 192px:
149
+
150
+ ```json
151
+ "spacing": {
152
+ "spacing-14": {
153
+ "$type": "dimension",
154
+ "$value": { "value": 12, "unit": "rem" },
155
+ "$description": "Spacing token — 192px."
156
+ }
157
+ }
158
+ ```
159
+
160
+ This will add `$spacing-14: 12rem !default;` to `_spacing.scss` and
161
+ `export const spacing14 = '12rem';` to `layout-tokens.js`.
162
+
163
+ ### Adding a token in a new category
164
+
165
+ A new group (e.g. `gap`) also needs a change to the Style Dictionary pipeline:
166
+
167
+ 1. Add the group and its tokens to `layout.tokens.json`.
168
+ 2. Add a new format function in
169
+ [`style-dictionary/formats/scss-layout.js`](../style-dictionary/formats/scss-layout.js):
170
+ ```js
171
+ function formatGap({ dictionary }) {
172
+ const tokens = tokensForGroup(dictionary, 'gap');
173
+ return buildStandardFile(tokens, 'gap', BANNER_2023);
174
+ }
175
+ ```
176
+ 3. Export it from the `module.exports` array at the bottom of that file.
177
+ 4. Register the new output file in
178
+ [`style-dictionary/sd.config.js`](../style-dictionary/sd.config.js) under the
179
+ `scss` platform's `files` array:
180
+ ```js
181
+ { destination: '_gap.scss', format: 'carbon/scss-gap' }
182
+ ```
183
+ 5. Forward the new file from the appropriate `scss/_*.scss` entry point (or
184
+ create a new one and forward it from `index.scss`).
185
+
186
+ ---
187
+
188
+ ## Adding a new token
189
+
190
+ 1. Add an entry to `layout.tokens.json` following the examples above.
191
+ 2. Put it in the right group (see the table above).
192
+ 3. Choose the right `$type` and `$value`:
193
+ - A `rem` or `px` value? Use `"dimension"` with `{ "value", "unit" }`.
194
+ - A viewport-relative value? Use `"number"` with the `layout.unit` extension.
195
+ 4. Run the build to regenerate the Sass and JS outputs:
196
+ ```bash
197
+ cd packages/layout
198
+ yarn build
199
+ ```
200
+ 5. Check the value in `js/generated/layout-tokens.js` and the matching
201
+ `scss/generated/_*.scss` file.
202
+ 6. Run `yarn test packages/layout` from the repo root. The conformance test
203
+ validates the file against the DTCG schema.
204
+
205
+ ---
206
+
207
+ ## Build pipeline
208
+
209
+ ```
210
+ tokens/layout.tokens.json ← you edit this
211
+ │
212
+ ▼ yarn build:tokens
213
+ │ (style-dictionary/sd.config.js)
214
+ │
215
+ ├── scss/generated/*.scss (Sass variables + maps, one file per token group)
216
+ └── js/generated/layout-tokens.{js,d.ts} (ES module + types)
217
+ │
218
+ ▼
219
+ src/index.ts re-exports all tokens
220
+ ```