Skip to content

Commit 908d09c

Browse files
committed
Add light/dark :where mixins
1 parent c9a37f4 commit 908d09c

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
"scripts": {
3939
"build": "tsc --project tsconfig.build.json && echo 'declare module \"postcss-preset-mantine\";' > dist/index.d.ts",
4040
"lint": "eslint src --cache",
41-
"format": "prettier src",
42-
"format:check": "prettier --check src",
41+
"prettier:check": "prettier --check src",
4342
"typecheck": "tsc --noEmit",
4443
"jest": "jest",
44+
"jest:update-snapshots": "jest --updateSnapshot",
4545
"prepublish": "yarn test && yarn build",
46-
"test": "npm run lint && npm run format:check && npm run typecheck && npm run jest",
46+
"test": "npm run lint && npm run prettier:check && npm run typecheck && npm run jest",
4747
"clean": "rm -rf dist",
4848
"release": "esno scripts/release"
4949
},

src/preset.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,34 @@ const lightDark = require('./postcss-light-dark');
66
const converters = require('./converters');
77
const theme = require('./postcss-mantine-theme');
88

9-
function colorSchemeMixin(colorScheme: 'light' | 'dark') {
9+
function colorSchemeMixin(colorScheme: 'light' | 'dark', type: 'where' | 'default' = 'default') {
10+
if (type === 'where') {
11+
return {
12+
[`:where([data-mantine-color-scheme='${colorScheme}']) &`]: {
13+
'@mixin-content': {},
14+
},
15+
};
16+
}
17+
1018
return {
1119
[`[data-mantine-color-scheme='${colorScheme}'] &`]: {
1220
'@mixin-content': {},
1321
},
1422
};
1523
}
1624

17-
function rootColorSchemeMixin(colorScheme: 'light' | 'dark') {
25+
function rootColorSchemeMixin(
26+
colorScheme: 'light' | 'dark',
27+
type: 'where' | 'default' = 'default'
28+
) {
29+
if (type === 'where') {
30+
return {
31+
[`&:where(:root[data-mantine-color-scheme='${colorScheme}'])`]: {
32+
'@mixin-content': {},
33+
},
34+
};
35+
}
36+
1837
return {
1938
[`&[data-mantine-color-scheme='${colorScheme}']`]: {
2039
'@mixin-content': {},
@@ -86,6 +105,10 @@ module.exports = () => {
86105
dark: colorSchemeMixin('dark'),
87106
'light-root': rootColorSchemeMixin('light'),
88107
'dark-root': rootColorSchemeMixin('dark'),
108+
'where-light': colorSchemeMixin('light', 'where'),
109+
'where-dark': colorSchemeMixin('dark', 'where'),
110+
'where-light-root': rootColorSchemeMixin('light', 'where'),
111+
'where-dark-root': rootColorSchemeMixin('dark', 'where'),
89112
hover: hoverMixin,
90113
rtl: rtlMixin,
91114
ltr: ltrMixin,

src/tests/__snapshots__/mixin-light-dark.test.ts.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,36 @@ exports[`mixin-light-dark transforms light and dark mixins correctly 1`] = `
1010
}
1111
"
1212
`;
13+
14+
exports[`mixin-light-dark transforms light-root and dark-root mixins correctly 1`] = `
15+
"
16+
:root[data-mantine-color-scheme='light'] {
17+
color: red
18+
}
19+
:root[data-mantine-color-scheme='dark'] {
20+
color: blue
21+
}
22+
"
23+
`;
24+
25+
exports[`mixin-light-dark transforms where-light and where-dark mixins correctly 1`] = `
26+
"
27+
:where([data-mantine-color-scheme='light']) .demo {
28+
color: red
29+
}
30+
:where([data-mantine-color-scheme='dark']) .demo {
31+
color: blue
32+
}
33+
"
34+
`;
35+
36+
exports[`mixin-light-dark transforms where-light-root and where-dark-root mixins correctly 1`] = `
37+
"
38+
:root:where(:root[data-mantine-color-scheme='light']) {
39+
color: red
40+
}
41+
:root:where(:root[data-mantine-color-scheme='dark']) {
42+
color: blue
43+
}
44+
"
45+
`;

src/tests/mixin-light-dark.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,60 @@ const baseInput = `
1212
}
1313
`;
1414

15+
const whereInput = `
16+
.demo {
17+
@mixin where-light {
18+
color: red;
19+
}
20+
21+
@mixin where-dark {
22+
color: blue;
23+
}
24+
}
25+
`;
26+
27+
const rootInput = `
28+
:root {
29+
@mixin light-root {
30+
color: red;
31+
}
32+
33+
@mixin dark-root {
34+
color: blue;
35+
}
36+
}
37+
`;
38+
39+
const rootWhereInput = `
40+
:root {
41+
@mixin where-light-root {
42+
color: red;
43+
}
44+
45+
@mixin where-dark-root {
46+
color: blue;
47+
}
48+
}
49+
`;
50+
1551
describe('mixin-light-dark', () => {
1652
it('transforms light and dark mixins correctly', async () => {
1753
const res = await testTransform(baseInput);
1854
expect(res.css).toMatchSnapshot();
1955
});
56+
57+
it('transforms where-light and where-dark mixins correctly', async () => {
58+
const res = await testTransform(whereInput);
59+
expect(res.css).toMatchSnapshot();
60+
});
61+
62+
it('transforms light-root and dark-root mixins correctly', async () => {
63+
const res = await testTransform(rootInput);
64+
expect(res.css).toMatchSnapshot();
65+
});
66+
67+
it('transforms where-light-root and where-dark-root mixins correctly', async () => {
68+
const res = await testTransform(rootWhereInput);
69+
expect(res.css).toMatchSnapshot();
70+
});
2071
});

0 commit comments

Comments
 (0)