# Blazor Tabs, TabStrip and Accordion

URL: https://flexcoreui.com/demo/tabs-accordion  
Section: Components  
Controls: TabControl, TabStripControl, AccordionControl  
Keywords: tabs, tab control, tabstrip, accordion, expansion panel, document tabs

Fit more on one screen with Blazor tabs and an accordion: tab panes, a strip of open documents and stacked sections, in five themes.

## Documentation

### Tab control

| Name | Type | Default | Description |
|---|---|---|---|
| SelectedIndex | int | 0 | Index of the visible pane. Two-way with @bind-SelectedIndex. |
| SelectedIndexChanged | EventCallback<int> |  | Fires when a different header is selected, by a click or by the arrow keys, Home or End. |
| ChildContent | RenderFragment? |  | TabItems, optionally inside a TabItems wrapper. Order follows declaration for items present on the first render. An item rendered later is added at the end, and one removed from the markup keeps its header and pane. |
| CssClass | string? | null | Appended to the fx-tabs root. There is no Style; this control does not derive from FlexControlBase. |
| Orientation | TabOrientation | TabOrientation.HybridVertical | Layout of the header strip, passed to its TabStripControl: HybridVertical wraps the headers onto rows, Horizontal keeps one scrolling row, Vertical puts a rail beside the pane, 220 px wide or 35% of the control if that is less. Two-way with @bind-Orientation. The strip's right-click menu and overflow buttons are always on here; the control has no switch for them. |
| OrientationChanged | EventCallback<TabOrientation> |  | After the right-click menu picks an orientation. |
| Theme | TabTheme | TabTheme.WindowsClassic | Colors of the header strip: WindowsClassic, FolderTabs, ColorfulCards, Minimal or Dark; see the strip's Theme. Two-way with @bind-Theme. |
| ThemeChanged | EventCallback<TabTheme> |  | After the right-click menu picks a theme. |

### Tab item

| Name | Type | Default | Description |
|---|---|---|---|
| HeaderText | string | "" | Header label. The tab control reads it when it renders, before the item takes a new value, so a changed HeaderText does not show until the tab control renders again; treat it as fixed. |
| Content | RenderFragment? | null | Pane markup; takes precedence over child content. |
| ChildContent | RenderFragment? | null | Pane markup when Content is not set. |

### Strip

| Name | Type | Default | Description |
|---|---|---|---|
| Items | List<TabStripItem> | empty | Tabs, in order. |
| SelectedIndex | int | 0 | Active tab. Two-way with @bind-SelectedIndex. Not clamped when items are removed; clamp it yourself. |
| SelectedIndexChanged | EventCallback<int> |  | Fires when an enabled tab is selected: a click, the arrow keys, Home, End, Enter or Space. A click on the active tab fires it again. |
| OnTabClose | EventCallback<int> |  | Fires with the index when a closable tab's × is clicked or Delete is pressed on it. The strip does not remove the item. |
| CssClass | string? | null | Appended to the fx-tabstrip root. Not a FlexControlBase control. |
| AriaLabel | string | "Tabs" | aria-label of the role="tablist" element. |
| Theme | TabTheme | TabTheme.WindowsClassic | Writes the --fx-tab-* color variables inline on the root (see CSS variables) and data-tab-theme: WindowsClassic (beveled gray; the menu calls it VB6 / Windows), FolderTabs, ColorfulCards (five fills (four pastels and white) rotating over the resting tabs), Minimal, Dark. Two-way with @bind-Theme. |
| ThemeChanged | EventCallback<TabTheme> |  | After the right-click menu changes the theme; the strip then scrolls the selected tab into view. |
| Orientation | TabOrientation | TabOrientation.HybridVertical | HybridVertical wraps the tabs onto rows and scrolls them vertically past 160 px; Horizontal is one row that scrolls sideways; Vertical is a 220 px rail of 52 px tabs with height: 100%, which the host lays out beside the content. Written to data-tab-orientation. Two-way with @bind-Orientation. |
| OrientationChanged | EventCallback<TabOrientation> |  | After the right-click menu changes the orientation; the strip then scrolls the selected tab into view. |
| ShowOrientationMenu | bool | true | A right-click on the strip opens a context menu with the three orientations and five themes, the current ones checked. False ignores the right-click. |
| OnOrientationMenuRequested | EventCallback<MouseEventArgs> |  | With a delegate, the right-click raises this with the mouse position instead of opening the built-in menu, for a host that draws its own (the report writer does). |
| EnableOverflowNavigation | bool | false | When the tabs overflow, shows Previous and Next buttons that scroll them (left and right, or up and down), and hides the native scrollbar. Off, the tabs scroll natively with no buttons. The library's script fits labels and reveals the selected tab either way. |
| ShowSeparators | bool | false | Adds fx-tabstrip-separated to the root. The stylesheet has no rule for it yet, so it is a hook for the host's CSS. |

