UNPKG

4.62 kBMarkdownView Raw
1<!--docs:
2title: "Text field helper text"
3layout: detail
4section: components
5excerpt: "The helper text provides supplemental information and/or validation messages to users"
6iconId: text_field
7path: /catalog/input-controls/text-field/helper-text/
8-->
9
10# Text field helper text
11
12Helper text gives context about a field’s input, such as how the input will be used. It should be visible either persistently or only on focus.
13
14## Basic usage
15
16### HTML structure
17
18```html
19<div class="mdc-text-field-helper-text" aria-hidden="true">helper text</div>
20```
21
22> NOTE: Place this inside `.mdc-text-field-helper-line` element which is an immediate sibling of `.mdc-text-field`.
23
24### Styles
25
26```scss
27@use "@material/textfield/helper-text";
28
29@include helper-text.helper-text-core-styles;
30```
31
32### JavaScript instantiation
33
34```js
35import {MDCTextFieldHelperText} from '@material/textfield/helper-text';
36
37const helperText = new MDCTextFieldHelperText(document.querySelector('.mdc-text-field-helper-text'));
38```
39
40#### Accessibility
41
42Note that in every example where the helper text is dependent on the state of the input element, we
43assign an id to the `mdc-text-field-helper-text` element and set that id to the value of the
44`aria-controls` and `aria-describedby` attributes on the input element. We recommend doing this as well as it will help
45indicate to assistive devices that the display of the helper text is dependent on the interaction with
46the input element.
47
48```html
49<label class="mdc-text-field mdc-text-field--filled">
50 <span class="mdc-text-field__ripple"></span>
51 <span class="mdc-floating-label" id="my-label-id">Username</span>
52 <input class="mdc-text-field__input" type="text"
53 aria-labelledby="my-label-id"
54 aria-controls="username-helper-text"
55 aria-describedby="username-helper-text">
56 <span class="mdc-line-ripple"></span>
57</label>
58<div class="mdc-text-field-helper-line">
59 <div id="username-helper-text" class="mdc-text-field-helper-text" aria-hidden="true">
60 This will be displayed on your public profile
61 </div>
62</div>
63```
64
65## API
66
67### CSS classes
68
69CSS Class | Description
70--- | ---
71`mdc-text-field-helper-text` | Mandatory.
72`mdc-text-field-helper-text--persistent` | Makes the helper text permanently visible.
73`mdc-text-field-helper-text--validation-msg` | Indicates the helper text is a validation message.
74
75### Sass mixins
76
77Mixin | Description
78--- | ---
79`helper-text-color($color)` | Customizes the color of the helper text following an enabled text-field.
80`disabled-helper-text-color($color)` | Customizes the color of the helper text following a disabled text-field.
81`helper-text-validation-color($color)` | Customizes the color of the helper text when it's used as a validation message.
82
83## `MDCTextFieldHelperText` properties and methods
84
85Property | Value Type | Description
86--- | --- | ---
87`foundation` | `MDCTextFieldHelperTextFoundation` | Returns the helper text's foundation. This allows the parent `MDCTextField` component to access the public methods on the `MDCTextFieldHelperTextFoundation` class.
88
89## Usage within frameworks
90
91If you are using a JavaScript framework, such as React or Angular, you can create a Helper Text for your framework. Depending on your needs, you can use the _Simple Approach: Wrapping MDC Web Vanilla Components_, or the _Advanced Approach: Using Foundations and Adapters_. Please follow the instructions [here](../../../docs/integrating-into-frameworks.md).
92
93### `MDCTextFieldHelperTextAdapter`
94
95Method Signature | Description
96--- | ---
97`addClass(className: string) => void` | Adds a class to the helper text element.
98`removeClass(className: string) => void` | Removes a class from the helper text element.
99`hasClass(className: string) => boolean` | Returns true if classname exists for the helper text element.
100`setAttr(attr: string, value: string) => void` | Sets an attribute with a given value on the helper text element.
101`removeAttr(attr: string) => void` | Removes an attribute on the helper text element.
102`setContent(attr: string) => void` | Sets the text content for the helper text element.
103
104### `MDCTextFieldHelperTextFoundation`
105
106Method Signature | Description
107--- | ---
108`setContent(content: string) => void` | Sets the content of the helper text.
109`setPersistent(isPersistent: boolean) => void` | Sets the helper text as persistent.
110`setValidation(isValidation: boolean) => void` | Sets the helper text as a validation message.
111`showToScreenReader() => void` | Makes the helper text visible to the screen reader.
112`setValidity(inputIsValid: boolean) => void` | Sets the validity of the helper text based on the input validity.