Skip to content

Add light-dark() support - #26

Merged
ai merged 9 commits into
postcss:mainfrom
VladBrok:add-light-dark-support
Feb 10, 2024
Merged

ai merged 9 commits into
postcss:mainfrom
VladBrok:add-light-dark-support

Conversation

@VladBrok

@VladBrok VladBrok commented Feb 9, 2024

Copy link
Copy Markdown
Contributor

Closes #25

If we try to convert code from this:

a {
  color: light-dark(black, white);
}

To this:

:where(html.is-light) a {
  color: black;
}
:where(html.is-dark) a {
  color: white;
}

Then it will not respect the user's preferred color scheme specified via system settings.
So we also need to use prefers-color-scheme media query.
This plugin already can transform prefers-color-scheme, so if we convert this:

a {
  color: light-dark(black, white);
}

To this:

@media (prefers-color-scheme: dark) {
  a {
    color: white;
  }
}

@media (prefers-color-scheme: light) {
  a {
    color: black;
  }
}

then the current plugin code will run and we will get the following:

@media (prefers-color-scheme: dark) {
  :where(html:not(.is-light)) a {
    color: white;
  }
}
:where(html.is-dark) a {
  color: white;
}

@media (prefers-color-scheme: light) {
  :where(html:not(.is-dark)) a {
    color: black;
  }
}
:where(html.is-light) a {
  color: black;
}

Comment thread README.md Outdated
theme. If user want to use dark theme, you set `html.is-dark` class.
If user want to force light theme, you use `html.is-light`.

This plugin also supports the [light-dark()](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark) color function:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too complex. Just add lines to the main example.

Comment thread index.js Outdated
return string.replace(new RegExp(escapeRegExp(find), 'g'), replace)
}

function addColorSchemeMedia(isDark, color, postcss, declaration) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s move postcss at the end, since it is complex arg

Comment thread index.js Outdated
function addColorSchemeMedia(isDark, color, postcss, declaration) {
let mediaQuery = postcss.atRule({
name: 'media',
params: `(prefers-color-scheme: ${isDark ? 'dark' : 'light'})`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space will not work if you apply it after minification.

Do we have it originally? If not, let’s just remove space.

Comment thread index.js Outdated
}
},
DeclarationExit: (declaration, { postcss }) => {
if (!declaration.value.startsWith('light-dark(')) return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that all decl should start from ligh-dark().

What about: border-color: black light-dark(black, white)

Comment thread index.js Outdated
DeclarationExit: (declaration, { postcss }) => {
if (!declaration.value.startsWith('light-dark(')) return

let matches = [...declaration.value.matchAll(LIGHT_DARK)][0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have multiple light-dark() in the value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, we might have something like radial-gradient() which can include multiple light-dark()

we can also have light-dark() inside strings, in which case we should not touch it

Comment thread README.md Outdated
Comment thread README.md Outdated
VladBrok and others added 3 commits February 10, 2024 12:33
Co-authored-by: Andrey Sitnik <andrey@sitnik.ru>
Co-authored-by: Andrey Sitnik <andrey@sitnik.ru>
@ai
ai merged commit dcdb8b8 into postcss:main Feb 10, 2024
@ai

ai commented Feb 10, 2024

Copy link
Copy Markdown
Member

Thanks!

Do you have Twitter and Mastodon to mention you?

@VladBrok

Copy link
Copy Markdown
Contributor Author

Thanks!

Do you have Twitter and Mastodon to mention you?

yes, I have Twitter: https://twitter.com/VladBrok99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add light-dark() support

2 participants