shadcn-expansions
UI
Forms
Input

floating-label-input

Material UI design system floating label.

input
form
label
floating
animation
View Docs

Source Code

Files
floating-label-input.tsx
1import * as React from "react";
2
3import { Input } from "@/components/ui/input";
4import { Label } from "@/components/ui/label";
5import { cn } from "@/lib/utils";
6
7export interface InputProps
8  extends React.InputHTMLAttributes<HTMLInputElement> {}
9
10const FloatingInput = React.forwardRef<HTMLInputElement, InputProps>(
11  ({ className, ...props }, ref) => {
12    return (
13      <Input
14        placeholder=" "
15        className={cn("peer", className)}
16        ref={ref}
17        {...props}
18      />
19    );
20  },
21);
22FloatingInput.displayName = "FloatingInput";
23
24const FloatingLabel = React.forwardRef<
25  React.ElementRef<typeof Label>,
26  React.ComponentPropsWithoutRef<typeof Label>
27>(({ className, ...props }, ref) => {
28  return (
29    <Label
30      className={cn(
31        "peer-focus:secondary peer-focus:dark:secondary absolute start-2 top-2 z-10 origin-[0] -translate-y-4 scale-75 transform cursor-text bg-background px-2 text-sm text-gray-500 duration-300 peer-placeholder-shown:top-1/2 peer-placeholder-shown:-translate-y-1/2 peer-placeholder-shown:scale-100 peer-focus:top-2 peer-focus:-translate-y-4 peer-focus:scale-75 peer-focus:px-2 dark:bg-background rtl:peer-focus:left-auto rtl:peer-focus:translate-x-1/4",
32        className,
33      )}
34      ref={ref}
35      {...props}
36    />
37  );
38});
39FloatingLabel.displayName = "FloatingLabel";
40
41type FloatingLabelInputProps = InputProps & { label?: string };
42
43const FloatingLabelInput = React.forwardRef<
44  React.ElementRef<typeof FloatingInput>,
45  React.PropsWithoutRef<FloatingLabelInputProps>
46>(({ id, label, ...props }, ref) => {
47  return (
48    <div className="relative">
49      <FloatingInput ref={ref} id={id} {...props} />
50      <FloatingLabel htmlFor={id}>{label}</FloatingLabel>
51    </div>
52  );
53});
54FloatingLabelInput.displayName = "FloatingLabelInput";
55
56export { FloatingInput, FloatingLabel, FloatingLabelInput };
57