Skip to main content
twblazor
Home
  • Get started
  • Tailwind with Blazor
  • Why twblazor?
  • Theme
  • Class Merge
    • Configuring localization
    • RTL Provider
  • Breadcrumb
  • Card
  • Carousel
  • Collapse
  • Context Menu
  • Drawer
  • Footer
  • Menu
  • Navbar
  • On This Page
  • Separator
  • Sidebar
  • Stepper
  • Tabs
  • Tree List
  • Container
  • Flex
  • Grid
  • Text
  • Link
  • Button
  • Form
    • AutocompleteNew
    • Checkbox
    • Color Picker
    • Date Picker
    • Date Range Picker
    • Datetime Picker
    • Datetime Range Picker
    • File Upload
    • Input GroupNew
    • NumericNew
    • Masked InputNew
    • Radio Button
    • Range SliderNew
    • RatingNew
    • Select
    • Slider
    • Switch
    • Tag InputNew
    • Text EditorNew
    • Textfield
    • Time Picker
    • Time Range Picker
  • Toggle
  • Toggle Group
  • Alert
  • Avatar
  • Badge
  • Chip
  • Empty
  • Icon
  • Popover
  • Progress
  • Skeleton
  • Spinner
  • Tooltip
  • Data Table
  • SpreadsheetNew
  • Column Filter Editor
  • Pagination
  • Pick List
  • Barcode
  • QR Code
  • Calendar
  • Table
  • Attachment
  • Overview
    • Bar
    • Bullet
    • Column
    • Dot Plot
    • Dumbbell
    • Icon
    • Lollipop
    • Radial Bar
    • Range
    • Word Cloud
    • Area
    • Bump
    • Bump Area
    • Candlestick
    • Gantt
    • Horizon
    • Line
    • Slope
    • Small Multiples
    • Stream Graph
    • Funnel
    • Gauge
    • Marimekko
    • Nightingale
    • Pie
    • Treemap
    • Waffle
    • Waterfall
    • Circular Treemap
    • Convex Treemap
    • Dendrogram
    • Sunburst
    • Barcode
    • Beeswarm
    • Box Plot
    • Density Plot
    • Histogram
    • Radial Histogram
    • Ridgeline
    • Strip Plot
    • Violin Plot
    • Categorical Scatter
    • Contour
    • Hexagonal Binning
    • Quadrant
    • Scatter
    • Heatmap
    • Matrix
    • Parallel Coordinates
    • Radar
    • Arc Diagram
    • Chord Diagram
    • Flowchart
    • Network Diagram
    • Sankey
    • Euler Diagram
    • Venn Diagram
  • Bubble
  • Marker
  • Message
  • Dialog Service
  • Toast Service
API DocumentationAI Agents & Skills
Home Get Started

Layout

TwContextMenu

TwContextMenu is a Blazor right-click context menu with icons, shortcut hints, check and radio items, submenus and full keyboard support.

Last updated 9 November 2026

Basic context menu

Put the area that is right-clicked in TwContextMenu and its items in MenuContent. The menu opens at the pointer instead of the browser's own menu and stays inside the window. It takes the same items as TwMenu: give a TwMenuItem a StartIcon and a Shortcut hint. Choosing an item runs its OnClick and closes the menu; Escape, a click elsewhere or a scroll close it too.
Right-click here

Nothing chosen yet.

RAZOR
<TwContextMenu Id="clipboard-context-menu" Class="flex h-40 items-center justify-center rounded-lg border-2 border-dashed border-gray-300 text-sm text-gray-600 dark:border-gray-600 dark:text-gray-300">
    <ChildContent>Right-click here</ChildContent>
    <MenuContent>
        <TwMenuItem Label="Cut" StartIcon="Icon.Scissors" Shortcut="Ctrl+X" OnClick="@(() => lastAction = "Cut")" />
        <TwMenuItem Label="Copy" StartIcon="Icon.Copy" Shortcut="Ctrl+C" OnClick="@(() => lastAction = "Copy")" />
        <TwMenuItem Label="Paste" StartIcon="Icon.Clipboard" Shortcut="Ctrl+V" OnClick="@(() => lastAction = "Paste")" />
        <TwMenuDivider />
        <TwMenuItem Label="Print" StartIcon="Icon.Printer" Shortcut="Ctrl+P" OnClick="@(() => lastAction = "Print")" />
    </MenuContent>
</TwContextMenu>
<p class="mt-3" role="status">@(lastAction is null ? "Nothing chosen yet." : $"You chose {lastAction}.")</p>
CSHARP
@code {
    private string? lastAction;
}

Submenus, disabled and destructive items

