Skip to content

Commit b5b0c5e

Browse files
committed
Accessibility + required and validation styling
1 parent 4c07948 commit b5b0c5e

File tree

9 files changed

+97
-31
lines changed

9 files changed

+97
-31
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release History
22

3+
## 1.1.0
4+
5+
* Fixed keyboard accessibility
6+
* Added required styling
7+
* Added validation state styling
8+
39
## 1.0.12
410

511
* Upgraded to Bootstrap 4.3.1

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bootstrap4c-custom-switch",
33
"description": "Bootstrap 4 Component - Custom switch",
4-
"version": "1.0.12",
4+
"version": "1.1.0",
55
"license": "MIT",
66
"authors": [
77
{ "name": "Martin Haubek", "email": "[email protected]", "homepage": "https://haubek.github.io" }

dist/css/component-custom-switch.css

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
*
44
*
55
* Bootstrap 4 Component - Custom switch
6-
* Version: 1.0.12
6+
* Version: 1.1.0
77
* Copyright (c) 2017-18 Martin Haubek
88
*
99
*
1010
*
1111
*/
1212
.custom-switch .custom-switch-input {
13-
display: none; }
13+
position: absolute;
14+
z-index: -1;
15+
opacity: 0; }
1416
.custom-switch .custom-switch-input, .custom-switch .custom-switch-input:after, .custom-switch .custom-switch-input:before,
1517
.custom-switch .custom-switch-input *,
1618
.custom-switch .custom-switch-input *:after,
@@ -38,7 +40,7 @@
3840
padding: 4px;
3941
background: #adb5bd;
4042
border-radius: 76px;
41-
transition: all 200ms ease; }
43+
transition: all 150ms ease; }
4244
.custom-switch .custom-switch-input + .custom-switch-btn:after, .custom-switch .custom-switch-input + .custom-switch-btn:before {
4345
position: relative;
4446
display: block;
@@ -49,7 +51,7 @@
4951
left: 2px;
5052
border-radius: 50%;
5153
background: white;
52-
transition: all 200ms ease; }
54+
transition: all 150ms ease; }
5355
.custom-switch .custom-switch-input + .custom-switch-btn:before {
5456
display: none; }
5557
.custom-switch .custom-switch-input + .custom-switch-btn.text-hide {
@@ -77,8 +79,10 @@
7779
cursor: default; }
7880
.custom-switch .custom-switch-input[disabled]:checked + .custom-switch-btn {
7981
background: rgba(40, 167, 69, 0.4); }
80-
.custom-switch .custom-switch-input:not([disabled]):active ~ .custom-switch-btn, .custom-switch .custom-switch-input:not([disabled]):focus ~ .custom-switch-btn, .custom-switch .custom-switch-input:not([disabled]):hover ~ .custom-switch-btn {
82+
.custom-switch .custom-switch-input:not([disabled]):focus ~ .custom-switch-btn {
8183
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 0.25rem rgba(56, 123, 189, 0.25); }
84+
.custom-switch .custom-switch-input[required] ~ .custom-switch-btn {
85+
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 0.125rem rgba(220, 53, 69, 0.5); }
8286

8387
.custom-switch .custom-switch-form-text {
8488
display: inline-block;
@@ -205,3 +209,7 @@
205209
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='76' height='24'%3E%3Ctext x='6.85714' y='15' font-size='10px' font-family='-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji' fill='%23fff'%3EEnabled%3C/text%3E%3C/svg%3E"); }
206210
.custom-switch-xs.custom-switch-label-status .custom-switch-input:checked + .custom-switch-btn:after {
207211
left: 52px; }
212+
213+
.is-invalid .custom-switch .custom-switch-input ~ .custom-switch-btn,
214+
.was-validated .custom-switch:invalid .custom-switch-input ~ .custom-switch-btn {
215+
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 0.25rem #dc3545; }

dist/css/component-custom-switch.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.html

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,58 @@
1212
<div class="d-flex align-items-start flex-column h-100">
1313
<div class="w-100 m-auto py-5 text-center">
1414

15-
<div class="custom-switch">
15+
<div class="custom-switch pl-0">
1616
<input class="custom-switch-input" id="example_01" type="checkbox">
1717
<label class="custom-switch-btn" for="example_01"></label>
1818
</div>
19+
1920
<hr>
20-
<div class="custom-switch custom-switch-label-io">
21+
22+
<div class="custom-switch custom-switch-label-io pl-0">
2123
<input class="custom-switch-input" id="example_02" type="checkbox">
2224
<label class="custom-switch-btn" for="example_02"></label>
2325
</div>
26+
2427
<hr>
25-
<div class="custom-switch custom-switch-label-onoff">
28+
29+
<div class="custom-switch custom-switch-label-onoff pl-0">
2630
<input class="custom-switch-input" id="example_03" type="checkbox">
2731
<label class="custom-switch-btn" for="example_03"></label>
2832
</div>
33+
2934
<hr>
30-
<div class="custom-switch custom-switch-label-yesno">
35+
36+
<div class="custom-switch custom-switch-label-yesno pl-0">
3137
<input class="custom-switch-input" id="example_04" type="checkbox">
3238
<label class="custom-switch-btn" for="example_04"></label>
3339
</div>
40+
3441
<hr>
35-
<div class="custom-switch custom-switch-label-status">
42+
43+
<div class="custom-switch custom-switch-label-status pl-0">
3644
<input class="custom-switch-input" id="example_05" type="checkbox">
3745
<label class="custom-switch-btn" for="example_05"></label>
3846
</div>
47+
3948
<hr>
40-
<div class="custom-switch custom-switch-label-onoff custom-switch-sm">
49+
50+
<div class="custom-switch custom-switch-label-onoff custom-switch-sm pl-0">
4151
<input class="custom-switch-input" id="example_06" type="checkbox">
4252
<label class="custom-switch-btn" for="example_06"></label>
4353
</div>
4454
<p>I'm <code>sm</code></p>
55+
4556
<hr>
46-
<div class="custom-switch custom-switch-label-onoff custom-switch-xs">
57+
58+
<div class="custom-switch custom-switch-label-onoff custom-switch-xs pl-0">
4759
<input class="custom-switch-input" id="example_07" type="checkbox">
4860
<label class="custom-switch-btn" for="example_07"></label>
4961
</div>
5062
<p>I'm <code>xs</code></p>
63+
5164
<hr>
52-
<div class="custom-switch custom-switch-label-yesno">
65+
66+
<div class="custom-switch custom-switch-label-yesno pl-0">
5367
<input class="custom-switch-input" id="example_08" type="checkbox">
5468
<label class="custom-switch-btn" for="example_08"></label>
5569
<div class="custom-switch-content-checked">
@@ -59,25 +73,49 @@
5973
<span class="text-danger">I'm unchecked (click me!)</span>
6074
</div>
6175
</div>
76+
6277
<hr>
63-
<div class="custom-switch custom-switch-label-io">
64-
<input class="custom-switch-input" id="example_09" type="checkbox" disabled>
78+
79+
<div class="custom-switch pl-0">
80+
<input class="custom-switch-input" id="example_09" type="checkbox" required>
6581
<label class="custom-switch-btn" for="example_09"></label>
6682
</div>
67-
<div class="custom-switch custom-switch-label-io">
68-
<input class="custom-switch-input" id="example_10" type="checkbox" checked disabled>
83+
<p>I'm <code>required</code></p>
84+
85+
<hr>
86+
87+
<div class="custom-switch pl-0">
88+
<input class="custom-switch-input" id="example_10" type="checkbox" disabled>
6989
<label class="custom-switch-btn" for="example_10"></label>
7090
</div>
71-
<p>I'm disabled</p>
91+
<div class="custom-switch pl-0">
92+
<input class="custom-switch-input" id="example_11" type="checkbox" checked disabled>
93+
<label class="custom-switch-btn" for="example_11"></label>
94+
</div>
95+
<p>I'm <code>disabled</code></p>
96+
7297
<hr>
98+
7399
<div class="form-group">
74-
<div for="example_11">I'm a form label:</div>
75-
<div class="custom-switch">
76-
<input type="checkbox" class="custom-switch-input" id="example_11" checked>
77-
<label class="custom-switch-btn text-hide" for="example_11">{value}</label>
100+
<div for="example_12">I'm a &lt;div&gt; form label:</div>
101+
<div class="custom-switch pl-0">
102+
<input type="checkbox" class="custom-switch-input" id="example_12" checked>
103+
<label class="custom-switch-btn text-hide" for="example_12">{value}</label>
78104
</div>
79105
</div>
80106

107+
<hr>
108+
<legend>Validation</legend>
109+
<form class="was-validated">
110+
<div class="form-group is-invalid">
111+
<div class="custom-switch pl-0">
112+
<input class="custom-switch-input" id="example_13" type="checkbox">
113+
<label class="custom-switch-btn" for="example_13"></label>
114+
</div>
115+
</div>
116+
</form>
117+
<p>I'm <code>invalid</code></p>
118+
81119
</div>
82120
</div>
83121
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bootstrap4c-custom-switch",
33
"description": "Bootstrap 4 Component - Custom switch",
4-
"version": "1.0.12",
4+
"version": "1.1.0",
55
"license": "MIT",
66
"author": {
77
"name": "Martin Haubek",

src/scss/_component-custom-switch.scss

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.custom-switch {
22
.custom-switch-input {
3-
display: none;
3+
position: absolute;
4+
z-index: -1;
5+
opacity: 0;
46
&,
57
&:after,
68
&:before,
@@ -83,14 +85,17 @@
8385
}
8486
}
8587
&:not([disabled]) {
86-
&:active,
87-
&:focus,
88-
&:hover {
88+
&:focus {
8989
~ .custom-switch-btn {
9090
box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 0 .25rem rgba(56,123,189,.25);
9191
}
9292
}
9393
}
94+
&[required] {
95+
~ .custom-switch-btn {
96+
box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 0 .125rem rgba($switch-required-color,.5);
97+
}
98+
}
9499
}
95100
.custom-switch-form-text {
96101
display: inline-block;
@@ -288,3 +293,10 @@
288293
}
289294
}
290295
}
296+
297+
// Validation
298+
299+
.is-invalid .custom-switch .custom-switch-input ~ .custom-switch-btn,
300+
.was-validated .custom-switch:invalid .custom-switch-input ~ .custom-switch-btn {
301+
box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 0 .25rem rgba($switch-invalid-color,1);
302+
}

src/scss/_component-variables.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ $switch-font-size: 12;
1212
$switch-font-size-sm: 11;
1313
$switch-font-size-xs: 10;
1414
$switch-font-family: $font-family-sans-serif;
15-
$switch-duration: 200ms;
15+
$switch-duration: 150ms;
16+
$switch-required-color: $danger;
17+
$switch-invalid-color: $form-feedback-invalid-color;
1618

1719
$switch-checked-color: $white;
1820
$switch-checked-bg: $green;

src/scss/build.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
*
55
* Bootstrap 4 Component - Custom switch
6-
* Version: 1.0.12
6+
* Version: 1.1.0
77
* Copyright (c) 2017-18 Martin Haubek
88
*
99
*

0 commit comments

Comments
 (0)