Skip to content

Commit

Permalink
fixes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
argyleink committed Oct 16, 2023
1 parent 1e692e2 commit b520b89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ const getAdaptivePropSelector = (selector) => {

/** @type { import('postcss').PluginCreator<any> }*/
module.exports = (options) => {
const { files, adaptive_prop_selector, custom_selector, layer, ...props } = options
const {
files,
adaptive_prop_selector,
custom_selector,
custom_selector_dark,
layer, ...props
} = options

const FilePropsCache = new Map();

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

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

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

if (layer) {
Expand Down
24 changes: 24 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,30 @@ a {
)
})

it('Can jit light & dark props to a custom selector for use with a client side switch', async () => {
await run(
`a {
color: var(--text);
}`,
`.light {
--text: white;
}
a {
color: var(--text);
}
@media (prefers-color-scheme: dark) {
.dark {
--text: black;
}
}`,
{
... MockProps,
custom_selector: '.light',
custom_selector_dark: '.dark',
}
)
})

it('Wont create a :root {} context unless props are found', async () => {
await run(
`a {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b520b89

Please sign in to comment.