Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,84 @@ describe('@stylexjs/babel-plugin', () => {
`);
});

test('media queries with a leading not and last query wins', () => {
const { code, metadata } = transform(
`
import * as stylex from '@stylexjs/stylex';
export const styles = stylex.create({
root: {
filter: {
default: 'grayscale(1)',
'@media not all and (monochrome)': 'none',
},
backgroundColor: {
default: 'red',
'@media not screen and (min-width: 100px)': 'blue',
'@media (min-width: 500px)': 'purple',
}
},
});
`,
{ enableMediaQueryOrder: true },
);
expect(code).toMatchInlineSnapshot(`
"import * as stylex from '@stylexjs/stylex';
export const styles = {
root: {
ku685b: "xifi7vn x342vwv",
kWkggS: "xrkmrrc x8tgh9g x132kqxt",
$$css: true
}
};"
`);
expect(metadata).toMatchInlineSnapshot(`
{
"stylex": [
[
"xifi7vn",
{
"ltr": ".xifi7vn{filter:grayscale(1)}",
"rtl": null,
},
3000,
],
[
"x342vwv",
{
"ltr": "@media not all and (monochrome){.x342vwv.x342vwv{filter:none}}",
"rtl": null,
},
3200,
],
[
"xrkmrrc",
{
"ltr": ".xrkmrrc{background-color:red}",
"rtl": null,
},
3000,
],
[
"x8tgh9g",
{
"ltr": "@media print and (not (min-width: 500px)), (max-width: 99.98px){.x8tgh9g.x8tgh9g{background-color:blue}}",
"rtl": null,
},
3200,
],
[
"x132kqxt",
{
"ltr": "@media (min-width: 500px){.x132kqxt.x132kqxt{background-color:purple}}",
"rtl": null,
},
3200,
],
],
}
`);
});

test('media queries without last query wins', () => {
const { code, metadata } = transform(
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,172 @@ describe('Media Query Transformer', () => {
expect(JSON.stringify(result)).toBe(JSON.stringify(expectedStyles));
});

test('a leading not negates the whole query', () => {
const originalStyles = {
filter: {
default: 'grayscale(1)',
'@media not all and (monochrome)': 'none',
},
color: {
default: 'black',
'@media not screen and (min-width: 100px)': 'red',
},
};

expect(lastMediaQueryWinsTransform(originalStyles)).toMatchInlineSnapshot(`
{
"color": {
"@media not screen and (min-width: 100px)": "red",
"default": "black",
},
"filter": {
"@media not all and (monochrome)": "none",
"default": "grayscale(1)",
},
}
`);
});

test('a leading not followed by a later query', () => {
const originalStyles = {
color: {
default: 'black',
'@media not all and (monochrome)': 'blue',
'@media (min-width: 500px)': 'red',
},
};

expect(lastMediaQueryWinsTransform(originalStyles)).toMatchInlineSnapshot(`
{
"color": {
"@media (min-width: 500px)": "red",
"@media (not (min-width: 500px)) and (not (monochrome))": "blue",
"default": "black",
},
}
`);
});

test('a later query with a leading not', () => {
const originalStyles = {
color: {
default: 'black',
'@media (min-width: 500px)': 'red',
'@media not all and (monochrome)': 'blue',
},
width: {
default: '100%',
'@media (max-width: 900px)': '80%',
'@media not print and (min-width: 600px)': '60%',
},
};

expect(lastMediaQueryWinsTransform(originalStyles)).toMatchInlineSnapshot(`
{
"color": {
"@media (min-width: 500px) and (monochrome)": "red",
"@media not all and (monochrome)": "blue",
"default": "black",
},
"width": {
"@media not print and (min-width: 600px)": "60%",
"@media print and (max-width: 900px) and (min-width: 600px)": "80%",
"default": "100%",
},
}
`);
});

test('a leading not over an or condition stays linear in size', () => {
const originalStyles = {
color: {
default: 'black',
'@media (min-width: 10px)': 'red',
'@media not screen and ((min-width: 100px) or (min-width: 200px) or (min-width: 300px) or (min-width: 400px))':
'blue',
},
};

expect(lastMediaQueryWinsTransform(originalStyles)).toMatchInlineSnapshot(`
{
"color": {
"@media not screen and ((min-width: 100px) or (min-width: 200px) or (min-width: 300px) or (min-width: 400px))": "blue",
"@media screen and (min-width: 100px) and (min-width: 10px), screen and (min-width: 200px) and (min-width: 10px), screen and (min-width: 300px) and (min-width: 10px), screen and (min-width: 400px) and (min-width: 10px)": "red",
"default": "black",
},
}
`);
});

test('a leading not over mixed and/or conditions', () => {
const originalStyles = {
color: {
default: 'black',
'@media not all and (min-width: 500px) and ((max-width: 900px) or (max-height: 600px))':
'blue',
},
width: {
default: '100%',
'@media (min-width: 10px)': '80%',
'@media not all and (min-width: 500px) and ((max-width: 900px) or (max-height: 600px))':
'60%',
},
};

expect(lastMediaQueryWinsTransform(originalStyles)).toMatchInlineSnapshot(`
{
"color": {
"@media not all and (min-width: 500px) and ((max-width: 900px) or (max-height: 600px))": "blue",
"default": "black",
},
"width": {
"@media (max-width: 900px) and (min-width: 10px) and (min-width: 500px), (max-height: 600px) and (min-width: 10px) and (min-width: 500px)": "80%",
"@media not all and (min-width: 500px) and ((max-width: 900px) or (max-height: 600px))": "60%",
"default": "100%",
},
}
`);
});

test('an or condition inside an and keeps its parentheses', () => {
const originalStyles = {
color: {
default: 'black',
'@media (min-width: 1px) and ((max-width: 200px) or (max-height: 600px))':
'blue',
},
};

expect(lastMediaQueryWinsTransform(originalStyles)).toMatchInlineSnapshot(`
{
"color": {
"@media (min-width: 1px) and ((max-width: 200px) or (max-height: 600px))": "blue",
"default": "black",
},
}
`);
});

test('a media type from a negated later query is moved to the front', () => {
const originalStyles = {
color: {
default: 'black',
'@media (max-width: 900px)': 'red',
'@media not print, (max-width: 100px)': 'blue',
},
};

expect(lastMediaQueryWinsTransform(originalStyles)).toMatchInlineSnapshot(`
{
"color": {
"@media not print, (max-width: 100px)": "blue",
"@media print and (max-width: 900px) and (not (max-width: 100px))": "red",
"default": "black",
},
}
`);
});

test('basic usage: does not modify single queries', () => {
const originalStyles = {
gridColumn: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,28 @@ describe('style-value-parser/at-queries', () => {
expect(parsed).toMatchInlineSnapshot(`
MediaQuery {
"queries": {
"key": "all",
"not": true,
"type": "media-keyword",
"rule": {
"rules": [
{
"key": "all",
"not": false,
"only": false,
"type": "media-keyword",
},
{
"keyValue": "monochrome",
"type": "word-rule",
},
],
"type": "and",
},
"type": "not",
},
}
`);
expect(parsed.toString()).toMatchInlineSnapshot('"@media not all"');
expect(parsed.toString()).toMatchInlineSnapshot(
'"@media not all and (monochrome)"',
);
});
});

Expand Down Expand Up @@ -945,13 +960,28 @@ describe('style-value-parser/at-queries', () => {
expect(parsed).toMatchInlineSnapshot(`
MediaQuery {
"queries": {
"key": "all",
"not": true,
"type": "media-keyword",
"rule": {
"rules": [
{
"key": "all",
"not": false,
"only": false,
"type": "media-keyword",
},
{
"keyValue": "monochrome",
"type": "word-rule",
},
],
"type": "and",
},
"type": "not",
},
}
`);
expect(parsed.toString()).toMatchInlineSnapshot('"@media not all"');
expect(parsed.toString()).toMatchInlineSnapshot(
'"@media not all and (monochrome)"',
);
});

test('@media screen and (min-width: 400px)', () => {
Expand Down Expand Up @@ -2033,13 +2063,38 @@ describe('style-value-parser/at-queries', () => {
expect(parsed).toMatchInlineSnapshot(`
MediaQuery {
"queries": {
"key": "all",
"not": true,
"type": "media-keyword",
"rule": {
"rules": [
{
"key": "all",
"not": false,
"only": false,
"type": "media-keyword",
},
{
"keyValue": "monochrome",
"type": "word-rule",
},
{
"key": "min-width",
"type": "pair",
"value": {
"signCharacter": undefined,
"type": "integer",
"unit": "px",
"value": 600,
},
},
],
"type": "and",
},
"type": "not",
},
}
`);
expect(parsed.toString()).toMatchInlineSnapshot('"@media not all"');
expect(parsed.toString()).toMatchInlineSnapshot(
'"@media not all and (monochrome) and (min-width: 600px)"',
);
});

test('@media (max-width: 1440px) and (not (max-width: 1024px)) and (not (max-width: 768px)) and (not (max-width: 458px))', () => {
Expand Down
Loading
Loading