From 7f222a309a2d92105ee3d9379f241d708484609f Mon Sep 17 00:00:00 2001 From: Josh Black Date: Thu, 9 Jun 2022 10:16:39 -0500 Subject: [PATCH] fix(styles): emit correct values for component tokens in zones (#11573) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/styles/package.json | 1 + packages/styles/scss/__tests__/zone-test.js | 76 +++++++++++++++++++++ packages/styles/scss/_zone.scss | 4 +- yarn.lock | 1 + 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 packages/styles/scss/__tests__/zone-test.js diff --git a/packages/styles/package.json b/packages/styles/package.json index 20ebf6399bed..1702231057df 100644 --- a/packages/styles/package.json +++ b/packages/styles/package.json @@ -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", diff --git a/packages/styles/scss/__tests__/zone-test.js b/packages/styles/scss/__tests__/zone-test.js new file mode 100644 index 000000000000..111a0ad8ba16 --- /dev/null +++ b/packages/styles/scss/__tests__/zone-test.js @@ -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); + } + }); +}); diff --git a/packages/styles/scss/_zone.scss b/packages/styles/scss/_zone.scss index aba9557a7304..bd222aa4c2d4 100644 --- a/packages/styles/scss/_zone.scss +++ b/packages/styles/scss/_zone.scss @@ -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 { diff --git a/yarn.lock b/yarn.lock index 655d75d88358..39372632aa37 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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