Skip to content

Commit b520b89

Browse files
committed
fixes #44
1 parent 1e692e2 commit b520b89

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-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 || ':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

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

416+
it('Can jit light & dark props to a custom selector for use with a client side switch', async () => {
417+
await run(
418+
`a {
419+
color: var(--text);
420+
}`,
421+
`.light {
422+
--text: white;
423+
}
424+
a {
425+
color: var(--text);
426+
}
427+
@media (prefers-color-scheme: dark) {
428+
.dark {
429+
--text: black;
430+
}
431+
}`,
432+
{
433+
... MockProps,
434+
custom_selector: '.light',
435+
custom_selector_dark: '.dark',
436+
}
437+
)
438+
})
439+
416440
it('Wont create a :root {} context unless props are found', async () => {
417441
await run(
418442
`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)