{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "mention-input",
  "title": "Mention Input",
  "type": "registry:ui",
  "files": [
    {
      "path": "packages/registry-native/components/mention-input/MentionInput.tsx",
      "content": "import * as React from 'react'\nimport { View, type TextInput } from 'react-native'\nimport { Input, type InputProps } from '@/components/ui/input'\nimport { cn } from '@/lib/utils'\nimport {\n  applyTextChange,\n  findQuery,\n  insertMention,\n  validateMentions,\n  type MentionItem,\n  type MentionQuery,\n  type MentionValue,\n  type TextSelection,\n} from './mention-utils'\n\nexport interface MentionSuggestionsProps {\n  query: MentionQuery\n  /** Replaces the query token with `trigger + label`, records the mention, and moves the caret after it. */\n  select: (item: MentionItem) => void\n}\n\nexport interface MentionInputProps extends Omit<\n  InputProps,\n  'value' | 'defaultValue' | 'onChangeText' | 'selection' | 'children'\n> {\n  value?: MentionValue\n  defaultValue?: MentionValue\n  onValueChange?: (value: MentionValue) => void\n  /** Single-character trigger(s). Default `'@'`. */\n  trigger?: string | readonly string[]\n  /** Fires once per distinct query, and with `null` when the query ends. */\n  onQueryChange?: (query: MentionQuery | null) => void\n  /** Rendered below the field while a query is active. Compose your own list, popover, or sheet. */\n  renderSuggestions?: (props: MentionSuggestionsProps) => React.ReactNode\n  disabled?: boolean\n  wrapperClassName?: string\n}\n\nconst EMPTY: MentionValue = { text: '', mentions: [] }\n\nconst MentionInput = React.forwardRef<React.ComponentRef<typeof TextInput>, MentionInputProps>(\n  (\n    {\n      value,\n      defaultValue,\n      onValueChange,\n      trigger = '@',\n      onQueryChange,\n      renderSuggestions,\n      disabled,\n      editable = true,\n      onSelectionChange,\n      wrapperClassName,\n      ...props\n    },\n    ref,\n  ) => {\n    const controlled = value !== undefined\n    const [inner, setInner] = React.useState<MentionValue>(() => {\n      const initial = defaultValue ?? EMPTY\n      return { text: initial.text, mentions: validateMentions(initial.text, initial.mentions) }\n    })\n    const current = React.useMemo(\n      () => (controlled ? { text: value.text, mentions: validateMentions(value.text, value.mentions) } : inner),\n      [controlled, value, inner],\n    )\n    const end = current.text.length\n    const [selection, setSelection] = React.useState<TextSelection>({ start: end, end })\n    // Caret to force after inserting a mention; cleared on the next native selection event.\n    const [pendingCaret, setPendingCaret] = React.useState<number | null>(null)\n    const enabled = editable && !disabled\n    const triggers = React.useMemo(() => (typeof trigger === 'string' ? [trigger] : trigger), [trigger])\n\n    const query = React.useMemo(\n      () => (enabled ? findQuery(current.text, selection, triggers, current.mentions) : null),\n      [enabled, current, selection, triggers],\n    )\n\n    const queryKey = query ? `${query.trigger}|${query.text}|${query.start}|${query.end}` : ''\n    const lastQueryKey = React.useRef('')\n    const onQueryChangeRef = React.useRef(onQueryChange)\n    onQueryChangeRef.current = onQueryChange\n    React.useEffect(() => {\n      if (queryKey === lastQueryKey.current) return\n      lastQueryKey.current = queryKey\n      onQueryChangeRef.current?.(query)\n    }, [queryKey, query])\n\n    const commit = (next: MentionValue, caret: number) => {\n      if (!controlled) setInner(next)\n      setSelection({ start: caret, end: caret })\n      onValueChange?.(next)\n    }\n\n    const select = (item: MentionItem) => {\n      if (!query || !enabled) return\n      const result = insertMention(current, query, item)\n      commit(result.value, result.caret)\n      setPendingCaret(result.caret)\n    }\n\n    // ponytail: the controlled `selection` prop is only sent while a caret is pending and within the\n    // shown text, so a refused controlled update can never push an out-of-range selection to Android.\n    const forcedSelection =\n      pendingCaret !== null && pendingCaret <= end ? { start: pendingCaret, end: pendingCaret } : undefined\n\n    return (\n      <View className={cn('gap-2', wrapperClassName)}>\n        <Input\n          ref={ref}\n          {...props}\n          editable={enabled}\n          value={current.text}\n          selection={forcedSelection}\n          onChangeText={(text) => {\n            if (!enabled) return\n            setPendingCaret(null)\n            // onChangeText vs onSelectionChange order differs by platform; the diff's own caret is\n            // correct for typing/paste, and a later selection event corrects anything else.\n            const result = applyTextChange(current, text, Math.min(selection.start, selection.end))\n            commit(result.value, result.caret)\n          }}\n          onSelectionChange={(event) => {\n            setPendingCaret(null)\n            setSelection(event.nativeEvent.selection)\n            onSelectionChange?.(event)\n          }}\n        />\n        {query && renderSuggestions ? renderSuggestions({ query, select }) : null}\n      </View>\n    )\n  },\n)\nMentionInput.displayName = 'MentionInput'\n\nexport { MentionInput }\nexport type { Mention, MentionItem, MentionQuery, MentionValue } from './mention-utils'\n",
      "type": "registry:ui",
      "target": "~/components/ui/mention-input/MentionInput.tsx"
    },
    {
      "path": "packages/registry-native/components/mention-input/mention-utils.ts",
      "content": "/** A committed mention: `text.slice(start, end) === trigger + label`. `end` is exclusive. */\nexport interface Mention {\n  start: number\n  end: number\n  id: string\n  label: string\n  trigger: string\n}\n\nexport interface MentionValue {\n  text: string\n  mentions: Mention[]\n}\n\n/**\n * The active mention query. `text` is what was typed between the trigger and the caret (use it to\n * filter); `start..end` is the whole token that `select` replaces, so picking a suggestion with the\n * caret mid-word replaces the full word, not just the part before the caret.\n */\nexport interface MentionQuery {\n  trigger: string\n  text: string\n  start: number\n  end: number\n}\n\nexport interface MentionItem {\n  id: string\n  label: string\n}\n\nexport interface TextSelection {\n  start: number\n  end: number\n}\n\nconst isSpace = (char: string | undefined) => char !== undefined && /\\s/.test(char)\n\n/**\n * A query is active only with a collapsed caret inside a run that starts with a trigger at the start\n * of the text or after whitespace, with no whitespace between trigger and caret. `a@b` never\n * triggers, and neither does a caret inside or at the end of a committed mention.\n */\nexport function findQuery(\n  text: string,\n  selection: TextSelection,\n  triggers: readonly string[],\n  mentions: readonly Mention[] = [],\n): MentionQuery | null {\n  if (selection.start !== selection.end) return null\n  const caret = Math.min(Math.max(selection.start, 0), text.length)\n  let start = caret\n  while (start > 0 && !isSpace(text[start - 1])) start--\n  if (start === caret || !triggers.includes(text[start]!)) return null\n  if (mentions.some((m) => (start >= m.start && start < m.end) || (caret > m.start && caret <= m.end))) return null\n  let end = caret\n  while (end < text.length && !isSpace(text[end])) end++\n  return { trigger: text[start]!, text: text.slice(start + 1, caret), start, end }\n}\n\nexport interface TextEdit {\n  /** First changed index (same in both strings). */\n  start: number\n  /** End of the removed range in the previous text. */\n  removedEnd: number\n  /** End of the inserted range in the next text. */\n  insertedEnd: number\n}\n\n/**\n * Common prefix/suffix diff. With repeated characters the edit position is ambiguous (typing `@`\n * before `@Ann` looks like an insertion after the first `@`), so `caretHint` — the caret before the\n * edit — caps the prefix to where the user was actually typing.\n */\nexport function diffText(prev: string, next: string, caretHint = Infinity): TextEdit {\n  const max = Math.min(prev.length, next.length)\n  let start = 0\n  while (start < max && start < caretHint && prev[start] === next[start]) start++\n  let suffix = 0\n  while (suffix < max - start && prev[prev.length - 1 - suffix] === next[next.length - 1 - suffix]) suffix++\n  return { start, removedEnd: prev.length - suffix, insertedEnd: next.length - suffix }\n}\n\n/**\n * Shifts mentions after an edit and drops any mention the edit touched. A partial edit breaks the\n * token: the label no longer names the entity, so keeping the id would lie. Pure insertions exactly at\n * a mention's start or end leave it intact.\n */\nexport function shiftMentions(mentions: readonly Mention[], edit: TextEdit): Mention[] {\n  const delta = edit.insertedEnd - edit.removedEnd\n  const pureInsert = edit.removedEnd === edit.start\n  return mentions.flatMap((m) => {\n    const touched = pureInsert\n      ? edit.start > m.start && edit.start < m.end\n      : edit.start < m.end && edit.removedEnd > m.start\n    if (touched) return []\n    return edit.removedEnd <= m.start ? [{ ...m, start: m.start + delta, end: m.end + delta }] : [m]\n  })\n}\n\n/** Applies a native text change to the value; returns the caret the edit implies. */\nexport function applyTextChange(\n  value: MentionValue,\n  nextText: string,\n  caretHint?: number,\n): { value: MentionValue; caret: number } {\n  const edit = diffText(value.text, nextText, caretHint)\n  return { value: { text: nextText, mentions: shiftMentions(value.mentions, edit) }, caret: edit.insertedEnd }\n}\n\n/** Replaces the query's token with `trigger + label` plus a trailing space and records the range. */\nexport function insertMention(\n  value: MentionValue,\n  query: MentionQuery,\n  item: MentionItem,\n): { value: MentionValue; caret: number } {\n  const token = query.trigger + item.label\n  const spacer = isSpace(value.text[query.end]) ? '' : ' '\n  const text = value.text.slice(0, query.start) + token + spacer + value.text.slice(query.end)\n  const edit = { start: query.start, removedEnd: query.end, insertedEnd: query.start + token.length + spacer.length }\n  const mention: Mention = { start: query.start, end: query.start + token.length, ...item, trigger: query.trigger }\n  const mentions = [...shiftMentions(value.mentions, edit), mention].sort((a, b) => a.start - b.start)\n  return { value: { text, mentions }, caret: mention.end + 1 }\n}\n\n/** Keeps only mentions whose range matches the text exactly and does not overlap an earlier one. */\nexport function validateMentions(text: string, mentions: readonly Mention[] = []): Mention[] {\n  let lastEnd = 0\n  return [...mentions]\n    .sort((a, b) => a.start - b.start)\n    .filter((m) => {\n      const ok =\n        Number.isInteger(m.start) &&\n        m.start >= lastEnd &&\n        m.end <= text.length &&\n        text.slice(m.start, m.end) === m.trigger + m.label &&\n        m.end > m.start\n      if (ok) lastEnd = m.end\n      return ok\n    })\n}\n",
      "type": "registry:ui",
      "target": "~/components/ui/mention-input/mention-utils.ts"
    },
    {
      "path": "packages/registry-native/components/mention-input/index.ts",
      "content": "export * from './MentionInput'\nexport * from './mention-utils'\n",
      "type": "registry:ui",
      "target": "~/components/ui/mention-input/index.ts"
    }
  ],
  "dependencies": [],
  "devDependencies": [],
  "registryDependencies": [
    "https://native.uipkge.dev/r/native/input.json",
    "https://native.uipkge.dev/r/native/utils.json"
  ],
  "description": "Text field with trigger detection (default @), caret-aware mention queries, range-tracked mention tokens that survive edits around them, and caller-composed suggestions.",
  "categories": [
    "control"
  ]
}