Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Floating Input for Astro

Astro text fields with a label that moves when focused or filled.

Installation

bunx --bun shadcn@latest add @gnostic/floating-input

Usage

import { FloatingInput } from "@/components/ui/floating-input"
<FloatingInput id="email" name="email" type="email" label="Email" />

Set id. The label uses that id. Omit placeholder when the label is the only hint. The component then uses a space, and that placeholder stays invisible.

Size

md is the default. size="sm" makes the field shorter.

Description and error

description adds helper text. error adds a validation message and marks the control invalid.

This name is public.

Textarea

FloatingTextarea uses the same label. The empty label sits on the first line.

Input and label classes

Use the slot classes when you build the field from Input and Label. Place the label after the input. Keep helper text outside that wrapper. The empty label uses half the height of the wrapper.

---
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import {
  floatingInputRootClass,
  floatingInputVariants,
  floatingLabelVariants,
} from "@/components/ui/floating-input"
---

<div class={floatingInputRootClass}>
  <Input id="city" class={floatingInputVariants()} placeholder=" " />
  <Label class={floatingLabelVariants()} for="city">City</Label>
</div>

Notes

  • class styles the wrapper. Use it for width.
  • size sets the field height. It accepts sm and md.
  • The label follows the control so the empty, focus, and filled states work.
  • Pass error to show a message and set aria-invalid.
  • Pass description for helper text. The control references that text.

API Reference

Read the installed source for the complete prop types.

When to use Floating Input

Provide both id and label. FloatingInput composes an Input and a linked label, with optional description and error text. The placeholder supports the floating-label layout; it is not the accessible name.

Keyboard and behavior

The native input keeps normal keyboard and form behavior. Keep the visible label clear even after typing. Error text should explain the correction and remain associated with the field.

Theme and layout

Test browser autofill, long labels, and zoom with the selected size. The floating label sits over the field surface, so custom backgrounds must match the surrounding input design.