# Blazor Menu Bar and Dropdown Menu

URL: https://flexcoreui.com/demo/menu-bar  
Section: Components  
Controls: MenuBarControl, MenuDropDownControl, MenuActionControl, MenuSeparatorControl  
Keywords: menu bar, dropdown menu, application menu, menu button

A Blazor menu bar: MenuBarControl keeps one dropdown open at a time; menus list entries and dividers, or work alone as a menu or split button.

## Documentation

### Bar

| Name | Type | Default | Description |
|---|---|---|---|
| ChildContent | RenderFragment? | null | The MenuDropDownControls, and any other markup such as links. |
| CssClass | string | "" | Appended after fx-menu-bar. |

### Drop down

| Name | Type | Default | Description |
|---|---|---|---|
| Label | string | "" | Text of the trigger, and of the main half in split mode. |
| ChildContent | RenderFragment? | null | The menu's entries, rendered only while it is open. |
| Disabled | bool | false | Disables the trigger; the menu cannot open. |
| SplitButton | bool | false | Draws two buttons: a main half and an arrow that opens the menu. |
| ArrowLabel | string? | null | Split mode: accessible name of the arrow. The default is the label followed by "menu". |
| TriggerContent | RenderFragment? | null | Your own markup for the trigger in place of Label, such as a label with a count; its text is the trigger's name. In split mode it replaces the arrow glyph. |
| PrimaryContent | RenderFragment? | null | Split mode: markup for the main half in place of Label. |
| CssClass · TriggerCssClass · PanelCssClass | string | "" | Appended to the wrapper, the trigger and the open panel. |
| SplitCssClass · PrimaryCssClass | string | "" | Split mode: appended to the pair's wrapper and to the main half. |
| Parent (cascading) | MenuBarControl? | null | The enclosing MenuBarControl, which keeps one menu open at a time. Without one the menu tracks its own state. |

### Action

| Name | Type | Default | Description |
|---|---|---|---|
| Text | string | "" | Label of the entry. |
| Icon | string | "" | Glyph before the label, printed as text: an emoji or a character. Markup and icon-font class names are printed as literal text, not drawn. |
| Disabled | bool | false | Sets the disabled attribute; the keyboard skips the entry. |
| CssClass | string | "" | Appended after fx-menu-action. |
| ParentMenu (cascading) | MenuDropDownControl? | null | The enclosing MenuDropDownControl, which the entry closes before raising OnClick. |

### Events

| Name | Type | Description |
|---|---|---|
| MenuDropDownControl.OnOpening | none | Each time the menu opens, by click or key; the place to refresh its entries. The panel opens without waiting: an async handler re-renders it when it finishes. |
| MenuDropDownControl.PrimaryClick | none | Split mode: the main half was clicked or pressed; the menu then closes. Without a handler the main half opens the menu. |
| MenuActionControl.OnClick | none | The entry was picked, after the menu closes. |

### Keys

| Name | Description |
|---|---|
| Enter or Space on a trigger | Opens the menu and focuses its first entry; closes it when open. |
| Down / Up on a trigger | Opens the menu at its first or last entry. |
| Enter or Space on the main half of a split button | Runs PrimaryClick and closes the menu. With no handler, opens or closes the menu like the arrow. |
| Down / Up / Escape on the main half of a split button | Same as on a trigger: Down and Up open the menu at its first or last entry, Escape closes it. |
| Down / Up in a menu | Focuses the next or previous enabled entry, wrapping at the ends. |
| Home / End in a menu | Focuses the first or last enabled entry. |
| Enter or Space in a menu | Picks the focused entry. |
| Escape | Closes the menu; from inside it, focus returns to the trigger. |

### Css

| Name | Description |
|---|---|
| fx-menu-bar | The bar: a row of triggers. |
| fx-menu-dropdown | Wrapper of one menu; the panel is positioned against it. |
| fx-menu-trigger · .active · .disabled | The trigger button; active while its menu is open. |
| fx-menu-split · fx-menu-primary · fx-menu-trigger-arrow | Split mode: the pair, its main half and the arrow glyph. |
| fx-menu-panel · fx-menu-keyboard-active | The open menu, under the trigger and left-aligned; the second class is set while the keyboard is driving it. |
| fx-menu-backdrop | Full-screen transparent button that closes the menu on a click outside. |
| fx-menu-action · fx-menu-action-icon · fx-menu-action-text | One entry, its glyph and its label. |
| fx-menu-separator | A divider line between entries. |

## Examples

### Bar

