# Text field icon Icons describe the type of input a text field requires. They can also be interaction targets. ## Basic usage ### HTML structure ```html event ``` #### Icon set We recommend using [Material Icons](https://material.io/tools/icons/) from Google Fonts: ```html
``` However, you can also use SVG, [Font Awesome](https://fontawesome.com/), or any other icon library you wish. ### Styles ```scss @use "@material/textfield/icon"; @include icon.icon-core-styles; ``` ### JavaScript instantiation ```js import {MDCTextFieldIcon} from '@material/textfield/icon'; const icon = new MDCTextFieldIcon(document.querySelector('.mdc-text-field-icon')); ``` ## Variants Leading and trailing icons can be applied to default or `mdc-text-field--outlined` Text Fields. To add an icon, add the relevant class (`mdc-text-field--with-leading-icon` and/or `mdc-text-field--with-trailing-icon`) to the root element, add an `i` element with your preferred icon, and give it a class of `mdc-text-field__icon` with the modifier `mdc-text-field__icon--leading` or `mdc-text-field__icon--trailing`. > **NOTE:** if you would like to display un-clickable icons, simply omit `tabindex="0"` and `role="button"`, and the CSS will ensure the cursor is set to default, and that interacting with an icon doesn't do anything unexpected. ### Leading icon In text field: ```html ``` In outlined text field: ```html ``` ### Trailing icon In text field: ```html ``` In outlined text field: ```html ``` ### Leading and trailing icons In text field: ```html ``` In outlined text field: ```html ``` ## API ### CSS classes CSS Class | Description --- | --- `mdc-text-field__icon` | Mandatory. `mdc-text-field__icon--leading` | Mandatory for leading icons. `mdc-text-field__icon--trailing` | Mandatory for trailing icons. ### Sass mixins Mixin | Description --- | --- `leading-icon-color($color)` | Customizes the color for the leading icon in an enabled text-field. `trailing-icon-color($color)` | Customizes the color for the trailing icon in an enabled text-field. `disabled-icon-color($color)` | Customizes the color for the leading/trailing icons in a disabled text-field. `size($size)` | Sets the size of the leading and trailing icons. ## `MDCTextFieldIcon` properties and methods Property | Value Type | Description --- | --- | --- `foundation` | `MDCTextFieldIconFoundation` | Returns the icon's foundation. This allows the parent `MDCTextField` component to access the public methods on the `MDCTextFieldIconFoundation` class. ## Usage within frameworks If you are using a JavaScript framework, such as React or Angular, you can create a Text Field Icon 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). ### `MDCTextFieldIconAdapter` Method Signature | Description --- | --- `getAttr(attr: string) => string` | Gets the value of an attribute on the icon element. `setAttr(attr: string, value: string) => void` | Sets an attribute with a given value on the icon element. `removeAttr(attr: string) => void` | Removes an attribute from the icon element. `setContent(content: string) => void` | Sets the text content of the icon element. `registerInteractionHandler(evtType: string, handler: EventListener) => void` | Registers an event listener for a given event. `deregisterInteractionHandler(evtType: string, handler: EventListener) => void` | Deregisters an event listener for a given event. `notifyIconAction() => void` | Emits a custom event "MDCTextField:icon" denoting a user has clicked the icon, which bubbles to the top-level text field element. ### `MDCTextFieldIconFoundation` Method Signature | Description --- | --- `setDisabled(disabled: boolean) => void` | Updates the icon's disabled state. `setAriaLabel(label: string) => void` | Updates the icon's aria-label. `setContent(content: string) => void` | Updates the icon's text content. `handleInteraction(evt: Event) => void` | Handles a text field interaction event.