1 |
|
2 | title: "Floating Label"
|
3 | layout: detail
|
4 | section: components
|
5 | excerpt: "An animated text caption for a Text Field or Select."
|
6 | path: /catalog/input-controls/floating-label/
|
7 | -->
|
8 |
|
9 | # Floating Label
|
10 |
|
11 | Floating labels display the type of input a field requires. Every Text Field and Select should have a label, except for full-width text fields, which use the input's `placeholder` attribute instead. Labels are aligned with the input line and always visible. They can be resting (when a field is inactive and empty) or floating. The label is a text caption or description for the Text Field.
|
12 |
|
13 | ## Design & API Documentation
|
14 |
|
15 | <ul class="icon-list">
|
16 | <li class="icon-list-item icon-list-item--spec">
|
17 | <a href="https://material.io/go/design-text-fields#text-fields-layout">Material Design guidelines: Text Fields Layout</a>
|
18 | </li>
|
19 | <li class="icon-list-item icon-list-item--spec">
|
20 | <a href="https://material-components.github.io/material-components-web-catalog/#/component/text-field">Demo with Text Field</a>
|
21 | </li>
|
22 | </ul>
|
23 |
|
24 | ## Installation
|
25 |
|
26 | ```
|
27 | npm install @material/floating-label
|
28 | ```
|
29 |
|
30 | ## Basic Usage
|
31 |
|
32 | ### HTML Structure
|
33 |
|
34 | ```html
|
35 | <span class="mdc-floating-label" id="my-label-id">Hint text</span>
|
36 | ```
|
37 |
|
38 | ### Styles
|
39 |
|
40 | ```scss
|
41 | @use "@material/floating-label/mdc-floating-label";
|
42 | ```
|
43 |
|
44 | ### JavaScript Instantiation
|
45 |
|
46 | ```js
|
47 | import {MDCFloatingLabel} from '@material/floating-label';
|
48 |
|
49 | const floatingLabel = new MDCFloatingLabel(document.querySelector('.mdc-floating-label'));
|
50 | ```
|
51 |
|
52 | ## Style Customization
|
53 |
|
54 | ### CSS Classes
|
55 |
|
56 | CSS Class | Description
|
57 | --- | ---
|
58 | `mdc-floating-label` | Mandatory.
|
59 | `mdc-floating-label--float-above` | Indicates the label is floating in the floating position.
|
60 | `mdc-floating-label--shake` | Shakes the label.
|
61 | `mdc-floating-label--required` | Indicates the label is required and adds an asterisk.
|
62 |
|
63 | ### Sass Mixins
|
64 |
|
65 | Mixin | Description
|
66 | --- | ---
|
67 | `ink-color($color)` | Customizes the ink color of the label.
|
68 | `fill-color($color)` | Customizes the fill color of the label.
|
69 | `shake-keyframes($modifier, $positionY, $positionX, $scale)` | Generates a CSS `@keyframes` at-rule for an invalid label shake. Used in conjunction with the `shake-animation` mixin.
|
70 | `shake-animation($modifier)` | Applies shake keyframe animation to label.
|
71 | `float-position($positionY, $positionX, $scale)` | Sets position of label when floating.
|
72 | `max-width($max-width)` | Sets the max width of the label.
|
73 | `float-transition($duration-ms, $timing-function)` | Customizes the duration and optional timing function for the "float" transition.
|
74 |
|
75 | ## `MDCFloatingLabel` Properties and Methods
|
76 |
|
77 | Method Signature | Description
|
78 | --- | ---
|
79 | `shake(shouldShake: boolean) => void` | Proxies to the foundation's `shake()` method.
|
80 | `float(shouldFloat: boolean) => void` | Proxies to the foundation's `float()` method.
|
81 | `setRequired(isRequired: boolean) => void` | Proxies to the foundation's `setRequired()` method.
|
82 | `getWidth() => number` | Proxies to the foundation's `getWidth()` method.
|
83 |
|
84 | ## Usage Within Frameworks
|
85 |
|
86 | If you are using a JavaScript framework, such as React or Angular, you can create a Floating Label 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).
|
87 |
|
88 | ### `MDCFloatingLabelAdapter`
|
89 |
|
90 | Method Signature | Description
|
91 | --- | ---
|
92 | `addClass(className: string) => void` | Adds a class to the label element.
|
93 | `removeClass(className: string) => void` | Removes a class from the label element.
|
94 | `getWidth() => number` | Returns the width of the label element.
|
95 | `registerInteractionHandler(evtType: string, handler: EventListener) => void` | Registers an event listener for a given event.
|
96 | `deregisterInteractionHandler(evtType: string, handler: EventListener) => void` | Deregisters an event listener for a given event.
|
97 |
|
98 | ### `MDCFloatingLabelFoundation`
|
99 |
|
100 | Method Signature | Description
|
101 | --- | ---
|
102 | `shake(shouldShake: boolean)` | Shakes or stops shaking the label, depending on the value of `shouldShake`.
|
103 | `float(shouldFloat: boolean)` | Floats or docks the label, depending on the value of `shouldFloat`.
|
104 | `setRequired(isRequired: boolean)` | Styles the label as required, depending on the value of `isRequired`.
|
105 | `getWidth() => number` | Returns the width of the label element.
|