Rich input

A run matching a pattern you declared becomes one object: the caret steps over it and Backspace takes the whole thing. The value underneath stays a plain string.

Ask NRNadia Rahman to review it

Installation

bunx --bun shadcn@latest add https://oss.flikk.dev/ui/r/rich-input.json

Usage

import { defineInputField, RichInput } from "@/components/ui/rich/editor"
const user = defineInputField("user", {
  pattern: /@([\w-]+)/,
  render: ({ groups }) => <span>@{groups[0]}</span>,
})

export function Compose() {
  const [value, setValue] = useState("Ask @nadia")
  return <RichInput components={[user]} value={value} onValueChange={setValue} />
}

Composition

RichInput
└── renderPicker
    └── RichPicker

That is the whole tree. A rich input is not a compound component: there is no Root, no Item, nothing to arrange. The one thing you nest inside it is a picker, and only when you want one.

What would be children elsewhere are values here. A token is an object you build with defineInputField and hand over in components, because the field matches patterns against a string long before anything renders, and an element cannot be matched against.

Examples

Async tokens

A mention names a lookup but carries no answer. Type @priya, move the caret away, and watch the value underneath rewrite itself into a form that needs no second lookup.

Ping amara about it
Ping @amara about it

No fields

With nothing declared it is a text field, and it is tested as one: value and caret against a native input after the same keystrokes, across a hundred generated scripts.

API reference

defineInputField(key, {
  pattern:   RegExp                      // a complete token
  render:    (match) => ReactNode        // or { draft, resolved }
  opens?:    RegExp                      // an unfinished one, anchored at the caret
  resolved?: RegExp                      // the settled form of an async token
  resolve?:  (match) => Promise<string | null>
  editable?: boolean                     // caret may re-enter it. default false
  className?: string                     // the chip shell
})

Only pattern and render are load bearing. The rest is a thing you may want, and the surface never branches on what a token means.

<RichInput
  components={fields}
  value | defaultValue
  onValueChange={(value: string) => void}
  renderPicker={({ query, mode, current, rect, start, end, replace, close }) => ReactNode}
  placeholder disabled className
  ref={{ value, selectionStart, selectionEnd, setSelectionRange, focus, undo, redo }}
/>

One line means one line: Enter does nothing, and a newline arriving by paste, macro or input method is dropped, the way an input drops it. The handle is input shaped on purpose, so a host already driving a text field by ref keeps doing that.

<RichPicker
  query        what is being typed
  search       (query) => items | Promise<items>
  items        or a fixed list
  searchable   render an input of its own
  rect side    bottom | top | left | right
  onPick close
/>

A convenience, not the interface. It owns the arrows, Tab, Enter, Escape and the care about a slow search landing after a faster one, so a host does not write that twice. Render your own list whenever the arrangement matters.