mirror of
https://github.com/radzenhq/radzen-blazor.git
synced 2026-07-08 18:16:08 +00:00
Add custom CssPath to RadzenTheme and its using ThemeService
Based on #2523 by @trucidare Closes #2523 Closes #2354
This commit is contained in:
95
Radzen.Blazor.Tests/ThemeTests.cs
Normal file
95
Radzen.Blazor.Tests/ThemeTests.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using Bunit;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace Radzen.Blazor.Tests
|
||||
{
|
||||
public class ThemeTests
|
||||
{
|
||||
[Fact]
|
||||
public void Theme_Renders_Embedded_Default_CssPath()
|
||||
{
|
||||
const string path = "_content/Radzen.Blazor/css";
|
||||
|
||||
using var ctx = new TestContext();
|
||||
ctx.Services.AddScoped<ThemeService>();
|
||||
|
||||
var component = ctx.RenderComponent<RadzenTheme>(parameters =>
|
||||
{
|
||||
parameters.Add(p => p.Theme, "material");
|
||||
});
|
||||
|
||||
Assert.Contains(path, component.Markup);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Theme_Renders_Non_Embedded_Default_CssPath()
|
||||
{
|
||||
const string path = "\"css";
|
||||
|
||||
using var ctx = new TestContext();
|
||||
ctx.Services.AddScoped<ThemeService>();
|
||||
|
||||
var component = ctx.RenderComponent<RadzenTheme>(parameters =>
|
||||
{
|
||||
parameters.Add(p => p.Theme, "awesome");
|
||||
});
|
||||
|
||||
Assert.Contains($"{path}/awesome-base.css", component.Markup);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Theme_Renders_Non_Embedded_Custom_CssPath()
|
||||
{
|
||||
const string path = "_content/custom-assembly/css";
|
||||
|
||||
using var ctx = new TestContext();
|
||||
ctx.Services.AddScoped<ThemeService>();
|
||||
|
||||
var component = ctx.RenderComponent<RadzenTheme>(parameters =>
|
||||
{
|
||||
parameters.Add(p => p.CssPath, path);
|
||||
parameters.Add(p => p.Theme, "my-light");
|
||||
});
|
||||
|
||||
Assert.Contains(path, component.Markup);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Theme_Renders_Embedded_CssPath_For_Embedded_Themes()
|
||||
{
|
||||
const string path = "_content/Radzen.Blazor/css";
|
||||
const string customPath = "_content/custom-assembly/css";
|
||||
|
||||
using var ctx = new TestContext();
|
||||
ctx.Services.AddScoped<ThemeService>();
|
||||
|
||||
var component = ctx.RenderComponent<RadzenTheme>(parameters =>
|
||||
{
|
||||
parameters.Add(p => p.CssPath, customPath);
|
||||
parameters.Add(p => p.Theme, "material");
|
||||
});
|
||||
|
||||
Assert.Contains(path, component.Markup);
|
||||
Assert.DoesNotContain(customPath, component.Markup);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Theme_WcagHref_Uses_Custom_CssPath()
|
||||
{
|
||||
const string path = "_content/custom-assembly/css";
|
||||
|
||||
using var ctx = new TestContext();
|
||||
ctx.Services.AddScoped<ThemeService>();
|
||||
|
||||
var component = ctx.RenderComponent<RadzenTheme>(parameters =>
|
||||
{
|
||||
parameters.Add(p => p.CssPath, path);
|
||||
parameters.Add(p => p.Theme, "my-light");
|
||||
parameters.Add(p => p.Wcag, true);
|
||||
});
|
||||
|
||||
Assert.Contains($"{path}/my-light-wcag.css", component.Markup);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,17 @@ namespace Radzen.Blazor
|
||||
public string? Theme { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// When set to true the icon font will be preloadd.
|
||||
/// When set to true the icon font will be preloaded.
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public bool PreloadIconFont { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Custom css path. If set, it overwrites the default path.
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public string? CssPath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Enables WCAG contrast requirements. If set to true additional CSS file will be loaded.
|
||||
/// </summary>
|
||||
@@ -52,6 +58,7 @@ namespace Radzen.Blazor
|
||||
{
|
||||
persistentComponentState = ServiceProvider.GetService<PersistentComponentState>();
|
||||
|
||||
ThemeService.SetCssPath(CssPath);
|
||||
theme = ThemeService.Theme ?? GetCurrentTheme();
|
||||
wcag = ThemeService.Wcag ?? Wcag;
|
||||
|
||||
|
||||
@@ -405,6 +405,11 @@ namespace Radzen
|
||||
/// </summary>
|
||||
public bool? RightToLeft { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Custom css path. If set, it overwrites the default path.
|
||||
/// </summary>
|
||||
public string? CssPath { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Raised when the theme changes.
|
||||
/// </summary>
|
||||
@@ -473,7 +478,11 @@ namespace Radzen
|
||||
|
||||
internal string WcagHref => $"{Path}/{Theme}-wcag.css?v={Version}";
|
||||
|
||||
private string Path => Embedded ? $"_content/Radzen.Blazor/css" : "css";
|
||||
private string Path => Embedded
|
||||
? $"_content/Radzen.Blazor/css"
|
||||
: !string.IsNullOrEmpty(CssPath)
|
||||
? CssPath
|
||||
: "css";
|
||||
|
||||
internal bool Embedded => Theme switch
|
||||
{
|
||||
@@ -517,5 +526,14 @@ namespace Radzen
|
||||
TriggerChange = true
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a specific css path to the theme service.
|
||||
/// </summary>
|
||||
/// <param name="cssPath">New css path to look for themes.</param>
|
||||
public void SetCssPath(string? cssPath)
|
||||
{
|
||||
CssPath = cssPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user