A TwSubmenu opens a further menu beside its item when it is hovered, clicked or reached with Arrow Right, and closes with Arrow Left or Escape. It opens towards the start in a right to left layout, where the two arrows swap. A Disabled item is skipped by the arrow keys and cannot be chosen. Set Destructive on an action that is hard to undo to mark it in a warning colour, and Inset to line an item without an icon up with those that have one.
Right-click for page actions
RAZOR
<TwContextMenu Id="browser-context-menu" Class="flex h-40 items-center justify-center rounded-lg border-2 border-dashed border-gray-300 text-sm text-gray-600 dark:border-gray-600 dark:text-gray-300">
    <ChildContent>Right-click for page actions</ChildContent>
    <MenuContent>
        <TwMenuItem Label="Back" Shortcut="Alt+←" Inset />
        <TwMenuItem Label="Forward" Shortcut="Alt+→" Inset Disabled />
        <TwMenuItem Label="Reload" Shortcut="Ctrl+R" Inset />
        <TwSubmenu Label="More tools" Inset>
            <TwMenuItem Label="Save page as…" Shortcut="Ctrl+S" />
            <TwMenuItem Label="Create shortcut…" />
            <TwMenuItem Label="Name window…" />
            <TwMenuDivider />
            <TwSubmenu Label="Developer tools">
                <TwMenuItem Label="Console" />
                <TwMenuItem Label="Network" />
            </TwSubmenu>
            <TwMenuDivider />
            <TwMenuItem Label="Delete" StartIcon="Icon.Trash" Destructive />
        </TwSubmenu>
    </MenuContent>
</TwContextMenu>

Tick and radio options

A TwMenuCheckboxItem is an option that is on or off; bind @bind-Checked. A TwMenuRadioGroup holds TwMenuRadioItem options of which one is chosen; bind its @bind-Value and give it a Label, which screen readers read as the group's name. Choosing either kind leaves the menu open, so several options can be changed in a row; set CloseOnClick to close it instead.
Right-click to change the view

Bookmarks shown, full URLs hidden, signed in as Pedro Duarte.

RAZOR
<TwContextMenu Id="options-context-menu" Class="flex h-40 items-center justify-center rounded-lg border-2 border-dashed border-gray-300 text-sm text-gray-600 dark:border-gray-600 dark:text-gray-300">
    <ChildContent>Right-click to change the view</ChildContent>
    <MenuContent>
        <TwMenuCheckboxItem Label="Show bookmarks" Shortcut="Ctrl+Shift+B" @bind-Checked="showBookmarks" />
        <TwMenuCheckboxItem Label="Show full URLs" @bind-Checked="showFullUrls" />
        <TwMenuDivider />
        <TwMenuRadioGroup Label="People" @bind-Value="person">
            <TwMenuRadioItem Value="@("Pedro Duarte")" Label="Pedro Duarte" />
            <TwMenuRadioItem Value="@("Colm Tuite")" Label="Colm Tuite" />
        </TwMenuRadioGroup>
    </MenuContent>
</TwContextMenu>
<p class="mt-3">Bookmarks @(showBookmarks ? "shown" : "hidden"), full URLs @(showFullUrls ? "shown" : "hidden"), signed in as @person.</p>
CSHARP
@code {
    private bool showBookmarks = true;
    private bool showFullUrls;
    private string? person = "Pedro Duarte";
}

Groups

Use TwMenuGroup to put items under a heading, which screen readers read as the group's name, and TwMenuDivider for a line between groups. The arrow keys pass over both.
Right-click for account actions
RAZOR
<TwContextMenu Id="groups-context-menu" Class="flex h-40 items-center justify-center rounded-lg border-2 border-dashed border-gray-300 text-sm text-gray-600 dark:border-gray-600 dark:text-gray-300">
    <ChildContent>Right-click for account actions</ChildContent>
    <MenuContent>
        <TwMenuGroup Label="My account">
            <TwMenuItem Label="Profile" StartIcon="Icon.Person" Shortcut="Ctrl+Shift+P" />
            <TwMenuItem Label="Settings" StartIcon="Icon.Gear" Shortcut="Ctrl+," />
        </TwMenuGroup>
        <TwMenuDivider />
        <TwMenuGroup Label="Team">
            <TwMenuItem Label="Invite people" StartIcon="Icon.Person_Plus" />
            <TwMenuItem Label="New team" StartIcon="Icon.People" />
        </TwMenuGroup>
        <TwMenuDivider />
        <TwMenuItem Label="Sign out" StartIcon="Icon.Box_Arrow_Right" Destructive />
    </MenuContent>
</TwContextMenu>

One menu for each item in a list

Wrap each item of a list or each row of a table in its own TwContextMenu to give it actions of its own. Keyboard users open the menu of the focused item with Shift+F10 or the Menu key; it opens below the item with focus on its first action, and Escape returns focus to the item.

3 files.

