Synertech Web/ui v0.57.5 Site Storybook

Documentation / Forms

MentionInput

Client@synertech/uiOpen in Storybook

A text field where typing @ opens a list of suggestions: the chosen person is inserted as a non-editable Chip, removed with its cross or with Backspace. Up / down arrows move through the list, Enter or Tab confirm, Escape closes it.

The component does not filter the suggestions itself: it shows the ones it is given and reports what is typed through onMentionSearch(query); the application does the filtering (locally or with a request). onChange returns a MentionInputValue: the plain text, line breaks included, and the list of mentions with their position. The field shows the value it is given: at mount, to reopen an existing message, and whenever the application changes it ({ text: "", mentions: [] } empties it). The one it has just returned through onChange comes back identical, and it leaves it alone.

Client component ("use client"), built on a contentEditable exposed with role="textbox" and named by its label.

Examples

Basic usage

Type @ then a few letters: onMentionSearch filters the list passed in suggestions.

Open in a tab
import { useState } from "react";
import { MentionInput, type MentionSuggestion } from "@synertech/ui";

const team: MentionSuggestion[] = [
  { id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
  { id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
  { id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
  { id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];

function filterTeam(query: string) {
  const q = query.toLowerCase();
  return team.filter((member) => member.label.includes(q));
}

export function Basic() {
  const [suggestions, setSuggestions] = useState(team);

  return (
    <MentionInput
      label="Message"
      placeholder="Écrivez @ pour mentionner un collègue…"
      suggestions={suggestions}
      onMentionSearch={(query) => setSuggestions(filterTeam(query))}
    />
  );
}

Reading the text and the mentions

onChange returns the plain text and each mention with its position in that text.

Open in a tab
import { useState } from "react";
import { MentionInput, type MentionInputValue, type MentionSuggestion } from "@synertech/ui";

const team: MentionSuggestion[] = [
  { id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
  { id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
  { id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
  { id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];

const emptyValue: MentionInputValue = { text: "", mentions: [] };

function filterTeam(query: string) {
  const q = query.toLowerCase();
  return team.filter((member) => member.label.includes(q));
}

export function ReadValue() {
  const [suggestions, setSuggestions] = useState(team);
  const [value, setValue] = useState<MentionInputValue>(emptyValue);

  return (
    <div className="flex flex-col gap-3">
      <MentionInput
        label="Note interne"
        suggestions={suggestions}
        onMentionSearch={(query) => setSuggestions(filterTeam(query))}
        value={value}
        onChange={setValue}
      />
      <p className="text-sm text-gray-600 dark:text-gray-300">
        Personnes mentionnées :{" "}
        {value.mentions.map((m) => m.suggestion.label).join(", ") || "aucune"}
      </p>
    </div>
  );
}

Editing an existing message

The field shows the value it is given, mentions and line breaks included: passing it back as onChange returned it is enough to reopen a message.

Open in a tab
import { useState } from "react";
import { MentionInput, type MentionInputValue, type MentionSuggestion } from "@synertech/ui";

const team: MentionSuggestion[] = [
  { id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
  { id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
  { id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
  { id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];

function filterTeam(query: string) {
  const q = query.toLowerCase();
  return team.filter((member) => member.label.includes(q));
}

export function EditExisting() {
  const [suggestions, setSuggestions] = useState(team);
  const [value, setValue] = useState<MentionInputValue>({
    text: "Merci @lea.dubois pour la correction.\nOn la livre demain ?",
    mentions: [{ trigger: "@", suggestion: team[2], startIndex: 6, endIndex: 17 }],
  });

  return (
    <MentionInput
      label="Modifier le message"
      suggestions={suggestions}
      onMentionSearch={(query) => setSuggestions(filterTeam(query))}
      value={value}
      onChange={setValue}
    />
  );
}

Custom suggestions

renderSuggestion draws each row of the list, from the free-form data field.

Open in a tab
import { useState } from "react";
import { MentionInput, type MentionSuggestion } from "@synertech/ui";

const team: MentionSuggestion[] = [
  { id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
  { id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
  { id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
  { id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];

function filterTeam(query: string) {
  const q = query.toLowerCase();
  return team.filter((member) => member.label.includes(q));
}

export function CustomSuggestion() {
  const [suggestions, setSuggestions] = useState(team);

  return (
    <MentionInput
      label="Assigner la tâche"
      suggestions={suggestions}
      onMentionSearch={(query) => setSuggestions(filterTeam(query))}
      maxSuggestions={3}
      chipColor="success"
      renderSuggestion={(s) => (
        <div className="flex flex-col px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700">
          <span className="text-sm font-medium text-gray-900 dark:text-white">
            {String(s.data?.name)}
          </span>
          <span className="text-xs text-gray-500 dark:text-gray-400">{String(s.data?.role)}</span>
        </div>
      )}
    />
  );
}

Comment with sending

A real case: the mentions are notified on send, then the field is cleared by passing an empty value again.

Open in a tab
import { useState } from "react";
import { Button, MentionInput, type MentionInputValue, type MentionSuggestion } from "@synertech/ui";

const team: MentionSuggestion[] = [
  { id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
  { id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
  { id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
  { id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];

const emptyValue: MentionInputValue = { text: "", mentions: [] };

function filterTeam(query: string) {
  const q = query.toLowerCase();
  return team.filter((member) => member.label.includes(q));
}

export function CommentBox() {
  const [suggestions, setSuggestions] = useState(team);
  const [value, setValue] = useState<MentionInputValue>(emptyValue);

  const send = () => {
    const ids = value.mentions.map((m) => m.suggestion.id);
    console.log("Commentaire envoyé :", value.text, "— notifiés :", ids);
    setValue(emptyValue);
  };

  return (
    <div className="flex flex-col gap-3">
      <MentionInput
        label="Ajouter un commentaire"
        placeholder="Votre commentaire…"
        suggestions={suggestions}
        onMentionSearch={(query) => setSuggestions(filterTeam(query))}
        onMentionSelect={(member) => console.log("Mentionné :", member.label)}
        value={value}
        onChange={setValue}
        helperText="Les personnes mentionnées recevront une notification."
      />
      <Button onClick={send} disabled={value.text.trim() === ""} className="self-end">
        Publier
      </Button>
    </div>
  );
}

API reference

Components

MentionInput

ComponentClient
import { MentionInput } from "@synertech/ui";

Props — MentionInputProps

PropTypeDefaultDescription
borderColor string —
chipColor ChipColor "primary"
chipSize ChipSize "sm"
chipVariant ChipVariant "soft"
disabled boolean false
error boolean | string —

true marks the field invalid; a message also does, and is written under the field in place of helperText. The field is then aria-invalid and described by the message. false and "" are no error.

helperText string —

Written under the field; a message passed as error takes its place.

hideLabel boolean false

Keeps the label for screen readers only: it is still the field's name.

icon React.ReactNode —
iconPosition "left" | "right" "left"
inline boolean false
label string —
labelColor string —
loading boolean false
loadingLabel string —

Shown in the suggestion list while loading is set. Defaults to the mentionInputLoading label.

maxSuggestions number 10
mentionTrigger string "@"
onChange (value: MentionInputValue) => void —
onMentionRemove (mention: MentionSuggestion) => void —
onMentionSearch (query: string) => void —
onMentionSelect (mention: MentionSuggestion) => void —
placeholder string —
readOnly boolean false
renderSuggestion (suggestion: MentionSuggestion) => React.ReactNode —
size "sm" | "md" | "lg" "md"
suggestions MentionSuggestion[] []
textColor string —
value MentionInputValue —

+ 2 inherited native attributes from RefAttributes, Attributes (className, id, aria-*, event handlers…).

Types

MentionData

Interface
interface MentionData
FieldTypeDefaultDescription
endIndex• number —
startIndex• number —
suggestion• MentionSuggestion —
trigger• string —
FieldTypeDefaultDescription
mentions• MentionData[] —
text• string —
FieldTypeDefaultDescription
id• string —
label• string —
avatar string —
data Record<string, unknown> —