import { composeRefs } from "@radix-ui/react-compose-refs"; import { cva, type VariantProps } from "class-variance-authority"; import { type ComponentProps, forwardRef, useRef } from "react"; import { cn } from "../../hooks"; const inputRootVariants = cva( ["flex", "gap-md", "items-center", "rounded-uk-sm", "overflow-hidden", "bg-bg-secondary"], { variants: { state: { default: ["hover:bg-bg-secondary/80", "has-[input:focus]:bg-bg-secondary"], disabled: ["disabled:cursor-not-allowed", "bg-bg-quaternary", "text-text-secondary"], error: ["hover:bg-danger-primary/10", "bg-danger-primary/20"], }, size: { sm: [ "h-6", "text-uk-sm", "rounded-[10px]", "[&_.input-slot>svg:not([class*='size-'])]:size-3", "px-sm", ], md: [ "h-8", "text-uk-md", "rounded-[11px]", "[&_.input-slot>svg:not([class*='size-'])]:size-4", "px-md", ], lg: [ "h-9", "text-uk-lg", "rounded-[12px]", "[&_.input-slot>svg:not([class*='size-'])]:size-5", "px-lg", ], }, }, defaultVariants: { state: "default", size: "md", }, }, ); export interface InputRootProps extends Omit, "size">, VariantProps { state?: "default" | "error"; side?: "left" | "right" | "center"; asChild?: boolean; inputClassName?: string; } /** * Stylized input root that provides consistent container and input styling. * Includes clickable slots on left/right to control caret position. * * @param state - Visual state ("default" | "error") * @param size - Control size ("sm" | "base" | "lg") * @param side - Align text for inputs ("left" | "right" | "center") * @param inputClassName - Additional classes applied to the internal input element */ export const InputRoot = forwardRef(function InputRoot( { style, className, side, children, disabled, state, size, type, inputClassName, ...props }, ref, ) { const inputRef = useRef(null); return (
{ const target = event.target as HTMLElement; if (target.closest("input, button, a")) return; const input = inputRef.current; if (!input) return; // Same selector as in the CSS to find the right slot const isRightSlot = target.closest(` .input-slot[data-side='right'], .input-slot:not([data-side='right']) ~ .input-slot:not([data-side='left']) `); const cursorPosition = isRightSlot ? input.value.length : 0; requestAnimationFrame(() => { // Only some input types support this, browsers will throw an error if not supported // See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange#:~:text=Note%20that%20according,not%20support%20selection%22. try { input.setSelectionRange(cursorPosition, cursorPosition); } catch { /* empty */ } input.focus(); }); }} > {children}
); }); export interface InputSlotProps extends ComponentProps<"div"> { side?: "left" | "right"; } /** * Decorative/interactive slot that can be placed on either side of the input. * Useful for icons, prefixes, or suffixes. * * @param side - Which side this slot is on ("left" | "right") */ export const InputSlot = forwardRef(function ( { className, children, side = "left", ...props }, ref, ) { return (
{children}
); }); /* note-ge: add this since a bunch of tools defaults to import Input. If you want to change this, look up the usage references first. */ export { InputRoot as Input };