AppStateService

The AppStateService provide methods to read or edit the Common AppState such as the sidebar visibility and the header color.

Initialization

In order to Initialize all services, go to the Program.cs file at the project root, add NVServices.AddNovaDesignSystem(builder.Services); to the Main method.

It will initialize all services from Nova Framework.

Code

using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Nova.Services;
using NovaDev;
using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient {BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)});

NVServices.AddNovaDesignSystem(builder.Services);

await builder.Build().RunAsync();

DisplaySidebar

DisplaySidebar show or hide the sidebar.

Parameter NameType
showbool
Return type

bool

Code

@inject Nova.Services.NVAppStateService AppState;

public void ShowSideBar()
{
  AppState.DisplaySidebar(true);
}

public void HideSideBar()
{
  AppState.DisplaySidebar(false);
}

SetHeaderColor

SetHeaderColor is used to change the main color of the app.
It set the value of private string _headerColor AppState class.

Parameter NameType
colorstring
Return type

string

Code

@inject Nova.Services.NVAppStateService AppState;

 <NVButton OnClick="@(AppState.SetHeaderColor("primary"))">
  Primary
 </NVButton>

 <NVButton OnClick="@(AppState.SetHeaderColor("Secondary"))">
  Secondary
 </NVButton>

GetHeaderColor

GetHeaderColor is used to retrieve the current main color associated to the header of the app.
Get the value of the private string _headerColor.

Parameter NameType
colorstring
Return type

string

Code

<NVBlock>@(AppState.GetHeaderColor())</NVBlock>

Result

<div>primary</div>

IsSidebarVisible

Check if the sidebar is visible or not.

Return type

bool

Code

@inject Nova.Services.NVAppStateService AppState;

<NVBlock>
  @($"{(AppState.IsSidebarVisible() ? "visible" : "hidden")}")
<NVBlock>

Result

<div>visible</div>

ToggleDisplaySidebar

Show or hide the sidebar of the app.

Return type

bool

Code

@implements IDisposable;
@inject Nova.Services.NVAppStateService AppState;

<NVBlock OnClick="@(() => AppState.ToggleDisplaySidebar())">
  @($"{(AppState.IsSidebarVisible() ? "visible" : "hidden")}")
<NVBlock>

@code {
  protected override void OnInitialized()
  {
    base.OnInitialized();
    AppState.OnChange += StateHasChanged;

    _isSidebarVisible = AppState.IsSidebarVisible();
  }
  
  public void Dispose()
  {
    AppState.OnChange -= StateHasChanged;
  }
}

Result

<div>visible</div>