RTB.Blazor.Styled 1.0.0-preview

This is a prerelease version of RTB.Blazor.Styled.
There is a newer version of this package available.
See the version list below for details.
dotnet add package RTB.Blazor.Styled --version 1.0.0-preview
                    
NuGet\Install-Package RTB.Blazor.Styled -Version 1.0.0-preview
                    
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.Styled" Version="1.0.0-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RTB.Blazor.Styled" Version="1.0.0-preview" />
                    
Directory.Packages.props
<PackageReference Include="RTB.Blazor.Styled" />
                    
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.Styled --version 1.0.0-preview
                    
#r "nuget: RTB.Blazor.Styled, 1.0.0-preview"
                    
#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.Styled@1.0.0-preview
                    
#: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.Styled&version=1.0.0-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=RTB.Blazor.Styled&version=1.0.0-preview&prerelease
                    
Install as a Cake Tool

๐ŸŽจ RTB.Blazor.Styled

<div align="center"> <strong>Dynamic CSS-in-Blazor styling system with zero-runtime overhead</strong><br> <em>Part of the RTB.Blazor ecosystem</em> </div>

<div align="center">

.NET 9.0 Blazor License: MIT

</div>


๐ŸŒŸ About

RTB.Blazor.Styled brings CSS-in-JS concepts to Blazor with a declarative, component-based styling approach. Write your styles directly in your Razor components using C# components and let the system handle CSS generation, injection, and cleanup automatically.

โœจ Key Features

  • ๐ŸŽฏ Declarative Styling - Define styles using intuitive C# components
  • โšก Zero Runtime Overhead - Styles are generated and cached efficiently
  • ๐Ÿงน Automatic Cleanup - Unused styles are automatically removed
  • ๐Ÿ”ง Type-Safe - Strongly typed units, colors, and CSS properties
  • ๐Ÿ“ฑ Responsive Design - Built-in breakpoint support
  • ๐ŸŽจ Rich API - Complete CSS property coverage with intelligent defaults
  • ๐Ÿ’พ Efficient Caching - Smart hash-based style deduplication

๐Ÿ“ฆ Installation

<PackageReference Include="RTB.Blazor.Styled" Version="1.0.0-preview" />

๐Ÿš€ Quick Start

1. Register the Service

In your Program.cs:

builder.Services.UseRTBStyled();

2. Basic Usage

@using RTB.Blazor.Styled
@using RTB.Blazor.Styled.Components
@using RTB.Blazor.Styled.Helper

<Styled Context="ClassName">
    <Background Color="@RTBColors.Blue" />
    <Size FullWidth Height="@SizeUnit.Rem(10)" />
    <Padding All="@Spacing.Rem(2)" />
    
    <div class="@ClassName">
        <h1>Styled Component</h1>
        <p>This div has dynamic styling applied!</p>
    </div>
</Styled>

3. Advanced Styling

<Styled Context="ClassName">
    <Background Color="@RTBColor.FromHex("#ff6b6b")" />
    <Size Width="@SizeUnit.Percent(100)" MaxWidth="@SizeUnit.Px(600)" />
    <Margin Horizontal="@Spacing.Auto" Vertical="@Spacing.Rem(2)" />
    <Border Radius="@SizeUnit.Px(12)" Width="@SizeUnit.Px(2)" Color="@RTBColor.Gray" />
    <Flex Direction="FlexDirection.Column" Gap="@Spacing.Rem(1)" />
    
    <div class="@ClassName">
        <h2>Advanced Styled Card</h2>
        <p>Multiple styling components working together</p>
    </div>
</Styled>

๐Ÿงฐ Component Library

Layout & Positioning

  • Size - Width, height, min/max dimensions
  • Margin - External spacing with directional control
  • Padding - Internal spacing with directional control
  • Positioned - Position, top, left, right, bottom, z-index
  • Flex - Flexbox properties (direction, wrap, justify, align)
  • Grid - CSS Grid container properties
  • GridPlacement - Grid item placement (row, column, area)

Visual Styling

  • Background - Background colors, images, gradients
  • Border - Border properties (width, style, color, radius)
  • Color - Text and foreground colors
  • Other - Miscellaneous CSS properties

Responsive & Interactive

  • Overflow - Overflow behavior control
  • Transition - CSS transitions and animations
  • Selector - Pseudo-selectors (:hover, :focus, etc.)

๐ŸŽฏ Type System

SizeUnit

// Pixels
SizeUnit.Px(16)        // 16px
SizeUnit.Px(1.5)       // 1.5px

// Relative units
SizeUnit.Rem(1.5)      // 1.5rem
SizeUnit.Em(2)         // 2em
SizeUnit.Percent(100)  // 100%

// Viewport units
SizeUnit.Vw(50)        // 50vw
SizeUnit.Vh(100)       // 100vh

// Special values
SizeUnit.Auto          // auto
SizeUnit.FitContent    // fit-content

RTBColors

// Predefined colors
RTBColors.Red
RTBColors.Blue
RTBColors.Transparent
...

// Custom colors
RTBColor.FromRgb(255, 100, 50)
RTBColor.FromRgba(255, 100, 50, 128)
RTBColor.FromHex("#ff6432")
RTBColor.Parse("#ff6432")

