Skip to content

Commit 0f25cd6

Browse files
author
Quang Phan
committed
docs(README.md): init
1 parent a075074 commit 0f25cd6

3 files changed

Lines changed: 103 additions & 7 deletions

File tree

.changeset/gold-moons-search.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'postcss-color-scheme': minor
3+
---
4+
5+
bootstrap `README.md` with initial documentation

README.md

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,35 @@
44

55
## [Changelog][changelog]
66

7-
Postcss plugin ...
7+
Postcss plugin for handling `prefers-color-scheme`
88

99
Input
1010

1111
```css
12+
.my-class {
13+
color: black;
14+
@dark {
15+
color: white;
16+
}
17+
}
1218
```
1319

1420
Output
1521

1622
```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+
}
1736
```
1837

1938
## Installation
@@ -32,22 +51,94 @@ module.exports = {
3251
};
3352
```
3453

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+
35100
## Supported At Rules
36101

37102
| At Rule | Description |
38103
| --- | --- |
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.
110+
111+
Input
112+
113+
```css
114+
.my-class {
115+
@dark global {
116+
color: white;
117+
}
118+
}
119+
```
120+
121+
```css
122+
:global(html[data-color-scheme="dark"]) .my-class {
123+
color: white;
124+
}
125+
@media (prefers-color-scheme: dark) {
126+
:global(html:not([data-color-scheme="light"])) .my-class {
127+
color: white;
128+
}
129+
}
130+
```
40131

41132
## Test Cases & Examples
42133

43134
The following table lists test cases covered by this plugin, please refer to [tests][tests] for details and to test input css as examples
44135

45136
| Test Case | Description | Input | Output |
46137
| --- | --- | --- | --- |
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] |
51142

52143
[changelog]: ./CHANGELOG.md
53144
[tests]: https://github.com/vnphanquang/postcss-color-scheme/blob/main/src/color-scheme.test.js

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "postcss-color-scheme",
33
"version": "0.0.0",
4-
"description": "postcss plugin for handling different color scheme",
4+
"description": "postcss plugin for handling prefers-color-scheme",
55
"main": "src/index.js",
66
"scripts": {
77
"test": "vitest --ui --coverage",

0 commit comments

Comments
 (0)