| 1 | "use client"; |
| 2 | |
| 3 | import { Input as InputPrimitive } from "@base-ui/react/input"; |
| 4 | import type * as React from "react"; |
| 5 | |
| 6 | import { cn } from "~/lib/utils"; |
| 7 | |
| 8 | type InputProps = Omit<InputPrimitive.Props & React.RefAttributes<HTMLInputElement>, "size"> & { |
| 9 | size?: "sm" | "compact" | "default" | "lg" | number; |
| 10 | /** Monospace with tabular digits, for paths, commands, colors and numbers. */ |
| 11 | font?: "default" | "mono"; |
| 12 | unstyled?: boolean; |
| 13 | nativeInput?: boolean; |
| 14 | }; |
| 15 | |
| 16 | function Input({ |
| 17 | className, |
| 18 | size = "default", |
| 19 | font = "default", |
| 20 | unstyled = false, |
| 21 | nativeInput = false, |
| 22 | ...props |
| 23 | }: InputProps) { |
| 24 | const inputClassName = cn( |
| 25 | "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]", |
| 26 | size === "compact" && "h-7 px-[calc(--spacing(2.5)-1px)] text-xs leading-7 sm:h-7 sm:leading-7", |
| 27 | size === "sm" && "h-7.5 px-[calc(--spacing(2.5)-1px)] leading-7.5 sm:h-6.5 sm:leading-6.5", |
| 28 | size === "lg" && "h-9.5 leading-9.5 sm:h-8.5 sm:leading-8.5", |
| 29 | props.type === "search" && |
| 30 | "[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none [&::-webkit-search-results-button]:appearance-none [&::-webkit-search-results-decoration]:appearance-none", |
| 31 | // Number fields are typed into; browser spin buttons crowd narrow fields and step by 1. |
| 32 | props.type === "number" && |
| 33 | "[appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none", |
| 34 | props.type === "file" && |
| 35 | "text-muted-foreground file:me-3 file:bg-transparent file:font-medium file:text-foreground file:text-sm", |
| 36 | ); |
| 37 | let inputElement: React.ReactElement; |
| 38 | |
| 39 | if (nativeInput) { |
| 40 | const { style, onValueChange: _onValueChange, ...nativeInputProps } = props; |
| 41 | const nativeStyle = typeof style === "function" ? undefined : style; |
| 42 | |
| 43 | inputElement = ( |
| 44 | <input |
| 45 | className={inputClassName} |
| 46 | data-slot="input" |
| 47 | size={typeof size === "number" ? size : undefined} |
| 48 | style={nativeStyle} |
| 49 | {...(nativeInputProps as React.ComponentProps<"input">)} |
| 50 | /> |
| 51 | ); |
| 52 | } else { |
| 53 | inputElement = ( |
| 54 | <InputPrimitive |
| 55 | className={inputClassName} |
| 56 | data-slot="input" |
| 57 | size={typeof size === "number" ? size : undefined} |
| 58 | {...props} |
| 59 | /> |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | return ( |
| 64 | <span |
| 65 | className={ |
| 66 | cn( |
| 67 | !unstyled && |
| 68 | "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%)]", |
| 69 | !unstyled && |
| 70 | size === "compact" && |
| 71 | "rounded-md before:rounded-[calc(var(--radius-md)-1px)]", |
| 72 | font === "mono" && "font-mono tabular-nums", |
| 73 | className, |
| 74 | ) || undefined |
| 75 | } |
| 76 | data-size={size} |
| 77 | data-slot="input-control" |
| 78 | > |
| 79 | {inputElement} |
| 80 | </span> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | export { Input, type InputProps }; |