Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit a6ac842

Browse files
committed
doc(cb-a11y): Labelling form controls: content
1 parent 95db2f1 commit a6ac842

File tree

2 files changed

+45
-73
lines changed

2 files changed

+45
-73
lines changed

public/docs/_examples/cb-a11y/ts/a11y.css

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ hr {
1212
color: #fff;
1313
}
1414

15+
/* #docregion cb-a11y-form-controls-visually-hidden-style*/
1516
.visually-hidden {
1617
border: 0;
1718
clip: rect(0 0 0 0);
@@ -22,25 +23,4 @@ hr {
2223
position: absolute;
2324
width: 1px;
2425
}
25-
26-
/*This is a copy of the .form-control class in bootstrap
27-
to enable adding it to all form controls without to much
28-
noise in the examples.*/
29-
/*div:not(.radio):not(.checkbox) input, textarea, select {*/
30-
/*display: block;*/
31-
/*width: 100%;*/
32-
/*height: 34px;*/
33-
/*padding: 6px 12px;*/
34-
/*font-size: 14px;*/
35-
/*line-height: 1.42857143;*/
36-
/*color: #555;*/
37-
/*background-color: #fff;*/
38-
/*background-image: none;*/
39-
/*border: 1px solid #ccc;*/
40-
/*border-radius: 4px;*/
41-
/*-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);*/
42-
/*box-shadow: inset 0 1px 1px rgba(0,0,0,.075);*/
43-
/*-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;*/
44-
/*-o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;*/
45-
/*transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;*/
46-
/*}*/
26+
/* #enddocregion */

public/docs/ts/latest/cookbook/a11y.jade

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,14 @@ code-example(language="html" escape="html").
174174
:marked
175175
#### Inputs and textareas
176176

177-
+makeTabs('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html, cb-a11y/ts/app/form-controls/a11y-form-controls.component.html',
178-
'cb-a11y-form-controls-input-implicit,cb-a11y-form-controls-textarea-implicit',
179-
'Implicitly labelled input,Implicitly labelled textarea')
177+
Labelling an input:
178+
179+
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html','cb-a11y-form-controls-input-implicit')
180+
181+
:marked
182+
Labelling a textarea:
183+
184+
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html','cb-a11y-form-controls-textarea-implicit')
180185

181186
:marked
182187
#### Checkboxes and radiobuttons
@@ -186,10 +191,16 @@ code-example(language="html" escape="html").
186191
Because of the many `input` fields making up a `checkbox group` or `radiobutton group`, the usual rule applies
187192
for each input but the entire group also needs labelled and this is done by using `fieldset` and `legend`.
188193

189-
+makeTabs('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html, cb-a11y/ts/app/form-controls/a11y-form-controls.component.html',
190-
'cb-a11y-form-controls-checkboxes-implicit,cb-a11y-form-controls-radiobuttons-implicit',
191-
'Implicitly labelled Checkboxes,Implicitly labelled Radiobuttons')
194+
:marked
195+
Labelling checkboxes:
196+
197+
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html','cb-a11y-form-controls-checkboxes-implicit')
192198

199+
:marked
200+
Labelling radiobuttons:
201+
202+
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html','cb-a11y-form-controls-radiobuttons-implicit')
203+
193204
:marked
194205
#### Select lists
195206

@@ -206,42 +217,13 @@ code-example(language="html" escape="html").
206217
the `HTML` form element. This is then connected up up via a `for / id` pairing *[The `for` attribute of the label has to
207218
match the `id` of the element being labelled]* .
208219

209-
This is also the reason why `implicit labelling` could be more useful with reusable Angular 2 components.
210-
Remember that an `id` should be unique on a page. This means that any component with `explicit labelling` in its
211-
template, will need a way ensure that its elements are uniquely labelled, unless it is certain that the component
212-
will only be used once per page.
220+
Remember, this is why we said that `implicit labelling` is recommended due to the extra code you may required to keep
221+
the id's unique per page.
213222

214-
As an example, we will only look at adding an `explicit label` to an `input`. The examples under the `implicit labelling`
223+
As an example, we will only look at adding an `explicit label` to a `text input`. The examples under the `implicit labelling`
215224
section can then easily be adjusted to use this syntax.
216225

217-
.l-sub-section
218-
:marked
219-
In the example application code we are generating `GUID's` for id's to ensure that they are unique. You can use your
220-
own method to do this, as long as every id on a page is unique.
221-
222-
:marked
223-
Component template:
224-
225-
+makeExample('cb-a11y/ts/app/form-controls/a11y-input-explicit.component.html')
226-
227-
:marked
228-
User writes:
229-
230-
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html','cb-a11y-form-controls-input-explicit-usage')
231-
232-
:marked
233-
Rendered out:
234-
235-
code-example(language="html" escape="html" format="linenums").
236-
<a11y-input-explicit>
237-
<div class="form-group">
238-
<label for="c90c7f99-330e-4723-bfa8-9ab72a4bd435">
239-
Label for input:
240-
</label>
241-
<input class="form-control"
242-
id="c90c7f99-330e-4723-bfa8-9ab72a4bd435">
243-
</div>
244-
</a11y-input-explicit>
226+
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html', 'cb-a11y-form-controls-input-explicit')
245227

246228
:marked
247229
### Hidden labels
@@ -265,27 +247,32 @@ code-example(language="html" escape="html" format="linenums").
265247
shrinking it right down to be absolutely minute. We wont see it but screen readers will still find it and read it while
266248
still linking it to the correct input via the `for / id` linkup. The following example contains such a
267249
`visually hidden` style.
268-
250+
269251
:marked
270-
Let us create a component showing off both, and use the internal component styles to hide the label.
271-
272-
+makeTabs('cb-a11y/ts/app/form-controls/a11y-hidden-labels.component.html, cb-a11y/ts/app/form-controls/a11y-hidden-labels.component.ts, cb-a11y/ts/app/form-controls/a11y-hidden-labels.component.css', null, 'a11y-hidden-labels.component.html,a11y-hidden-labels.component.ts,a11y-hidden-labels.component.css')
273-
252+
Example of a good visually hidden css style:
253+
254+
+makeExample('cb-a11y/ts/a11y.css', 'cb-a11y-form-controls-visually-hidden-style')
255+
274256
:marked
275-
Which can then be used as:
257+
Applying the style to a control to hide the label:
276258

277-
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html','cb-a11y-form-controls-hidden-labels-usage')
259+
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html', 'cb-a11y-form-controls-hidden-label-explicit')
278260

279261
:marked
280-
The way the label is rendered int he final `HTML` is no secret to us anymore, but when you inspect the second input,
281-
you will see that an `aria-label` attribute has been added to the input. Simplified:
262+
Alternatively labelling a control with `aria-label`:
282263

283-
code-example(language="html" escape="html" format="linenums").
284-
<input aria-label="Filter:">
264+
+makeExample('cb-a11y/ts/app/form-controls/a11y-form-controls.component.html','cb-a11y-form-controls-hidden-label-aria')
285265

286266
:marked
287-
This `aria-label` attribute serves the same purpose as our `LABEL` tags above and will tell screen readers what
267+
This `aria-label` attribute serves the same `a11y` purpose as our `label` tags above and will tell screen readers what
288268
the field is called.
269+
270+
.l-sub-section
271+
:marked
272+
So why not simply always use `aria-label`? This is because adding the `label` element not only provides the visual
273+
label, but linking a `label` to a `form control` also means that clicking on the `label` will select the
274+
`form control` itself. This touches on another area of `a11y`, as this also assists users with motor disabilites
275+
to more easily select them.
289276

290277
:marked
291278
### Labelling custom form controls
@@ -324,6 +311,11 @@ code-example(language="html" escape="html" format="linenums").
324311
</div>
325312
</a11y-custom-control>
326313

314+
.l-sub-section
315+
:marked
316+
In the example application code we are generating `GUID's` for id's to ensure that they are unique. You can use your
317+
own method to do this, as long as every id on a page is unique.
318+
327319

328320
.l-main-section
329321
<a id="keyboard-shortcuts"></a>

0 commit comments

Comments
 (0)