"use client"
import * as React from "react"
import { Field as FieldPrimitive } from "@base-ui/react/field"
import { cn } from "@/registry/lib/utils"
// @use-when a multi-line text field. With its own actions or a character count
// in a footer row inside the field's edge (a composer, a reply box, a post
// editor), `TextareaGroup` with `TextareaGroupControl` and `TextareaGroupFooter`,
// the count a `FieldCounter`. Not RichTextEditor, which formats the text (bold, italic, links).
function Textarea({ className, ...props }: React.ComponentProps<"textarea">) {
// Rendered through Base UI's field control, so inside a `Field` it takes the
// label, help and error links and the invalid state the same way Input does.
// Base UI ships no textarea of its own; the control renders one through
// `render`, and its props are typed for an input, hence the casts.
const controlProps = props as unknown as Omit<React.ComponentProps<typeof FieldPrimitive.Control>, "render" | "className">
return (
<FieldPrimitive.Control
{...controlProps}
render={(rendered) => <textarea {...(rendered as React.ComponentProps<"textarea">)} />}
data-slot="textarea"
className={cn(
"placeholder:text-subtle-foreground selection:bg-accent selection:text-accent-foreground border-border block w-full min-w-0 min-h-16 rounded-action border bg-input-bg bg-clip-padding backdrop-blur-sm px-3 py-2 text-field md:text-sm shadow-xs transition-[color,box-shadow,border-color] duration-small ease-small outline-none disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-70",
// Hover darkens the border one step; focus and invalid sort after it.
"hover:border-border-strong read-only:hover:border-border",
"focus-visible:border-border-focus",
"aria-invalid:border-destructive",
// Read-only: flat on the muted fill, full opacity, still focusable.
// Disabled: the same fill, dimmed, so it never reads as an empty field.
"[&[readonly]]:bg-muted [&[readonly]]:shadow-none disabled:bg-muted disabled:shadow-none",
className
)}
/>
)
}
/**
* TextareaGroup: a text field that carries its own controls in a footer row,
* under a full-bleed rule, inside the field's edge (a composer, a reply box,
* a post with its character count). The group IS the field: it owns the
* border, fill, radius, shadow and every state, read off the control inside
* it, so the control renders bare and the footer needs no fill of its own.
* Anything pinned over the text (a copy button) is the caller's composition:
* wrap the group in a `relative` box.
*/
function TextareaGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="textarea-group"
className={cn(
"border-border flex w-full min-w-0 flex-col rounded-action border bg-input-bg bg-clip-padding backdrop-blur-sm shadow-xs transition-[color,box-shadow,border-color] duration-small ease-small",
// Hover darkens the group's border one step, as on Textarea; a disabled
// or read-only control inside keeps it plain.
"hover:border-border-strong has-[[data-slot=textarea-group-control]:read-only]:hover:border-border",
"has-[[data-slot=textarea-group-control]:focus-visible]:border-border-focus",
"has-[[data-slot=textarea-group-control][aria-invalid=true]]:border-destructive",
// Read-only: flat on the muted fill, full opacity, still focusable.
// Disabled: the same fill, dimmed, so it never reads as an empty field.
"has-[[data-slot=textarea-group-control][readonly]]:bg-muted has-[[data-slot=textarea-group-control][readonly]]:shadow-none has-[[data-slot=textarea-group-control]:disabled]:bg-muted has-[[data-slot=textarea-group-control]:disabled]:shadow-none",
// Disabled dims the field's text only (the control, below), never the
// group: the footer holds buttons that keep their own enabled look.
className
)}
{...props}
/>
)
}
/** The text control inside a TextareaGroup. Bare: the group draws the field. */
function TextareaGroupControl({ className, ...props }: React.ComponentProps<"textarea">) {
return (
<textarea
data-slot="textarea-group-control"
className={cn(
"placeholder:text-subtle-foreground selection:bg-accent selection:text-accent-foreground block w-full min-w-0 min-h-16 resize-none bg-transparent px-3 py-2 text-field md:text-sm outline-none disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-70",
className
)}
{...props}
/>
)
}
/** The footer row: actions at the start, a count or status at the end. */
function TextareaGroupFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="textarea-group-footer"
className={cn("border-border flex min-h-12 items-center justify-between gap-2 border-t px-2 py-2", className)}
{...props}
/>
)
}
export { Textarea, TextareaGroup, TextareaGroupControl, TextareaGroupFooter }