RTB.Blazor.Styled
1.0.0-preview
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
<PackageReference Include="RTB.Blazor.Styled" Version="1.0.0-preview" />
<PackageVersion Include="RTB.Blazor.Styled" Version="1.0.0-preview" />
<PackageReference Include="RTB.Blazor.Styled" />
paket add RTB.Blazor.Styled --version 1.0.0-preview
#r "nuget: RTB.Blazor.Styled, 1.0.0-preview"
#:package RTB.Blazor.Styled@1.0.0-preview
#addin nuget:?package=RTB.Blazor.Styled&version=1.0.0-preview&prerelease
#tool nuget:?package=RTB.Blazor.Styled&version=1.0.0-preview&prerelease
๐จ 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">
</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 dimensionsMargin
- External spacing with directional controlPadding
- Internal spacing with directional controlPositioned
- Position, top, left, right, bottom, z-indexFlex
- Flexbox properties (direction, wrap, justify, align)Grid
- CSS Grid container propertiesGridPlacement
- Grid item placement (row, column, area)
Visual Styling
Background
- Background colors, images, gradientsBorder
- Border properties (width, style, color, radius)Color
- Text and foreground colorsOther
- Miscellaneous CSS properties
Responsive & Interactive
Overflow
- Overflow behavior controlTransition
- CSS transitions and animationsSelector
- 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
- Style Definition - Components define styles using declarative syntax
- CSS Generation - StyleBuilder creates optimized CSS strings
- Hash-based Caching - Identical styles get the same class name
- Dynamic Injection - CSS is injected into the document via JavaScript
- 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 componentStyleBuilder
- CSS generation utilitySizeUnit
- Type-safe CSS unitsRTBColor
- Color manipulation and representationSpacing
- Predefined spacing valuesBreakPoint
- 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 | Versions 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. |
-
net9.0
- Microsoft.AspNetCore.Components.Web (>= 9.0.6)
- Microsoft.Extensions.ObjectPool (>= 9.0.6)
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.