# @material/floating-label

> The Material Components for the web floating-label component

Latest version **14.0.0** (published 2022-04-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @material/floating-label
pnpm add @material/floating-label
yarn add @material/floating-label
bun add @material/floating-label
```

## Health

**Score 28/100 (F)** — status: abandoned.

Positive: has types package; esm support; no vulnerabilities; popular repo.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 14.0.0 |
| Published | 2022-04-28 |
| First published | 2018-03-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/material__floating-label) |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 215.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17051 |
| Maintainers | yefim, abhiomkar, azakus, bicknellr, material-admin, aprigogin, patrickrodee, aomarks, emarquez, allanchen, esgonzalez, asyncliz, candysonya, ssuarez, taylorv |
| Keywords | material components, material design, floatinglabel, floating label |

## Links

- npm: https://www.npmjs.com/package/@material/floating-label
- Repository: https://github.com/material-components/material-components-web
- Homepage: https://github.com/material-components/material-components-web#readme
- Issues: https://github.com/material-components/material-components-web/issues
- npm.io page: https://npm.io/package/@material/floating-label

## Dependencies (8)

- [tslib](https://npm.io/package/tslib.md) ^2.1.0
- [@material/dom](https://npm.io/package/@material/dom.md) ^14.0.0
- [@material/rtl](https://npm.io/package/@material/rtl.md) ^14.0.0
- [@material/base](https://npm.io/package/@material/base.md) ^14.0.0
- [@material/theme](https://npm.io/package/@material/theme.md) ^14.0.0
- [@material/animation](https://npm.io/package/@material/animation.md) ^14.0.0
- [@material/typography](https://npm.io/package/@material/typography.md) ^14.0.0
- [@material/feature-targeting](https://npm.io/package/@material/feature-targeting.md) ^14.0.0

## Recent versions

- 14.0.0 (latest) — 2022-04-28
- 15.0.0-canary.423edc3dc.0 (canary) — 2024-06-26
- 12.0.0-nightly.778a0e8a.0 (nightly) — 2021-07-20
- 4.0.0-alpha.0 (next) — 2019-08-07
- 15.0.0-canary.cfec83c74.0 — 2024-05-20
- 15.0.0-canary.2f5b899bc.0 — 2024-05-20
- 15.0.0-canary.f80ac92b0.0 — 2024-05-02
- 15.0.0-canary.4b35cb7d0.0 — 2024-05-02
- 15.0.0-canary.65c10a622.0 — 2024-04-25
- 15.0.0-canary.311f29a60.0 — 2024-04-23
- 15.0.0-canary.5bebc0064.0 — 2024-04-11
- 15.0.0-canary.68edc03c6.0 — 2024-04-08
- 15.0.0-canary.453a6248a.0 — 2024-03-25
- 15.0.0-canary.819498d8c.0 — 2024-03-15
- 15.0.0-canary.c43b3438b.0 — 2024-02-23
- … 1649 more at https://npm.io/package/@material/floating-label/versions

## README

<!--docs:
title: "Floating Label"
layout: detail
section: components
excerpt: "An animated text caption for a Text Field or Select."
path: /catalog/input-controls/floating-label/
-->

# Floating Label

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.

## Design & API Documentation

<ul class="icon-list">
  <li class="icon-list-item icon-list-item--spec">
    <a href="https://material.io/go/design-text-fields#text-fields-layout">Material Design guidelines: Text Fields Layout</a>
  </li>
  <li class="icon-list-item icon-list-item--spec">
    <a href="https://material-components.github.io/material-components-web-catalog/#/component/text-field">Demo with Text Field</a>
  </li>
</ul>

## Installation

```
npm install @material/floating-label
```

## Basic Usage

### HTML Structure

```html
<span class="mdc-floating-label" id="my-label-id">Hint text</span>
```

### Styles

```scss
@use "@material/floating-label/mdc-floating-label";
```

### JavaScript Instantiation

```js
import {MDCFloatingLabel} from '@material/floating-label';

const floatingLabel = new MDCFloatingLabel(document.querySelector('.mdc-floating-label'));
```

## Style Customization

### CSS Classes

CSS Class | Description
--- | ---
`mdc-floating-label` | Mandatory.
`mdc-floating-label--float-above` | Indicates the label is floating in the floating position.
`mdc-floating-label--shake` | Shakes the label.
`mdc-floating-label--required` | Indicates the label is required and adds an asterisk.

### Sass Mixins

Mixin | Description
--- | ---
`ink-color($color)` | Customizes the ink color of the label.
`fill-color($color)` | Customizes the fill color of the label.
`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.
`shake-animation($modifier)` | Applies shake keyframe animation to label.
`float-position($positionY, $positionX, $scale)` | Sets position of label when floating.
`max-width($max-width)` | Sets the max width of the label.
`float-transition($duration-ms, $timing-function)` | Customizes the duration and optional timing function for the "float" transition.

## `MDCFloatingLabel` Properties and Methods

Method Signature | Description
--- | ---
`shake(shouldShake: boolean) => void` | Proxies to the foundation's `shake()` method.
`float(shouldFloat: boolean) => void` | Proxies to the foundation's `float()` method.
`setRequired(isRequired: boolean) => void` | Proxies to the foundation's `setRequired()` method.
`getWidth() => number` | Proxies to the foundation's `getWidth()` method.

## Usage Within Frameworks

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

### `MDCFloatingLabelAdapter`

Method Signature | Description
--- | ---
`addClass(className: string) => void` | Adds a class to the label element.
`removeClass(className: string) => void` | Removes a class from the label element.
`getWidth() => number` | Returns the width of the label 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.

### `MDCFloatingLabelFoundation`

Method Signature | Description
--- | ---
`shake(shouldShake: boolean)` | Shakes or stops shaking the label, depending on the value of `shouldShake`.
`float(shouldFloat: boolean)` | Floats or docks the label, depending on the value of `shouldFloat`.
`setRequired(isRequired: boolean)` | Styles the label as required, depending on the value of `isRequired`.
`getWidth() => number` | Returns the width of the label element.

---
_Source: https://npm.io/package/@material/floating-label · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
