Skip to content

Commit

Permalink
feat: Support media queries in optional modes
Browse files Browse the repository at this point in the history
  • Loading branch information
gethinwebster committed Jun 9, 2023
1 parent eab1f5d commit a599dc6
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/__fixtures__/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const colorMode: Mode = {
id: 'color',
states: {
light: { default: true },
dark: { selector: '.dark' },
dark: { selector: '.dark', media: 'not print' },
},
};

Expand Down
36 changes: 18 additions & 18 deletions src/browser/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports[`with baseThemeId attaches one style node containing overrides with the
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark.secondary-theme:not(#\\\\9){
@media not print {.dark.dark.secondary-theme:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:purple;
Expand All @@ -44,7 +44,7 @@ exports[`with baseThemeId attaches one style node containing overrides with the
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
}}
.disabled-motion.disabled-motion.secondary-theme:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
Expand Down Expand Up @@ -115,7 +115,7 @@ exports[`with baseThemeId attaches one style node containing overrides with the
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark.secondary-theme .navigation:not(#\\\\9){
@media not print {.dark.dark.secondary-theme .navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:purple;
Expand All @@ -129,8 +129,8 @@ exports[`with baseThemeId attaches one style node containing overrides with the
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark.navigation.secondary-theme:not(#\\\\9){
}}
@media not print {.dark.dark.navigation.secondary-theme:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:purple;
Expand All @@ -144,7 +144,7 @@ exports[`with baseThemeId attaches one style node containing overrides with the
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
}}
.disabled-motion.disabled-motion.secondary-theme .navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
Expand Down Expand Up @@ -201,7 +201,7 @@ exports[`with secondary theme attaches one style node containing override 1`] =
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark:not(#\\\\9){
@media not print {.dark.dark:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:black;
Expand All @@ -215,7 +215,7 @@ exports[`with secondary theme attaches one style node containing override 1`] =
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
}}
.disabled-motion.disabled-motion:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
Expand Down Expand Up @@ -269,7 +269,7 @@ exports[`with secondary theme attaches one style node containing override 1`] =
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark .navigation:not(#\\\\9){
@media not print {.dark.dark .navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:black;
Expand All @@ -283,8 +283,8 @@ exports[`with secondary theme attaches one style node containing override 1`] =
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark.navigation:not(#\\\\9){
}}
@media not print {.dark.dark.navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:black;
Expand All @@ -298,7 +298,7 @@ exports[`with secondary theme attaches one style node containing override 1`] =
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
}}
.disabled-motion.disabled-motion .navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
Expand Down Expand Up @@ -355,7 +355,7 @@ exports[`without secondary theme attaches one style node containing override 1`]
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark:not(#\\\\9){
@media not print {.dark.dark:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:black;
Expand All @@ -369,7 +369,7 @@ exports[`without secondary theme attaches one style node containing override 1`]
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
}}
.disabled-motion.disabled-motion:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
Expand Down Expand Up @@ -423,7 +423,7 @@ exports[`without secondary theme attaches one style node containing override 1`]
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark .navigation:not(#\\\\9){
@media not print {.dark.dark .navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:black;
Expand All @@ -437,8 +437,8 @@ exports[`without secondary theme attaches one style node containing override 1`]
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
.dark.dark.navigation:not(#\\\\9){
}}
@media not print {.dark.dark.navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
--black-css:black;
Expand All @@ -452,7 +452,7 @@ exports[`without secondary theme attaches one style node containing override 1`]
--medium-css:3px;
--containerShadowBase-css:2px 3px orange, -1px 0 8px olive;
--modalShadowContainer-css:2px 3px orange, -1px 0 8px olive;
}
}}
.disabled-motion.disabled-motion .navigation:not(#\\\\9){
--fontFamilyBase-css:\\"Helvetica Neue\\", Arial, sans-serif;
--fontFamilyBody-css:\\"Helvetica Neue\\", Arial, sans-serif;
Expand Down
6 changes: 4 additions & 2 deletions src/build/tasks/__tests__/__snapshots__/preset.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ exports[`renderCJSPreset matches previous snapshot 1`] = `
\\"default\\": true
},
\\"dark\\": {
\\"selector\\": \\".dark\\"
\\"selector\\": \\".dark\\",
\\"media\\": \\"not print\\"
}
}
},
Expand Down Expand Up @@ -343,7 +344,8 @@ exports[`renderPreset matches previous snapshot 1`] = `
\\"default\\": true
},
\\"dark\\": {
\\"selector\\": \\".dark\\"
\\"selector\\": \\".dark\\",
\\"media\\": \\"not print\\"
}
}
},
Expand Down
48 changes: 24 additions & 24 deletions src/shared/declaration/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ exports[`renderDeclarations includes secondary theme 1`] = `
.compact{
--scaledSize-css:1px;
}
.dark{
@media not print {.dark{
--shadow-css:black;
--buttonShadow-css:black;
--boxShadow-css:brown;
--lineShadow-css:brown;
}
}}
.disabled-motion{
--appear-css:0;
}
Expand All @@ -42,26 +42,26 @@ exports[`renderDeclarations includes secondary theme 1`] = `
--boxShadow-css:purple;
--lineShadow-css:black;
}
.dark .navigation{
@media not print {.dark .navigation{
--shadow-css:brown;
--buttonShadow-css:brown;
--lineShadow-css:purple;
}
.dark.navigation{
}}
@media not print {.dark.navigation{
--shadow-css:brown;
--buttonShadow-css:brown;
--lineShadow-css:purple;
}
}}
.secondary-theme{
--black-css:purple;
--brown-css:black;
}
.dark.secondary-theme{
@media not print {.dark.secondary-theme{
--shadow-css:purple;
--buttonShadow-css:purple;
--boxShadow-css:black;
--lineShadow-css:black;
}
}}
.secondary-theme .navigation{
--shadow-css:purple;
--buttonShadow-css:purple;
Expand All @@ -72,17 +72,17 @@ exports[`renderDeclarations includes secondary theme 1`] = `
--buttonShadow-css:purple;
--lineShadow-css:purple;
}
.dark.secondary-theme .navigation{
@media not print {.dark.secondary-theme .navigation{
--shadow-css:grey;
--buttonShadow-css:grey;
--boxShadow-css:black;
--lineShadow-css:black;
}
.dark.navigation.secondary-theme{
}}
@media not print {.dark.navigation.secondary-theme{
--shadow-css:grey;
--buttonShadow-css:grey;
--lineShadow-css:black;
}"
}}"
`;

exports[`renderDeclarations renders declarations for theme with :root selector and context 1`] = `
Expand All @@ -106,12 +106,12 @@ exports[`renderDeclarations renders declarations for theme with :root selector a
:global .compact{
--scaledSize-css:1px;
}
:global .dark{
@media not print {:global .dark{
--shadow-css:black;
--buttonShadow-css:black;
--boxShadow-css:brown;
--lineShadow-css:brown;
}
}}
:global .disabled-motion{
--appear-css:0;
}
Expand All @@ -121,16 +121,16 @@ exports[`renderDeclarations renders declarations for theme with :root selector a
--boxShadow-css:purple;
--lineShadow-css:black;
}
:global .dark .navigation{
@media not print {:global .dark .navigation{
--shadow-css:brown;
--buttonShadow-css:brown;
--lineShadow-css:purple;
}
:global .dark.navigation{
}}
@media not print {:global .dark.navigation{
--shadow-css:brown;
--buttonShadow-css:brown;
--lineShadow-css:purple;
}"
}}"
`;

exports[`renderDeclarations renders declarations for theme with non :root selector 1`] = `
Expand All @@ -154,12 +154,12 @@ exports[`renderDeclarations renders declarations for theme with non :root select
.compact.secondary-theme{
--scaledSize-css:1px;
}
.dark.secondary-theme{
@media not print {.dark.secondary-theme{
--shadow-css:purple;
--buttonShadow-css:purple;
--boxShadow-css:black;
--lineShadow-css:black;
}
}}
.disabled-motion.secondary-theme{
--appear-css:0;
}
Expand All @@ -175,16 +175,16 @@ exports[`renderDeclarations renders declarations for theme with non :root select
--boxShadow-css:purple;
--lineShadow-css:purple;
}
.dark.secondary-theme .navigation{
@media not print {.dark.secondary-theme .navigation{
--shadow-css:grey;
--buttonShadow-css:grey;
--boxShadow-css:black;
--lineShadow-css:black;
}
.dark.navigation.secondary-theme{
}}
@media not print {.dark.navigation.secondary-theme{
--shadow-css:grey;
--buttonShadow-css:grey;
--boxShadow-css:black;
--lineShadow-css:black;
}"
}}"
`;
4 changes: 3 additions & 1 deletion src/shared/declaration/multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class MultiThemeCreator extends AbstractCreator implements StylesheetCrea
const optionalState = mode.states[state] as OptionalState;
const modeResolution = reduce(secondaryResolution, secondary, modeReducer(mode, state));
const modeRule = this.ruleCreator.create(
{ global: [secondary.selector, optionalState.selector] },
{ global: [secondary.selector, optionalState.selector], media: optionalState.media },
modeResolution
);
const parentModeRule = stylesheet.findRule(
Expand Down Expand Up @@ -119,6 +119,7 @@ export class MultiThemeCreator extends AbstractCreator implements StylesheetCrea
{
global: [secondary.selector, optionalState.selector],
local: [context.selector],
media: optionalState.media,
},
contextResolution
);
Expand Down Expand Up @@ -159,6 +160,7 @@ export class MultiThemeCreator extends AbstractCreator implements StylesheetCrea
const contextAndModeRuleGlobal = this.ruleCreator.create(
{
global: [secondary.selector, optionalState.selector, context.selector],
media: optionalState.media,
},
contextResolution
);
Expand Down
3 changes: 2 additions & 1 deletion src/shared/declaration/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SpecificResolution } from '../theme';
export interface SelectorConfig {
global: string[];
local?: string[];
media?: string;
}
export class RuleCreator {
selector: Selector;
Expand All @@ -20,7 +21,7 @@ export class RuleCreator {
}

create(config: SelectorConfig, resolution: SpecificResolution): Rule {
const rule = new Rule(this.selectorFor(config));
const rule = new Rule(this.selectorFor(config), config.media);
entries(resolution).forEach(([token, value]) => {
const property = this.registry.get(token);
if (property) {
Expand Down
8 changes: 5 additions & 3 deletions src/shared/declaration/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export class SingleThemeCreator extends AbstractCreator implements StylesheetCre

SingleThemeCreator.forEachOptionalModeState(this.theme, (mode, state) => {
const modeResolution = reduce(this.resolution, this.theme, modeReducer(mode, state));
const stateDetails = mode.states[state] as OptionalState;
const modeRule = this.ruleCreator.create(
{ global: [this.theme.selector, (mode.states[state] as OptionalState).selector] },
{ global: [this.theme.selector, stateDetails.selector], media: stateDetails.media },
modeResolution
);
SingleThemeCreator.appendRuleToStylesheet(stylesheet, modeRule, [rootRule]);
Expand All @@ -61,8 +62,9 @@ export class SingleThemeCreator extends AbstractCreator implements StylesheetCre

SingleThemeCreator.forEachContextWithinOptionalModeState(this.theme, (context, mode, state) => {
const contextResolution = reduce(resolveContext(this.theme, context), this.theme, modeReducer(mode, state));
const stateDetails = mode.states[state] as OptionalState;
const contextAndModeRule = this.ruleCreator.create(
{ global: [this.theme.selector, (mode.states[state] as OptionalState).selector], local: [context.selector] },
{ global: [this.theme.selector, stateDetails.selector], local: [context.selector], media: stateDetails.media },
contextResolution
);
const contextRule = stylesheet.findRule(
Expand All @@ -84,7 +86,7 @@ export class SingleThemeCreator extends AbstractCreator implements StylesheetCre
);

const contextRuleAndModeRuleGlobal = this.ruleCreator.create(
{ global: [this.theme.selector, (mode.states[state] as OptionalState).selector, context.selector] },
{ global: [this.theme.selector, stateDetails.selector, context.selector], media: stateDetails.media },
contextResolution
);
SingleThemeCreator.appendRuleToStylesheet(
Expand Down
Loading

0 comments on commit a599dc6

Please sign in to comment.