Skip to content

Commit 520feee

Browse files
authored
feat(form elements): provide possibility to visually hide the label (#628)
* feat: provide general styling for visually hidden labels * docs: typo * feat: provided the possibility for all forms to visually hide the label * feat: provided the possibility for all forms to visually hide the label * Update _form-elements.scss * refactor: added missing references * refactor: not providing an empty option on hidden label
1 parent e36ea82 commit 520feee

24 files changed

+131
-12
lines changed

source/_patterns/01-elements/_form-elements.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
}
3434
}
3535

36+
%form-label {
37+
&[data-label-hidden="true"] {
38+
@include a11y-visually-hidden($partial: $partial);
39+
}
40+
}
41+
3642
// ### Style variations
3743
// Semitransparent
3844
%form-element-semitransparent {

source/_patterns/01-elements/checkbox/checkbox.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
value="{{ value }}"
66
{{#if describedbyid }} aria-describedby="{{ describedbyid }}"{{/if }}
77
{{#if invalid }} aria-invalid="{{ invalid }}"{{/if }}>
8-
<label class="elm-label" for="{{ id }}">{{ label }}</label>
8+
<label class="elm-label" for="{{ id }}"{{#if labelHidden }} data-label-hidden="true"{{/if }}>{{ label }}</label>
99
{{#if indeterminate}}
1010
<script>
1111
document.querySelector('#{{ id }}').indeterminate = true;

source/_patterns/01-elements/checkbox/checkbox.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
@import "../../../css/partials.meta";
44
@import "checkbox.variables";
5+
@import "../form-elements";
56

67
.elm-checkbox {
78
@include icon("\00A0", 24, "outline", $partial: $partial);
@@ -64,6 +65,8 @@
6465
}
6566

6667
& + label {
68+
@extend %form-label;
69+
6770
margin-left: $db-spacing-xs;
6871
}
6972
}

source/_patterns/01-elements/checkbox/checkbox~indeterminate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Checkbox - indeterminate
44

55
## State: indeterminate
66

7-
Please keep in mind, that the special state of an indeterminate element is necessary to get set programatically with JavaScript:
7+
Please keep in mind, that the special state of an indeterminate element is necessary to get set programmatically with JavaScript:
88

99
```js
1010
inputInstance.indeterminate = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": "checkbox05",
3+
"labelHidden": true
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Checkbox - Hidden label
3+
---
4+
5+
## Possibility to hide the label
6+
7+
> A label for a form control helps everyone better understand its purpose. In some cases, the purpose may be clear enough from the context when the content is rendered visually. The label can be hidden visually, though it still needs to be provided within the code to support other forms of presentation and interaction, such as for screen reader and speech input users.
8+
9+
source: <https://www.w3.org/WAI/tutorials/forms/labels/#hiding-label-text>
10+
11+
You could set the attribute `data-label-hidden="true"` for the rare case that you would want to visually hide the textual label for a checkbox element.

source/_patterns/01-elements/checkbox/checkbox~on-dark-background-indeterminate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Checkbox - indeterminate on dark background
44

55
## State: indeterminate
66

7-
Please keep in mind, that the special state of an indeterminate element is necessary to get set programatically with JavaScript:
7+
Please keep in mind, that the special state of an indeterminate element is necessary to get set programmatically with JavaScript:
88

99
```js
1010
inputInstance.indeterminate = true;

source/_patterns/01-elements/input/input.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<label class="elm-label"
1616
for="{{ id }}"
1717
aria-hidden="true"
18-
id="{{ id }}-label">{{ label }}</label>
18+
id="{{ id }}-label"
19+
{{#if labelHidden }} data-label-hidden="true"{{/if }}>{{ label }}</label>
1920
{{#if description }}<p{{#if describedbyid }} id="{{ describedbyid }}"{{/if }} class="description">{{{ description }}}</p>{{/if }}
2021
{{#if datalist }}<datalist id="datalist{{ id }}">
2122
{{#each datalistItems }}

source/_patterns/01-elements/input/input.scss

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@charset "utf-8";
22

3+
@import "../../../css/partials.meta";
34
@import "input.variables";
45
@import "../form-elements";
56

@@ -13,10 +14,14 @@
1314
line-height: to-rem($pxValue: 24);
1415
max-width: 100%;
1516

16-
padding: $db-spacing-s $db-spacing-m;
17-
1817
width: 100%;
1918

19+
padding-inline: $db-spacing-m;
20+
// No need to reposition the included value on hidden label
21+
&:not(:has(+ label[data-label-hidden="true"])) {
22+
padding-block: $db-spacing-s;
23+
}
24+
2025
&[list] {
2126
&::-webkit-calendar-picker-indicator {
2227
margin-right: -1rem;
@@ -25,7 +30,10 @@
2530

2631
&:not(:placeholder-shown) {
2732
padding-bottom: 0;
28-
padding-top: to-rem($pxValue: 16);
33+
// No need to reposition the included value on hidden label
34+
&:not(:has(+ label[data-label-hidden="true"])) {
35+
padding-top: to-rem($pxValue: 16);
36+
}
2937
}
3038

3139
// * TODO: to get evaluated
@@ -89,6 +97,8 @@
8997
}
9098

9199
& + .elm-label {
100+
@extend %form-label;
101+
92102
color: $db-color-cool-gray-500;
93103
display: block;
94104
font-size: to-rem($pxValue: 12);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": "input20",
3+
"labelHidden": "true"
4+
}

0 commit comments

Comments
 (0)