AuthorBuddy/Services/ThemeService.cs

30 lines
780 B
C#
Raw Permalink Normal View History

2026-05-16 16:13:35 +02:00
using Microsoft.JSInterop;
2026-05-25 06:38:16 +02:00
using System.Reflection.Metadata.Ecma335;
2026-05-16 16:13:35 +02:00
namespace AuthorBuddy.Web.Services;
public class ThemeService : IThemeService
{
private readonly IJSRuntime _js;
2026-05-25 06:38:16 +02:00
private string _theme = "";
2026-05-16 16:13:35 +02:00
public ThemeService(IJSRuntime js)
{
_js = js;
}
2026-05-25 06:38:16 +02:00
public string Theme { get => _theme; }
2026-05-16 16:13:35 +02:00
public async Task ApplyTheme(string theme)
{
2026-05-25 06:38:16 +02:00
_theme = theme;
2026-05-16 16:13:35 +02:00
await _js.InvokeVoidAsync("eval",
$"document.getElementById('theme-css')?.remove(); " +
$"var link = document.createElement('link'); " +
$"link.id = 'theme-css'; " +
$"link.rel = 'stylesheet'; " +
$"link.href = 'css/themes/{theme}.css'; " +
$"document.head.appendChild(link);");
}
}