Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ::selection to color-mode-theme() mixin #2472

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-vans-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/css": patch
---

Add `::selection` to `color-mode-theme()` mixin
68 changes: 64 additions & 4 deletions src/support/mixins/color-modes.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
// This mixin is used to output the tokens/variables from Primitives
//
// Example:
//
// @include color-mode-theme(dark) {
// --color: black;
// }
//
// Warning!!!
// Don't use this mixin with a class. E.g.
// @include color-mode-theme(dark) {
// .my-class {
// color: red;
// }
// }
//
// The outputted `::selection .my-class` will make the selector invalid and the entire ruleset is disregarded.
// At some point we hopefully can remove the need for `&, &::selection {}` again (once the spec/implementation changes).

@mixin color-mode-theme($theme-name, $include-root: false) {
@if $include-root {
:root,
[data-color-mode="light"][data-light-theme="#{$theme-name}"],
[data-color-mode="dark"][data-dark-theme="#{$theme-name}"] {
@content;
&,
&::selection {
@content;
}

/*! */ // This is a fix for a cssstats bug see https://github.com/cssstats/cssstats/issues/331
}
Expand All @@ -12,23 +34,61 @@
@else {
[data-color-mode="light"][data-light-theme="#{$theme-name}"],
[data-color-mode="dark"][data-dark-theme="#{$theme-name}"] {
@content;
&,
&::selection {
@content;
}
}
}

@media (prefers-color-scheme: light) {
[data-color-mode="auto"][data-light-theme="#{$theme-name}"] {
@content;
&,
&::selection {
@content;
}
}
}

@media (prefers-color-scheme: dark) {
[data-color-mode="auto"][data-dark-theme="#{$theme-name}"] {
@content;
&,
&::selection {
@content;
}
}
}
}

// This mixin wraps styles with light or dark mode selectors
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment is unrelated, but maybe good to document the color-mode() mixin too.

// If possible, use a color variable instead.
//
// Example:
//
// @include color-mode('dark') {
// .my-class {
// color: red;
// }
// }
//
// Returns:
//
// [data-color-mode=light][data-light-theme*=dark] .my-class,
// [data-color-mode=dark][data-dark-theme*=dark] .my-class {
// color: red;
// }
//
// @media (prefers-color-scheme: light) {
// [data-color-mode=auto][data-light-theme*=dark] .my-class {
// color: red;
// }
// }
// @media (prefers-color-scheme: dark) {
// [data-color-mode=auto][data-dark-theme*=dark] .my-class {
// color: red;
// }
// }

@mixin color-mode($mode) {
@if $mode == light {
:root,
Expand Down