### Strip item

| Name | Type | Default | Description |
|---|---|---|---|
| Text | string | "" | Label. |
| IconCss | string? | null | Leading icon classes. |
| Closable | bool | false | Shows the × button. |
| Disabled | bool | false | Grays the tab and ignores clicks. |
| Tag | object? | null | Anything the page wants to carry with the tab. |
| ctor |  |  | new(text, iconCss, closable) or an object initializer. |

### Accordion

| Name | Type | Default | Description |
|---|---|---|---|
| Multiple | bool | false | True keeps several items open; false closes the others when one opens. Turned off at run time, it leaves open items open until the next one opens. |
| ChildContent | RenderFragment? |  | AccordionItemControls. |
| CssClass / Style (base) | string? | null | On the fx-accordion root. |
| (unmatched attributes) | Dictionary<string,object> |  | Forwarded to the fx-accordion root. |
| Visible / Enabled / Id / Title / Tag / Data / Parent (base) |  |  | Accepted from FlexControlBase but not used: Visible="false" does not hide the accordion and Id is not rendered. |

### Accordion item

| Name | Type | Default | Description |
|---|---|---|---|
| Title | string? | null | Header text when HeaderContent is not set. |
| IconCss | string? | null | Icon before the title. |
| Disabled | bool | false | Disables the header button. |
| InitiallyExpanded | bool | false | Open on first render. In single mode the last such item wins. |
| HeaderContent | RenderFragment? | null | Replaces icon and title inside the header button; the arrow stays. |
| ChildContent | RenderFragment? | null | Body, rendered only while expanded. |
| Expanded (read-only) | bool |  | Current state, asked of the parent. |

### Css

| Name | Description |
|---|---|
| fx-tabs · fx-tab-headers · fx-tab-content | Tab control root, the class it adds to its header strip, and the bordered pane. The headers themselves are the strip's fx-tabstrip-tab elements. |
| fx-tabstrip (data-tab-orientation, data-tab-theme) · fx-tabstrip-viewport · fx-tabstrip-tab (-active, -disabled) · fx-tabstrip-icon · fx-tabstrip-text · fx-tabstrip-close | Strip root with the orientation and theme attributes; the scrolling role="tablist"; each bordered tab (Tahoma, 12 px) with its icon, two-line text and × button. The active tab's bottom edge takes --fx-tab-active-border-color, which each theme sets to the tab's own fill. |
| fx-tabstrip-navigation · fx-tabstrip-scroll · fx-tabstrip-navigable · fx-tabstrip-separated | The Previous/Next buttons shown by EnableOverflowNavigation, and the root classes that parameter and ShowSeparators add. |
| fx-accordion · fx-accordion-item (.fx-accordion-expanded, .fx-accordion-disabled) | Rounded bordered stack and each item; the expanded class rotates the arrow. |
| fx-accordion-header · -icon · -title · -arrow · -body | Header button and its parts; the body carries the padding. |

### Vars

| Name | Type | Description |
|---|---|---|
| --fx-tab-bg · --fx-tab-item-bg · --fx-tab-color · --fx-tab-hover-bg | #e4e4e4 · #d4d4d4 · #222 · #dfdfdf | Strip background, resting tab fill, text and hover fill. Theme writes all eleven --fx-tab-* variables inline on the root, so a page rule needs !important to override one. |
| --fx-tab-active-bg · --fx-tab-active-color · --fx-tab-active-border-color | #f5f5f5 · #000 · #f5f5f5 | Active tab fill and text, and its bottom edge. Each theme sets the edge to the tab's own fill; TabControl's pane stays white in every theme. Vertical draws all four edges in its own border color and ignores the edge variable. |
| --fx-tab-border · --fx-tab-separator-color · --fx-tab-radius · --fx-tab-shadow | #7a7a7a · #aaa · 3px · inset 1px 1px 0 #efefef, inset -1px 0 0 #a0a0a0 | Tab and strip borders, the line before the scroll buttons, the top corner radius and the tab shadow. Every theme sets the separator to its border color. Vertical ignores the radius and shadow. |
| --fx-font-size · --fx-zoom · --fx-text-muted | 12px · 1 · #d1d5db | Read from the page, not set by the theme: tab font size, zoom factor and disabled text. |
| (none) | — | TabControl hands Theme and Orientation to its strip; the accordion reads no variables. |

## Examples

### Tabs