// Color manipulation
var lighter = myColor.Lighten(0.2);
var darker = myColor.Darken(0.1);
var withAlpha = myColor.WithAlpha(0.5);

// Custom spacing Spacing.Rem(2.5) // 2.5rem Spacing.Px(24) // 24px Spacing.Auto // auto


### Conditional Styling

```razor
<Styled Context="ClassName">
    <Background Color="@RTBColors.White" />
    <Background Color="@RTBColors.Gray" Condition="@isDisabled" />
    <Border Width="@SizeUnit.Px(2)" Color="@RTBColors.Red" Condition="@hasError" />
    
    <button class="@ClassName" disabled="@isDisabled">
        Submit
    </button>
</Styled>

Pseudo-Selectors

<Styled Context="ClassName">
    <Background Color="@RTBColor.Blue" />
    
    <Selector Pseudo=":hover">
        <Background Color="@RTBColor.DarkBlue" />
        <Transition Property="background-color" Duration="@TimeSpan.FromMilliseconds(200)" />
    </Selector>
    
    <button class="@ClassName">Hover me!</button>
</Styled>

Complex Layouts

<Styled Context="ContainerClass">
    <Grid Columns="repeat(auto-fit, minmax(300px, 1fr))" Gap="@Spacing.Large" />
    
    <div class="@ContainerClass">
        @foreach (var item in items)
        {
            <Styled Context="ItemClass">
                <Background Color="@RTBColor.White" />
                <Border Radius="@SizeUnit.Px(8)" />
                <Padding All="@Spacing.Medium" />
                <GridPlacement Column="auto" />
                
                <div class="@ItemClass">
                    <h3>@item.Title</h3>
                    <p>@item.Description</p>
                </div>
            </Styled>
        }
    </div>
</Styled>

๐Ÿ—๏ธ Architecture

Core Components

Component Purpose
Styled Main wrapper component that generates CSS classes
StyleBuilder Fluent API for building CSS strings
StyleRegistry Manages style injection and cleanup
RTBStyleBase Base class for all styling components

How it Works

  1. Style Definition - Components define styles using declarative syntax
  2. CSS Generation - StyleBuilder creates optimized CSS strings
  3. Hash-based Caching - Identical styles get the same class name
  4. Dynamic Injection - CSS is injected into the document via JavaScript
  5. Automatic Cleanup - Unused styles are removed when components dispose

Performance Features

  • Deduplication - Identical styles share the same CSS class
  • Lazy Loading - CSS is only generated when components render
  • Smart Caching - Reference counting prevents premature cleanup
  • Minimal DOM - Only necessary styles are injected

๐ŸŽจ Integration with RTB.BlazorUI

RTB.Blazor.Styled seamlessly integrates with RTB.BlazorUI components:

<Stack Horizontal Gap="@Spacing.Medium">
    <Styled Context="CardClass">
        <Background Color="@currentTheme.CardBackground" />
        <Border Radius="@SizeUnit.Px(12)" />
        <Padding All="@Spacing.Large" />
        
        <Card Class="@CardClass" Title="Styled Card">
            <Text>This card uses dynamic styling!</Text>
        </Card>
    </Styled>
</Stack>

๐Ÿ“š Best Practices

1. Component Reusability

// Create reusable styled components
public class StyledButton : ComponentBase
{
    [Parameter] public string? Class { get; set; }
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public bool Primary { get; set; }
    
    // ... styling logic
}

2. Theme Integration

@inject IThemeService<IAppTheme> ThemeService

<Styled Context="ClassName">
    <Background Color="@ThemeService.Current.PrimaryColor" />
    <Color Value="@ThemeService.Current.TextColor" />
    
    
</Styled>

3. Performance Optimization

  • Group related styles in a single <Styled> component
  • Use conditional styling instead of multiple <Styled> components
  • Leverage the built-in caching by reusing common styles

๐Ÿ”ง API Reference

Core Types

  • Styled - Main styling component
  • StyleBuilder - CSS generation utility
  • SizeUnit - Type-safe CSS units
  • RTBColor - Color manipulation and representation
  • Spacing - Predefined spacing values
  • BreakPoint - Responsive breakpoint definitions

Extension Methods

  • UseRTBStyled() - Service registration
  • Various CSS property extensions on StyleBuilder

๐Ÿ“„ License

RTB.Blazor.Styled 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 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 (3)

Showing the top 3 NuGet packages that depend on RTB.Blazor.Styled:

Package Downloads
RTB.Blazor.Charts

Simple Charts

RTB.Blazor.UI

WPF like Components for Blazor

RTB.Blazor

WPF like Components for Blazor

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.9 129 9/8/2025
1.0.8 117 9/5/2025
1.0.7 130 9/2/2025
1.0.6 131 9/1/2025
1.0.5 141 8/31/2025
1.0.4 169 8/29/2025
1.0.3 172 8/29/2025
1.0.2 88 7/5/2025
1.0.1 83 7/5/2025
1.0.1-preview 106 6/28/2025
1.0.0-preview 127 6/27/2025