Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

input.tsx

73 lines3.1 KB

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" | "default" | "lg" | number;
10 unstyled?: boolean;
11 nativeInput?: boolean;
12};
13​
14function Input({
15 className,
16 size = "default",
17 unstyled = false,
18 nativeInput = false,
19 ...props
20}: InputProps) {
21 const inputClassName = cn(
22 "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]",
23 size === "sm" && "h-7.5 px-[calc(--spacing(2.5)-1px)] leading-7.5 sm:h-6.5 sm:leading-6.5",
24 size === "lg" && "h-9.5 leading-9.5 sm:h-8.5 sm:leading-8.5",
25 props.type === "search" &&
26 "[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none [&::-webkit-search-results-button]:appearance-none [&::-webkit-search-results-decoration]:appearance-none",
27 props.type === "file" &&
28 "text-muted-foreground file:me-3 file:bg-transparent file:font-medium file:text-foreground file:text-sm",
29 );
30 let inputElement: React.ReactElement;
31​
32 if (nativeInput) {
33 const { style, onValueChange: _onValueChange, ...nativeInputProps } = props;
34 const nativeStyle = typeof style === "function" ? undefined : style;
35​
36 inputElement = (
37 <input
38 className={inputClassName}
39 data-slot="input"
40 size={typeof size === "number" ? size : undefined}
41 style={nativeStyle}
42 {...(nativeInputProps as React.ComponentProps<"input">)}
43 />
44 );
45 } else {
46 inputElement = (
47 <InputPrimitive
48 className={inputClassName}
49 data-slot="input"
50 size={typeof size === "number" ? size : undefined}
51 {...props}
52 />
53 );
54 }
55​
56 return (
57 <span
58 className={
59 cn(
60 !unstyled &&
61 "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] 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%)]",
62 className,
63 ) || undefined
64 }
65 data-size={size}
66 data-slot="input-control"
67 >
68 {inputElement}
69 </span>
70 );
72​
73export { Input, type InputProps };