Skip to content

Commit

Permalink
Add ::selection to color-mode-theme() mixin (#2472)
Browse files Browse the repository at this point in the history
* Add `::selection` to color-mode-theme mixin

* Document color-mode() mixin

* Clarify warning

* Create eleven-vans-repair.md
  • Loading branch information
simurai authored Jun 28, 2023
1 parent 07fa8a3 commit 3ee117e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
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
// 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

0 comments on commit 3ee117e

Please sign in to comment.