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(themes): correctly set carbon--theme when nesting themes #5760

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
18 changes: 18 additions & 0 deletions packages/themes/__tests__/mixins-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ describe('_mixins.scss', () => {
expect(colors[2]).toBe(`var(--cds-interactive-01, ${colors[0]})`);
});

it('should set the global carbon--theme to match the given theme', async () => {
const { calls } = await render(`
@import '../scss/themes';
$carbon--theme: ( value-01: #000000 );
$custom-theme: ( value-01: #ffffff );

@include carbon--theme($custom-theme) {
$t: test($carbon--theme);
}

$t: test($carbon--theme);
`);

const [custom, reset] = calls.map(([call]) => convert(call));
expect(custom['value-01']).toBe('#ffffff');
expect(reset['value-01']).toBe('#000000');
});

describe('@mixin custom-property', () => {
it('should create a custom property for a given token name and value', async () => {
const { result } = await render(`
Expand Down
21 changes: 18 additions & 3 deletions packages/themes/tasks/builders/mixins.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ function buildMixinsFile(themes, tokens, defaultTheme, defaultThemeMapName) {
],
body: t.BlockStatement({
body: [
t.Assignment({
id: t.Identifier('parent-carbon-theme'),
init: t.Identifier('carbon--theme'),
}),
t.Assignment({
id: t.Identifier('carbon--theme'),
init: t.Identifier('theme'),
global: true,
}),
...Object.keys(tokens).flatMap(group => {
return tokens[group].flatMap(token => {
const name = formatTokenName(token);
Expand Down Expand Up @@ -131,7 +140,7 @@ function buildMixinsFile(themes, tokens, defaultTheme, defaultThemeMapName) {
t.IfStatement({
test: t.SassFunctionCall(t.Identifier('should-emit'), [
t.Identifier('theme'),
t.Identifier('carbon--theme'),
t.Identifier('parent-carbon-theme'),
t.SassString(name),
t.Identifier('emit-difference'),
]),
Expand All @@ -151,14 +160,20 @@ function buildMixinsFile(themes, tokens, defaultTheme, defaultThemeMapName) {
),
}),
t.AtContent(),
t.Newline(),
t.Comment(' Reset to default theme after apply in content'),
t.IfStatement({
test: t.LogicalExpression({
left: t.Identifier('theme'),
left: t.Identifier('carbon--theme'),
operator: '!=',
right: t.Identifier(defaultThemeMapName),
right: t.Identifier('parent-carbon-theme'),
}),
consequent: t.BlockStatement([
t.Assignment({
id: t.Identifier('carbon--theme'),
init: t.Identifier('parent-carbon-theme'),
global: true,
}),
t.SassMixinCall(t.Identifier('carbon--theme')),
]),
}),
Expand Down