Home

Awesome

Blazor Context Menu

Build status Nuget (with prereleases) Nuget Donate

A context menu component for Blazor!

demo-img

Samples / Demo

You can find a live demo here.

Installation

1. Add the nuget package in your Blazor project

> dotnet add package Blazor.ContextMenu

OR

PM> Install-Package Blazor.ContextMenu

Nuget package page can be found here.

2. Add the following line in your Blazor project's startup class

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBlazorContextMenu();
    }
}

3. Add the following line in your _Imports.razor

@using BlazorContextMenu

4. Reference the static files

Add the following static file references in your _Host.cshtml (server-side blazor) or in your index.html (client-side blazor). Make sure that there is a call to app.UseStaticFiles(); in your server project's Startup.cs.

<link href="_content/Blazor.ContextMenu/blazorContextMenu.min.css" rel="stylesheet" />
<script src="_content/Blazor.ContextMenu/blazorContextMenu.min.js"></script>

Basic usage


<ContextMenu Id="myMenu">
    <Item OnClick="@OnClick">Item 1</Item>
    <Item OnClick="@OnClick">Item 2</Item>
    <Item OnClick="@OnClick" Enabled="false">Item 3 (disabled)</Item>
    <Seperator />
    <Item>Submenu
        <SubMenu>
            <Item OnClick="@OnClick">Submenu Item 1</Item>
            <Item OnClick="@OnClick">Submenu Item 2</Item>
        </SubMenu>
    </Item>
</ContextMenu>

<ContextMenuTrigger MenuId="myMenu">
    <p>Right-click on me to show the context menu !!</p>
</ContextMenuTrigger>

@code{
    void OnClick(ItemClickEventArgs e)
    {
        Console.WriteLine($"Item Clicked => Menu: {e.ContextMenuId}, MenuTarget: {e.ContextMenuTargetId}, IsCanceled: {e.IsCanceled}, MenuItem: {e.MenuItemElement}, MouseEvent: {e.MouseEvent}");
    }
}

Customization

Templates

You can create templates in the configuration that you can then apply to context menus.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBlazorContextMenu(options =>
        {
            options.ConfigureTemplate("myTemplate", template =>
            {
                template.MenuCssClass = "my-menu";
                template.MenuItemCssClass = "my-menu-item";
                //...
            });
        });
    }
}
<style>
    .my-menu { color: darkblue; }
    
    /* using css specificity to override default background-color */
    .my-menu .my-menu-item { background-color: #ffb3b3;}
    .my-menu .my-menu-item:hover { background-color: #c11515;} 
</style>

<ContextMenu Id="myMenu" Template="myTemplate">
    <Item>Item 1</Item>
    <Item>Item 2</Item>
</ContextMenu>

You can also change the default template that will apply to all context menus (unless specified otherwise).

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBlazorContextMenu(options =>
        {
            //Configures the default template
            options.ConfigureTemplate(defaultTemplate =>
            {
                defaultTemplate.MenuCssClass = "my-default-menu";
                defaultTemplate.MenuItemCssClass = "my-default-menu-item";
                //...
            });

            options.ConfigureTemplate("myTemplate", template =>
            {
                template.MenuCssClass = "my-menu";
                template.MenuItemCssClass = "my-menu-item";
                //...
            });
        });
    }
}

Explicit customization

All components expose CssClass parameters that you can use to add css classes. These take precedence over any template configuration.

<ContextMenu Id="myMenu" CssClass="my-menu">
    <Item CssClass="red-menuitem">Red looking Item</Item>
    <Item>Default looking item</Item>
</ContextMenu>

Overriding default css

You can override the default css classes completely in the following ways (not recommended unless you want to achieve advanced customization).

Override default css using templates

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBlazorContextMenu(options =>
        {
            //This will override the default css classes for the default template
            options.ConfigureTemplate(defaultTemplate =>
            {
                defaultTemplate.DefaultCssOverrides.MenuCssClass  = "custom-menu";
                defaultTemplate.DefaultCssOverrides.MenuItemCssClass= "custom-menu-item";
                defaultTemplate.DefaultCssOverrides.MenuItemDisabledCssClass = "custom-menu-item--disabled";
                //...
            });
        });
    }
}

Using the OverrideDefaultXXX parameters on components. These take precedence over the template overrides.

<ContextMenu Id="myMenu" OverrideDefaultCssClass="custom-menu">
    <Item OverrideDefaultCssClass="custom-menu-item" OverrideDefaultDisabledCssClass="custom-menu-item--disabled">Item 1</Item>
    <Item OverrideDefaultCssClass="custom-menu-item" OverrideDefaultDisabledCssClass="custom-menu-item--disabled">Item 2</Item>
</ContextMenu>

⚠️ Breaking changes ⚠️

<details open="open"><summary>Upgrading from 1.9.0 to 1.10.0</summary>
</details> <details><summary>Upgrading from 0.19 to 0.20</summary>
</details> <details><summary>Upgrading from 0.16 to 0.17</summary>
</details> <details><summary>Upgrading from 0.15 to 0.16</summary>
</details> <details><summary>Upgrading from 0.12 to 0.13</summary>
</details> <details><summary>Upgrading from 0.11 to 0.12</summary>
</details> <details><summary>Upgrading from 0.10 to 0.11</summary>
</details> <details><summary>Upgrading from 0.5 to 0.6</summary>
</details> <details><summary>Upgrading from 0.1 to 0.2</summary>
</details>

Release Notes

<details open="open"><summary>2.1</summary>
</details> <details><summary>2.0</summary>
</details> <details><summary>1.17</summary>
</details> <details><summary>1.16</summary>
</details> <details><summary>1.15</summary>
</details> <details><summary>1.14</summary>
</details> <details><summary>1.13</summary>
</details> <details><summary>1.12</summary>
</details> <details><summary>1.11</summary>
</details> <details><summary>1.10.1</summary>
</details> <details><summary>1.10</summary>
</details> <details><summary>1.9</summary>
</details> <details><summary>1.8</summary>
</details> <details><summary>1.7</summary>
</details> <details><summary>1.6</summary>
</details> <details><summary>1.5</summary>
</details> <details><summary>1.4</summary>
</details> <details><summary>1.3</summary>
</details> <details><summary>1.2</summary>
</details> <details><summary>1.1</summary>
</details> <details><summary>1.0</summary>
</details> <details><summary>0.21</summary>
</details> <details><summary>0.20</summary>
</details> <details><summary>0.19</summary>
</details> <details><summary>0.18</summary>
</details> <details><summary>0.17</summary>
</details> <details><summary>0.16</summary>
</details> <details><summary>0.15</summary>
</details> <details><summary>0.14</summary>
</details> <details><summary>0.13</summary>
</details> <details><summary>0.12</summary>
</details> <details><summary>0.11</summary>
</details> <details><summary>0.10</summary>
</details> <details><summary>0.9</summary>
</details> <details><summary>0.8</summary>
</details> <details><summary>0.7</summary>
</details> <details><summary>0.6</summary>
</details> <details><summary>0.5</summary>
</details> <details><summary>0.4</summary>
</details> <details><summary>0.3</summary>
</details> <details><summary>0.2</summary>
</details> <details><summary>0.1</summary>
</details>

Special Thanks

This project was inspired by https://github.com/fkhadra/react-contexify and https://github.com/vkbansal/react-contextmenu