Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit cc7538f

Browse files
authored
feat(checkbox): Add color theme mixins and update default color to secondary (#1365)
- Removed `[aria-disabled="true"]` selector, as it doesn't appear to be necessary - Fixed preexisting state transition color animation bugs Resolves #1146
1 parent 8b05e88 commit cc7538f

File tree

9 files changed

+851
-409
lines changed

9 files changed

+851
-409
lines changed

demos/checkbox.html

Lines changed: 270 additions & 64 deletions
Large diffs are not rendered by default.

demos/checkbox.scss

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@
1515
//
1616

1717
@import "./common";
18+
@import "../packages/mdc-button/mdc-button";
1819
@import "../packages/mdc-checkbox/mdc-checkbox";
1920
@import "../packages/mdc-form-field/mdc-form-field";
21+
@import "../packages/mdc-theme/color_palette";
22+
@import "../packages/mdc-theme/mixins";
2023
@import "../packages/mdc-typography/mdc-typography";
24+
25+
.mdc-checkbox.demo-checkbox--custom-all {
26+
$color: $material-color-red-500;
27+
28+
@include mdc-checkbox-focus-indicator-color($color);
29+
@include mdc-checkbox-ripple((base-color: $color, opacity: $mdc-checkbox-ripple-opacity));
30+
@include mdc-checkbox-container-colors(
31+
$unmarked-stroke-color: $color,
32+
$unmarked-fill-color: rgba($color, .25),
33+
$marked-fill-color: $color);
34+
35+
@include mdc-theme-dark(".mdc-checkbox", $compound: true) {
36+
@include mdc-checkbox-ink-color(black);
37+
@include mdc-checkbox-container-colors(
38+
$unmarked-stroke-color: $color,
39+
$unmarked-fill-color: rgba($color, .35),
40+
$marked-fill-color: $color);
41+
}
42+
}
43+
44+
.mdc-checkbox.demo-checkbox--custom-stroke-and-fill {
45+
@include mdc-checkbox-container-colors(
46+
$unmarked-stroke-color: $material-color-blue-500,
47+
$marked-fill-color: $material-color-purple-500);
48+
}

packages/mdc-checkbox/README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ easily position checkboxes and their labels.
9191

9292
#### Disabled Checkboxes
9393

94+
Note that `mdc-checkbox--disabled` is necessary on the root element of CSS-only checkboxes to prevent hover states from activating. Checkboxes that use the JavaScript component do not need this class; a `disabled` attribute on the `<input>` element is sufficient.
95+
9496
```html
9597
<div class="mdc-checkbox mdc-checkbox--disabled">
9698
<input type="checkbox"
@@ -111,8 +113,6 @@ easily position checkboxes and their labels.
111113
<label for="basic-disabled-checkbox" id="basic-disabled-checkbox-label">This is my disabled checkbox</label>
112114
```
113115

114-
Note that `mdc-checkbox--disabled` is necessary on the root element to prevent hover states from activating.
115-
116116
### Using the JS Component
117117

118118
MDC Checkbox ships with a Component / Foundation combo which progressively enhances the checkbox
@@ -255,8 +255,30 @@ not return an object.
255255

256256
## Theming
257257

258-
MDC Checkboxes use the theme's primary color by default for checked and indeterminate states, and are completely dark theme
259-
aware.
258+
MDC Checkboxes use the theme's secondary color by default for "marked" states (i.e., checked or indeterminate), and are completely dark theme aware.
259+
260+
### Sass Mixins
261+
262+
The following mixins apply only to _enabled_ checkboxes. It is not currently possible to customize the color of a _disabled_ checkbox.
263+
264+
Mixin | Description
265+
--- | ---
266+
`mdc-checkbox-container-colors($unmarked-stroke-color, $unmarked-fill-color, $marked-fill-color, $generate-keyframes)` | Generates CSS classes to set and animate the stroke color and/or container fill color of a checkbox
267+
`mdc-checkbox-ink-color($color)` | Sets the ink color of the checked and indeterminate icons
268+
`mdc-checkbox-focus-indicator-color($color)` | Sets the color of the focus indicator
269+
`mdc-checkbox-ripple($ripple-config)` | Sets the ripple to the given [ripple configuration][ripple-readme]
270+
271+
[ripple-readme]: https://github.com/material-components/material-components-web/blob/master/packages/mdc-ripple/README.md
272+
273+
#### `mdc-checkbox-container-colors($unmarked-stroke-color, $unmarked-fill-color, $marked-fill-color, $generate-keyframes)`
274+
275+
Generates CSS classes to set the container stroke color and/or fill color of a checkbox in its marked and unmarked states.
276+
In the unmarked state, stroke and fill color may be customized independently; in the marked state, only the fill color
277+
may be customized, and the stroke will automatically match the fill color.
278+
279+
All parameters are optional, and if left unspecified will use their default values.
280+
281+
If you plan to use CSS-only checkboxes, set `$generate-keyframes` to `false` to prevent the mixin from generating `@keyframes` and CSS classes used by the JavaScript component.
260282

261283
### Caveat: Edge and CSS Variables
262284

packages/mdc-checkbox/_functions.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright 2016 Google Inc. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import "@material/theme/functions";
18+
19+
$mdc-checkbox-container-keyframes-uid_: -1;
20+
21+
@function mdc-checkbox-transition-enter($property, $delay: 0ms, $duration: $mdc-checkbox-transition-duration) {
22+
@return mdc-animation-enter($property, $duration, $delay);
23+
}
24+
25+
@function mdc-checkbox-transition-exit($property, $delay: 0ms, $duration: $mdc-checkbox-transition-duration) {
26+
@return mdc-animation-exit-temporary($property, $duration, $delay);
27+
}
28+
29+
@function mdc-checkbox-container-keyframes-uid_() {
30+
$mdc-checkbox-container-keyframes-uid_: $mdc-checkbox-container-keyframes-uid_ + 1 !global;
31+
32+
@return $mdc-checkbox-container-keyframes-uid_;
33+
}

packages/mdc-checkbox/_keyframes.scss

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,42 @@
1818
@import "@material/theme/mixins";
1919
@import "./variables";
2020

21-
@keyframes mdc-checkbox-fade-in-background {
22-
0% {
23-
border-color: $mdc-checkbox-border-color;
24-
background-color: transparent;
25-
}
26-
27-
50% {
28-
@include mdc-theme-prop(border-color, primary);
29-
@include mdc-theme-prop(background-color, primary);
30-
}
31-
}
32-
33-
@keyframes mdc-checkbox-fade-out-background {
34-
0%,
35-
80% {
36-
@include mdc-theme-prop(border-color, primary);
37-
@include mdc-theme-prop(background-color, primary);
38-
}
39-
40-
100% {
41-
border-color: $mdc-checkbox-border-color;
42-
background-color: transparent;
43-
}
44-
}
45-
46-
@keyframes mdc-checkbox-fade-in-background-dark {
47-
0% {
48-
border-color: $mdc-checkbox-border-color-dark;
49-
background-color: transparent;
50-
}
51-
52-
50% {
53-
@include mdc-theme-prop(border-color, primary);
54-
@include mdc-theme-prop(background-color, primary);
55-
}
56-
}
57-
58-
@keyframes mdc-checkbox-fade-out-background-dark {
59-
0%,
60-
80% {
61-
@include mdc-theme-prop(border-color, primary);
62-
@include mdc-theme-prop(background-color, primary);
63-
}
64-
65-
100% {
66-
border-color: $mdc-checkbox-border-color-dark;
67-
background-color: transparent;
21+
@mixin mdc-checkbox-container-keyframes_(
22+
$from-stroke-color,
23+
$to-stroke-color,
24+
$from-fill-color,
25+
$to-fill-color,
26+
$uid) {
27+
@keyframes mdc-checkbox-fade-in-background-#{$uid} {
28+
0% {
29+
@include mdc-theme-prop(border-color, $from-stroke-color);
30+
@include mdc-theme-prop(background-color, $from-fill-color);
31+
}
32+
33+
50% {
34+
@include mdc-theme-prop(border-color, $to-stroke-color);
35+
@include mdc-theme-prop(background-color, $to-fill-color);
36+
}
37+
}
38+
39+
@keyframes mdc-checkbox-fade-out-background-#{$uid} {
40+
0%,
41+
80% {
42+
@include mdc-theme-prop(border-color, $to-stroke-color);
43+
@include mdc-theme-prop(background-color, $to-fill-color);
44+
}
45+
46+
100% {
47+
@include mdc-theme-prop(border-color, $from-stroke-color);
48+
@include mdc-theme-prop(background-color, $from-fill-color);
49+
}
6850
}
6951
}
7052

7153
@keyframes mdc-checkbox-unchecked-checked-checkmark-path {
7254
0%,
7355
50% {
74-
stroke-dashoffset: $_mdc-checkbox-mark-path-length;
56+
stroke-dashoffset: $mdc-checkbox-mark-path-length_;
7557
}
7658

7759
50% {
@@ -107,7 +89,7 @@
10789

10890
to {
10991
opacity: 0;
110-
stroke-dashoffset: $_mdc-checkbox-mark-path-length * -1;
92+
stroke-dashoffset: $mdc-checkbox-mark-path-length_ * -1;
11193
}
11294
}
11395

@@ -126,7 +108,7 @@
126108

127109
@keyframes mdc-checkbox-indeterminate-checked-checkmark {
128110
from {
129-
animation-timing-function: $_mdc-checkbox-indeterminate-checked-easing-function;
111+
animation-timing-function: $mdc-checkbox-indeterminate-checked-easing-function_;
130112
transform: rotate(45deg);
131113
opacity: 0;
132114
}
@@ -152,7 +134,7 @@
152134

153135
@keyframes mdc-checkbox-indeterminate-checked-mixedmark {
154136
from {
155-
animation-timing-function: $_mdc-checkbox-indeterminate-checked-easing-function;
137+
animation-timing-function: $mdc-checkbox-indeterminate-checked-easing-function_;
156138
transform: rotate(0deg);
157139
opacity: 1;
158140
}

0 commit comments

Comments
 (0)