RAZOR
<ul class="divide-y divide-gray-200 rounded-lg border border-gray-200 dark:divide-gray-700 dark:border-gray-700">
    @foreach (var file in files)
    {
        <li>
            <TwContextMenu Id="@($"file-{file.Id}")" AriaLabel="@($"{file.Name} actions")">
                <ChildContent>
                    <button type="button" class="flex w-full items-center gap-3 px-4 py-3 text-start text-sm hover:bg-gray-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-purple-500 dark:hover:bg-white/5" @onclick="@(() => openedFile = file.Name)">
                        <TwIcon Icon="Icon.File_Earmark_Text" Class="text-gray-500 dark:text-gray-400" />
                        <span>@file.Name</span>
                        <span class="ms-auto text-xs text-gray-500 dark:text-gray-400">@file.Size</span>
                    </button>
                </ChildContent>
                <MenuContent>
                    <TwMenuItem Label="Open" StartIcon="Icon.Box_Arrow_Up_Right" OnClick="@(() => openedFile = file.Name)" />
                    <TwMenuItem Label="Download" StartIcon="Icon.Download" />
                    <TwSubmenu Label="Share" StartIcon="Icon.Share">
                        <TwMenuItem Label="Email" StartIcon="Icon.Envelope" />
                        <TwMenuItem Label="Copy link" StartIcon="Icon.Link_45Deg" />
                    </TwSubmenu>
                    <TwMenuDivider />
                    <TwMenuItem Label="Delete" StartIcon="Icon.Trash" Destructive OnClick="@(() => Delete(file))" />
                </MenuContent>
            </TwContextMenu>
        </li>
    }
</ul>
<p class="mt-3" role="status">@(openedFile is null ? $"{files.Count} files." : $"Opened {openedFile}.")</p>
CSHARP
@code {
    private sealed record FileEntry(int Id, string Name, string Size);
    
    private readonly List<FileEntry> files =
    [
        new(1, "report.pdf", "2.4 MB"),
        new(2, "budget.xlsx", "86 KB"),
        new(3, "notes.txt", "4 KB"),
    ];
    
    private string? openedFile;
    
    private void Delete(FileEntry file) => files.Remove(file);
}

An area that can take focus

When nothing inside the area can take focus, such as a canvas or an image, set Focusable to make the area a Tab stop of its own, and name it with an aria-label attribute. Screen readers are then told that Shift+F10 or the Menu key opens its menu. Set Disabled to turn the menu off and leave the browser's own menu in its place.
Tab here, then press Shift+F10Press Shift+F10 or the Menu key for more options.
RAZOR
<div class="flex flex-col gap-3">
    <TwContextMenu Id="canvas-context-menu" Focusable Disabled="@menuOff" aria-label="Drawing canvas" Class="flex h-40 items-center justify-center rounded-lg bg-gray-100 text-sm text-gray-600 dark:bg-gray-800 dark:text-gray-300">
        <ChildContent>Tab here, then press Shift+F10</ChildContent>
        <MenuContent>
            <TwMenuItem Label="Add shape" StartIcon="Icon.Square" />
            <TwMenuItem Label="Add text" StartIcon="Icon.Fonts" />
            <TwMenuDivider />
            <TwMenuItem Label="Clear canvas" StartIcon="Icon.Eraser" Destructive />
        </MenuContent>
    </TwContextMenu>
    <TwCheckbox Label="Turn the menu off" @bind-Value="menuOff" />
</div>
CSHARP
@code {
    private bool menuOff;
}

Keyboard navigation

Right-click the area to open the menu at the pointer. When the area can take focus, Shift+F10 or the Menu key opens it too, with focus on its first item. The menu follows the ARIA menu pattern: one item has focus at a time, and the arrow keys move between them.
Key What it does
Tab Moves focus to the area, when the area is a Tab stop.
Shift+ plus F10 or Menu On the focusable area: opens the menu with focus on its first item.
Arrow Down or Arrow Up In the menu: moves to the next or previous item, wrapping round at either end. Disabled items are skipped.
Home or End In the menu: moves to the first or last item.
A to Z In the menu: moves to the next item that starts with the typed letter.
Enter or Space In the menu: chooses the focused item. An action closes the menu and returns focus to where it was; a tick option flips and the menu stays open.
Escape Closes the menu and returns focus to where it was.
Tab Closes the menu and moves focus on.

Theme configuration

Styling is controlled by TwBlazorTheme.Components (a TwMenuTheme), configured via the AddTwBlazor() options callback described on the Get Started page. Every property is a set of Tailwind classes, so changes are picked up by your own Tailwind build.

Inherited from TwMenu - this component reuses that theme rather than defining its own.

Property Type Controls
Root string
Panel string
Item string
ItemDisabled string
ItemIcon string
CheckIcon string
CheckIconHidden string
Divider string
GroupLabel string
ItemShortcut string
ItemDestructive string
ItemInset string
SubmenuTrigger string
SubmenuIcon string
ContextArea string

Full reference: TwMenuTheme on the API docs.

On this page

  • Basic context menu
  • Submenus, disabled and destructive items
  • Tick and radio options
  • Groups
  • One menu for each item in a list
  • An area that can take focus
  • Keyboard navigation
  • Theme configuration
twblazor

The Tailwind CSS component library for Blazor. Accessible, themeable and MIT licensed.

Discord (opens in a new tab) GitHub(opens in a new tab)

Get started

  • Install guide
  • Theming
  • Class merging
  • Localization

Guides

  • Tailwind CSS with Blazor
  • Why twblazor?
  • Right to left
  • AI agents and skills

Resources

  • API documentation (opens in a new tab)
  • llms.txt (opens in a new tab)
  • MIT License (opens in a new tab)

Community

  • Contributing (opens in a new tab)
  • Code of conduct (opens in a new tab)
  • Report an issue (opens in a new tab)

twblazor is and always will be free and open source.

An unhandled error has occurred. Reload