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 535bf1d
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 34 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
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
10 changes: 8 additions & 2 deletions src/shared/declaration/stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ export default class Stylesheet {

export class Rule {
selector: string;
media?: string;
declarationsMap: Map<string, [Declaration, number]> = new Map();
counter = 0;

constructor(selector: string) {
constructor(selector: string, media?: string) {
this.selector = selector;
this.media = media;
}

appendDeclaration(declaration: Declaration) {
Expand All @@ -81,7 +83,11 @@ export class Rule {

toString(): string {
const lines = asValuesArray(this.declarationsMap).map((decl) => decl.toString());
return `${this.selector}{\n\t${lines.join('\n\t')}\n}`;
const rule = `${this.selector}{\n\t${lines.join('\n\t')}\n}`;
if (this.media) {
return `@media ${this.media} {${rule}}`;
}
return rule;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Object {
"id": "color",
"states": Object {
"dark": Object {
"media": "not print",
"selector": ".dark",
},
"light": Object {
Expand Down
1 change: 1 addition & 0 deletions src/shared/theme/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
export interface OptionalState {
selector: string;
media?: string;
}

export interface DefaultState {
Expand Down

0 comments on commit 535bf1d

Please sign in to comment.