Docs · Hooks & Helpers

Hooks & Helpers

The developer-experience layer: a state hook, trigger preset factories, and segment helpers that remove boilerplate.

usePromptAreaState()

Manages the segment value, ref, and derived state. Call it once and spread bind onto the component.

const { bind, plainText, isEmpty, hasChips, chips, clear, focus } =
  usePromptAreaState()

<PromptArea {...bind} />
PropTypeDefaultDescription
bind{ ref, value, onChange }Spread onto <PromptArea {...bind} />.
plainTextstringDerived plain text of the current value.
isEmptybooleanTrue when empty or whitespace-only.
hasChipsbooleanTrue when the value contains a chip.
chipsChipSegment[]All chip segments in the value.
clear()() => voidClear all content.
focus() / blur()() => voidFocus or blur the input.
insertChip()(chip: Omit<ChipSegment, 'type'>) => voidInsert a chip imperatively.

useMarkdownMode()

Owns the markdown / plain-text variant as a named mode and returns a markdown boolean to spread onto the component, plus toggle. Switching is non-destructive — the text is kept, only its rendering changes. The a-large-small (ALargeSmall) icon is the conventional toggle control.

const { bind } = usePromptAreaState()
const { markdown, mode, toggle } = useMarkdownMode()

<PromptArea {...bind} markdown={markdown} />
<button onClick={toggle} aria-pressed={markdown}>
  <ALargeSmall className="size-4" />
</button>
PropTypeDefaultDescription
mode'markdown' | 'plain'The active variant.
markdownbooleanTrue in markdown mode — spread as the markdown prop.
isPlainTextbooleanTrue in plain-text mode (inverse of markdown).
toggle()() => voidFlip between markdown and plain text.
setMode(mode)(mode: 'markdown' | 'plain') => voidSwitch to an explicit mode.

Options: initialMode (default 'markdown'), mode for controlled use, and onModeChange. The pure oppositeMode(mode) helper is exported for custom toggles.

Trigger presets

Each returns a TriggerConfig for the triggers prop. Pass onSearch to supply items, plus optional styling.

PropTypeDefaultDescription
mentionTrigger(opts?)(opts) => TriggerConfig@ dropdown for users, agents, or documents.
commandTrigger(opts?)(opts) => TriggerConfig/ dropdown anywhere in the input. Pass position: "start" to limit it to the start of a line.
hashtagTrigger(opts?)(opts) => TriggerConfig# tags that auto-resolve on space.
callbackTrigger(opts)(opts) => TriggerConfigFire a callback (e.g. file picker) instead of a dropdown.

Segment helpers

Pure functions for building and reading segment arrays:

PropTypeDefaultDescription
text(value)(string) => TextSegmentCreate a text segment.
chip(opts)(Omit<ChipSegment,'type'>) => ChipSegmentCreate a chip segment.
getChips(segments)(Segment[]) => ChipSegment[]All chips in a segment array.
getChipsByTrigger(segments, trigger)(Segment[], string) => ChipSegment[]Chips for one trigger, e.g. '@' or '/'.
isSegmentsEmpty(segments)(Segment[]) => booleanTrue when empty or whitespace-only.
hasChips(segments)(Segment[]) => booleanTrue when at least one chip is present.
import { getChipsByTrigger } from '@/components/segment-helpers'

const mentions = getChipsByTrigger(bind.value, '@').map((c) => c.value)