Float labels
Five floating labels, each wrapping a real input: the label rests in the empty field and animates once the input is focused or has a value. The label is a real label element bound with htmlFor, so clicking it focuses the input, and colors resolve to ai2 tokens only.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/label-floatDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/label-float.tsx"use client"
import * as React from "react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Float label family: 5 labels that wrap a REAL input and move the label according
to focus/value state. The label rests in the empty field and rises when the input
is focused or a value is typed. The label is a real <label> bound to the input
with htmlFor (the id comes from React.useId()). Because the label sits over the
box it carries pointer-events-none; a click passes through to the input and focus
still happens.
The box height comes from its own float scale (a notch taller than the base
Button scale so the label has room to rise). The value and focus state live in
the root component, never inside a subtree that opens and closes. Color comes
ONLY from tokens, via alpha color-mix. The motion is disabled through
useReducedMotion(); spring transitions are written as type: "spring" as const. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
/* Box scale for the variants where the label sits outside. */
const outerBox: Record<StyledSize, string> = {
sm: "h-9",
md: "h-10",
lg: "h-11",
xl: "h-12",
}
/* A taller box for the variants where the label stays inside. */
const innerBox: Record<StyledSize, string> = {
sm: "h-11",
md: "h-12",
lg: "h-14",
xl: "h-16",
}
const text: Record<StyledSize, string> = {
sm: "text-xs",
md: "text-sm",
lg: "text-sm",
xl: "text-base",
}
const inputText: Record<StyledSize, string> = {
sm: "text-xs",
md: "text-sm",
lg: "text-sm",
xl: "text-base",
}
/* y offset for a label that rises above the box (from the centre). */
const aboveY: Record<StyledSize, number> = { sm: -26, md: -28, lg: -30, xl: -34 }
/* y offset for a label that sits on the border (half the box height). */
const borderY: Record<StyledSize, number> = { sm: -18, md: -20, lg: -22, xl: -24 }
/* y offset for a label that moves towards the top edge inside the box. */
const insideY: Record<StyledSize, number> = { sm: -10, md: -12, lg: -14, xl: -17 }
const inputBase =
"w-56 rounded-md border border-field-border bg-transparent px-3 text-foreground outline-none transition-colors placeholder:text-transparent focus-visible:ring-[3px] focus-visible:ring-ring/50"
const labelBase =
"pointer-events-none absolute inset-y-0 left-3 inline-flex w-fit items-center font-medium leading-none select-none"
const spring = { type: "spring" as const, stiffness: 420, damping: 32 }
export interface FloatProps {
className?: string
size?: StyledSize
children?: React.ReactNode
placeholder?: string
}
/* Focus and value state. active: the state in which the label should rise. */
function useFloatField() {
const id = React.useId()
const [value, setValue] = React.useState("")
const [focused, setFocused] = React.useState(false)
return {
id,
value,
setValue,
focused,
setFocused,
active: focused || value.length > 0,
}
}
/* Shared shell: the input plus an animated label over it. activeStyle carries each variant's motion signature. */
function FloatShell({
className,
size = "md",
children,
fallback,
boxClass,
inputClass,
labelClass,
activeStyle,
restStyle,
}: FloatProps & {
fallback: string
boxClass: string
inputClass?: string
labelClass?: string
activeStyle: { y: number; scale: number; x?: number }
restStyle?: { y: number; scale: number; x?: number }
}) {
const { id, value, setValue, focused, setFocused, active } = useFloatField()
const reduce = useReducedMotion()
const rest = restStyle ?? { y: 0, scale: 1, x: 0 }
return (
<span data-slot="styled-label" className={cn("inline-flex flex-col pt-3", className)}>
<span className="relative inline-flex w-fit">
<input
id={id}
type="text"
value={value}
placeholder={fallback}
onChange={(e) => setValue(e.target.value)}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
className={cn(inputBase, inputText[size], boxClass, inputClass)}
/>
<motion.label
htmlFor={id}
style={{ transformOrigin: "left center" }}
className={cn(
labelBase,
text[size],
active ? "text-info" : "text-muted-foreground",
labelClass
)}
animate={active ? activeStyle : rest}
transition={reduce ? { duration: 0 } : spring}
>
{children ?? fallback}
</motion.label>
</span>
</span>
)
}
/* Float: the label rises above the box, the classic float behavior. */
export function FloatLabel({ placeholder = "Email", ...props }: FloatProps) {
return (
<FloatShell
fallback={placeholder}
boxClass={outerBox[props.size ?? "md"]}
activeStyle={{ y: aboveY[props.size ?? "md"], scale: 0.85 }}
{...props}
/>
)
}
/* Shrink: the label stays INSIDE the box, moves towards the top edge and shrinks. */
export function ShrinkLabel({ placeholder = "Email", ...props }: FloatProps) {
const size = props.size ?? "md"
return (
<FloatShell
fallback={placeholder}
boxClass={innerBox[size]}
inputClass="pt-4"
activeStyle={{ y: insideY[size], scale: 0.75 }}
{...props}
/>
)
}
/* InlineFloat: the label stays inside the box but moves to the line above like an inline heading, without shrinking fully. */
export function InlineFloatLabel({ placeholder = "Email", ...props }: FloatProps) {
const size = props.size ?? "md"
return (
<FloatShell
fallback={placeholder}
boxClass={innerBox[size]}
inputClass="pt-4"
labelClass="uppercase tracking-[0.12em]"
activeStyle={{ y: insideY[size], scale: 0.85 }}
{...props}
/>
)
}
/* Overlap: the label rides on the top border; it carries a ground chip behind it so it looks like it cuts the border. */
export function OverlapLabel({ placeholder = "Email", ...props }: FloatProps) {
const size = props.size ?? "md"
return (
<FloatShell
fallback={placeholder}
boxClass={outerBox[size]}
labelClass="rounded bg-background px-1"
activeStyle={{ y: borderY[size], scale: 0.85 }}
{...props}
/>
)
}
/* Slide: etiket yukari cikarken sola da kayar, kutunun disinda hizalanir. */
export function SlideLabel({ placeholder = "Email", ...props }: FloatProps) {
const size = props.size ?? "md"
return (
<FloatShell
fallback={placeholder}
boxClass={outerBox[size]}
activeStyle={{ y: aboveY[size], scale: 0.9, x: -12 }}
restStyle={{ y: 0, scale: 1, x: 0 }}
{...props}
/>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Float
The classic float: the label rises above the box on focus or value.
import { FloatLabel } from "@/components/ui/label-float"
<FloatLabel>Email</FloatLabel>Shrink
The label stays inside the box and shrinks toward the top edge.
import { ShrinkLabel } from "@/components/ui/label-float"
<ShrinkLabel>Email</ShrinkLabel>Inline float
The label moves to an inline caption line inside the box.
import { InlineFloatLabel } from "@/components/ui/label-float"
<InlineFloatLabel>Email</InlineFloatLabel>Overlap
The label lands on the top border with a background chip behind it.
import { OverlapLabel } from "@/components/ui/label-float"
<OverlapLabel>Email</OverlapLabel>Slide
The label slides up and to the left as it leaves the box.
import { SlideLabel } from "@/components/ui/label-float"
<SlideLabel>Email</SlideLabel>ai2 Float labels: 5 styled variations on the token system
The ai2 Float labels are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around a label that floats out of the input on focus or value. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion springs the label between its resting and floated position. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the label jumps between the two positions with no transition.
What is in the ai2 Float labels?
5 exports in one file: Float, Shrink, Inline float, Overlap and Slide. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion springs the label between its resting and floated position.
- Reduced-motion aware: Under prefers-reduced-motion, the label jumps between the two positions with no transition.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Float labels sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.