@carbon/layout 12.0.0-alpha.2 → 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.
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Copyright IBM Corp. 2018, 2026
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * Style Dictionary format — carbon/js-layout
12
+ *
13
+ * Emits either:
14
+ * - An ES module (.js) with `export const camelName = value;`
15
+ * - A TypeScript declaration file (.d.ts) with `export declare const camelName: string;`
16
+ *
17
+ * Token names arrive as kebab-case from SD (e.g. "spacing-01") and are
18
+ * converted to camelCase ("spacing01") so the output matches the existing
19
+ * hand-authored exports in src/index.ts.
20
+ */
21
+
22
+ const FILE_BANNER = `// Code generated by @carbon/layout. DO NOT EDIT.
23
+ //
24
+ // Copyright IBM Corp. 2018, 2026
25
+ //
26
+ // This source code is licensed under the Apache-2.0 license found in the
27
+ // LICENSE file in the root directory of this source tree.
28
+ `;
29
+
30
+ /**
31
+ * Convert kebab-case to camelCase.
32
+ * "spacing-01" → "spacing01"
33
+ * "fluid-spacing-02" → "fluidSpacing02"
34
+ * "border-radius-08" → "borderRadius08"
35
+ * "size-xs" → "sizeXs" (kept for completeness; index re-exports under SizeName aliases)
36
+ * "icon-size-01" → "iconSize01"
37
+ * "layout-03" → "layout03"
38
+ *
39
+ * @param {string} str
40
+ * @returns {string}
41
+ */
42
+ function kebabToCamel(str) {
43
+ return str.replace(/-([a-z0-9])/g, (_, ch) => ch.toUpperCase());
44
+ }
45
+
46
+ /**
47
+ * @param {{ dictionary: import('style-dictionary').Dictionary, options: object }} args
48
+ * @returns {string}
49
+ */
50
+ function carbonJsLayoutFormat({ dictionary, options }) {
51
+ const outputMode = options?.output ?? 'js'; // 'js' | 'dts'
52
+ const lines = [FILE_BANNER];
53
+
54
+ for (const token of dictionary.allTokens) {
55
+ const camelName = kebabToCamel(token.name);
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;
59
+
60
+ const tsType = resolved === 0 ? 'number' : 'string';
61
+ if (outputMode === 'js') {
62
+ // Numeric 0 is emitted bare (no quotes) to preserve the original type.
63
+ const src = resolved === 0 ? '0' : `'${resolved}'`;
64
+ lines.push(`export const ${camelName} = ${src};`);
65
+ } else {
66
+ lines.push(`export declare const ${camelName}: ${tsType};`);
67
+ }
68
+ }
69
+
70
+ return lines.join('\n') + '\n';
71
+ }
72
+
73
+ module.exports = {
74
+ name: 'carbon/js-layout',
75
+ format: carbonJsLayoutFormat,
76
+ };
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Copyright IBM Corp. 2018, 2026
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * Style Dictionary format — carbon/scss-layout
12
+ *
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
+ *
17
+ * Files emitted:
18
+ * _spacing.scss variables + map, !default
19
+ * _fluid-spacing.scss variables + map, !default
20
+ * _container.scss variables + map, !default
21
+ * _icon-size.scss variables + map, !default
22
+ * _border-radius.scss variables + map, !default
23
+ * _layout.scss variables + map, !default (deprecated tokens)
24
+ * _size.scss bare assignments, no !default, no map
25
+ */
26
+
27
+ // Banners
28
+ // Most files use 2023; _size.scss uses 2026 — match exactly.
29
+ const BANNER_2023 = `// Code generated by @carbon/layout. DO NOT EDIT.
30
+ //
31
+ // Copyright IBM Corp. 2018, 2023
32
+ //
33
+ // This source code is licensed under the Apache-2.0 license found in the
34
+ // LICENSE file in the root directory of this source tree.
35
+ //`;
36
+
37
+ const BANNER_2026 = `// Code generated by @carbon/layout. DO NOT EDIT.
38
+ //
39
+ // Copyright IBM Corp. 2018, 2026
40
+ //
41
+ // This source code is licensed under the Apache-2.0 license found in the
42
+ // LICENSE file in the root directory of this source tree.
43
+ //`;
44
+
45
+ const VAR_COMMENT = `/// @type Number
46
+ /// @access public
47
+ /// @group @carbon/layout`;
48
+
49
+ const MAP_COMMENT = `/// @type Map
50
+ /// @access public
51
+ /// @group @carbon/layout`;
52
+
53
+ /**
54
+ * Build a standard file: banner + (comment + $var: value !default;) * n + map.
55
+ * @param {Array<{name: string, resolved: string}>} tokens
56
+ * @param {string} mapName — Sass map variable name
57
+ * @param {string} banner
58
+ * @returns {string}
59
+ */
60
+ function buildStandardFile(tokens, mapName, banner) {
61
+ const lines = [banner, ''];
62
+
63
+ for (const { name, resolved } of tokens) {
64
+ lines.push(VAR_COMMENT);
65
+ lines.push(`$${name}: ${resolved} !default;`);
66
+ lines.push('');
67
+ }
68
+
69
+ lines.push(MAP_COMMENT);
70
+ lines.push(`$${mapName}: (`);
71
+ for (const { name } of tokens) {
72
+ lines.push(` ${name}: $${name},`);
73
+ }
74
+ lines.push(');');
75
+
76
+ return lines.join('\n') + '\n';
77
+ }
78
+
79
+ /**
80
+ * Build _size.scss — no !default, no map, no per-variable comments.
81
+ * @param {Array<{name: string, resolved: string}>} tokens
82
+ * @returns {string}
83
+ */
84
+ function buildSizeFile(tokens) {
85
+ const lines = [BANNER_2026, ''];
86
+ lines.push(VAR_COMMENT);
87
+ for (const { name, resolved } of tokens) {
88
+ lines.push(`$${name}: ${resolved};`);
89
+ }
90
+ return lines.join('\n') + '\n';
91
+ }
92
+
93
+ /**
94
+ * Extract tokens belonging to a DTCG group from the SD dictionary.
95
+ * Routing is done by token.path[0] — the group name in the JSON — which is
96
+ * the native DTCG mechanism. No prefix inference needed.
97
+ *
98
+ * @param {import('style-dictionary').Dictionary} dictionary
99
+ * @param {string} groupName — top-level DTCG group key (e.g. "spacing")
100
+ * @returns {Array<{name: string, resolved: string}>}
101
+ */
102
+ function tokensForGroup(dictionary, groupName) {
103
+ return dictionary.allTokens
104
+ .filter((t) => t.path[0] === groupName)
105
+ .map((t) => ({ name: t.name, resolved: t.$value }));
106
+ }
107
+
108
+ // Format functions — one per DTCG group
109
+
110
+ function formatSpacing({ dictionary }) {
111
+ const tokens = tokensForGroup(dictionary, 'spacing');
112
+ return buildStandardFile(tokens, 'spacing', BANNER_2023);
113
+ }
114
+
115
+ function formatFluidSpacing({ dictionary }) {
116
+ const tokens = tokensForGroup(dictionary, 'fluid-spacing');
117
+ // Sass expects unitless 0, not the string "0"
118
+ const fixed = tokens.map((t) => ({
119
+ ...t,
120
+ resolved: t.resolved === '0' ? '0' : t.resolved,
121
+ }));
122
+ return buildStandardFile(fixed, 'fluid-spacing', BANNER_2023);
123
+ }
124
+
125
+ function formatContainer({ dictionary }) {
126
+ const tokens = tokensForGroup(dictionary, 'container');
127
+ return buildStandardFile(tokens, 'container', BANNER_2023);
128
+ }
129
+
130
+ function formatIconSize({ dictionary }) {
131
+ const tokens = tokensForGroup(dictionary, 'icon-size');
132
+ return buildStandardFile(tokens, 'icon-size', BANNER_2023);
133
+ }
134
+
135
+ function formatBorderRadius({ dictionary }) {
136
+ const tokens = tokensForGroup(dictionary, 'border-radius');
137
+ return buildStandardFile(tokens, 'border-radius', BANNER_2023);
138
+ }
139
+
140
+ function formatLayout({ dictionary }) {
141
+ const tokens = tokensForGroup(dictionary, 'layout');
142
+ return buildStandardFile(tokens, 'layout', BANNER_2023);
143
+ }
144
+
145
+ function formatSize({ dictionary }) {
146
+ const tokens = tokensForGroup(dictionary, 'size');
147
+ return buildSizeFile(tokens);
148
+ }
149
+
150
+ // Exports
151
+
152
+ module.exports = [
153
+ { name: 'carbon/scss-spacing', format: formatSpacing },
154
+ { name: 'carbon/scss-fluid-spacing', format: formatFluidSpacing },
155
+ { name: 'carbon/scss-container', format: formatContainer },
156
+ { name: 'carbon/scss-icon-size', format: formatIconSize },
157
+ { name: 'carbon/scss-border-radius', format: formatBorderRadius },
158
+ { name: 'carbon/scss-layout', format: formatLayout },
159
+ { name: 'carbon/scss-size', format: formatSize },
160
+ ];
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Copyright IBM Corp. 2018, 2026
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ /**
11
+ * Style Dictionary configuration for @carbon/layout.
12
+ *
13
+ * Reads tokens/layout.tokens.json and generates:
14
+ * scss/generated/*.scss — Sass variables + maps (one file per token group)
15
+ * js/generated/layout-tokens.js — ES module named exports
16
+ * js/generated/layout-tokens.d.ts — TypeScript declarations
17
+ */
18
+
19
+ const path = require('path');
20
+ const { default: StyleDictionary } = require('style-dictionary');
21
+
22
+ const scssFormats = require('./formats/scss-layout');
23
+ const carbonJsLayout = require('./formats/js-layout');
24
+
25
+ // Paths
26
+ const ROOT = path.resolve(__dirname, '..');
27
+ const TOKENS_SOURCE = path.join(ROOT, 'tokens', 'layout.tokens.json');
28
+ const SCSS_GENERATED = path.join(ROOT, 'scss', 'generated');
29
+ const JS_GENERATED = path.join(ROOT, 'js', 'generated');
30
+
31
+ // Custom name transform
32
+ // Uses only the token's own key (last path segment) as the name, discarding
33
+ // the group prefix. With DTCG groups, path is ["spacing", "spacing-01"] so
34
+ // joining would produce "spacing-spacing-01" — we want just "spacing-01".
35
+ const carbonNameKebab = {
36
+ name: 'carbon/name-kebab',
37
+ type: 'name',
38
+ transform(token) {
39
+ return token.path[token.path.length - 1];
40
+ },
41
+ };
42
+
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
+ ];
67
+
68
+ // SD config
69
+ const config = {
70
+ source: [TOKENS_SOURCE],
71
+ platforms: {
72
+ scss: {
73
+ transformGroup: 'carbon',
74
+ buildPath: SCSS_GENERATED + '/',
75
+ files: [
76
+ { destination: '_spacing.scss', format: 'carbon/scss-spacing' },
77
+ {
78
+ destination: '_fluid-spacing.scss',
79
+ format: 'carbon/scss-fluid-spacing',
80
+ },
81
+ { destination: '_container.scss', format: 'carbon/scss-container' },
82
+ { destination: '_icon-size.scss', format: 'carbon/scss-icon-size' },
83
+ {
84
+ destination: '_border-radius.scss',
85
+ format: 'carbon/scss-border-radius',
86
+ },
87
+ { destination: '_layout.scss', format: 'carbon/scss-layout' },
88
+ { destination: '_size.scss', format: 'carbon/scss-size' },
89
+ ],
90
+ },
91
+ js: {
92
+ transformGroup: 'carbon',
93
+ buildPath: JS_GENERATED + '/',
94
+ files: [
95
+ {
96
+ destination: 'layout-tokens.js',
97
+ format: 'carbon/js-layout',
98
+ options: { output: 'js' },
99
+ },
100
+ {
101
+ destination: 'layout-tokens.d.ts',
102
+ format: 'carbon/js-layout',
103
+ options: { output: 'dts' },
104
+ },
105
+ ],
106
+ },
107
+ },
108
+ };
109
+
110
+ // Build a registered SD instance
111
+ function createInstance() {
112
+ const sd = new StyleDictionary({});
113
+ sd.registerTransform(carbonNameKebab);
114
+ sd.registerTransform(carbonCssValue);
115
+ sd.registerTransformGroup({ name: 'carbon', transforms: CARBON_TRANSFORMS });
116
+ for (const fmt of scssFormats) sd.registerFormat(fmt);
117
+ sd.registerFormat(carbonJsLayout);
118
+ return sd;
119
+ }
120
+
121
+ // run
122
+ async function run() {
123
+ const sd = await createInstance().extend(config);
124
+ await sd.buildAllPlatforms();
125
+ }
126
+
127
+ module.exports = { run };
@@ -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
+ ```