|
| 1 | +using Microsoft.AspNetCore.Components; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Microsoft.JSInterop; |
1 | 4 | using System;
|
2 | 5 | using System.Collections;
|
3 | 6 | using System.Collections.Generic;
|
@@ -366,12 +369,31 @@ public static class Themes
|
366 | 369 | /// <summary>
|
367 | 370 | /// Service for theme registration and management.
|
368 | 371 | /// </summary>
|
369 |
| - public class ThemeService |
| 372 | + public class ThemeService(IJSRuntime jsRuntime, IServiceProvider serviceProvider) |
370 | 373 | {
|
| 374 | + |
| 375 | + private string theme; |
371 | 376 | /// <summary>
|
372 | 377 | /// Gets the current theme.
|
373 | 378 | /// </summary>
|
374 |
| - 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 | + } |
375 | 397 |
|
376 | 398 | /// <summary>
|
377 | 399 | /// Specify if the theme colors should meet WCAG contrast requirements.
|
@@ -430,8 +452,38 @@ public void SetTheme(ThemeOptions options)
|
430 | 452 | if (requiresChange && options.TriggerChange)
|
431 | 453 | {
|
432 | 454 | ThemeChanged?.Invoke();
|
| 455 | + |
| 456 | + try |
| 457 | + { |
| 458 | + jsRuntime.InvokeVoid("Radzen.setTheme", Href, WcagHref); |
| 459 | + } |
| 460 | + catch (Exception) |
| 461 | + { |
| 462 | + } |
433 | 463 | }
|
434 | 464 | }
|
| 465 | + private static readonly string Version = typeof(ThemeService).Assembly.GetName().Version?.ToString(); |
| 466 | + |
| 467 | + internal string Href => $"{Path}/{Theme}-base.css?v={Version}"; |
| 468 | + |
| 469 | + internal string WcagHref => $"{Path}/{Theme}-wcag.css?v={Version}"; |
| 470 | + |
| 471 | + private string Path => Embedded ? $"_content/Radzen.Blazor/css" : "css"; |
| 472 | + |
| 473 | + internal bool Embedded => Theme switch |
| 474 | + { |
| 475 | + "material" => true, |
| 476 | + "material-dark" => true, |
| 477 | + "standard" => true, |
| 478 | + "standard-dark" => true, |
| 479 | + "humanistic" => true, |
| 480 | + "humanistic-dark" => true, |
| 481 | + "software" => true, |
| 482 | + "software-dark" => true, |
| 483 | + "default" => true, |
| 484 | + "dark" => true, |
| 485 | + _ => false |
| 486 | + }; |
435 | 487 |
|
436 | 488 | /// <summary>
|
437 | 489 | /// Enables or disables WCAG contrast requirements.
|
|
0 commit comments