Skip to content

Commit 13cc518

Browse files
committed
Initialize the theme in ThemeService from RadzenTheme.
1 parent 059a824 commit 13cc518

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Radzen.Blazor/ThemeService.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.Extensions.DependencyInjection;
13
using Microsoft.JSInterop;
24
using System;
35
using System.Collections;
@@ -367,13 +369,31 @@ public static class Themes
367369
/// <summary>
368370
/// Service for theme registration and management.
369371
/// </summary>
370-
public class ThemeService(IJSRuntime jsRuntime)
372+
public class ThemeService(IJSRuntime jsRuntime, IServiceProvider serviceProvider)
371373
{
372374

375+
private string theme;
373376
/// <summary>
374377
/// Gets the current theme.
375378
/// </summary>
376-
public string Theme { get; private set; }
379+
public string Theme
380+
{
381+
get
382+
{
383+
if (theme == null)
384+
{
385+
var persistentComponentState = serviceProvider.GetService<PersistentComponentState>();
386+
387+
if (persistentComponentState?.TryTakeFromJson(nameof(Theme), out string persistedTheme) == true)
388+
{
389+
theme = persistedTheme;
390+
}
391+
}
392+
return theme;
393+
}
394+
395+
private set => theme = value;
396+
}
377397

378398
/// <summary>
379399
/// Specify if the theme colors should meet WCAG contrast requirements.

RadzenBlazorDemos/Pages/ThemeServicePage.razor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ The Radzen.Blazor library provides a built-in service that persists the current
3232

3333
[Inject]
3434
private ThemeService ThemeService { get; set; }
35+
36+
[Inject]
37+
private IOptions&lt;CookieThemeServiceOptions&gt; Options { get; set; }
3538

3639
protected override void OnInitialized()
3740
{
@@ -45,6 +48,10 @@ The Radzen.Blazor library provides a built-in service that persists the current
4548
{
4649
ThemeService.SetTheme(theme, false);
4750
}
51+
else
52+
{
53+
HttpContext.Response.Cookies.Append("MyApplicationTheme", "material" , new CookieOptions { Secure = Options.Value.IsSecure, Expires = DateTimeOffset.Now.Add(Options.Value.Duration) });
54+
}
4855
}
4956
}
5057
}

0 commit comments

Comments
 (0)