You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+97-6Lines changed: 97 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,35 @@
4
4
5
5
## [Changelog][changelog]
6
6
7
-
Postcss plugin ...
7
+
Postcss plugin for handling `prefers-color-scheme`
8
8
9
9
Input
10
10
11
11
```css
12
+
.my-class {
13
+
color: black;
14
+
@dark {
15
+
color: white;
16
+
}
17
+
}
12
18
```
13
19
14
20
Output
15
21
16
22
```css
23
+
.my-class {
24
+
color: black;
25
+
}
26
+
27
+
html[data-color-scheme="dark"] .my-class {
28
+
color: white;
29
+
}
30
+
31
+
@media (prefers-color-scheme: dark) {
32
+
html:not([data-color-scheme="light"]) .my-class {
33
+
color: white;
34
+
}
35
+
}
17
36
```
18
37
19
38
## Installation
@@ -32,22 +51,94 @@ module.exports = {
32
51
};
33
52
```
34
53
54
+
## Design Decisions
55
+
56
+
You might have noticed a couple of opinionated code at the top of this document. These are extracted from my work, and currently serve my use cases very well. Should you have concerns, suggestions for improvements, or solution for making this more generic, feel free to open an issue. Thanks!
57
+
58
+
1. Rely on `data-color-scheme` for explicit theme settings. This requires setting `data-color-scheme` on the root html element.
59
+
60
+
2. Provide fallback to when user has not explicitly select a theme. Let's refer to the demo above, with rules enumerated:
61
+
62
+
```css
63
+
/* (1) */
64
+
.my-class {
65
+
color: black;
66
+
}
67
+
68
+
/* (2) */
69
+
html[data-color-scheme="dark"] .my-class {
70
+
color: white;
71
+
}
72
+
73
+
/* (3) */
74
+
@media (prefers-color-scheme: dark) {
75
+
html:not([data-color-scheme="light"]) .my-class {
76
+
color: white;
77
+
}
78
+
}
79
+
```
80
+
81
+
Imagine your system provides 3 options: `dark`, `light`, and `system` (default). There are 4 possible scenarios.
82
+
83
+
1. User has not explicitly selected a theme (theme = `system`), and the system prefers `light` (`prefers-color-scheme` = `light`):
84
+
85
+
--> (1) applies.
86
+
87
+
2. User has not explicitly selected a theme (theme = `system`), and the system prefers `dark`
88
+
(`prefers-color-scheme` = `dark`):
89
+
90
+
--> (1) & (3) applies, (3) takes precedence because of its higher specificity.
91
+
92
+
3. User selected `dark` (`data-color-theme` set to `dark` on root html) :
93
+
94
+
--> (1) & (2) applies, (2) takes precedence because of its higher specificity.
95
+
96
+
4. User selected `light` (`data-color-theme` set to `light` on root html) :
97
+
98
+
--> (1) applies.
99
+
35
100
## Supported At Rules
36
101
37
102
| At Rule | Description |
38
103
| --- | --- |
39
-
<!-- | `@space-x` | Add horizontal spacing between direct children | -->
104
+
| `@dark` | apply rules for dark color scheme |
105
+
| `@light` | apply rules for light color scheme |
106
+
107
+
## Global Variant
108
+
109
+
`postcss-color-scheme` supports the `:global` syntax, often seen in css modules and similar systems.
The following table lists test cases covered by this plugin, please refer to [tests][tests] for details and to test input css as examples
44
135
45
136
| Test Case | Description | Input | Output |
46
137
| --- | --- | --- | --- |
47
-
| in media queries | `` |[input][tests.in-media-queries.input]|[output][tests.in-media-queries.output]|
48
-
| with combined selector | `` |[input][tests.with-combined-selector.input]|[output][tests.with-combined-selector.output]|
49
-
| with [postcss-nesting]| `` |[input][tests.with-postcss-nesting.input]|[output][tests.with-postcss-nesting.output]|
50
-
| with [postcss-nested]| `` |[input][tests.with-postcss-nested.input]|[output][tests.with-postcss-nested.output]|
138
+
|nest in other media queries |`@media (min-width: 768px) { .my-class { @dark { color: white } } }`|[input][tests.in-media-queries.input]|[output][tests.in-media-queries.output]|
139
+
| with combined selector |`.my-class, .others { @dark { color: white } }`|[input][tests.with-combined-selector.input]|[output][tests.with-combined-selector.output]|
140
+
| with [postcss-nesting]|`.my-class { & .nested { @dark { color: white } } }`|[input][tests.with-postcss-nesting.input]|[output][tests.with-postcss-nesting.output]|
141
+
| with [postcss-nested]|`.my-class { .nested { @dark { color: white } } }`|[input][tests.with-postcss-nested.input]|[output][tests.with-postcss-nested.output]|
0 commit comments