Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

Mentions

Since 0.1.0-alpha.24
Suggests people or tags as the user types @ or # in a text area, and writes the chosen name into the text.

A 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

Aisha Bello @aisha.bello
Daniel Kim @daniel.kim
Jordan Ellis @jordan.ellis
Mei Chen @mei.chen
Priya Nair @priya.nair
Tomás Ortega @tomas.ortega

Code

Usage.razor
@* 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>

@code {
    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:

bash
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.

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

aisha.bello
daniel.kim
jordan.ellis
mei.chen
priya.nair
tomas.ortega

Name at least two people.

aisha.bello
daniel.kim
jordan.ellis
mei.chen
priya.nair
tomas.ortega

Code

Form.razor
@* A nomination form: Mention.Parse counts the people named, and the form asks for two. *@
@using 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>

@code {
    [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

aisha.bello Aisha Bello
daniel.kim Daniel Kim
mei.chen Mei Chen
priya.nair Priya Nair
1042 Invoice PDF missing the tax line
1057 Login link expires too soon
1063 Export times out over 10,000 rows
1071 Dark mode chart labels too faint

Code

Triggers.razor
@* 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>

@code {
    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

What works in each mode, Mentions
FeatureStaticInteractive
The value is the plain text, posted with the formYesYes
Typing a trigger suggests names at the caret, narrowed as you typeYesYes
Search fills each trigger's suggestions when the page loadsYesYes
Each pause in typing asks Search for new suggestionsYesYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

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" and aria-controls naming the list; the highlighted suggestion is its aria-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

Mentions keys, In the text, with suggestions showing
Press this keyTo do this
Down or UpHighlight the next or previous suggestion
Enter or TabWrite the highlighted suggestion into the text
EscapeClose the suggestions, leaving the text as it is

Parameters

Mentions parameters
PropTypeDefault
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.MentionsPlacementMentionsPlacement.Bottom
DebounceHow long to wait after the last keystroke before searching.TimeSpanTimeSpan.FromMilliseconds(250)
RowsVisible lines.int4
AutoGrowGrows with the text instead of scrolling, from Rows lines up, where the browser supports CSS field-sizing.boolfalse
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.boolfalse
ReadOnlyMakes the field read-only: it can be focused and its text selected, and it still posts.boolfalse
@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?

See 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.

Connection lost

Rejoining the server…