```razor
@using Fx.ControlKit
@using Fx.ControlKit.Navigation

<div class="pickers">
    <ButtonGroupControl Items="_themeItems" Values="@(new[] { _tabTheme.ToString() })" ValuesChanged="SetTabTheme" Selection="ChoiceSelectionMode.Single" AriaLabel="Tab theme" />
    <ButtonGroupControl Items="_layoutItems" Values="@(new[] { _tabLayout.ToString() })" ValuesChanged="SetTabLayout" Selection="ChoiceSelectionMode.Single" AriaLabel="Tab layout" />
</div>
<div class="ta">
    <div class="leave-tabs">
        <TabControl @bind-SelectedIndex="_tab" @bind-Theme="_tabTheme" @bind-Orientation="_tabLayout">
            <TabItems>
                <TabItem HeaderText="Summary">
                    <dl class="summary">
                        <dt>Request</dt><dd>LR-0112 December leave</dd>
                        <dt>Employee</dt><dd>Albus Door, Tour Operations</dd>
                        <dt>Status</dt><dd>Awaiting approval, 3 lines</dd>
                        <dt>Total</dt><dd>8 days</dd>
                    </dl>
                    <ButtonControl CssClass="btn-sm" OnClick="() => _tab = 1">Review the lines</ButtonControl>
                </TabItem>
                <TabItem HeaderText="Lines">
                    <table class="lines">
                        <thead><tr><th>Leave type</th><th>Dates</th><th class="num">Days</th></tr></thead>
                        <tbody>
                            <tr><td>Vacation</td><td>Dec 14 to 18</td><td class="num">5</td></tr>
                            <tr><td>Training day</td><td>Dec 21</td><td class="num">1</td></tr>
                            <tr><td>Vacation</td><td>Dec 22 to 23</td><td class="num">2</td></tr>
                        </tbody>
                    </table>
                </TabItem>
                <TabItem HeaderText="Notes">
                    <Content>
                        <ul class="notes">
                            @foreach (var note in _notes)
                            {
                                <li>@note</li>
                            }
                        </ul>
                        <ButtonControl CssClass="btn-sm" Disabled="@(_notes.Count == 4)" OnClick="AddNote">Add a note</ButtonControl>
                    </Content>
                </TabItem>
            </TabItems>
        </TabControl>
    </div>
    <p class="result" role="status">Tab @(_tab + 1) of 3 selected · @_notes.Count notes · @_tabTheme, @_tabLayout</p>
</div>
```

```csharp
// Tab control
private int _tab;
private TabTheme _tabTheme = TabTheme.FolderTabs;
private TabOrientation _tabLayout = TabOrientation.Horizontal;
private readonly SelectionItem[] _themeItems =
{
    new("WindowsClassic", "Windows"), new("FolderTabs", "Folder"), new("ColorfulCards", "Cards"), new("Minimal", "Minimal"), new("Dark", "Dark")
};
private readonly SelectionItem[] _layoutItems =
{
    new("Horizontal", "One row"), new("HybridVertical", "Wrapping rows"), new("Vertical", "Side rail")
};
private readonly List<string> _notes = new() { "Rubeus Haggle covers for Albus in Tour Operations.", "The training day is a first aid course." };
private static readonly string[] MoreNotes = { "Manager approval is due by Friday.", "Six vacation days remain after this request." };
private void AddNote() => _notes.Add(MoreNotes[_notes.Count - 2]);
private void SetTabTheme(IReadOnlyList<string> values)
{
    if (values.Count > 0) _tabTheme = Enum.Parse<TabTheme>(values[0]);
}
private void SetTabLayout(IReadOnlyList<string> values)
{
    if (values.Count > 0) _tabLayout = Enum.Parse<TabOrientation>(values[0]);
}
```

### Strip

```razor
@using Fx.ControlKit

<div class="ta">
    <div class="strip-actions">
        <span class="result">Open a request:</span>
        @foreach (var request in Requests)
        {
            <ButtonControl CssClass="btn-sm" OnClick="() => Open(request)">@request.Code</ButtonControl>
        }
    </div>
    <div class="@(_orientation == TabOrientation.Vertical ? "docs docs-vertical" : "docs")">
        <TabStripControl Items="_docs" @bind-SelectedIndex="_doc" OnTabClose="CloseDoc" @bind-Theme="_docTheme" @bind-Orientation="_orientation" EnableOverflowNavigation="true" AriaLabel="Open leave requests" />
        <div class="strip-pane">@_docs[Math.Clamp(_doc, 0, _docs.Count - 1)].Tag</div>
    </div>
    <div class="strip-actions">
        <ButtonControl CssClass="btn-sm" OnClick="Reopen" Disabled="@(_closed.Count == 0)">Reopen last closed</ButtonControl>
        <span class="result" role="status">@(_docs.Count - 2) request@(_docs.Count == 3 ? "" : "s") open, @_closed.Count closed · @_docTheme, @_orientation</span>
    </div>
</div>
```

