RTB.Blazor.Theme 1.0.4

Suggested Alternatives

RTB.Blazor

Additional Details

Package has been moved to RTB.Blazor

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package RTB.Blazor.Theme --version 1.0.4
                    
NuGet\Install-Package RTB.Blazor.Theme -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RTB.Blazor.Theme" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RTB.Blazor.Theme" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="RTB.Blazor.Theme" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RTB.Blazor.Theme --version 1.0.4
                    
#r "nuget: RTB.Blazor.Theme, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RTB.Blazor.Theme@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RTB.Blazor.Theme&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=RTB.Blazor.Theme&version=1.0.4
                    
Install as a Cake Tool

🎨 RTB.Blazor.Theme

<div align="center"> <strong>Dynamic theming system for Blazor applications</strong><br> <em>Part of the RTB.BlazorUI ecosystem</em> </div>

<div align="center">

.NET 9.0 .NET 8.0 Blazor License: MIT

</div>


🌟 About

RTB.Blazor.Theme provides a robust theming system for Blazor applications with automatic theme discovery, persistence, and dynamic switching capabilities. It seamlessly integrates with the RTB.BlazorUI component library to provide consistent theming across your entire application.

✨ Key Features

  • 🔍 Auto-Discovery - Automatically discovers theme classes implementing ITheme
  • 💾 Persistence - Saves theme preferences to localStorage
  • ⚡ Dynamic Switching - Change themes at runtime without page refresh
  • 🎯 Type-Safe - Strongly-typed theme system with generic constraints
  • 🏷️ Attribute-Based - Mark default themes with [Theme(IsDefault = true)]
  • 🔧 Easy Setup - Simple dependency injection registration

📦 Installation

<PackageReference Include="RTB.Blazor.Theme" Version="1.0.2" />

🚀 Quick Start

1. Define Your Theme Interface

public interface IMyAppTheme : ITheme
{
    string PrimaryColor { get; }
    string SecondaryColor { get; }
    string BackgroundColor { get; }
}

2. Create Theme Implementations

[Theme(IsDefault = true)]
public class LightTheme : IMyAppTheme
{
    public string Name => "Light";
    public string PrimaryColor => "#0066cc";
    public string SecondaryColor => "#6c757d";
    public string BackgroundColor => "#ffffff";
}

public class DarkTheme : IMyAppTheme
{
    public string Name => "Dark";
    public string PrimaryColor => "#4dabf7";
    public string SecondaryColor => "#adb5bd";
    public string BackgroundColor => "#212529";
}

3. Register the Service

In your Program.cs:

builder.Services.UseRTBTheme(typeof(IMyAppTheme));

4. Add Theme Context

Wrap your app with the theme context in App.razor:

<RTBThemeContext TTheme="IMyAppTheme">
    <Router AppAssembly="@typeof(App).Assembly">
        
    </Router>
</RTBThemeContext>

5. Use Themes in Components

@inject IThemeService<IMyAppTheme> ThemeService

@code {
    [CascadingParameter] private IMyAppTheme Theme { get; set; } = default!;
}

<div style="background-color: @Theme(Service.Current).BackgroundColor; 
           color: @Theme(Service.Current).PrimaryColor">
    <h1>Hello, @Theme(Service.Current).Name Theme!</h1>
    
    <button @onclick="SwitchTheme">Switch Theme</button>
</div>

@code {
    private async Task SwitchTheme()
    {
        var nextTheme = ThemeService.Themes
            .SkipWhile(t => t != ThemeService.Current)
            .Skip(1)
            .FirstOrDefault() ?? ThemeService.Themes.First();
            
        await ThemeService.SetThemeAsync(nextTheme);
    }
}

🏗️ Architecture

Core Components

Component Description
ITheme Base interface that all themes must implement
IThemeService<T> Service interface for theme management
RTBThemeService<T> Default implementation with auto-discovery
RTBThemeContext Blazor component that provides theme context
ThemeAttribute Attribute to mark default themes

Theme Discovery

The theme service automatically discovers all classes that:

  • Implement your theme interface
  • Are concrete classes (not abstract)
  • Have a parameterless constructor
  • Are loaded in the current AppDomain

Persistence

Themes are automatically persisted to the browser's localStorage using the key "rtbtheme". The theme is restored when the application loads.

Theme-Aware Components

Create components that automatically respond to theme changes:

@implements IDisposable
@inject IThemeService<IMyAppTheme> ThemeService

<div class="themed-component" style="@GetThemedStyles()">
    @ChildContent
</div>

@code {
    [Parameter] public RenderFragment? ChildContent { get; set; }
    
    protected override void OnInitialized()
    {
        ThemeService.OnThemeChanged += StateHasChanged;
    }
    
    private string GetThemedStyles()
    {
        var theme = ThemeService.Current;
        return $"background: {theme.BackgroundColor}; color: {theme.PrimaryColor};";
    }
    
    public void Dispose()
    {
        ThemeService.OnThemeChanged -= StateHasChanged;
    }
}

🔧 API Reference

ITheme

public interface ITheme
{
    string Name { get; }
}

IThemeService<T>

public interface IThemeService<TTheme> where TTheme : ITheme
{
    TTheme Current { get; }
    TTheme Default { get; }
    event Action? OnThemeChanged;
    ValueTask SetThemeAsync(TTheme theme);
    IList<TTheme> Themes { get; }
}

ThemeAttribute

[AttributeUsage(AttributeTargets.Class)]
public class ThemeAttribute : Attribute
{
    public bool IsDefault { get; set; } = false;
}

📄 License

RTB.Blazor.Theme is released under the MIT License.


<p align="center"> <i>Part of the RTB.BlazorUI ecosystem - Crafted with ❤ by a developer who refused to brew</i> </p>

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated