Package Description
$ dotnet add package NimbleBlazorms-dotnettools.csharp extension.The built-in Blazor template projects are good starting points. Starting with .NET 8, there's a unified Blazor Web App project type, which supports multiple render modes (see the Blazor render modes documentation for more information). Also see the "Supported Render Modes" section below.
Visual Studio: Choose "New" >> "Project", and pick "Blazor Web App". Choose the appropriate settings for Interactive Render Mode and Interactivity Location, based on your project's needs.
VS Code: Create a new folder, then open it in VS Code. Choose "View" >> "Terminal", and type dotnet new blazor and press Enter, to create a new Blazor Web App. Open the Command Palette ("View" >> "Command Palette" or Ctrl-Shift-P), enter ".NET Generate Assets for Build and Debug" and press Enter.
Additional Resources: Microsoft tutorial: Build a web app with Blazor; dotnet new documentation
dotnet add package NimbleBlazor --source https://api.nuget.org/v3/index.json --prerelease in the Terminal window.npm run build, and then npm run pack -w @ni/nimble-blazor from the root of the Nimble repo[NimbleRepoDirectory]\packages\blazor-workspace\dist and commit/ close Settings. Pick "NimbleBlazor" in the "Package Source" dropdown, and ensure "Include prerelease" is checked. Search for "NimbleBlazor", select it and click the "Install" button.dotnet add package NimbleBlazor --source [NimbleRepoDirectory]\packages\blazor-workspace\dist in the Terminal window.Open _Imports.razor, and add a new line at the bottom: @using NimbleBlazor
Open the HTML page (generally App.razor for Blazor Web Apps, or wwwroot/index.html for Blazor WebAssembly / Hybrid)
At the bottom of the <head> section (right before </head>), add
<link href="_content/NimbleBlazor/nimble-tokens/css/fonts.css" rel="stylesheet" />
At the bottom of the <body> section (right before </body>), add
<script src="_content/NimbleBlazor/nimble-components/all-components-bundle.min.js"></script>
Additional Resources: dotnet add package documentation
For a simple modification to the Blazor template project: open Index.razor and add the following code at the bottom, to add a Nimble text field that updates when a Nimble button is clicked:
<NimbleTextField Value="@ButtonClickStatus"></NimbleTextField>
<NimbleButton Appearance="ButtonAppearance.Outline" @onclick="OnButtonClicked">Click Me</NimbleButton>
@code {
protected string ButtonClickStatus { get; set; } = string.Empty;
private int _buttonClickCount = 0;
private void OnButtonClicked(MouseEventArgs args)
{
_buttonClickCount++;
ButtonClickStatus = $"Button Clicked {_buttonClickCount} times";
}
}
To test out your changes, do "Debug" >> "Start without Debugging" in Visual Studio, or dotnet watch run in the VS Code Terminal.
More complete examples can be found in the Demo.Client/Server example projects.
Nimble supports all of the Blazor render modes:
RenderMode.InteractiveServerRenderMode.InteractiveWebAssemblyRenderMode.InteractiveAutoBlazor with .NET 8 uses prerendering by default for interactive render modes. With it enabled, components are initially rendered server-side without event handlers connected, which could cause unexpected behavior (no effect when users interact with controls immediately after page load).
See the Blazor prerendering docs for information on how to opt out of prerendering.
To use Nimble's theme-aware design tokens in a Blazor app, you should have a <NimbleThemeProvider> element as an ancestor to all of the Nimble components you use. The app's default layout (MainLayout.razor in the examples) is a good place to put the theme provider (as the root content of the page).
Custom Blazor components should provide their own scoped CSS file (in addition to a separate .cs file for the template-independent logic). Providing a separate CSS file is necessary to access other Blazor styling mechanisms that are helpful to use.
Often you will need to provide CSS for the Razor components to control things like layout behaviors within a parent container. To accomplish this, in the scoped CSS file for the component containing the Razor component (e.g. NimbleTextField), you must use the ::deep pseudo-selector to target that component.
MyComponent.razor
<div>
<NimbleTextField class="text-field"></NimbleTextField>
</div>
MyComponent.razor.css
::deep .text-field {
flex: 1;
}
::deep targets all descendants in a component's scoped styles, so ::deep styles should be written as targeted as possible (typically adding CSS classes to the selector). Components also must be wrapped in a containing element in order to work with the ::deep pseudo-selector. For more info see the Microsoft docs.
Blazor doesn't have built-in support for using/ building SCSS files, however Nimble's design tokens can be used as CSS variables (var(--ni-nimble-...)) in Blazor apps without any additional work.
For a full list of supported variable names, see the Nimble Storybook, "Tokens" >> "Theme-aware tokens".
Recommended: Nimble Tokens SCSS file support
In order to use the Nimble design tokens as SCSS in Blazor projects (which results in better IntelliSense and compile-time checking for the Nimble tokens and variables):
.csproj where you have a PackageReference to NimbleBlazor, add the following:<PropertyGroup>
<NimbleBlazor_CopyNimbleDesignTokens>true</NimbleBlazor_CopyNimbleDesignTokens>
<!-- Optional: Override default destination directory of Nimble tokens, relative to project directory -->
<!-- <NimbleBlazor_NimbleDesignTokensDestinationDirectory>CustomDirectory</NimbleBlazor_NimbleDesignTokensDestinationDirectory> -->
</PropertyGroup>
AspNetCore.SassCompiler in your Blazor Project.sasscompiler.json to your project directory:{
"Arguments": "--style=expanded --silence-deprecation=import --error-css --no-source-map"
}
By default, your Razor files and accompanying SCSS can be in Views, Pages, Shared, Components folders (or subfolders), and non-scoped SCSS can be in Styles (which will be placed in wwwroot/css after building).
See the package docs for additional options.
MyComponent.razor.scss), and @import '../NimbleDesignTokens/tokens'; in it (updating the import relative path as needed).@forward/@use with AspNetCore.SassCompiler, but IntelliSense currently only works correctly with @import.$ni-nimble-... variables in your Blazor application SCSS.Other notes:
wwwroot/css to instead be SCSS files in Styles.::deep in an SCSS file (needed when targeting Nimble components specifically), Visual Studio may show a warning (which can be ignored): ::deep is only allowed in file names ending with ".razor.css" or ".cshtml.css". See aspnetcore#58572.Most user-visible strings displayed by Nimble components are provided by the client application and are expected to be localized by the application if necessary. However, some strings are built into Nimble components and are provided only in English.
To provide localized strings in a localized Blazor app:
<NimbleThemeProvider>:
<NimbleLabelProviderCore>: Used for labels for all components besides the table<NimbleLabelProviderTable>: Used for labels for the table (and table sub-components / column types).resx). You can either add to an existing resx file, or create a new one just for the Nimble strings. The resource value should be the Nimble-provided English default string shown in Storybook.popupDismiss label on NimbleLabelProviderCore, if you load your string resources with a .NET IStringLocalizer instance, your label provider may look like the following:
<NimbleLabelProviderCore PopupDismiss="@LabelStringLocalizer["popupDismiss"]"></NimbleLabelProviderCore>
Requirement: Microsoft.AspNetCore.Components.WebView v8.0.4+
This updated WebView package include a fix so that the JavaScript initialization code that Nimble/Spright Blazor uses gets loaded correctly.
Note that if using the Microsoft.AspNetCore.Components.WebView.Wpf package, it only specifies a dependency of Microsoft.AspNetCore.Components.WebView v8.0+, so you may to add need an explicit dependency on Microsoft.AspNetCore.Components.WebView to ensure a recent enough version is included.
The Demo.Hybrid project in the Blazor solution illustrates this setup.
Follow the instructions in CONTRIBUTING.md to modify this library.