Skip to content

Commit

Permalink
fix(styles): emit correct values for component tokens in zones (#11573)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
joshblack and kodiakhq[bot] authored Jun 9, 2022
1 parent a44075a commit 7f222a3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/styles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"browserslist-config-carbon": "^11.0.0",
"css": "^3.0.0",
"cssnano": "^5.1.9",
"lodash.isequal": "^4.5.0",
"postcss": "^8.4.14",
"postcss-flexbugs-fixes": "^5.0.2",
"rimraf": "^3.0.2",
Expand Down
76 changes: 76 additions & 0 deletions packages/styles/scss/__tests__/zone-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright IBM Corp. 2018, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment node
*/

'use strict';

const { SassRenderer } = require('@carbon/test-utils/scss');
const css = require('css');
const isEqual = require('lodash.isequal');

const { render } = SassRenderer.create(__dirname);

describe('zone', () => {
it('should set a component token value to the theme of a zone', async () => {
const { get, result } = await render(`
@use '../themes';
@use '../components/button/tokens';
@use '../zone';
$_: get('themes', (
white: themes.$white,
g10: themes.$g10,
g90: themes.$g90,
g100: themes.$g100,
));
$_: get('tokens', tokens.$button-tokens);
`);

const themes = Array.from(Object.entries(get('themes').value));
const tokensByTheme = new Map(
themes.map(([theme]) => {
return [theme, new Map()];
})
);

// Group each of the button tokens into their values by theme
const buttonTokens = get('tokens');
for (const [token, { values }] of Object.entries(buttonTokens.value)) {
for (const { theme, value } of values) {
const match = themes.find(([_id, themeMap]) => {
return isEqual(themeMap, theme);
});

tokensByTheme.get(match[0]).set(token, value);
}
}

// When including the `_zone.scss` file, we generate a stylesheet with four
// classes, one per theme.
const { stylesheet } = css.parse(result.css.toString());

for (const rule of stylesheet.rules) {
// For each selector, we check that every button token that we have
// defined for this theme is emitted in CSS with the expected value
const [_prefix, theme] = rule.selectors[0].split('--');
const tokens = tokensByTheme.get(theme);
const includesComponentTokens = Array.from(tokens.entries()).every(
([token, value]) => {
return rule.declarations.find((declaration) => {
return (
declaration.property.includes(token) &&
declaration.value === value
);
});
}
);

expect(includesComponentTokens).toBe(true);
}
});
});
4 changes: 2 additions & 2 deletions packages/styles/scss/_zone.scss
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ $-components: (
$theme-values: map.get($value, values);

@each $theme-value in $theme-values {
$theme: map.get($theme-value, theme);
$value: map.get($theme-value, value);
@if theme.matches($theme, theme.$theme) and

@if theme.matches(map.get($theme-value, theme), $theme) and
meta.type-of($value) ==
color
{
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,7 @@ __metadata:
browserslist-config-carbon: ^11.0.0
css: ^3.0.0
cssnano: ^5.1.9
lodash.isequal: ^4.5.0
postcss: ^8.4.14
postcss-flexbugs-fixes: ^5.0.2
rimraf: ^3.0.2
Expand Down

0 comments on commit 7f222a3

Please sign in to comment.