```razor
@using Fx.ControlKit

<div class="menu-window">
    <div class="mw-chrome" aria-hidden="true"><span></span><span></span><span></span></div>
    <MenuBarControl>
        <MenuDropDownControl Label="Task">
            <MenuActionControl Text="New task" Icon="📝" Disabled="@(_tasks.Count >= 6)" OnClick="AddTask" />
            <MenuActionControl Text="Start first to-do task" Icon="🔨" Disabled="@(!_tasks.Any(t => t.Status == "To do"))" OnClick="StartTask" />
            <MenuSeparatorControl />
            <MenuActionControl Text="Finish last open task" Icon="🏁" Disabled="@(!_tasks.Any(t => t.Status != "Done"))" OnClick="FinishLast" />
        </MenuDropDownControl>
        <MenuDropDownControl Label="View">
            <MenuActionControl Text="Show done" Icon="@Check(_showDone)" OnClick="@(() => _showDone = !_showDone)" />
            <MenuActionControl Text="Compact rows" Icon="@Check(_compact)" OnClick="@(() => _compact = !_compact)" />
        </MenuDropDownControl>
        <MenuDropDownControl Label="Reports" Disabled="true" />
    </MenuBarControl>
    <ul class="menu-list @(_compact ? "compact" : null)">
        @foreach (var t in _tasks.Where(t => _showDone || t.Status != "Done"))
        {
            <li><span>@t.Title</span><BadgeControl Text="@t.Status" Variant="@(t.Status == "Done" ? BadgeVariant.Success : t.Status == "In progress" ? BadgeVariant.Primary : t.Status == "In review" ? BadgeVariant.Warning : BadgeVariant.Secondary)" /></li>
        }
    </ul>
</div>
```

```csharp
// Menu bar
private sealed class ProjectTask
{
    public string Title { get; init; } = "";
    public string Status { get; set; } = "To do";
}
private readonly List<ProjectTask> _tasks = new()
{
    new() { Title = "WEB-113 Site footer", Status = "Done" },
    new() { Title = "WEB-123 Photo gallery", Status = "In progress" },
    new() { Title = "WEB-124 Allergen icons" },
};
private static readonly string[] More = { "WEB-125 Contact form", "WEB-126 Opening hours banner", "WEB-127 Launch checklist" };
private bool _showDone, _compact;
private void AddTask() => _tasks.Add(new() { Title = More[(_tasks.Count - 3) % More.Length] });
private void StartTask() => _tasks.First(t => t.Status == "To do").Status = "In progress";
private void FinishLast() => _tasks.Last(t => t.Status != "Done").Status = "Done";
// A menu entry has no checked state, so the page shows a check mark through Icon
private static string Check(bool on) => on ? "✓" : "";
```

### Standalone

```razor
@using Fx.ControlKit

<div class="mb">
    <div class="demo-row">
        <MenuDropDownControl Label="Send status report" SplitButton="true" ArrowLabel="More things to send" PrimaryClick="@(() => Send("Status report"))">
            <MenuActionControl Text="Send invoice" OnClick="@(() => Send("Invoice"))" />
            <MenuActionControl Text="Send meeting notes" OnClick="@(() => Send("Meeting notes"))" />
        </MenuDropDownControl>
        <span class="result">Website relaunch for Green Fork Cafe, client contact Albus Door</span>
    </div>
    <ul class="sent" role="status">
        @foreach (var line in _sent)
        {
            <li>@line</li>
        }
    </ul>
</div>
```

```csharp
// Split button
private readonly List<string> _sent = new() { "Nothing sent yet" };
private void Send(string what)
{
    if (_sent[0] == "Nothing sent yet") _sent.Clear();
    _sent.Insert(0, $"{what} sent to Albus Door");
}
```

### Opening

```razor
@using Fx.ControlKit

<div class="mb">
    <div class="demo-row">
        <MenuDropDownControl OnOpening="LoadWaiting">
            <TriggerContent>In review <span class="count">@_waiting.Count</span></TriggerContent>
            <ChildContent>
                @if (_menuTasks.Count == 0)
                {
                    <MenuActionControl Text="Nothing in review" Disabled="true" />
                }
                @foreach (var task in _menuTasks)
                {
                    <MenuActionControl Text="@($"Approve {task}")" OnClick="@(() => Approve(task))" />
                }
            </ChildContent>
        </MenuDropDownControl>
        <ButtonControl CssClass="btn-sm" Disabled="@(_waiting.Count == 3)" OnClick="ResetWaiting">Start over</ButtonControl>
    </div>
    <p class="result" role="status">@_approved The list was read @_loads time@(_loads == 1 ? "" : "s").</p>
</div>
```

```csharp
// A menu filled when it opens
private List<string> _waiting = new() { "WEB-117 Order form", "WEB-121 Menu copy", "WEB-122 About page" };
private List<string> _menuTasks = new();
private string _approved = "Nothing approved yet.";
private int _loads;
// OnOpening runs each time the menu opens: fill the entries then, so they are current
private void LoadWaiting()
{
    _loads++;
    _menuTasks = _waiting.ToList();
}
private void Approve(string task)
{
    _waiting.Remove(task);
    _approved = $"Approved {task}.";
}
private void ResetWaiting()
{
    _waiting = new() { "WEB-117 Order form", "WEB-121 Menu copy", "WEB-122 About page" };
    (_approved, _loads) = ("Nothing approved yet.", 0);
}
```

