@carbon/layout 11.61.0 → 11.62.0-rc.0

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
@@ -43,9 +43,10 @@ proposing the new addition!
43
43
 
44
44
  ### Modifying token values
45
45
 
46
- Token values are defined in [`src/dtcg/layout.json`](./src/dtcg/layout.json)
47
- using the [DTCG token format](https://tr.designtokens.org/format/). This is the
48
- single source of truth for the package — **do not edit generated files
46
+ Token values are defined in
47
+ [`tokens/layout.tokens.json`](./tokens/layout.tokens.json) using the
48
+ [DTCG token format](https://www.designtokens.org/tr/2025.10/format/). This is
49
+ the single source of truth for the package — **do not edit generated files
49
50
  directly**.
50
51
 
51
52
  The following files are generated at build time and should not be hand-edited:
@@ -56,13 +57,13 @@ The following files are generated at build time and should not be hand-edited:
56
57
 
57
58
  To add or update a token:
58
59
 
59
- 1. Edit `src/dtcg/layout.json`
60
+ 1. Edit `tokens/layout.tokens.json`
60
61
  2. Run `yarn build` in this package to regenerate all outputs
61
62
  3. Run `yarn test --testPathPatterns=packages/layout` from the repo root to
62
63
  confirm nothing regressed
63
64
 
64
- For a detailed guide to the token format, converters, and how to add a new token
65
- category, see [`src/dtcg/README.md`](./src/dtcg/README.md).
65
+ For a detailed guide to the token format and how to add a new token category,
66
+ see [`tokens/README.md`](./tokens/README.md).
66
67
 
67
68
  ## 🙌 Contributing
68
69
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@carbon/layout",
3
3
  "description": "Layout helpers for digital and software products using the Carbon Design System",
4
- "version": "11.61.0",
4
+ "version": "11.62.0-rc.0",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": [
7
7
  "index.scss",
@@ -32,17 +32,14 @@
32
32
  },
33
33
  "scripts": {
34
34
  "build:types": "tsc -p tsconfig.types.json",
35
- "build:dtcg": "node -e \"require('./style-dictionary/sd.config.js').run()\"",
36
- "build": "yarn clean && yarn build:dtcg && carbon-cli bundle src/index.ts --name CarbonLayout && yarn build:types",
35
+ "build:tokens": "node -e \"require('./style-dictionary/sd.config.js').run()\"",
36
+ "build": "yarn clean && yarn build:tokens && carbon-cli bundle src/index.ts --name CarbonLayout && yarn build:types",
37
37
  "clean": "rimraf es lib umd scss/generated js/generated",
38
38
  "postinstall": "ibmtelemetry --config=telemetry.yml"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@carbon/cli": "^11.52.0",
42
- "@carbon/cli-reporter": "^10.9.0",
43
- "@carbon/scss-generator": "^10.20.0",
44
- "@carbon/test-utils": "^10.42.0",
45
- "core-js": "^3.16.0",
42
+ "@carbon/test-utils": "^10.43.0-rc.0",
46
43
  "rimraf": "^6.0.1",
47
44
  "style-dictionary": "^5.5.0",
48
45
  "typescript": "^6.0.3",
@@ -51,5 +48,5 @@
51
48
  "dependencies": {
52
49
  "@ibm/telemetry-js": "^1.5.0"
53
50
  },
54
- "gitHead": "44f90d8d6b24889af06cee1422ceb37f58e40bed"
51
+ "gitHead": "ba585c78173520aefb8dfcdb388f9183b9cc1195"
55
52
  }
package/src/index.ts CHANGED
@@ -5,9 +5,9 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- // Re-export individual token constants generated from src/dtcg/layout.json
9
- // via the Style Dictionary pipeline (tasks/build.mjs → style-dictionary/sd.config.js).
10
- // The generated file is kept in sync with the DTCG source of truth.
8
+ // Re-export individual token constants generated from
9
+ // tokens/layout.tokens.json via the Style Dictionary pipeline
10
+ // (style-dictionary/sd.config.js). Run `yarn build` to generate them.
11
11
  export * from '../js/generated/layout-tokens';
12
12
 
13
13
  import { unstable_tokens } from './tokens';
@@ -43,33 +43,6 @@ function kebabToCamel(str) {
43
43
  return str.replace(/-([a-z0-9])/g, (_, ch) => ch.toUpperCase());
44
44
  }
45
45
 
46
- const BASE_FONT_SIZE = 16;
47
- const MINI_UNIT = 8;
48
-
49
- /**
50
- * Resolve a token's raw $value using the carbon.layout.converter extension.
51
- * - "miniUnits": steps × 8px ÷ 16 → rem string
52
- * - "rem": px ÷ 16 → rem string
53
- * - (none): value is already a resolved CSS string, returned as-is
54
- *
55
- * @param {string|number} value
56
- * @param {object|undefined} carbonLayout — token.$extensions?.['carbon.layout']
57
- * @returns {string}
58
- */
59
- function resolveValue(value, carbonLayout) {
60
- const converter = carbonLayout?.converter;
61
- if (converter === 'miniUnits') {
62
- return `${(Number(value) * MINI_UNIT) / BASE_FONT_SIZE}rem`;
63
- }
64
- if (converter === 'rem') {
65
- return `${Number(value) / BASE_FONT_SIZE}rem`;
66
- }
67
- // Unitless zero — preserve as number 0, not the string "0", to match the
68
- // original hand-authored export: `export const fluidSpacing01 = 0`
69
- if (value === '0' || value === 0) return 0;
70
- return String(value);
71
- }
72
-
73
46
  /**
74
47
  * @param {{ dictionary: import('style-dictionary').Dictionary, options: object }} args
75
48
  * @returns {string}
@@ -80,10 +53,9 @@ function carbonJsLayoutFormat({ dictionary, options }) {
80
53
 
81
54
  for (const token of dictionary.allTokens) {
82
55
  const camelName = kebabToCamel(token.name);
83
- const carbonLayout =
84
- token.$extensions?.['carbon.layout'] ??
85
- token.extensions?.['carbon.layout'];
86
- const resolved = resolveValue(token.value ?? token.$value, carbonLayout);
56
+ // Unitless zero — preserve as number 0, not the string "0", to match the
57
+ // original hand-authored export: `export const fluidSpacing01 = 0`
58
+ const resolved = token.$value === '0' ? 0 : token.$value;
87
59
 
88
60
  const tsType = resolved === 0 ? 'number' : 'string';
89
61
  if (outputMode === 'js') {
@@ -10,9 +10,9 @@
10
10
  /**
11
11
  * Style Dictionary format — carbon/scss-layout
12
12
  *
13
- * Generates per-category Sass files from layout.json whose content is
14
- * byte-for-byte identical to what build.mjs / @carbon/scss-generator produced,
15
- * so that nothing downstream breaks.
13
+ * Generates per-category Sass files from layout.tokens.json. The content is
14
+ * byte-for-byte identical to what the previous @carbon/scss-generator build
15
+ * produced, so that nothing downstream breaks.
16
16
  *
17
17
  * Files emitted:
18
18
  * _spacing.scss variables + map, !default
@@ -24,7 +24,7 @@
24
24
  * _size.scss bare assignments, no !default, no map
25
25
  */
26
26
 
27
- // ── Banners ───────────────────────────────────────────────────────────────────
27
+ // Banners
28
28
  // Most files use 2023; _size.scss uses 2026 — match exactly.
29
29
  const BANNER_2023 = `// Code generated by @carbon/layout. DO NOT EDIT.
30
30
  //
@@ -50,27 +50,6 @@ const MAP_COMMENT = `/// @type Map
50
50
  /// @access public
51
51
  /// @group @carbon/layout`;
52
52
 
53
- // ── Constants ─────────────────────────────────────────────────────────────────
54
- const BASE_FONT_SIZE = 16;
55
- const MINI_UNIT = 8;
56
-
57
- /**
58
- * Resolve a token's $value using carbon.layout.converter if present.
59
- * @param {string|number} value
60
- * @param {object|undefined} carbonLayout
61
- * @returns {string}
62
- */
63
- function resolveValue(value, carbonLayout) {
64
- const converter = carbonLayout?.converter;
65
- if (converter === 'miniUnits') {
66
- return `${(Number(value) * MINI_UNIT) / BASE_FONT_SIZE}rem`;
67
- }
68
- if (converter === 'rem') {
69
- return `${Number(value) / BASE_FONT_SIZE}rem`;
70
- }
71
- return String(value);
72
- }
73
-
74
53
  /**
75
54
  * Build a standard file: banner + (comment + $var: value !default;) * n + map.
76
55
  * @param {Array<{name: string, resolved: string}>} tokens
@@ -123,17 +102,10 @@ function buildSizeFile(tokens) {
123
102
  function tokensForGroup(dictionary, groupName) {
124
103
  return dictionary.allTokens
125
104
  .filter((t) => t.path[0] === groupName)
126
- .map((t) => {
127
- const carbonLayout =
128
- t.$extensions?.['carbon.layout'] ?? t.extensions?.['carbon.layout'];
129
- return {
130
- name: t.name,
131
- resolved: resolveValue(t.value ?? t.$value, carbonLayout),
132
- };
133
- });
105
+ .map((t) => ({ name: t.name, resolved: t.$value }));
134
106
  }
135
107
 
136
- // ── Format functions — one per DTCG group ─────────────────────────────────────
108
+ // Format functions — one per DTCG group
137
109
 
138
110
  function formatSpacing({ dictionary }) {
139
111
  const tokens = tokensForGroup(dictionary, 'spacing');
@@ -175,7 +147,7 @@ function formatSize({ dictionary }) {
175
147
  return buildSizeFile(tokens);
176
148
  }
177
149
 
178
- // ── Exports ───────────────────────────────────────────────────────────────────
150
+ // Exports
179
151
 
180
152
  module.exports = [
181
153
  { name: 'carbon/scss-spacing', format: formatSpacing },
@@ -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
+ ```