Mentions
Since 0.1.0-alpha.24A text area where typing @ suggests names: choosing one writes "@name " into the text. The value is the plain text; Mention.Parse finds the names in it, and MentionText shows it with the names marked.
Mentioning teammates
Mentions binds to a string. Typing @ lists Items; choosing one writes @ and its ItemText into the text, then a space. ItemTemplate shows more in the list, such as a picture and a full name.
Static
Preview
Code
@* A task note that can mention teammates; the list shows each one's picture and team. *@
<Mentions @bind-Value="_note" Label="Note" Items="Team" ItemText="p => p.Handle"
Placeholder="Type @@ to mention someone" Rows="3" MaxWidth="32.Rem" Grow>
<ItemTemplate Context="person">
<Avatar Name="person.Name" Size="AvatarSize.Xs" Decorative />
<span>person.Name <span class="ib-option-hint">@@person.Handle</span></span>
</ItemTemplate>
</Mentions>
{
private string? _note = "Ready for review. @daniel.kim can you check the totals?";
private sealed record Person(string Name, string Handle);
private static readonly Person[] Team =
[
new("Aisha Bello", "aisha.bello"), new("Daniel Kim", "daniel.kim"),
new("Jordan Ellis", "jordan.ellis"), new("Mei Chen", "mei.chen"),
new("Priya Nair", "priya.nair"), new("Tomás Ortega", "tomas.ortega"),
];
}Add this example to a page with the izzy CLI:
izzy add component Mentions --at <file>:<line>A mention ends at a space, so ItemText should be a handle without spaces. Placement="MentionsPlacement.Top" opens the list above the line, for a box at the bottom of the page.
Searching a directory
For a list too long to send whole, set Search. It gets a MentionQuery, the trigger and what's typed after it, and returns the first matches. Each pause in typing asks again.
Static
Preview
Code
@* A company directory too long to send whole: Search returns the first matches for each pause in typing. *@
<Mentions TItem="Employee" @bind-Value="_message" Label="Message" Search="FindPeople"
ItemText="e => e.Handle" Placeholder="Type @@ and a name" Rows="3" MaxWidth="32.Rem" Grow>
<ItemTemplate Context="employee">
<Avatar Name="employee.Name" Size="AvatarSize.Xs" Decorative />
<span>employee.Name <span class="ib-option-hint">employee.Department</span></span>
</ItemTemplate>
</Mentions>
{
private string? _message;
// Called with "" when @ is typed, then for each pause in typing.
private static async Task<IEnumerable<Employee>> FindPeople(MentionQuery query, CancellationToken token)
{
await Task.Delay(query.Text.Length == 0 ? 0 : 300, token); // a database query
return Staff.Where(e => e.Name.Contains(query.Text, StringComparison.OrdinalIgnoreCase)
|| e.Handle.Contains(query.Text, StringComparison.OrdinalIgnoreCase))
.Take(8);
}
}In a form
Mention.Parse finds the names in the text, for a rule such as "name at least two people" or to notify them after saving. Rows="1" with AutoGrow gives a one-line field that grows.
Static
Preview
Code
@* A nomination form: Mention.Parse counts the people named, and the form asks for two. *@
System.ComponentModel.DataAnnotations
<Form Model="Model" FormName="mentions-form" OnValidSubmit="Send" MaxWidth="32.Rem" Grow>
<Mentions @bind-Value="Model!.Nominees" Items="Team" Rows="1" AutoGrow
Hint="Name at least two people." />
<Mentions @bind-Value="Model!.Reason" Items="Team" Rows="3" AutoGrow
Placeholder="What did they do? Type @@ to credit others." />
<FormActions>
<Button Submit>Nominate</Button>
</FormActions>
if (_sent is not null)
{
<Alert Status="Status.Success">Nominated _sent.</Alert>
}
</Form>
{
[SupplyParameterFromForm(FormName = "mentions-form")]
private Nomination? Model { get; set; }
private string? _sent;
protected override void OnInitialized() => Model ??= new();
private void Send() =>
_sent = string.Join(" and ", Mention.Parse(Model!.Nominees).Select(m => m.Text).Distinct());
public sealed class Nomination : IValidatableObject
{
[Required, Display(Name = "Nominees")]
public string? Nominees { get; set; }
[Required, StringLength(500), Display(Name = "Why")]
public string? Reason { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext context)
{
if (!string.IsNullOrWhiteSpace(Nominees) && Mention.Parse(Nominees).Select(m => m.Text).Distinct().Count() < 2)
{
yield return new("Name at least two people, each with @.", [nameof(Nominees)]);
}
}
}
}MentionText shows the saved text with its names marked or linked.
People and issues
Prefixes sets the triggers. Search gets the one typed, so @ can list people and # issues.
Static
Preview
Code
@* A support reply that mentions people with @ and links issues with #: Search gets the trigger and returns a list for each. *@
<Mentions TItem="Suggestion" @bind-Value="_reply" Label="Reply" Prefixes="Prefixes" Search="Find"
ItemText="s => s.Key" Placeholder="@@ to mention someone, # to link an issue" Rows="3" MaxWidth="32.Rem" Grow>
<ItemTemplate Context="s">
<span>s.Key <span class="ib-option-hint">s.Detail</span></span>
</ItemTemplate>
</Mentions>
{
private string? _reply;
private static readonly string[] Prefixes = ["@", "#"];
private static Task<IEnumerable<Suggestion>> Find(MentionQuery query, CancellationToken token)
{
var source = query.Prefix == "#" ? Issues : People;
return Task.FromResult(source.Where(s => s.Key.Contains(query.Text, StringComparison.OrdinalIgnoreCase)
|| s.Detail.Contains(query.Text, StringComparison.OrdinalIgnoreCase)));
}
}Details
Suggestions come from Items (narrowed in the browser, for every trigger) or from Search, called with the trigger and the text typed after it ("" when the trigger is first typed), so each trigger can have its own: people for @, tags for #. On interactive pages each pause in typing searches on the circuit. Static pages do too, with script: each pause fetches the page with the typed text in its address and takes this field's suggestions out of the response. That render sees the address, not the form's current values.
A mention starts at a trigger at the start of the text or after a space, and ends at the next space, so ItemText should be a handle without spaces ("ana.ruiz", not "Ana Ruiz"); show the full name in ItemTemplate.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| The value is the plain text, posted with the form | Yes | Yes |
| Typing a trigger suggests names at the caret, narrowed as you type | Yes | Yes |
| Search fills each trigger's suggestions when the page loads | Yes | Yes |
| Each pause in typing asks Search for new suggestions | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, it's a plain text area: names typed after @ post as text, with no suggestions.
Accessibility
- The text area keeps its own role (a multi-line text box, which is what it is). Once the script runs it has
aria-autocomplete="list"andaria-controlsnaming the list; the highlighted suggestion is itsaria-activedescendant. ARIA's combobox role is for one-line inputs and isn't allowed on a text area. - When the list opens, a status says how many suggestions there are; the first is highlighted, so Enter takes it, and Escape closes the list for Enter to start a new line.
- With JavaScript off it's a plain text area.
Keyboard navigation
In the text, with suggestions showing
| Press this key | To do this |
|---|---|
| Down or Up | Highlight the next or previous suggestion |
| Enter or Tab | Write the highlighted suggestion into the text |
| Escape | Close the suggestions, leaving the text as it is |
Parameters
| Prop | Type | Default |
|---|---|---|
ItemsSuggestions for every trigger, narrowed in the browser as the user types. | IEnumerable<TItem>? | |
SearchFinds suggestions for a trigger and the text typed after it, for lists too long to send whole or a list for each trigger. | Func<MentionQuery, CancellationToken, Task<IEnumerable<TItem>>>? | |
ItemTextWhat's written into the text for an item, after the trigger: a handle without spaces. | Func<TItem, string>? | computed |
ItemTemplateHow a suggestion looks in the list, such as an avatar beside a full name. | RenderFragment<TItem>? | computed |
ItemDisabledSuggestions shown but not chosen, such as someone without access. | Func<TItem, bool>? | |
PrefixesThe characters that start a mention, such as ["@", "#"]. | IReadOnlyList<string>? | @ |
SplitWritten after a chosen name, unless the text there already starts with it. | string | " " |
PlacementWhether the suggestions open below the caret's line (the default) or above it. | MentionsPlacement | MentionsPlacement.Bottom |
DebounceHow long to wait after the last keystroke before searching. | TimeSpan | TimeSpan.FromMilliseconds(250) |
RowsVisible lines. | int | 4 |
AutoGrowGrows with the text instead of scrolling, from Rows lines up, where the browser supports CSS field-sizing. | bool | false |
PlaceholderText in the empty field, such as "Type @ to mention someone". | string? | |
MaxLengthMaximum length. | int? | computed |
ClearableA button in the field that empties it, there while it holds text. | bool | false |
ReadOnlyMakes the field read-only: it can be focused and its text selected, and it still posts. | bool | false |
@bind-ValueThe value, bound with @bind-Value. | string? | |
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed. | string? |
Plus 28 field props.
Full APISee also
- Autocomplete: suggests as the user types, and takes any text.
- TextArea: lets the user enter several lines of text, and can grow and count them.
- MentionText: shows text written in a Mentions field, with each @name marked or linked.