Back to Material Components Web

Text field helper text

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

14.0.04.5 KB
Original Source
<!--docs: title: "Text field helper text" layout: detail section: components excerpt: "The helper text provides supplemental information and/or validation messages to users" iconId: text_field path: /catalog/input-controls/text-field/helper-text/ -->

Text field helper text

Helper 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.

Basic usage

HTML structure

html
<div class="mdc-text-field-helper-text" aria-hidden="true">helper text</div>

NOTE: Place this inside .mdc-text-field-helper-line element which is an immediate sibling of .mdc-text-field.

Styles

scss
@use "@material/textfield/helper-text";

@include helper-text.helper-text-core-styles;

JavaScript instantiation

js
import {MDCTextFieldHelperText} from '@material/textfield/helper-text';

const helperText = new MDCTextFieldHelperText(document.querySelector('.mdc-text-field-helper-text'));

Accessibility

Note that in every example where the helper text is dependent on the state of the input element, we assign an id to the mdc-text-field-helper-text element and set that id to the value of the aria-controls and aria-describedby attributes on the input element. We recommend doing this as well as it will help indicate to assistive devices that the display of the helper text is dependent on the interaction with the input element.

html
<label class="mdc-text-field mdc-text-field--filled">
  <span class="mdc-text-field__ripple"></span>
  <span class="mdc-floating-label" id="my-label-id">Username</span>
  <input class="mdc-text-field__input" type="text"
         aria-labelledby="my-label-id"
         aria-controls="username-helper-text"
         aria-describedby="username-helper-text">
  <span class="mdc-line-ripple"></span>
</label>
<div class="mdc-text-field-helper-line">
  <div id="username-helper-text" class="mdc-text-field-helper-text" aria-hidden="true">
    This will be displayed on your public profile
  </div>
</div>

API

CSS classes

CSS ClassDescription
mdc-text-field-helper-textMandatory.
mdc-text-field-helper-text--persistentMakes the helper text permanently visible.
mdc-text-field-helper-text--validation-msgIndicates the helper text is a validation message.

Sass mixins

MixinDescription
helper-text-color($color)Customizes the color of the helper text following an enabled text-field.
disabled-helper-text-color($color)Customizes the color of the helper text following a disabled text-field.
helper-text-validation-color($color)Customizes the color of the helper text when it's used as a validation message.

MDCTextFieldHelperText properties and methods

PropertyValue TypeDescription
foundationMDCTextFieldHelperTextFoundationReturns the helper text's foundation. This allows the parent MDCTextField component to access the public methods on the MDCTextFieldHelperTextFoundation class.

Usage within frameworks

If 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.

MDCTextFieldHelperTextAdapter

Method SignatureDescription
addClass(className: string) => voidAdds a class to the helper text element.
removeClass(className: string) => voidRemoves a class from the helper text element.
hasClass(className: string) => booleanReturns true if classname exists for the helper text element.
setAttr(attr: string, value: string) => voidSets an attribute with a given value on the helper text element.
removeAttr(attr: string) => voidRemoves an attribute on the helper text element.
setContent(attr: string) => voidSets the text content for the helper text element.

MDCTextFieldHelperTextFoundation

Method SignatureDescription
setContent(content: string) => voidSets the content of the helper text.
setPersistent(isPersistent: boolean) => voidSets the helper text as persistent.
setValidation(isValidation: boolean) => voidSets the helper text as a validation message.
showToScreenReader() => voidMakes the helper text visible to the screen reader.
setValidity(inputIsValid: boolean) => voidSets the validity of the helper text based on the input validity.