"use client"; // Composer.Textarea — the editor surface: a hand-rolled contenteditable over // the flat segment engine (useComposerEditor). Acts like a native textarea // with atomic mention chips: native typing/IME, form participation via a // hidden input mirror, and the same data-attribute contract as always // (data-composer-editor="", data-command-badge, data-mention-chip). No // styling crosses this boundary. // // The `placeholder` string is drawn by the consumer's CSS, not by this part: // it sets aria-placeholder and stamps data-placeholder on the editable while // the hint should show, and the consumer styles ::before from those. Rich or // animated hints are the consumer's own element, passed as children; rendered // children win, so the two never draw on top of each other. // // The editable div renders with no JSX children — the engine owns its DOM; // React never reconciles inside it. The inline white-space styles are load- // bearing, not cosmetic: without break-spaces, the browser corrupts // consecutive spaces to NBSP and the model drifts from what's on screen. import type { CSSProperties, HTMLAttributes, ReactNode } from "react"; import type { ChipData } from "../chip-markdown"; import type { PrimitiveProps } from "../internal/primitive-props"; import { useRenderElement } from "../internal/render/useRenderElement"; import type { ComposerSubmitOn } from "./keyboard"; import { useComposer, useComposerContextStore } from "./store"; import { useComposerEditor } from "./use-composer-editor"; export type ComposerTextareaState = { /** Present as data-disabled while the editor is non-editable. */ disabled: boolean; /** Present as data-filled while the editor has content. */ filled: boolean; /** Present as data-composing during an IME composition. */ composing: boolean; }; export type ComposerTextareaProps = Omit< PrimitiveProps<"div", ComposerTextareaState>, "children" > & { value?: string; onValueChange?: (text: string) => void; disabled?: boolean; autoFocus?: boolean; /** Native-textarea-style placeholder string. Sets aria-placeholder and stamps data-placeholder on the editable while it should show; the consumer's CSS draws it. With rendered children it only sets aria-placeholder. */ placeholder?: string; /** Which Enter chord sends the message; the other inserts a soft break. Consider pairing "shift-enter" with enterKeyHint="enter". */ submitOn?: ComposerSubmitOn; /** Logical-length cap: one per character, one per chip. IME input is capped at composition commit. */ maxLength?: number; /** Native form validation via the hidden input — blocks empty submits. */ required?: boolean; /** Mirrors the serialized text (chips as chip-markdown) into FormData. */ name?: string; spellCheck?: boolean; autoCapitalize?: string; enterKeyHint?: HTMLAttributes["enterKeyHint"]; inputMode?: HTMLAttributes["inputMode"]; dir?: string; "aria-label"?: string; "aria-labelledby"?: string; "aria-describedby"?: string; /** Custom renderer for committed chips in the editor. Defaults to a label-only Chip. */ renderChip?: (chip: ChipData) => ReactNode; /** A custom placeholder, rendered beside the editable inside the Textarea's relative wrapper. A function receives the editor state, to hide it while filled or composing. While it renders anything, data-placeholder stays off. */ children?: ReactNode | ((state: ComposerTextareaState) => ReactNode); }; // Focusable-but-invisible (not display:none — Chrome refuses to submit forms // whose invalid control can't be focused for the validation bubble). const visuallyHiddenStyle: CSSProperties = { position: "absolute", width: 1, height: 1, margin: -1, padding: 0, overflow: "hidden", clipPath: "inset(50%)", border: 0, }; const ignoreInput = () => {}; export const ComposerTextarea = ({ value, onValueChange, className, render, style, disabled = false, autoFocus = false, placeholder, submitOn, maxLength, required = false, name, spellCheck = false, autoCapitalize, enterKeyHint, inputMode, dir, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, // Interactive callbacks route to the editable element (not the wrapper), // native-textarea style: they run before the engine, and preventDefault in // onKeyDown/onPaste/onCopy/onCut overrides the engine's handling. onFocus, onBlur, onKeyDown, onKeyUp, onPaste, onCopy, onCut, renderChip, children, ...elementProps }: ComposerTextareaProps) => { const store = useComposerContextStore(); // Combobox wiring: expanded state and the highlighted option's id flow from // the store mirrors the command machinery already maintains. const commandsActive = useComposer((composer) => composer.commands.active); const activeOptionId = useComposer((composer) => composer.commands.activeOptionId); const { attachRoot, editableProps, chipPortals, hasContent, isComposing, serializedText } = useComposerEditor({ disabled, autoFocus, submitOn, maxLength, value, onValueChange, renderChip, onFocus, onBlur, onKeyDown, onKeyUp, onPaste, onCopy, onCut, }); // Hidden during composition too: the browser paints marked text before any // model commit, so hasContent alone would lag the preview. const state: ComposerTextareaState = { disabled, filled: hasContent, composing: isComposing }; const customPlaceholder = typeof children === "function" ? children(state) : children; const hasCustomPlaceholder = customPlaceholder != null && typeof customPlaceholder !== "boolean"; const showPlaceholder = !!placeholder && !hasCustomPlaceholder && !hasContent && !isComposing; const editorContent = ( // Relative wrapper so a custom placeholder overlay sizes to the editable, // not the outer slot (which may carry padding/scroll from the styled layer).
, so // these references are the ONLY field↔list association AT perceives. aria-autocomplete="list" aria-haspopup="listbox" aria-controls={commandsActive ? store.listboxId : undefined} aria-activedescendant={activeOptionId ?? undefined} data-composer-editor="" data-placeholder={showPlaceholder ? "" : undefined} spellCheck={spellCheck} autoCapitalize={autoCapitalize} enterKeyHint={enterKeyHint} inputMode={inputMode} dir={dir} style={{ whiteSpace: "break-spaces", overflowWrap: "break-word" }} {...editableProps} /> {customPlaceholder}
); return useRenderElement( "div", { className, render, style }, { state, props: [ { "data-composer-textarea": "", children: ( <> {editorContent} {chipPortals} {name != null && ( // Not readOnly: browsers skip constraint validation on readonly // inputs, so `required` would never block a form submit. store.controller.focus()} style={visuallyHiddenStyle} /> )} ), }, elementProps, ], }, ); };