Skip to content

Commit bcb68aa

Browse files
committed
fix(checkbox): styles
Add focus styling and updating to M3 styling which is more simple and also usable without complicated markup.
1 parent 81b7c7d commit bcb68aa

File tree

2 files changed

+111
-16
lines changed

2 files changed

+111
-16
lines changed

components/checkbox/_checkboxes.scss

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,114 @@
11
/* Checkboxes */
22

3-
/* Remove default checkbox */
4-
[type="checkbox"]:not(:checked),
5-
[type="checkbox"]:checked {
3+
@at-root {
4+
:root {
5+
--checkbox-size: 18px; // 18px
6+
--checkbox-shape: 2px;
7+
}
8+
}
9+
10+
input[type="checkbox"] {
11+
/* Add if not using autoprefixer */
12+
-webkit-appearance: none;
13+
appearance: none;
14+
/* For iOS < 15 to remove gradient background */
15+
// background-color: #fff;
16+
margin: 0;
17+
position: relative;
18+
cursor: pointer;
19+
}
20+
21+
input[type="checkbox"]:focus {
22+
outline: 3px solid var(--md-sys-color-secondary);
23+
border-radius: 50%;
24+
outline-offset: 0.5rem;
25+
}
26+
27+
input[type="checkbox"]:before {
28+
opacity: 1;
629
position: absolute;
7-
opacity: 0;
8-
pointer-events: none;
30+
display: block;
31+
width: var(--checkbox-size);
32+
height: var(--checkbox-size);
33+
border: 2px solid var(--md-sys-color-on-surface-variant);
34+
border-radius: var(--checkbox-shape);
35+
content: "";
36+
// background: #fff;
37+
}
38+
39+
input[type="checkbox"]:after {
40+
position: relative;
41+
display: block;
42+
// left: 2px;
43+
// top: -11px;
44+
width: var(--checkbox-size);
45+
height: var(--checkbox-size);
46+
// border-width: 2px;
47+
// border-style: solid;
48+
// border-color: var(--md-sys-color-on-surface-variant);
49+
content: "";
50+
// background-image: linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
51+
border-radius: var(--checkbox-shape);
52+
background-repeat: no-repeat;
53+
background-position: center;
54+
}
55+
56+
input[type="checkbox"]:checked:after {
57+
background-color: var(--md-sys-color-primary);
58+
clip-path: polygon(0 48%, 0 0, 56% 0, 74% 17%, 40% 64%, 23% 50%, 14% 61%, 42% 82%, 81% 31%, 32% 0, 100% 0, 100% 100%, 0 100%, 0 49%);
59+
}
60+
61+
// Disabled
62+
input[type="checkbox"]:disabled:before {
63+
filter: opacity(0.2);
64+
}
65+
input[type="checkbox"]:disabled:after {
66+
filter: opacity(0.2);
67+
}
68+
69+
// Hover
70+
input[type="checkbox"]:not(:disabled):checked:hover:after{
71+
// background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%,#FFF 100%);
72+
background-color: var(--md-sys-color-primary);
73+
}
74+
75+
input[type="checkbox"]:not(:disabled):hover:after{
76+
// background-image: linear-gradient(135deg, #8BB0C2 0%,#FFF 100%);
77+
border-color: #85A9BB #92C2DA #92C2DA #85A9BB;
78+
}
79+
80+
input[type="checkbox"]:not(:disabled):hover:before{
81+
border-color: #3D7591;
982
}
1083

84+
/*
1185
// Checkbox Styles
1286
[type="checkbox"] {
1387
// Text Label Style
1488
+ span:not(.lever) {
1589
position: relative;
16-
padding-left: 35px;
1790
cursor: pointer;
1891
display: inline-block;
19-
height: 25px;
20-
line-height: 25px;
92+
height: var(--checkbox-size);
93+
line-height: var(--checkbox-size);
94+
padding-left: var(--checkbox-size);
95+
// background-color: red;
2196
font-size: 1rem;
2297
user-select: none;
2398
}
2499
25-
/* checkbox aspect */
100+
// checkbox aspect
26101
+ span:not(.lever):before,
27102
&:not(.filled-in) + span:not(.lever):after {
28103
content: '';
29104
position: absolute;
30105
top: 0;
31106
left: 0;
32-
width: 18px;
33-
height: 18px;
107+
width: var(--checkbox-size);
108+
height: var(--checkbox-size);
34109
z-index: 0;
35110
border: 2px solid var(--md-sys-color-on-surface-variant);
36111
border-radius: 1px;
37-
margin-top: 3px;
38112
transition: .2s;
39113
}
40114
@@ -82,7 +156,7 @@
82156
}
83157
}
84158
85-
/* Indeterminate checkbox */
159+
// Indeterminate checkbox
86160
[type="checkbox"]:indeterminate {
87161
+ span:not(.lever):before {
88162
top: -11px;
@@ -117,7 +191,7 @@
117191
content: '';
118192
left: 0;
119193
position: absolute;
120-
/* .1s delay is for check animation */
194+
// .1s delay is for check animation
121195
transition: border .25s, background-color .25s, width .20s .1s, height .20s .1s, top .20s .1s, left .20s .1s;
122196
z-index: 1;
123197
}
@@ -151,8 +225,8 @@
151225
height: 13px;
152226
border-top: 2px solid transparent;
153227
border-left: 2px solid transparent;
154-
border-right: 2px solid var(--md-sys-color-on-primary);
155-
border-bottom: 2px solid var(--md-sys-color-on-primary);
228+
border-right: 2px solid var(--md-sys-color-on-primary);
229+
border-bottom: 2px solid var(--md-sys-color-on-primary);
156230
transform: rotateZ(37deg);
157231
transform-origin: 100% 100%;
158232
}
@@ -199,3 +273,4 @@
199273
border: none;
200274
}
201275
}
276+
*/

components/checkbox/checkbox.stories.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,44 @@ export const Default: StoryObj = {
88
render() {
99
return `
1010
<form action="#">
11+
12+
<p>Normal basic checkbox input</p>
13+
14+
<input type="checkbox" />
15+
<input type="checkbox" checked="checked" />
16+
<input type="checkbox" disabled />
17+
<input type="checkbox" disabled checked="checked" />
18+
19+
<p>Compound Checkbox Elements</p>
20+
1121
<p>
1222
<label>
1323
<input type="checkbox" />
1424
<span>Unchecked</span>
1525
</label>
1626
</p>
27+
1728
<p>
1829
<label>
1930
<input type="checkbox" checked="checked" />
2031
<span>Checked</span>
2132
</label>
2233
</p>
34+
35+
<p>
36+
<label>
37+
<input type="checkbox" />
38+
<span></span>
39+
</label>
40+
</p>
41+
2342
<p>
2443
<label>
2544
<input type="checkbox" disabled="disabled" />
2645
<span>Disabled</span>
2746
</label>
2847
</p>
48+
2949
<p>
3050
<label>
3151
<input type="checkbox" checked="checked" disabled="disabled" />

0 commit comments

Comments
 (0)