Open History to see the last commit
1"use client";
2
3import { Input as InputPrimitive } from "@base-ui/react/input";
4import type * as React from "react";
5
6import { cn } from "~/lib/utils";
7
8type InputProps = Omit<InputPrimitive.Props & React.RefAttributes<HTMLInputElement>, "size"> & {
9 size?: "sm" | "compact" | "default" | "lg" | number;
10 font?: "default" | "mono";
11 unstyled?: boolean;
12 nativeInput?: boolean;
13};
14
15function Input({
16 className,
17 size = "default",
18 font = "default",
19 unstyled = false,
20 nativeInput = false,
21 ...props
22}: InputProps) {
23 const inputClassName = cn(
24 "h-8.5 w-full min-w-0 rounded-[inherit] px-[calc(--spacing(3)-1px)] leading-8.5 outline-none placeholder:text-placeholder sm:h-7.5 sm:leading-7.5 [transition:background-color_5000000s_ease-in-out_0s]",
25 size === "compact" && "h-7 px-[calc(--spacing(2.5)-1px)] text-xs leading-7 sm:h-7 sm:leading-7",
26 size === "sm" && "h-7.5 px-[calc(--spacing(2.5)-1px)] leading-7.5 sm:h-6.5 sm:leading-6.5",
27 size === "lg" && "h-9.5 leading-9.5 sm:h-8.5 sm:leading-8.5",
28 props.type === "search" &&
29 "[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none [&::-webkit-search-results-button]:appearance-none [&::-webkit-search-results-decoration]:appearance-none",
30 props.type === "number" &&
31 "[appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none",
32 props.type === "file" &&
33 "text-muted-foreground file:me-3 file:bg-transparent file:font-medium file:text-foreground file:text-sm",
34 );
35 let inputElement: React.ReactElement;
36
37 if (nativeInput) {
38 const { style, onValueChange: _onValueChange, ...nativeInputProps } = props;
39 const nativeStyle = typeof style === "function" ? undefined : style;
40
41 inputElement = (
42 <input
43 className={inputClassName}
44 data-slot="input"
45 size={typeof size === "number" ? size : undefined}
46 style={nativeStyle}
47 {...(nativeInputProps as React.ComponentProps<"input">)}
48 />
49 );
50 } else {
51 inputElement = (
52 <InputPrimitive
53 className={inputClassName}
54 data-slot="input"
55 size={typeof size === "number" ? size : undefined}
56 {...props}
57 />
58 );
59 }
60
61 return (
62 <span
63 className={
64 cn(
65 !unstyled &&
66 "relative inline-flex w-full rounded-lg border border-input bg-background not-dark:bg-clip-padding text-base text-foreground shadow-xs/5 ring-ring/24 transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] not-has-disabled:not-has-focus-visible:not-has-aria-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] has-focus-visible:has-aria-invalid:border-destructive/64 has-focus-visible:has-aria-invalid:ring-destructive/16 has-aria-invalid:border-destructive/36 has-focus-visible:border-ring has-autofill:bg-foreground/4 has-disabled:opacity-64 has-[:disabled,:focus-visible,[aria-invalid]]:shadow-none has-focus-visible:ring-[3px] has-focus-visible:ring-inset sm:text-sm dark:bg-input/32 dark:has-autofill:bg-foreground/8 dark:has-aria-invalid:ring-destructive/24 dark:not-has-disabled:not-has-focus-visible:not-has-aria-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)]",
67 !unstyled &&
68 size === "compact" &&
69 "rounded-md before:rounded-[calc(var(--radius-md)-1px)]",
70 font === "mono" && "font-mono tabular-nums",
71 className,
72 ) || undefined
73 }
74 data-size={size}
75 data-slot="input-control"
76 >
77 {inputElement}
78 </span>
79 );
80}
81
82export { Input, type InputProps };