Floating Input for Astro
Astro text fields with a label that moves when focused or filled.
Installation
bunx --bun shadcn@latest add @gnostic/floating-inputnpx shadcn@latest add @gnostic/floating-inputpnpm dlx shadcn@latest add @gnostic/floating-inputComplete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/floating-input/floating-field.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import { Label } from "../label/index.ts"
import {
floatingInputDescriptionId,
floatingInputErrorId,
floatingInputRootClass,
floatingLabelVariants,
type FloatingFieldProps,
type FloatingLabelVariantProps,
} from "./floating-input.ts"
type Props = FloatingFieldProps & {
class?: HTMLAttributes<"div">["class"]
/** `data-slot` value for this field. */
dataSlot?: string
/** Chooses the empty-state position of the label. */
control?: FloatingLabelVariantProps["control"]
}
const {
class: className,
label,
id,
description,
error,
size = "md",
control = "input",
dataSlot = "floating-input",
} = Astro.props
const descriptionId = description ? floatingInputDescriptionId(id) : undefined
const errorId = error ? floatingInputErrorId(id) : undefined
---
{/**
The control slot comes first.
The label follows it so the peer styles can see the control.
Both sit in one relative box. The empty label uses half of that box.
The description stays outside the box, so it does not pull the label down.
*/}
<div
data-slot={dataSlot}
data-size={size}
data-invalid={error ? "true" : undefined}
class={cn(floatingInputRootClass, className)}
>
<div class="relative">
<slot />
<Label class={floatingLabelVariants({ size, control })} for={id}>
{label}
</Label>
</div>
{descriptionId && (
<p
id={descriptionId}
data-slot="floating-input-description"
class="text-muted-foreground mt-1 px-2 text-sm font-normal"
>
{description}
</p>
)}
{errorId && (
<p
id={errorId}
role="alert"
data-slot="floating-input-error"
class="text-destructive absolute top-full right-2 left-2 mt-px text-sm"
>
{error}
</p>
)}
</div>
src/components/ui/floating-input/floating-input.astro
---
import type { HTMLAttributes } from "astro/types"
import { Input } from "../input/index.ts"
import FloatingField from "./floating-field.astro"
import {
floatingInputDescribedBy,
floatingInputVariants,
type FloatingFieldProps,
} from "./floating-input.ts"
type Props = Omit<HTMLAttributes<"input">, "size"> & FloatingFieldProps
const {
class: className,
label,
id,
description,
error,
size = "md",
// An empty placeholder keeps the label inside the control.
// The placeholder stays invisible.
placeholder = " ",
"aria-describedby": describedBy,
"aria-invalid": ariaInvalid,
...props
} = Astro.props
---
<FloatingField
class={className}
label={label}
id={id}
description={description}
error={error}
size={size}
control="input"
dataSlot="floating-input"
>
<Input
{...props}
id={id}
class={floatingInputVariants({ size })}
placeholder={placeholder}
aria-invalid={error ? "true" : ariaInvalid}
aria-describedby={floatingInputDescribedBy(id, {
description,
error,
describedBy,
})}
/>
</FloatingField>
src/components/ui/floating-input/floating-input.ts
import { cva, type VariantProps } from 'class-variance-authority'
/**
* Classes for a control with a floating label.
*
* The control and the label use `cva` because they have size variants.
* The root class is shared with callers that compose `Input` and `Label`.
* Description and error classes stay on the field component.
* The label must come after the control. The control uses `peer`.
*
* `py-0` clears the padding on `Input` and `Textarea`.
* The size classes use `!` so their padding wins over that padding.
*
* The empty label uses `top-1/2`. That value is half the height of the
* nearest relative parent. That parent must wrap only the control and
* the label. Helper text outside that parent does not move the label.
*/
/** Text that identifies the control, plus the optional help and error text. */
export interface FloatingFieldProps {
/** Text that floats above the control. */
label: string
/** Id of the control. The label uses this id. */
id: string
/** Helper text under the control. */
description?: string
/** Validation message. The field is invalid when this text is set. */
error?: string
/**
* Size of the input. `sm` has a height of 32px. `md` has a height of 40px.
*
* @default md
*/
size?: 'sm' | 'md'
/**
* The placeholder text is invisible, as the label is used in its place. It is set to an empty string to keep the label inside the control via the css pseudo-element `::placeholder`.
*
* @default " "
*/
placeholder?: string
}
const floatingControlClass = [
'peer block h-auto w-full min-w-0 appearance-none rounded-md border border-input bg-transparent px-2.5 py-0 text-sm text-foreground shadow-none',
'focus:border-primary focus:ring-0 focus:outline-none',
'focus-visible:border-primary focus-visible:ring-0',
'placeholder:opacity-0',
'aria-invalid:ring-0',
'dark:bg-transparent',
]
/** Classes for the single-line control. */
export const floatingInputVariants = cva(floatingControlClass, {
variants: {
size: {
sm: 'pt-3! pb-1.5!',
md: 'pt-4! pb-2.5!',
},
},
defaultVariants: {
size: 'md',
},
})
/**
* Classes for the multiline control.
* `field-sizing-fixed` keeps the `rows` height. The shared `Textarea`
* uses `field-sizing-content`, which would shrink the field to one line.
*/
export const floatingTextAreaVariants = cva(
[...floatingControlClass, 'field-sizing-fixed min-h-16 resize-y'],
{
variants: {
size: {
sm: 'pt-4! pb-1.5!',
md: 'pt-4! pb-2.5!',
},
},
defaultVariants: {
size: 'md',
},
},
)
/**
* Classes for the floating label.
*
* The resting position sits on the border.
* `placeholder-shown` moves the label into the empty control.
* `focus` is ordered after `placeholder-shown`, so a focused empty
* control keeps the label on the border.
*
* `translate` and `scale` are separate properties in Tailwind 4,
* so the transition lists those properties by name.
*/
export const floatingLabelVariants = cva(
[
'bg-background text-muted-foreground absolute inset-s-1 z-10 w-fit origin-left transform cursor-text px-2 text-sm leading-normal font-normal whitespace-nowrap transition-[top,translate,scale,color,background-color] duration-300 select-none',
'peer-focus:bg-background peer-focus:text-primary peer-focus:px-2',
'peer-placeholder-shown:bg-transparent',
'rtl:origin-right rtl:peer-focus:left-auto rtl:peer-focus:translate-x-1/4',
],
{
variants: {
size: {
sm: 'top-1 -translate-y-3 scale-75 peer-focus:top-1 peer-focus:-translate-y-3 peer-focus:scale-75',
md: 'top-2 -translate-y-4 scale-75 peer-focus:top-2 peer-focus:-translate-y-4 peer-focus:scale-75',
},
control: {
input: 'peer-placeholder-shown:top-1/2 peer-placeholder-shown:-translate-y-1/2 peer-placeholder-shown:scale-100',
textarea:
'peer-placeholder-shown:top-4 peer-placeholder-shown:translate-y-0 peer-placeholder-shown:scale-100',
},
},
defaultVariants: {
size: 'md',
control: 'input',
},
},
)
/**
* Classes for the field.
* The error uses this element as its position parent.
* Callers that compose `Input` and `Label` use this same class.
*/
export const floatingInputRootClass =
'relative w-full text-left data-invalid:mb-6'
/** Size and control choices for {@link floatingLabelVariants}. */
export type FloatingLabelVariantProps = VariantProps<
typeof floatingLabelVariants
>
/** Id of the helper text for `id`. */
export function floatingInputDescriptionId(id: string) {
return `${id}-description`
}
/** Id of the validation message for `id`. */
export function floatingInputErrorId(id: string) {
return `${id}-error`
}
/**
* Value for `aria-describedby` on the control.
* Includes the helper text, the validation message, and any extra id.
*/
export function floatingInputDescribedBy(
id: string,
options: {
description?: string | undefined | null
error?: string | undefined | null
describedBy?: string | undefined | null
},
) {
const ids = [
options.description ? floatingInputDescriptionId(id) : undefined,
options.error ? floatingInputErrorId(id) : undefined,
options.describedBy,
].filter((value): value is string => Boolean(value))
if (ids.length === 0) return undefined
return ids.join(' ')
}
src/components/ui/floating-input/floating-textarea.astro
---
import type { HTMLAttributes } from "astro/types"
import { Textarea } from "../textarea/index.ts"
import FloatingField from "./floating-field.astro"
import {
floatingInputDescribedBy,
floatingTextAreaVariants,
type FloatingFieldProps,
} from "./floating-input.ts"
type Props = HTMLAttributes<"textarea"> & FloatingFieldProps
const {
class: className,
label,
id,
description,
error,
size = "md",
// An empty placeholder keeps the label inside the control.
// The placeholder stays invisible.
placeholder = " ",
"aria-describedby": describedBy,
"aria-invalid": ariaInvalid,
...props
} = Astro.props
---
<FloatingField
class={className}
label={label}
id={id}
description={description}
error={error}
size={size}
control="textarea"
dataSlot="floating-textarea"
>
<Textarea
{...props}
id={id}
class={floatingTextAreaVariants({ size })}
placeholder={placeholder}
aria-invalid={error ? "true" : ariaInvalid}
aria-describedby={floatingInputDescribedBy(id, {
description,
error,
describedBy,
})}
/>
</FloatingField>
src/components/ui/floating-input/index.ts
export { default as FloatingInput } from './floating-input.astro'
export { default as FloatingTextarea } from './floating-textarea.astro'
export {
floatingInputRootClass,
floatingInputVariants,
floatingLabelVariants,
type FloatingFieldProps,
} from './floating-input.ts'
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.
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
classstyles the wrapper. Use it for width.sizesets the field height. It acceptssmandmd.- The label follows the control so the empty, focus, and filled states work.
- Pass
errorto show a message and setaria-invalid. - Pass
descriptionfor 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.