Skip to content

Commit

Permalink
feat(form elements): provide possibility to visually hide the label (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
mfranzke authored Dec 15, 2023
1 parent e36ea82 commit 520feee
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 12 deletions.
6 changes: 6 additions & 0 deletions source/_patterns/01-elements/_form-elements.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
}
}

%form-label {
&[data-label-hidden="true"] {
@include a11y-visually-hidden($partial: $partial);
}
}

// ### Style variations
// Semitransparent
%form-element-semitransparent {
Expand Down
2 changes: 1 addition & 1 deletion source/_patterns/01-elements/checkbox/checkbox.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
value="{{ value }}"
{{#if describedbyid }} aria-describedby="{{ describedbyid }}"{{/if }}
{{#if invalid }} aria-invalid="{{ invalid }}"{{/if }}>
<label class="elm-label" for="{{ id }}">{{ label }}</label>
<label class="elm-label" for="{{ id }}"{{#if labelHidden }} data-label-hidden="true"{{/if }}>{{ label }}</label>
{{#if indeterminate}}
<script>
document.querySelector('#{{ id }}').indeterminate = true;
Expand Down
3 changes: 3 additions & 0 deletions source/_patterns/01-elements/checkbox/checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

@import "../../../css/partials.meta";
@import "checkbox.variables";
@import "../form-elements";

.elm-checkbox {
@include icon("\00A0", 24, "outline", $partial: $partial);
Expand Down Expand Up @@ -64,6 +65,8 @@
}

& + label {
@extend %form-label;

margin-left: $db-spacing-xs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Checkbox - indeterminate

## State: indeterminate

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

```js
inputInstance.indeterminate = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "checkbox05",
"labelHidden": true
}
11 changes: 11 additions & 0 deletions source/_patterns/01-elements/checkbox/checkbox~label-hidden.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Checkbox - Hidden label
---

## Possibility to hide the label

> 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.
source: <https://www.w3.org/WAI/tutorials/forms/labels/#hiding-label-text>

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.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Checkbox - indeterminate on dark background

## State: indeterminate

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

```js
inputInstance.indeterminate = true;
Expand Down
3 changes: 2 additions & 1 deletion source/_patterns/01-elements/input/input.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<label class="elm-label"
for="{{ id }}"
aria-hidden="true"
id="{{ id }}-label">{{ label }}</label>
id="{{ id }}-label"
{{#if labelHidden }} data-label-hidden="true"{{/if }}>{{ label }}</label>
{{#if description }}<p{{#if describedbyid }} id="{{ describedbyid }}"{{/if }} class="description">{{{ description }}}</p>{{/if }}
{{#if datalist }}<datalist id="datalist{{ id }}">
{{#each datalistItems }}
Expand Down
16 changes: 13 additions & 3 deletions source/_patterns/01-elements/input/input.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@charset "utf-8";

@import "../../../css/partials.meta";
@import "input.variables";
@import "../form-elements";

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

padding: $db-spacing-s $db-spacing-m;

width: 100%;

padding-inline: $db-spacing-m;
// No need to reposition the included value on hidden label
&:not(:has(+ label[data-label-hidden="true"])) {
padding-block: $db-spacing-s;
}

&[list] {
&::-webkit-calendar-picker-indicator {
margin-right: -1rem;
Expand All @@ -25,7 +30,10 @@

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

// * TODO: to get evaluated
Expand Down Expand Up @@ -89,6 +97,8 @@
}

& + .elm-label {
@extend %form-label;

color: $db-color-cool-gray-500;
display: block;
font-size: to-rem($pxValue: 12);
Expand Down
4 changes: 4 additions & 0 deletions source/_patterns/01-elements/input/input~hidden-label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "input20",
"labelHidden": "true"
}
13 changes: 13 additions & 0 deletions source/_patterns/01-elements/input/input~hidden-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Input - Hidden label
state: complete
order: 11
---

## Possibility to hide the label

> 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.
source: <https://www.w3.org/WAI/tutorials/forms/labels/#hiding-label-text>

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 input element.
2 changes: 1 addition & 1 deletion source/_patterns/01-elements/radio/_radio.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
{{#if describedbyid }} aria-describedby="{{ describedbyid }}"{{/if }}
{{#if invalid}} aria-invalid="true"{{/if }}
{{#if required}} required{{/if}}>
<label class="elm-label" for="{{ id }}">{{ label }}</label>
<label class="elm-label" for="{{ id }}"{{#if labelHidden }} data-label-hidden="true"{{/if }}>{{ label }}</label>
4 changes: 4 additions & 0 deletions source/_patterns/01-elements/radio/radio.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@charset "utf-8";

@import "../../../css/partials.meta";
@import "radio.variables";
@import "../form-elements";

.elm-radio {
appearance: none;
Expand Down Expand Up @@ -52,6 +54,8 @@
// }

& + label {
@extend %form-label;

margin-left: $db-spacing-xs;
}

Expand Down
2 changes: 1 addition & 1 deletion source/_patterns/01-elements/radio/radios.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#each radios }}
{{> elements-radio disabled=../disabled invalid=../invalid required=../required id=(unique-id) name=../name }}<br>
{{> elements-radio disabled=../disabled invalid=../invalid required=../required id=(unique-id) name=../name labelHidden=../labelHidden }}<br>
{{/each }}
4 changes: 4 additions & 0 deletions source/_patterns/01-elements/radio/radios~label-hidden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "radioLabelHidden",
"labelHidden": true
}
11 changes: 11 additions & 0 deletions source/_patterns/01-elements/radio/radios~label-hidden.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Radio - Hidden label
---

## Possibility to hide the label

> 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.
source: <https://www.w3.org/WAI/tutorials/forms/labels/#hiding-label-text>

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 radio element.
4 changes: 2 additions & 2 deletions source/_patterns/01-elements/select/select.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{#if required}} required{{/if}}
{{#if invalid }} aria-invalid="{{ invalid }}" {{/if }}
{{#if variant}} data-variant="{{variant}}"{{/if }}>
{{#if optgroup}}<optgroup label="Devices">{{else}}{{#unless multiple}}<option></option>{{/unless}}{{/if }}
{{#if optgroup}}<optgroup label="Devices">{{else}}{{#unless multiple}}{{#unless labelHidden}}<option></option>{{/unless}}{{/unless}}{{/if }}
<option value="smartphone">Smartphone</option>
<option value="softphone">Softphone</option>
<option value="headset">Headset</option>
Expand All @@ -18,4 +18,4 @@
<option value="developer">Developer</option>
</optgroup>{{/if }}
</select>
<label class="elm-label" for="{{ id }}">{{ label }}</label>
<label class="elm-label" for="{{ id }}"{{#if labelHidden }} data-label-hidden="true"{{/if }}>{{ label }}</label>
7 changes: 7 additions & 0 deletions source/_patterns/01-elements/select/select.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import "../../../css/partials.meta";
@import "select.variables";
@import "../form-elements";
@import "../../../css/db-ui-core.variables";
Expand Down Expand Up @@ -27,6 +28,10 @@
background-repeat: no-repeat;
height: to-rem($pxValue: 44);
padding: to-rem($pxValue: 20) to-rem($pxValue: 42) 0 $db-spacing-m;
// No need to reposition the included value on hidden label
&:has(+ label[data-label-hidden="true"]) {
padding-top: 0;
}

// Floating labels
& + .elm-label {
Expand Down Expand Up @@ -96,6 +101,8 @@

// Floating labels
& + .elm-label {
@extend %form-label;

display: block;
font-size: to-rem($pxValue: 12);
margin-bottom: -#{to-rem($pxValue: 18)};
Expand Down
5 changes: 5 additions & 0 deletions source/_patterns/01-elements/select/select~hidden-label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "select14",
"label": "Devices",
"labelHidden": "true"
}
13 changes: 13 additions & 0 deletions source/_patterns/01-elements/select/select~hidden-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Select - Hidden label
state: complete
order: 8
---

## Possibility to hide the label

> 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.
source: <https://www.w3.org/WAI/tutorials/forms/labels/#hiding-label-text>

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 select element.
2 changes: 1 addition & 1 deletion source/_patterns/01-elements/textarea/textarea.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! TODO: the retrieval of the standard values doesn't work so far }}
<label class="elm-label" for="{{ id }}">{{ label }}</label>
<label class="elm-label" for="{{ id }}"{{#if labelHidden }} data-label-hidden="true"{{/if }}>{{ label }}</label>
<textarea class="elm-textarea {{ styleModifier }}"
placeholder="{{ placeholder }}"
id="{{ id }}"
Expand Down
5 changes: 5 additions & 0 deletions source/_patterns/01-elements/textarea/textarea.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@charset "utf-8";

@import "../../../css/partials.meta";
@import "textarea.variables";
@import "../form-elements";

Expand Down Expand Up @@ -29,6 +30,10 @@
// align-items: center;
// justify-content: flex-start;

.elm-label:has(+ &) {
@extend %form-label;
}

&:not([rows]) {
max-height: to-rem($pxValue: 600);
min-height: to-rem($pxValue: 120);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "textarea08",
"value": "Input",
"labelHidden": "true"
}
13 changes: 13 additions & 0 deletions source/_patterns/01-elements/textarea/textarea~hidden-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Textarea - Hidden label
state: complete
order: 8
---

## Possibility to hide the label

> 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.
source: <https://www.w3.org/WAI/tutorials/forms/labels/#hiding-label-text>

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 textarea element.

0 comments on commit 520feee

Please sign in to comment.