Skip to content

Tewr/BlazorContextMenu

 
 

Repository files navigation

Blazor Context Menu

Build status Nuget (with prereleases) Nuget Donate

A context menu component for Blazor!

demo-img

⚠️ Warning

This project is build on top of an experimental framework. There are many limitations and there is a high propability that there will be breaking changes each version.

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 (Currently ONLY required for Blazor Server-Side projects)

Add the following static file references in your _Host.cshtml

<link href="_content/BlazorContextMenu/blazorContextMenu.min.css" rel="stylesheet" />
<script src="_content/BlazorContextMenu/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 ⚠️

Upgrading from 0.15 to 0.16
  • Only for Blazor Server-Side projects: You must reference the static files as described in the "Installation" section
Upgrading from 0.12 to 0.13
  • Remove the @addTagHelper *, BlazorContextMenu as it is no longer needed
Upgrading from 0.11 to 0.12
  • The following handlers are removed as they are no longer needed: ClickAsync, EnabledHandlerAsync, VisibleHandlerAsync
  • The Click handler has been renamed to OnClick to keep consistency with the framework/suggested event names
  • The MenuItemClickEventArgs class has been renamed to the more appropriate ItemClickEventArgs
  • The EnabledHandler and VisibleHandler parameters have been removed and replaced with the new OnAppearing event handler
  • The MenuItemEnabledHandlerArgs and MenuItemVisibleHandlerArgs classes have been removed and replaced with the new ItemAppearingEventArgs
Upgrading from 0.10 to 0.11
  • The CssOverrides API is removed and override configuration is moved into templates. The DefaultCssOverrides of the ConfigureTemplate API must be used.
Upgrading from 0.5 to 0.6
  • You must add in Startup.ConfigureServices of your Blazor client side project the following line services.AddBlazorContextMenu();
  • The BlazorContextMenu.BlazorContextMenuDefaults API is removed. Use the API provided in the service configuration.
Upgrading from 0.1 to 0.2
  • Rename "MenuItem" to "Item"
  • Rename "MenuSeperator" to "Seperator"
  • Replace "MenuItemWithSubmenu" with a regular "Item" component

Release Notes

0.16
  • Updated to 3.0 preview 6
0.15
  • Added new OnAppearing event to ContextMenu conponent, that can be used to prevent the menu from showing.
  • Added the WrapperTag parameter to the ContextMenuTrigger component, that can be used to change the ContextMenuTrigger component's element tag (default: div).
  • Added the Id parameter to the ContextMenuTrigger component.
0.14
  • Updated to 3.0 preview 5
0.13
  • Updated to 3.0 preview 4
0.12
  • Updated to Blazor 0.9.0
  • Changed event handlers to the new EventCallback<>. As a consequence the following handlers are no longer needed and they are removed: ClickAsync, EnabledHandlerAsync, VisibleHandlerAsync
  • Fixed menu display position when it doesn't fit on screen
  • The Click handler has been renamed to OnClick to keep consistency with the framework/suggested event names
  • The MenuItemClickEventArgs class has been renamed to the more appropriate ItemClickEventArgs
  • The EnabledHandler and VisibleHandler parameters have been removed and replaced with the new OnAppearing event handler
  • The MenuItemEnabledHandlerArgs and MenuItemVisibleHandlerArgs classes have been removed and replaced with the new ItemAppearingEventArgs
0.11
  • Updated to Blazor 0.8.0
  • Added animations
  • Default css overrides are now part of the Templates API so that you can easily have multiple custom overriden menus
  • Razor Components are not loading the static files included in the library => #6349. As a workaround you can download and reference directly the .css and .js from the /BlazorContextMenu/content folder until the issue is resolved.
0.10
  • Added proper support for Razor Components (aka server-side Blazor)
0.9
  • Updated to Blazor 0.7.0
  • Removed some js interop in favor of the new Cascading Values feature
0.8
  • Updated to Blazor 0.6.0
0.7
  • Added left-click trigger support
0.6
  • Updated to Blazor 0.5.1
  • Changed configuration setup
  • Added templates
0.5
  • Updated to Blazor 0.5.0
0.4
  • Added minification for included css/js
  • Updated to Blazor 0.4.0
0.3
  • Added dynamic EnabledHandlers for menu items
  • Added Active and dynamic ActiveHandlers for menu items
0.2
  • Updated to Blazor 0.3.0
  • Renamed "MenuItem" to "Item" to avoid conflicts with the html element "menuitem"
  • Renamed "MenuSeperator" to "Seperator" for consistency
  • Removed "MenuItemWithSubmenu" (just use a regular "Item")
0.1
  • Initial release

Special Thanks

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

About

A context menu component for Blazor !

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 43.9%
  • HTML 35.7%
  • CSS 12.3%
  • JavaScript 7.5%
  • Batchfile 0.6%