Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(styles): emit correct values for component tokens in zones #11573

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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