Skip to content

Commit b1d25a0

Browse files
authored
Adds config entry for custom dark mode selectors (#45)
* fixes #44 * catch a bug
1 parent 1e692e2 commit b1d25a0

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ const getAdaptivePropSelector = (selector) => {
3030

3131
/** @type { import('postcss').PluginCreator<any> }*/
3232
module.exports = (options) => {
33-
const { files, adaptive_prop_selector, custom_selector, layer, ...props } = options
33+
const {
34+
files,
35+
adaptive_prop_selector,
36+
custom_selector,
37+
custom_selector_dark,
38+
layer, ...props
39+
} = options
3440

3541
const FilePropsCache = new Map();
3642

@@ -55,6 +61,7 @@ module.exports = (options) => {
5561
return {
5662
Once(node, { parse, result, Rule, AtRule }) {
5763
let target_selector = custom_selector || ':root'
64+
let target_selector_dark = custom_selector_dark || custom_selector || ':root'
5865

5966
if (!files && !Object.keys(props).length) {
6067
return console.warn('postcss-jit-props: Variable source(s) not passed.')
@@ -121,7 +128,7 @@ module.exports = (options) => {
121128
STATE.mapped_dark = new Set()
122129

123130
STATE.target_rule = new Rule({ selector: target_selector, source: node.first.source })
124-
STATE.target_rule_dark = new Rule({ selector: target_selector, source: node.first.source })
131+
STATE.target_rule_dark = new Rule({ selector: target_selector_dark, source: node.first.source })
125132
STATE.target_media_dark = new AtRule({ name: 'media', params: '(prefers-color-scheme: dark)', source: node.first.source })
126133

127134
if (layer) {

index.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,53 @@ a {
413413
)
414414
})
415415

416+
it('Can jit light and dark props to a custom selector', async () => {
417+
await run(
418+
`a {
419+
color: var(--text);
420+
}`,
421+
`:global {
422+
--text: white;
423+
}
424+
a {
425+
color: var(--text);
426+
}
427+
@media (prefers-color-scheme: dark) {
428+
:global {
429+
--text: black;
430+
}
431+
}`,
432+
{
433+
... MockProps,
434+
custom_selector: ':global',
435+
}
436+
)
437+
})
438+
439+
it('Can jit light & dark props to a custom selector for use with a client side switch', async () => {
440+
await run(
441+
`a {
442+
color: var(--text);
443+
}`,
444+
`.light {
445+
--text: white;
446+
}
447+
a {
448+
color: var(--text);
449+
}
450+
@media (prefers-color-scheme: dark) {
451+
.dark {
452+
--text: black;
453+
}
454+
}`,
455+
{
456+
... MockProps,
457+
custom_selector: '.light',
458+
custom_selector_dark: '.dark',
459+
}
460+
)
461+
})
462+
416463
it('Wont create a :root {} context unless props are found', async () => {
417464
await run(
418465
`a {

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)