Using "ThemeService" for "Partial" themes

Hi Team Radzen,

In the app I'm working on at the moment, we have 3 sub apps, each of which have been requested to have their own "Brand Palette".

I've already got theme selection working in each of the layout files by adding code like this to each Layout.razor file where needed:

@code
{
  [Inject]
  private ThemeService _themeService { get; set; } = default!;

  protected override void OnInitialized()
  {
    _themeService.SetTheme("default");
    base.OnInitialized();
  }
}

This approach works great, and I can set "material", "software" any of the free themes, and IF I build my own theme file and save it in the css folder in my wwwroot, as "/css/-base.css" I can load that theme simply by swapping "default" for "" EG:
_themeService.SetTheme("mytheme");

Will look for "/css/mytheme-base.css" and load it, if it exists.

HOWEVER, what I'm finding is that SetTheme seems to replace EVERY css rule when used.

For example, if I do a full theme with EVERY option in, and load it in the root of my site, then say I do only a partial css and change just a handful of the colour vars, then load that. Instead of just changing the colours, it seems to wipe out everything.

The colours do change as expected, but everything else, tables, borders, inputs all get set back to browser defaults.

Is there away just to apply the base theme in the root pages, and customise only the colours in the parts that matter, or do I absolutely have to provide a full theme css file each time?

Hi @shawty,

By design SetTheme replaces the theme CSS file. If you are using partial themes you can include the original theme as a <link rel="stylesheet" href="...." /> before RadzenTheme. Then SetTheme will only replace the partial theme.

Hi Korchev,

how would I achieve that in a razor layout file?

The entire project is a single unified Blazor server side app.

This means there is no html. index, instead the main html for constructing the page is in App.razor.

It's in the App file where we have all the "non changing" style sheets, then we set the default as the last tag in just before using the tag.

This sets everything we need, borders, layout etc etc....

All we ever need to change after that are the colours such as '--rz-primary' in a simple root: {...} override.

These changes however, need to be in the razor layout files, where we are currently doing the ThemeService code I showed above.

A single layout looks like this:

@using GrapeWebApp.SharedUI.Components
@inherits LayoutComponentBase

<div class="page">
  <div class="sidebar">
    <AdminNavMenu />
  </div>

  <main>
    <div class="top-row px-4">
      <TopNavBar />
    </div>

    <h1 style="color: var(--rz-primary);">This text should be using RZ primary for it's colour.</h1>

    <article class="content px-4">
      @Body
    </article>
  </main>
</div>

<div id="blazor-error-ui" data-nosnippet>
  An unhandled error has occurred.
  <a href="." class="reload">Reload</a>
  <span class="dismiss">🗙</span>
</div>

@code
{
  [Inject]
  private ThemeService _themeService { get; set; } = default!;

  protected override void OnInitialized()
  {
    _themeService.SetTheme("admin");
    base.OnInitialized();
  }

}

Which as you can see is just pure body content, outside of any head scope in App.css.

I did a few experiments yesterday, I was able to "target" a "--data-app-theme attribute on the top level page div, but that meant I had to have all 3 color variations in the CSS files all loaded upfront in App.razor.

I guess if there was a way we could change the head tag from c# code in the body, then I could potentially make use of that.

I'll have a think on that one, but if you do come up with any ideas, please do let me know.

Cheers

Shawty

Where do you use RadzenTheme today? In that file you should register the theme CSS (usually App.razor).

Something like this:

<link rel="stylesheet" href="_content/Radzen.Blazor/css/material-base.css" />
<RadzenTheme Theme="my-theme" />

The other option is to just import the base CSS file from your custom theme one via @import. Then it will be included automatically.

oh, I see what you mean now....

I didn't quite follow what you meant in your first reply.

Currently we do the following: (App.razor)

Then in "Routes.razor" we have some logic that chooses "/app1/layout.razor", "/app2/layout.razor" as appropriate, these razor files are all shown as in my previous post.

I guess what your saying here is if I set my base theme in a proper "link" tag EG /css/grape-base.css, then use <RadzenTheme and/or ThemeService to just use a smaller file for customising the colours as needed in the different sections, then that would allow me to just perform the partial updates.

I've also just been looking at the HeadContent system in blazor see if I could use that.

Cheers

Shawty

Thanks to @korchev 's notes I've figured out how to make this work.

For anyone else coming here in future, the key is exactly as was described.

Instead of just relying purely on the theme handling, you must set the first base theme styles using a normal link tag in App.razor.

After the link tag, you then use either a RadzenTheme tag with the appropriate theme name, or a ThemeService in your layout file init override as I have shown in my code snippets above.

With different theme service names in either the direct API call or tag, in your layout files, you can set the "start point" in the link file, then just dynamically load ONLY the customisations in a root:{} context in a much smaller file, changing only what is needed.

Thanks again to team Radzen for pointing me in the correct direction.

I should probably either make a you-tube video or do some .NET talks about Radzen ....