Skip to content

Commit

Permalink
fix(transform): support for @media selectors inside :global (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber authored May 23, 2024
1 parent b407f83 commit 3cadae5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/slimy-bulldogs-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@wyw-in-js/transform': patch
'wyw-in-js': patch
---

Support for @media selectors inside :global selectors.
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,21 @@ describe('stylisGlobalPlugin', () => {
`".globalA .component {color:red;}.globalB .component {color:blue;}"`
);
});

it('@media', () => {
const cssRule = dedent(`
.component :global() {
@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
}
}
`);

expect(compileRule(cssRule)).toMatchInlineSnapshot(
`"@media (prefers-color-scheme: dark){html {color-scheme:dark;}}"`
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,26 @@ const isRuleset = (element: Element): element is Ruleset => {
return element.type === RULESET && propsAreStrings(element.props);
};

function nonMediaParent(element: Element): Element | null {
let { parent } = element;

while (parent) {
if (parent.type !== '@media') {
return parent;
}

parent = parent.parent;
}

return null;
}

/**
* Stylis plugin that mimics :global() selector behavior from Stylis v3.
*/
export const stylisGlobalPlugin: Middleware = (element) => {
function getGlobalSelectorModifiers(el: Element): IGlobalSelectorModifiers {
const { parent } = el;
const parent = nonMediaParent(el);

const value = getOriginalElementValue(el);
const parentValue = getOriginalElementValue(parent);
Expand Down

0 comments on commit 3cadae5

Please sign in to comment.