Skip to content

Commit 5214919

Browse files
authored
feat(styles): add support for a compat theme (#9235)
* feat(themes): add support for a compat theme * feat(styles): add support for a compat theme * refactor(styles): add support for compat theme * chore(themes): update builder to ensure filepath * chore(themes): ignore compat directory * chore(cli): update cli to ignore compat dirs * fix(styles): add support for compat in stylesheets * test: update test for custom properties
1 parent c9ce0fc commit 5214919

File tree

35 files changed

+571
-89
lines changed

35 files changed

+571
-89
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
run: yarn build --ignore '@carbon/sketch'
6868
- name: Check generated styles
6969
run: |
70-
yarn carbon-cli check --ignore '**/@(node_modules|examples|components|react|fixtures)/**' 'packages/**/*.scss'
70+
yarn carbon-cli check --ignore '**/@(node_modules|examples|components|react|fixtures|compat)/**' 'packages/**/*.scss'
7171
- name: Run tests
7272
run: yarn test --ci
7373

packages/carbon-react/.storybook/Welcome/welcome.scss

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// @import '~carbon-components/scss/globals/scss/typography';
2-
@use '@carbon/styles/scss/theme' as *;
1+
@use '@carbon/styles/scss/theme';
32
@use '@carbon/styles/scss/type';
43

54
.container-welcome {
@@ -19,7 +18,7 @@
1918
.welcome__heading {
2019
@include type.type-style('productive-heading-07');
2120

22-
color: $text-inverse;
21+
color: theme.$text-inverse;
2322
}
2423

2524
.welcome__heading--subtitle {

packages/cli/src/commands/ci-check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function check(args, env) {
2121
stdio: 'inherit',
2222
};
2323
const tasks = [
24-
`yarn carbon-cli check --ignore '**/@(node_modules|examples|components|react|fixtures)/**' 'packages/**/*.scss'`,
24+
`yarn carbon-cli check --ignore '**/@(node_modules|examples|components|react|fixtures|compat)/**' 'packages/**/*.scss'`,
2525
`cross-env BABEL_ENV=test yarn test --ci --maxWorkers 2 --reporters=default --reporters=jest-junit`,
2626
`cross-env BABEL_ENV=test yarn test:e2e --ci --maxWorkers 2 --reporters=default --reporters=jest-junit`,
2727
`cross-env PERCY_TOKEN=dd3392142006a6483c8f524697f39f052755fa9dc087ff4437cac3d64227abdd yarn run percy exec -- yarn workspace carbon-components-react test:e2e`,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright IBM Corp. 2018, 2018
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+
* @jest-environment node
8+
*/
9+
10+
'use strict';
11+
12+
const { SassRenderer } = require('@carbon/test-utils/scss');
13+
14+
const { render } = SassRenderer.create(__dirname);
15+
16+
describe('@carbon/styles/scss/compat', () => {
17+
it('should export white, g10, g90, and g100 themes', async () => {
18+
const { unwrap } = await render(`
19+
@use 'sass:map';
20+
@use 'sass:meta';
21+
@use '../scss/compat/themes';
22+
23+
$_: get('variables', map.keys(meta.module-variables('themes')));
24+
`);
25+
const themes = unwrap('variables').sort();
26+
expect(themes).toEqual(['white', 'g10', 'g90', 'g100'].sort());
27+
});
28+
29+
it('should export v10 tokens as Sass Variables', async () => {
30+
const { unwrap } = await render(`
31+
@use '../scss/config' with ( $prefix: 'cds' );
32+
@use '../scss/compat/themes';
33+
@use '../scss/compat/theme' with (
34+
$theme: themes.$white,
35+
);
36+
37+
$_: get('theme', themes.$white);
38+
$_: get('variable', theme.$interactive-01);
39+
`);
40+
const theme = unwrap('theme');
41+
const variable = unwrap('variable');
42+
43+
expect(variable).toEqual(
44+
`var(--cds-interactive-01, ${theme['interactive-01']})`
45+
);
46+
});
47+
48+
it('should export v11 tokens that match the fallback theme', async () => {
49+
const { unwrap } = await render(`
50+
@use '../scss/config' with ( $prefix: 'cds' );
51+
@use '../scss/themes';
52+
@use '../scss/compat/themes' as compat;
53+
@use '../scss/compat/theme' with (
54+
$fallback: themes.$g100,
55+
$theme: compat.$g100,
56+
);
57+
58+
$_: get('theme', themes.$g100);
59+
$_: get('variable', theme.$background);
60+
`);
61+
62+
const theme = unwrap('theme');
63+
const variable = unwrap('variable');
64+
65+
expect(variable).toEqual(`var(--cds-background, ${theme['background']})`);
66+
});
67+
});

packages/styles/docs/sass.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ between version updates.
4848
| :------------------------------------- | :--------------------------------------------------------- |
4949
| [`scss/breakpoint`](#breakpoint) | Helper functions and mixins for working with breakpoints |
5050
| [`scss/colors`](#colors) | Access colors from every swatch in the IBM Design Language |
51+
| [`scss/compat/`](#compatibility) | Helper themes and tokens for migrating from v10 to v11 |
5152
| [`scss/config`](#config) | Configure various options for the package |
5253
| [`scss/feature-flags`](#feature-flags) | Configure various feature flags for experiments |
5354
| [`scss/grid`](#grid) | Access and use the CSS Grid built-in to Carbon |
@@ -331,6 +332,48 @@ like to see changed. For example, if you wanted to change the component token
331332
| :---------------------------------------------------- | :---------- |
332333
| `@use '@carbon/styles/scss/utilities/focus-outline';` | |
333334

335+
## Compatibility
336+
337+
| Import | Filepath |
338+
| :------------------------------------------ | :------------------------- |
339+
| `@use '@carbon/styles/scss/compat/themes';` | `scss/compat/_themes.scss` |
340+
| `@use '@carbon/styles/scss/compat/theme';` | `scss/compat/_theme.scss` |
341+
342+
The compatibility entrypoints for themes and theme provide access to the v10
343+
tokens along with the v11 tokens. To make sure that the tokens that you're using
344+
from v10 have the correct value in v11, you will need to include the theme that
345+
you're using from `scss/compat/themes` and set that as your theme.
346+
347+
```scss
348+
@use '@carbon/styles/scss/compat/themes' as compat;
349+
@use '@carbon/styles/scss/themes';
350+
@use '@carbon/styles/scss/compat/theme' with (
351+
$fallback: themes.$g100,
352+
$theme: compat.$g100,
353+
);
354+
```
355+
356+
It's important that you specify the `$fallback` theme as a value from the
357+
`scss/themes` entrypoint. This will guarantee that all tokens that you are using
358+
from v11 will match the theme of the tokens that you are using from v10.
359+
360+
You can directly reference a token from the `scss/compat/theme` entrypoint. This
361+
entrypoint will also re-export all available v11 tokens and mixins from
362+
`scss/theme`.
363+
364+
```scss
365+
@use '@carbon/styles/scss/compat/theme';
366+
367+
body {
368+
// You can use both v10 and v11 tokens
369+
background: theme.$background;
370+
color: theme.$text-01;
371+
}
372+
```
373+
374+
_Note: all tokens from v10 are deprecated in v11. They will be removed in the
375+
next major release of Carbon_
376+
334377
## Configuration
335378

336379
You will need to configure Sass to be able to lookup packages from your

packages/styles/scss/__tests__/theme-test.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ describe('@carbon/styles/scss/theme', () => {
3636
Array [
3737
"fallback",
3838
"theme",
39-
"use-fallback-value",
4039
"background",
4140
"background-active",
4241
"background-selected",
@@ -145,28 +144,6 @@ Array [
145144
"border-subtle",
146145
"border-subtle-selected",
147146
"border-strong",
148-
"interactive-01",
149-
"interactive-04",
150-
"decorative-01",
151-
"hover-row",
152-
"ui-01",
153-
"ui-02",
154-
"ui-05",
155-
"disabled-01",
156-
"disabled-02",
157-
"hover-field",
158-
"hover-ui",
159-
"selected-ui",
160-
"hover-selected-ui",
161-
"hover-light-ui",
162-
"active-light-ui",
163-
"text-01",
164-
"text-02",
165-
"text-04",
166-
"text-05",
167-
"icon-01",
168-
"icon-02",
169-
"link-01",
170147
]
171148
`);
172149
});

packages/styles/scss/_theme.scss

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
@use '@carbon/themes/scss/modules/config' with (
1010
$prefix: $prefix,
1111
);
12-
@use '@carbon/themes/scss/modules/tokens';
13-
@forward '@carbon/themes/scss/modules/theme';
12+
@use './compat/themes' as compat;
13+
@use './themes';
14+
15+
@forward '@carbon/themes/scss/modules/theme' with (
16+
$fallback: themes.$white !default,
17+
$theme: compat.$white !default,
18+
);
1419
@forward '@carbon/themes/scss/modules/tokens';
20+
@use '@carbon/themes/scss/modules/tokens';
1521
@use './utilities/custom-property';
1622
@use './utilities/layer-set' with (
1723
$layer-sets: (
@@ -99,27 +105,3 @@ $field-hover: custom-property.get-var('field-hover');
99105
$border-subtle: custom-property.get-var('border-subtle');
100106
$border-subtle-selected: custom-property.get-var('border-subtle-selected');
101107
$border-strong: custom-property.get-var('border-strong');
102-
103-
// TODO remove
104-
$interactive-01: #0f62fe;
105-
$interactive-04: #0f62fe;
106-
$decorative-01: #e0e0e0;
107-
$hover-row: #636363;
108-
$ui-01: #f4f4f4;
109-
$ui-02: #ffffff;
110-
$ui-05: #161616;
111-
$disabled-01: #f4f4f4;
112-
$disabled-02: #c6c6c6;
113-
$hover-field: #e5e5e5;
114-
$hover-ui: #e5e5e5;
115-
$selected-ui: #e0e0e0;
116-
$hover-selected-ui: #cacaca;
117-
$hover-light-ui: #e5e5e5;
118-
$active-light-ui: #c6c6c6;
119-
$text-01: #161616;
120-
$text-02: #525252;
121-
$text-04: #ffffff;
122-
$text-05: #6f6f6f;
123-
$icon-01: #f4f4f4;
124-
$icon-02: #525252;
125-
$link-01: #0f62fe;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//
2+
// Copyright IBM Corp. 2018, 2018
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+
@forward '../theme';
9+
@forward '@carbon/themes/scss/compat/tokens';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Copyright IBM Corp. 2018, 2018
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+
@forward '@carbon/themes/scss/compat/themes' show $white, $g10, $g90, $g100;

packages/styles/scss/components/code-snippet/_code-snippet.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
// LICENSE file in the root directory of this source tree.
66
//
77

8-
@use 'mixins' as *;
9-
@use '../copy-button';
108
@use '../../config' as *;
119
@use '../../motion' as *;
1210
@use '../../spacing' as *;
13-
@use '../../theme' as *;
11+
@use '../../compat/theme' as *;
1412
@use '../../type' as *;
13+
@use '../copy-button';
1514
@use '../../utilities/convert' as *;
1615
@use '../../utilities/focus-outline' as *;
1716
@use '../../utilities/high-contrast-mode' as *;
1817
@use '../../utilities/keyframes' as *;
1918
@use '../../utilities/skeleton' as *;
2019
@use '../../utilities/tooltip' as *;
2120
@use '../../utilities/z-index' as *;
21+
@use 'mixins' as *;
2222

2323
/// @type Color
2424
/// @access public

0 commit comments

Comments
 (0)