Skip to content
GitHub11 stars

How to install dependencies and structure your app.

Choose the setup that matches your starting point.

New Project

You need the .NET 10 SDK. Nothing else: no Node.js, npm or package.json.

Create a Blazor app

Components need interactivity. Create a Blazor Web App with interactive server rendering:

dotnet new blazor -n MyApp --interactivity Server --all-interactive

Run the init command

From the app's folder, run init to set up Tailwind, the theme and the usings:

dnx Blazix.Cli init

Add components

You can now start adding components to your project.

dnx Blazix.Cli add button

The command above adds the Button component to Components/UI/Button. You can then use it in a page:

Components/Pages/Home.razor
@page "/"

<Button>Click me</Button>

Run your app

dotnet watch

Tailwind builds with your project, and recompiles as you edit while dotnet watch runs.

Existing Project

Run init in the folder of your Blazor app (or pass --project). It changes your project like this, and every change is a plain file you can read and edit:

  • Adds Tailwind.targets and imports it in your .csproj. Tailwind now builds with your project, using the standalone Tailwind CLI, which is downloaded on the first build.
  • Adds TailwindWatcher.cs and one line in Program.cs. In development, Tailwind recompiles as you edit, so hot reload shows new classes.
  • Moves your existing wwwroot/app.css to Styles/site.css and imports it, so none of your styles are lost.
  • Adds Styles/theme.css: the design tokens. See Theming.
  • Adds Styles/Vendor/tw-animate.css and Styles/Vendor/variants.css, the animation utilities and variants the components use.
  • Adds a global using for Blazix.Primitives to your .csproj, and an @using for your components to _Imports.razor when you add the first component.
  • Adds the Blazix.Primitives package, at the same version as the CLI.
  • Writes blazix.json, which records your namespace, components folder and what you've installed.
dnx Blazix.Cli init

Then add components with add, as above.

If your app has a .Client project (the WebAssembly and Auto render modes), run init in either project, or in the solution folder above them. The components, usings, package and blazix.json go in the .Client project, so pages in every render mode can use them. Tailwind and TailwindWatcher.cs go in the server project, which serves the CSS and scans the .Client project's files with @source. The server project gets the usings too, for its layouts and static pages. add and list also work from any of these folders. See Render Modes.

A standalone WebAssembly app has no server to run the watcher, so init leaves it out and links app.css in wwwroot/index.html. The CSS is recompiled on each build.

Render Modes

The components work in Interactive Server, Interactive WebAssembly and Interactive Auto apps, prerendered or not. In static server rendering, they render but don't respond: menus, dialogs and the rest need an interactive render mode.

Interactive Auto runs a page on the server the first time, while the WebAssembly runtime downloads, and in the browser once it's cached, usually from the next full page load. Nothing changes in how you use the components. To set up an Auto app:

dotnet new blazor -n MyApp --interactivity Auto
dnx Blazix.Cli init

Run init in the MyApp folder the template made. Then keep these in mind:

  • Components that use Blazix go in the .Client project, where add puts the components. The server can run them, but WebAssembly can only load what's in the client.
  • Give them a render mode. Without --all-interactive, the layout in the server project renders statically, so put @rendermode InteractiveAuto on each page, or render an interactive part of the layout (a header with a menu) as its own component: <Header @rendermode="InteractiveAuto" />.
  • Register services in both Program.cs files. A page runs on the server (prerendering, and Auto before WebAssembly) and in the browser, so a service it injects, like the ToastManager, must be registered in both. Otherwise the page fails only once it runs in the browser. add tells you when one is missing.
  • Cascading values stop at a render mode. A DirectionProvider or Toaster in a static layout doesn't reach an interactive page. Put the Toaster in an interactive part of the layout (<Toaster @rendermode="InteractiveAuto" />) or in the page. For right-to-left, cascade the direction from both Program.cs files instead: builder.Services.AddCascadingValue(_ => TextDirection.Rtl);. With --all-interactive the layout is interactive, and both work there as usual.

Parts of a page can run on different runtimes, for example a header on the server over a page on WebAssembly. That works: Escape, outside presses and modal dialogs see popups from both runtimes, and the ids they render can't collide. But each runtime has its own services, so a toast added on one runtime doesn't show in a Toaster on the other. Interactive Auto keeps the parts of a page on one runtime, so this only comes up when you mix fixed render modes.

Icons

The components draw their icons with the Icon component from Blazix.Icons. add installs the package of the set they use: Lucide (Blazix.Icons.Lucide) unless you pick another set with init --icon-library. For icons of your own, add the set's package too. See Icons.

The examples in these docs use Blazix.Icons.Lucide (<Icon Data="LucideIcons.ChevronRight" />). To copy them as they are, add the package and @using Blazix.Icons and @using Blazix.Icons.Lucide to _Imports.razor.