Giter Club home page Giter Club logo

menu's Introduction

Blazored Menu

A JavaScript free menu library for Blazor and Razor Components applications.

Build & Test Main

Nuget

Screenshot of component in action

Getting Setup

You can install the package via the nuget package manager just search for Blazored.Menu. You can also install via powershell using the following command.

Install-Package Blazored.Menu

Or via the dotnet CLI.

dotnet add package Blazored.Menu

Add reference to style sheet

Add the following line to the head tag of your index.html (Blazor WebAssembly App) or _Host.cshtml (Blazor Server app).

<link href="_content/Blazored.Menu/blazored-menu.css" rel="stylesheet" />

Add Imports

Add the following to your _Imports.razor

@using Blazored.Menu

Usage

Blazored Menu allows menus to be built either using markup or dynamically, using the MenuBuilder.

Building a menu with markup

You can build your menus using the following components.

  • BlazoredMenu
  • BlazoredMenuItem
  • BlazoredSubMenu

For example.

<BlazoredMenu>
  <BlazoredMenuItem>
    <NavLink href="/" Match="NavLinkMatch.All">Home</NavLink>
  </BlazoredMenuItem>
  <BlazoredSubMenu Header="Sub Menu">
        <MenuTemplate>
            <BlazoredMenuItem>
                <NavLink href="counter">Counter</NavLink>
            </BlazoredMenuItem>
        </MenuTemplate>
    </BlazoredSubMenu>
    <BlazoredMenuItem>
        <NavLink href="fetchdata">Fetch data</NavLink>
    </BlazoredMenuItem>
</BlazoredMenu>

You can also specify your own template for sub menu headers like so.

<BlazoredMenu>
  <BlazoredMenuItem>
    <NavLink href="/" Match="NavLinkMatch.All">Home</NavLink>
  </BlazoredMenuItem>
  <BlazoredSubMenu Header="Sub Menu">
        <HeaderTemplate>
            <i class="my-cool-icon-class"></i> Sub Menu
        </HeaderTemplate>
        <MenuTemplate>
            <BlazoredMenuItem>
                <NavLink href="counter">Counter</NavLink>
            </BlazoredMenuItem>
            <BlazoredMenuItem>
                <NavLink href="fetchdata">Fetch data</NavLink>
            </BlazoredMenuItem>
        </MenuTemplate>
    </BlazoredSubMenu>
</BlazoredMenu>

This feature is currently only available when building menus with markup.

You can also supply your own CSS classes to each of the 3 components

  • BlazoredMenu
  • BlazoredMenuItem
  • BlazoredSubMenu

By setting the Css parameter.

<BlazoredMenu Css="custom-menu-css">
  <BlazoredMenuItem Css="custom-menuitem-css">
    <NavLink href="/" Match="NavLinkMatch.All">Home</NavLink>
  </BlazoredMenuItem>
  <BlazoredSubMenu Header="Sub Menu" Css="custom-submenu-css">
        <HeaderTemplate>
            <i class="my-cool-icon-class"></i> Sub Menu
        </HeaderTemplate>
        <MenuTemplate>
            <BlazoredMenuItem>
                <NavLink href="counter">Counter</NavLink>
            </BlazoredMenuItem>
            <BlazoredMenuItem>
                <NavLink href="fetchdata">Fetch data</NavLink>
            </BlazoredMenuItem>
        </MenuTemplate>
    </BlazoredSubMenu>
</BlazoredMenu>

Building a menu dynamically using the MenuBuilder

If you prefer you can use the MenuBuilder to create your menus using C#. The MenuBuilder exposes two methods AddItem and AddSubMenu. You can build the same menu from the markup example as follows.

<BlazoredMenu MenuBuilder="@MenuBuilder" />

@functions {
    MenuBuilder MenuBuilder = new MenuBuilder();

    protected override void OnInit()
    {
        MenuBuilder.AddItem(1, "Home", "/")
                   .AddSubMenu(2, "Sub Menu", new MenuBuilder().AddItem(1, "Counter", "counter")
                   .AddItem(3, "FetchData", "fetchdata");
    }
}

MenuBuilder Options

When using the MenuBuilder you have a couple of extra options available via the AddItem and AddSubMenu methods.

  • IsEnabled (default: true)
  • IsVisible (default: true)

You can use these options to manipulate your menu items. IsVisible, if set to false, will mark your menu items as display: none making them invisible. Setting IsEnabled to false will render the item in a non-interactive state.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.