wihrl.RazorStyle
2.1.2
dotnet add package wihrl.RazorStyle --version 2.1.2
NuGet\Install-Package wihrl.RazorStyle -Version 2.1.2
<PackageReference Include="wihrl.RazorStyle" Version="2.1.2" />
<PackageVersion Include="wihrl.RazorStyle" Version="2.1.2" />
<PackageReference Include="wihrl.RazorStyle" />
paket add wihrl.RazorStyle --version 2.1.2
#r "nuget: wihrl.RazorStyle, 2.1.2"
#:package wihrl.RazorStyle@2.1.2
#addin nuget:?package=wihrl.RazorStyle&version=2.1.2
#tool nuget:?package=wihrl.RazorStyle&version=2.1.2
RazorStyle
A library to allow adding CSS locally inside .razor components without duplication.
Why?
While using <style> ... </style> within Blazor components works fine, each instance of the component creates a new
style tag.
This results in unnecessary junk in the DOM as well as potential styling conflicts.
Using CSS isolation (.razor.css) fixes these issues, but having styles in a separate file sucks.
Usage
Add
@using RazorStyleto your_Imports.cs. (This is necessary for IDE support)Add the
StyleRootcomponent to any singleton component, for exampleApp.razor. The root component creates a single<style>tag to be shared between all instances of<RazorStyle.Style>.
// App.razor
// ...
<StyleRoot />
<Router AppAssembly="@typeof(App).Assembly">
// ...
</Router>
@code {
protected override void OnInitialized()
{
base.OnInitialized();
#if DEBUG
// Enable hot reload for debugging only (performance impact)
StyleRoot.EnableHotReload = true;
#endif
}
}
// ...
- Use
<Style>instead of<style>in your components.
// SomeComponent.razor
<h1 class="title">Foo</h1>
<Style>
.title {
// ...
}
</Style>
BEWARE: This library does not handle CSS isolation! Make sure to use unique class names / selectors to avoid conflicts between components.
Triggered animations
Adding a _triggered_ prefix to @keyframes blocks will duplicate it with a different name.
This can be used in combination with AnimationTrigger to replay an animation programatically without relying on JS.
// SomeAnimatedComponent.razor
@code {
readonly AnimationTrigger _titleAnimation = new("fly-in");
void Trigger()
{
_titleAnimation.Trigger();
StateHasChanged();
}
}
<button @onclick="Trigger">Trigger</button>
<h1 class="title" style="@_titleAnimation">Home</h1>
<Style>
.title {
// ! do NOT include the animation name !
animation: 1s linear;
// ...
}
@@keyframes _triggered_fly-in {
// ...
}
</Style>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.23)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.