```csharp
// Tab strip
private sealed record LeaveRequest(string Code, string Summary);
private static readonly LeaveRequest[] Requests =
{
    new("LR-0112", "December leave for Albus Door, Tour Operations: eight days from December 14, awaiting approval."),
    new("LR-0113", "Sick leave for Minerva McGadget, Finance: two days, November 12 and 13, approved."),
    new("LR-0114", "Parental leave for Nymphadora Tonic, Marketing: twelve weeks from January 4, approved."),
    new("LR-0115", "Training day for Neville Longbutton, IT: one day on November 20, awaiting approval."),
    new("LR-0116", "Vacation for Ginny Measly, Reservations: five days from November 30, draft."),
};
private int _doc;
private readonly List<TabStripItem> _docs = new()
{
    new("Today", "bi bi-house") { Tag = "Today: new leave requests, requests awaiting approval and absences this week." },
    new("LR-0112", "bi bi-calendar-event", closable: true) { Tag = Requests[0].Summary },
    new("Payroll", "bi bi-receipt") { Disabled = true, Tag = "Not run yet." },
};
private readonly Stack<TabStripItem> _closed = new();
private TabTheme _docTheme = TabTheme.FolderTabs;
private TabOrientation _orientation = TabOrientation.Horizontal;
// A request that is already open is selected; a new one goes in before the disabled Payroll tab.
private void Open(LeaveRequest request)
{
    var at = _docs.FindIndex(d => d.Text == request.Code);
    if (at < 0)
    {
        at = _docs.Count - 1;
        _docs.Insert(at, new(request.Code, "bi bi-calendar-event", closable: true) { Tag = request.Summary });
    }
    _doc = at;
}
private void CloseDoc(int index)
{
    _closed.Push(_docs[index]);
    _docs.RemoveAt(index);
    // Keep the same tab selected when one before it closes, and step back past the disabled tab.
    if (index < _doc || _doc >= _docs.Count) _doc = Math.Max(0, _doc - 1);
    while (_doc > 0 && _docs[_doc].Disabled) _doc--;
}
private void Reopen()
{
    if (_closed.Count == 0) return;
    var item = _closed.Pop();
    var open = _docs.FindIndex(d => d.Text == item.Text);
    if (open >= 0) { _doc = open; return; }   // reopened from the buttons already: select it
    _docs.Insert(_docs.Count - 1, item);
    _doc = _docs.Count - 2;
}
```

### Accordion

```razor
@using Fx.ControlKit

<SwitchControl @bind-Checked="_several" Label="Several open" />
<div class="ta">
    <div class="acc">
        <AccordionControl @key="_several" Multiple="_several">
            <AccordionItemControl InitiallyExpanded="true">
                <HeaderContent><span class="acc-head"><span class="bi bi-file-earmark-text"></span> Contract <BadgeControl Text="@Step(_contract)" Variant="@Color(_contract)" Size="BadgeSize.Small" /></span></HeaderContent>
                <ChildContent><CheckBoxControl @bind-Checked="_contract" Label="Signed contract received from Cedric Diggle" /></ChildContent>
            </AccordionItemControl>
            <AccordionItemControl>
                <HeaderContent><span class="acc-head"><span class="bi bi-laptop"></span> Equipment <BadgeControl Text="@Step(_equipment)" Variant="@Color(_equipment)" Size="BadgeSize.Small" /></span></HeaderContent>
                <ChildContent><CheckBoxControl @bind-Checked="_equipment" Label="Laptop and headset ordered from IT" /></ChildContent>
            </AccordionItemControl>
            <AccordionItemControl>
                <HeaderContent><span class="acc-head"><span class="bi bi-building"></span> Office <BadgeControl Text="@Step(_office)" Variant="@Color(_office)" Size="BadgeSize.Small" /></span></HeaderContent>
                <ChildContent><CheckBoxControl @bind-Checked="_office" Label="Desk assigned in the Lisbon office" /></ChildContent>
            </AccordionItemControl>
            <AccordionItemControl @key="Ready" Title="@(Ready ? "Sign-off" : "Sign-off (finish the steps above)")" IconCss="bi bi-check2-circle" Disabled="@(!Ready)">
                Confirm the December 1 start for Cedric Diggle (EMP-0058) and send the welcome email.
            </AccordionItemControl>
        </AccordionControl>
    </div>
    <p class="result" role="status">@Done of 3 steps done@(Ready ? ": sign-off is unlocked" : "")</p>
</div>
```

```csharp
// Accordion
private bool _several, _contract = true, _equipment, _office;
private int Done => (_contract ? 1 : 0) + (_equipment ? 1 : 0) + (_office ? 1 : 0);
private bool Ready => Done == 3;
private static string Step(bool done) => done ? "Done" : "To do";
private static BadgeVariant Color(bool done) => done ? BadgeVariant.Success : BadgeVariant.Secondary;
```

