Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit b4e9f4b

Browse files
committed
chore: update docs to use one label
1 parent a04d1ab commit b4e9f4b

File tree

9 files changed

+51
-65
lines changed

9 files changed

+51
-65
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Material Components for the web is the successor to [Material Design Lite](https
4141

4242
<!-- Render textfield component -->
4343
<label class="mdc-text-field">
44-
<input type="text" id="my-text-field" class="mdc-text-field__input">
45-
<label class="mdc-floating-label" for="my-text-field">Label</label>
44+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
45+
<span class="mdc-floating-label" id="label-id">Label</span>
4646
<div class="mdc-line-ripple"></div>
4747
</label>
4848

@@ -72,8 +72,8 @@ Sample usage of text field component. Please see [MDC Textfield](packages/mdc-te
7272

7373
```html
7474
<label class="mdc-text-field">
75-
<input type="text" id="my-text-field" class="mdc-text-field__input">
76-
<label class="mdc-floating-label" for="my-text-field">Label</label>
75+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
76+
<span class="mdc-floating-label" id="label-id">Label</span>
7777
<div class="mdc-line-ripple"></div>
7878
</label>
7979
```

docs/migrating-from-mdl.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ MDC Web:
9999

100100
```html
101101
<label class="mdc-text-field">
102-
<input class="mdc-text-field__input" type="text" id="input">
103-
<label for="input" class="mdc-floating-label">Input Label</label>
102+
<input class="mdc-text-field__input" type="text" aria-labelledby="label-id">
103+
<span id="label-id" class="mdc-floating-label">Input Label</span>
104104
<div class="mdc-line-ripple"></div>
105105
</label>
106106
```
@@ -125,8 +125,8 @@ element, with the component’s class name as the value. For example:
125125

126126
```html
127127
<label class="mdc-text-field" data-mdc-auto-init="MDCTextField">
128-
<input class="mdc-text-field__input" type="text" id="input">
129-
<label for="input" class="mdc-floating-label">Input Label</label>
128+
<input class="mdc-text-field__input" type="text" aria-labelledby="label-id">
129+
<span id="label-id" class="mdc-floating-label">Input Label</span>
130130
<div class="mdc-line-ripple"></div>
131131
</label>
132132
```

packages/mdc-auto-init/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ properly.
3333

3434
```html
3535
<label class="mdc-text-field" data-mdc-auto-init="MDCTextField">
36-
<input class="mdc-text-field__input" type="text" id="input">
37-
<label for="input" class="mdc-floating-label">Input Label</label>
36+
<input class="mdc-text-field__input" type="text" aria-labelledby="label-id">
37+
<span id="label-id" class="mdc-floating-label">Input Label</span>
3838
<div class="mdc-line-ripple"></div>
3939
</label>
4040

@@ -53,8 +53,8 @@ using a property whose name is the value of `data-mdc-auto-init`. For example, g
5353

5454
```html
5555
<label class="mdc-text-field" data-mdc-auto-init="MDCTextField">
56-
<input class="mdc-text-field__input" type="text" id="input">
57-
<label for="input" class="mdc-floating-label">Input Label</label>
56+
<input class="mdc-text-field__input" type="text" aria-labelledby="label-id">
57+
<span id="label-id" class="mdc-floating-label">Input Label</span>
5858
<div class="mdc-line-ripple"></div>
5959
</label>
6060
```

packages/mdc-floating-label/README.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ npm install @material/floating-label
3232
### HTML Structure
3333

3434
```html
35-
<label class="mdc-floating-label" for="my-text-field-id">Hint text</label>
35+
<span class="mdc-floating-label" id="label-id">Hint text</span>
3636
```
3737

38+
> NOTE: Use `aria-labelledby` with the label's ID for accessibility.
39+
3840
### Styles
3941

4042
```scss
@@ -49,23 +51,6 @@ import {MDCFloatingLabel} from '@material/floating-label';
4951
const floatingLabel = new MDCFloatingLabel(document.querySelector('.mdc-floating-label'));
5052
```
5153

52-
## Variants
53-
54-
### Avoid Dynamic ID Generation
55-
56-
If you're using the JavaScript-enabled version of floating label, you can avoid needing to assign
57-
a unique `id` to each `<input>` by wrapping `mdc-text-field__input` within a `<label>`:
58-
59-
```html
60-
<label class="mdc-text-field">
61-
<input type="text" class="mdc-text-field__input">
62-
<span class="mdc-floating-label">Hint Text</span>
63-
<div class="mdc-text-field__bottom-line"></div>
64-
</label>
65-
```
66-
67-
> NOTE: This method also works with `<select>`.
68-
6954
## Style Customization
7055

7156
### CSS Classes

packages/mdc-notched-outline/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ npm install @material/notched-outline
3636
<div class="mdc-notched-outline">
3737
<div class="mdc-notched-outline__leading"></div>
3838
<div class="mdc-notched-outline__notch">
39-
<label class="mdc-floating-label">Label</label>
39+
<span class="mdc-floating-label">Label</span>
4040
</div>
4141
<div class="mdc-notched-outline__trailing"></div>
4242
</div>

packages/mdc-select/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ same.
149149
<div class="mdc-notched-outline">
150150
<div class="mdc-notched-outline__leading"></div>
151151
<div class="mdc-notched-outline__notch">
152-
<label id="outlined-select-label" class="mdc-floating-label">Pick a Food Group</label>
152+
<span id="outlined-select-label" class="mdc-floating-label">Pick a Food Group</span>
153153
</div>
154154
<div class="mdc-notched-outline__trailing"></div>
155155
</div>

packages/mdc-textfield/README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ npm install @material/textfield
3333

3434
```html
3535
<label class="mdc-text-field">
36-
<input type="text" id="my-text-field" class="mdc-text-field__input">
37-
<label class="mdc-floating-label" for="my-text-field">Hint text</label>
36+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
37+
<span class="mdc-floating-label" for="label-id">Hint text</span>
3838
<div class="mdc-line-ripple"></div>
3939
</label>
4040
```
@@ -82,11 +82,11 @@ included as part of the DOM structure of a full width text field.
8282

8383
```html
8484
<label class="mdc-text-field mdc-text-field--textarea">
85-
<textarea id="textarea" class="mdc-text-field__input" rows="8" cols="40"></textarea>
85+
<textarea class="mdc-text-field__input" aria-labelledby="label-id" rows="8" cols="40"></textarea>
8686
<div class="mdc-notched-outline">
8787
<div class="mdc-notched-outline__leading"></div>
8888
<div class="mdc-notched-outline__notch">
89-
<label for="textarea" class="mdc-floating-label">Textarea Label</label>
89+
<span id="label-id" class="mdc-floating-label">Textarea Label</span>
9090
</div>
9191
<div class="mdc-notched-outline__trailing"></div>
9292
</div>
@@ -97,11 +97,11 @@ included as part of the DOM structure of a full width text field.
9797

9898
```html
9999
<label class="mdc-text-field mdc-text-field--outlined">
100-
<input type="text" id="tf-outlined" class="mdc-text-field__input">
100+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
101101
<div class="mdc-notched-outline">
102102
<div class="mdc-notched-outline__leading"></div>
103103
<div class="mdc-notched-outline__notch">
104-
<label for="tf-outlined" class="mdc-floating-label">Your Name</label>
104+
<span for="label-id" class="mdc-floating-label">Your Name</span>
105105
</div>
106106
<div class="mdc-notched-outline__trailing"></div>
107107
</div>
@@ -118,8 +118,8 @@ To disable the text field, add the `disabled` attribute to the `<input>` element
118118

119119
```html
120120
<label class="mdc-text-field mdc-text-field--disabled">
121-
<input type="text" id="disabled-text-field" class="mdc-text-field__input" disabled>
122-
<label class="mdc-floating-label" for="disabled-text-field">Disabled text field</label>
121+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input" disabled>
122+
<span class="mdc-floating-label" id="label-id">Disabled text field</span>
123123
<div class="mdc-line-ripple"></div>
124124
</label>
125125
```
@@ -170,8 +170,8 @@ which is immediate sibling of `.mdc-text-field`. See [here](helper-text/) for mo
170170

171171
```html
172172
<label class="mdc-text-field">
173-
<input type="text" id="my-text-field" class="mdc-text-field__input">
174-
<label class="mdc-floating-label" for="my-text-field">My Label</label>
173+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
174+
<span class="mdc-floating-label" id="label-id">My Label</span>
175175
<div class="mdc-line-ripple"></div>
176176
</label>
177177
<div class="mdc-text-field-helper-line">
@@ -187,8 +187,8 @@ See [here](character-counter/) for more information on using character counter.
187187

188188
```html
189189
<label class="mdc-text-field">
190-
<input type="text" id="my-text-field" class="mdc-text-field__input" maxlength="10">
191-
<label class="mdc-floating-label" for="my-text-field">My Label</label>
190+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input" maxlength="10">
191+
<span class="mdc-floating-label" id="label-id">My Label</span>
192192
<div class="mdc-line-ripple"></div>
193193
</label>
194194
<div class="mdc-text-field-helper-line">
@@ -204,11 +204,11 @@ inside of text field component.
204204
```html
205205
<label class="mdc-text-field mdc-text-field--textarea">
206206
<div class="mdc-text-field-character-counter">0 / 140</div>
207-
<textarea id="textarea" class="mdc-text-field__input" rows="8" cols="40" maxlength="140"></textarea>
207+
<textarea class="mdc-text-field__input" aria-labelledby="label-id" rows="8" cols="40" maxlength="140"></textarea>
208208
<div class="mdc-notched-outline">
209209
<div class="mdc-notched-outline__leading"></div>
210210
<div class="mdc-notched-outline__notch">
211-
<label for="textarea" class="mdc-floating-label">Textarea Label</label>
211+
<span id="label-id" class="mdc-floating-label">Textarea Label</span>
212212
</div>
213213
<div class="mdc-notched-outline__trailing"></div>
214214
</div>
@@ -230,8 +230,8 @@ by HTML5's form validation API.
230230

231231
```html
232232
<label class="mdc-text-field">
233-
<input type="password" id="pw" class="mdc-text-field__input" required minlength=8>
234-
<label for="pw" class="mdc-floating-label">Password</label>
233+
<input type="password" aria-labelledby="label-id" class="mdc-text-field__input" required minlength=8>
234+
<span id="label-id" class="mdc-floating-label">Password</span>
235235
<div class="mdc-line-ripple"></div>
236236
</label>
237237
```
@@ -247,10 +247,10 @@ Un-styled Content (**FOUC**).
247247

248248
```html
249249
<label class="mdc-text-field">
250-
<input type="text" id="pre-filled" class="mdc-text-field__input" value="Pre-filled value">
251-
<label class="mdc-floating-label mdc-floating-label--float-above" for="pre-filled">
250+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input" value="Pre-filled value">
251+
<span class="mdc-floating-label mdc-floating-label--float-above" id="label-id">
252252
Label in correct place
253-
</label>
253+
</span>
254254
<div class="mdc-line-ripple"></div>
255255
</label>
256256
```

packages/mdc-textfield/helper-text/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ the input element.
5353

5454
```html
5555
<label class="mdc-text-field">
56-
<input type="text" id="username" class="mdc-text-field__input"
56+
<input type="text" class="mdc-text-field__input"
57+
aria-labelledby="username-label"
5758
aria-controls="username-helper-text"
5859
aria-describedby="username-helper-text">
59-
<label for="username" class="mdc-floating-label">Username</label>
60+
<span id="username-label" class="mdc-floating-label">Username</span>
6061
<div class="mdc-line-ripple"></div>
6162
</label>
6263
<div class="mdc-text-field-helper-line">

packages/mdc-textfield/icon/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ In text field:
6666
```html
6767
<label class="mdc-text-field mdc-text-field--with-leading-icon">
6868
<i class="material-icons mdc-text-field__icon" tabindex="0" role="button">event</i>
69-
<input type="text" id="my-input" class="mdc-text-field__input">
70-
<label for="my-input" class="mdc-floating-label">Your Name</label>
69+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
70+
<span for="label-id" class="mdc-floating-label">Your Name</span>
7171
<div class="mdc-line-ripple"></div>
7272
</label>
7373
```
@@ -77,11 +77,11 @@ In outlined text field:
7777
```html
7878
<label class="mdc-text-field mdc-text-field--outlined mdc-text-field--with-leading-icon">
7979
<i class="material-icons mdc-text-field__icon" tabindex="0" role="button">event</i>
80-
<input type="text" id="my-input" class="mdc-text-field__input">
80+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
8181
<div class="mdc-notched-outline">
8282
<div class="mdc-notched-outline__leading"></div>
8383
<div class="mdc-notched-outline__notch">
84-
<label for="my-input" class="mdc-floating-label">Your Name</label>
84+
<span for="label-id" class="mdc-floating-label">Your Name</span>
8585
</div>
8686
<div class="mdc-notched-outline__trailing"></div>
8787
</div>
@@ -94,8 +94,8 @@ In text field:
9494

9595
```html
9696
<label class="mdc-text-field mdc-text-field--with-trailing-icon">
97-
<input type="text" id="my-input" class="mdc-text-field__input">
98-
<label for="my-input" class="mdc-floating-label">Your Name</label>
97+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
98+
<span id="label-id" class="mdc-floating-label">Your Name</span>
9999
<i class="material-icons mdc-text-field__icon" tabindex="0" role="button">event</i>
100100
<div class="mdc-line-ripple"></div>
101101
</label>
@@ -105,12 +105,12 @@ In outlined text field:
105105

106106
```html
107107
<label class="mdc-text-field mdc-text-field--outlined mdc-text-field--with-trailing-icon">
108-
<input type="text" id="my-input" class="mdc-text-field__input">
108+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
109109
<i class="material-icons mdc-text-field__icon" tabindex="0" role="button">event</i>
110110
<div class="mdc-notched-outline">
111111
<div class="mdc-notched-outline__leading"></div>
112112
<div class="mdc-notched-outline__notch">
113-
<label for="my-input" class="mdc-floating-label">Your Name</label>
113+
<span id="label-id" class="mdc-floating-label">Your Name</span>
114114
</div>
115115
<div class="mdc-notched-outline__trailing"></div>
116116
</div>
@@ -124,8 +124,8 @@ In text field:
124124
```html
125125
<label class="mdc-text-field mdc-text-field--with-leading-icon mdc-text-field--with-trailing-icon">
126126
<i class="material-icons mdc-text-field__icon">phone</i>
127-
<input type="text" id="my-input" class="mdc-text-field__input">
128-
<label for="my-input" class="mdc-floating-label">Phone Number</label>
127+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
128+
<span id="label-id" class="mdc-floating-label">Phone Number</span>
129129
<i class="material-icons mdc-text-field__icon" tabindex="0" role="button">event</i>
130130
<div class="mdc-line-ripple"></div>
131131
</label>
@@ -136,12 +136,12 @@ In outlined text field:
136136
```html
137137
<label class="mdc-text-field mdc-text-field--outlined mdc-text-field--with-leading-icon mdc-text-field--with-trailing-icon">
138138
<i class="material-icons mdc-text-field__icon">phone</i>
139-
<input type="text" id="my-input" class="mdc-text-field__input">
139+
<input type="text" aria-labelledby="label-id" class="mdc-text-field__input">
140140
<i class="material-icons mdc-text-field__icon" tabindex="0" role="button">clear</i>
141141
<div class="mdc-notched-outline">
142142
<div class="mdc-notched-outline__leading"></div>
143143
<div class="mdc-notched-outline__notch">
144-
<label for="my-input" class="mdc-floating-label">Phone Number</label>
144+
<span id="label-id" class="mdc-floating-label">Phone Number</span>
145145
</div>
146146
<div class="mdc-notched-outline__trailing"></div>
147147
</div>

0 commit comments

Comments
 (0)