1 |
|
2 | title: "Text field icon"
|
3 | layout: detail
|
4 | section: components
|
5 | excerpt: "Icons describe the type of input a text field requires"
|
6 | iconId: text_field
|
7 | path: /catalog/input-controls/text-field/icon/
|
8 | -->
|
9 |
|
10 | # Text field icon
|
11 |
|
12 | Icons describe the type of input a text field requires. They can also be interaction targets.
|
13 |
|
14 | ## Basic usage
|
15 |
|
16 | ### HTML structure
|
17 |
|
18 | ```html
|
19 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--leading" tabindex="0" role="button">event</i>
|
20 | ```
|
21 |
|
22 | #### Icon set
|
23 |
|
24 | We recommend using [Material Icons](https://material.io/tools/icons/) from Google Fonts:
|
25 |
|
26 | ```html
|
27 | <head>
|
28 | <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
|
29 | </head>
|
30 | ```
|
31 |
|
32 | However, you can also use SVG, [Font Awesome](https://fontawesome.com/), or any other icon library you wish.
|
33 |
|
34 | ### Styles
|
35 |
|
36 | ```scss
|
37 | @use "@material/textfield/icon";
|
38 |
|
39 | @include icon.icon-core-styles;
|
40 | ```
|
41 |
|
42 | ### JavaScript instantiation
|
43 |
|
44 | ```js
|
45 | import {MDCTextFieldIcon} from '@material/textfield/icon';
|
46 |
|
47 | const icon = new MDCTextFieldIcon(document.querySelector('.mdc-text-field-icon'));
|
48 | ```
|
49 |
|
50 | ## Variants
|
51 |
|
52 | 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`.
|
53 |
|
54 | > **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.
|
55 |
|
56 | ### Leading icon
|
57 |
|
58 | In text field:
|
59 |
|
60 | ```html
|
61 | <label class="mdc-text-field mdc-text-field--filled mdc-text-field--with-leading-icon">
|
62 | <span class="mdc-text-field__ripple"></span>
|
63 | <span class="mdc-floating-label" id="my-label-id">Your Name</span>
|
64 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--leading" tabindex="0" role="button">event</i>
|
65 | <input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id">
|
66 | <span class="mdc-line-ripple"></span>
|
67 | </label>
|
68 | ```
|
69 |
|
70 | In outlined text field:
|
71 |
|
72 | ```html
|
73 | <label class="mdc-text-field mdc-text-field--outlined mdc-text-field--with-leading-icon">
|
74 | <span class="mdc-notched-outline">
|
75 | <span class="mdc-notched-outline__leading"></span>
|
76 | <span class="mdc-notched-outline__notch">
|
77 | <span class="mdc-floating-label" id="my-label-id">Your Name</span>
|
78 | </span>
|
79 | <span class="mdc-notched-outline__trailing"></span>
|
80 | </span>
|
81 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--leading" tabindex="0" role="button">event</i>
|
82 | <input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id">
|
83 | </label>
|
84 | ```
|
85 |
|
86 | ### Trailing icon
|
87 |
|
88 | In text field:
|
89 |
|
90 | ```html
|
91 | <label class="mdc-text-field mdc-text-field--filled mdc-text-field--with-trailing-icon">
|
92 | <span class="mdc-text-field__ripple"></span>
|
93 | <span class="mdc-floating-label" id="my-label-id">Your Name</span>
|
94 | <input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id">
|
95 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--trailing" tabindex="0" role="button">event</i>
|
96 | <span class="mdc-line-ripple"></span>
|
97 | </label>
|
98 | ```
|
99 |
|
100 | In outlined text field:
|
101 |
|
102 | ```html
|
103 | <label class="mdc-text-field mdc-text-field--outlined mdc-text-field--with-trailing-icon">
|
104 | <span class="mdc-notched-outline">
|
105 | <span class="mdc-notched-outline__leading"></span>
|
106 | <span class="mdc-notched-outline__notch">
|
107 | <span class="mdc-floating-label" id="my-label-id">Your Name</span>
|
108 | </span>
|
109 | <span class="mdc-notched-outline__trailing"></span>
|
110 | </span>
|
111 | <input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id">
|
112 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--trailing" tabindex="0" role="button">event</i>
|
113 | </label>
|
114 | ```
|
115 |
|
116 | ### Leading and trailing icons
|
117 |
|
118 | In text field:
|
119 |
|
120 | ```html
|
121 | <label class="mdc-text-field mdc-text-field--filled mdc-text-field--with-leading-icon mdc-text-field--with-trailing-icon">
|
122 | <span class="mdc-text-field__ripple"></span>
|
123 | <span class="mdc-floating-label" id="my-label-id">Phone Number</span>
|
124 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--leading">phone</i>
|
125 | <input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id">
|
126 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--trailing" tabindex="0" role="button">event</i>
|
127 | <div class="mdc-line-ripple"></div>
|
128 | </label>
|
129 | ```
|
130 |
|
131 | In outlined text field:
|
132 |
|
133 | ```html
|
134 | <label class="mdc-text-field mdc-text-field--outlined mdc-text-field--with-leading-icon mdc-text-field--with-trailing-icon">
|
135 | <span class="mdc-notched-outline">
|
136 | <span class="mdc-notched-outline__leading"></span>
|
137 | <span class="mdc-notched-outline__notch">
|
138 | <span class="mdc-floating-label" id="my-label-id">Phone Number</span>
|
139 | </span>
|
140 | <span class="mdc-notched-outline__trailing"></span>
|
141 | </span>
|
142 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--leading">phone</i>
|
143 | <input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id">
|
144 | <i class="material-icons mdc-text-field__icon mdc-text-field__icon--trailing" tabindex="0" role="button">clear</i>
|
145 | </label>
|
146 | ```
|
147 |
|
148 | ## API
|
149 |
|
150 | ### CSS classes
|
151 |
|
152 | CSS Class | Description
|
153 | --- | ---
|
154 | `mdc-text-field__icon` | Mandatory.
|
155 | `mdc-text-field__icon--leading` | Mandatory for leading icons.
|
156 | `mdc-text-field__icon--trailing` | Mandatory for trailing icons.
|
157 |
|
158 | ### Sass mixins
|
159 |
|
160 | Mixin | Description
|
161 | --- | ---
|
162 | `leading-icon-color($color)` | Customizes the color for the leading icon in an enabled text-field.
|
163 | `trailing-icon-color($color)` | Customizes the color for the trailing icon in an enabled text-field.
|
164 | `disabled-icon-color($color)` | Customizes the color for the leading/trailing icons in a disabled text-field.
|
165 | `size($size)` | Sets the size of the leading and trailing icons.
|
166 |
|
167 | ## `MDCTextFieldIcon` properties and methods
|
168 |
|
169 | Property | Value Type | Description
|
170 | --- | --- | ---
|
171 | `foundation` | `MDCTextFieldIconFoundation` | Returns the icon's foundation. This allows the parent `MDCTextField` component to access the public methods on the `MDCTextFieldIconFoundation` class.
|
172 |
|
173 | ## Usage within frameworks
|
174 |
|
175 | 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).
|
176 |
|
177 | ### `MDCTextFieldIconAdapter`
|
178 |
|
179 | Method Signature | Description
|
180 | --- | ---
|
181 | `getAttr(attr: string) => string` | Gets the value of an attribute on the icon element.
|
182 | `setAttr(attr: string, value: string) => void` | Sets an attribute with a given value on the icon element.
|
183 | `removeAttr(attr: string) => void` | Removes an attribute from the icon element.
|
184 | `setContent(content: string) => void` | Sets the text content of the icon element.
|
185 | `registerInteractionHandler(evtType: string, handler: EventListener) => void` | Registers an event listener for a given event.
|
186 | `deregisterInteractionHandler(evtType: string, handler: EventListener) => void` | Deregisters an event listener for a given event.
|
187 | `notifyIconAction() => void` | Emits a custom event "MDCTextField:icon" denoting a user has clicked the icon, which bubbles to the top-level text field element.
|
188 |
|
189 | ### `MDCTextFieldIconFoundation`
|
190 |
|
191 | Method Signature | Description
|
192 | --- | ---
|
193 | `setDisabled(disabled: boolean) => void` | Updates the icon's disabled state.
|
194 | `setAriaLabel(label: string) => void` | Updates the icon's aria-label.
|
195 | `setContent(content: string) => void` | Updates the icon's text content.
|
196 | `handleInteraction(evt: Event) => void` | Handles a text field interaction event.
|