Skip to content

A library to allow CSS inside .razor components without duplication

License

Notifications You must be signed in to change notification settings

wihrl/RazorStyle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RazorStyle

A library to allow adding CSS locally inside .razor components without duplication.

Nuget Nuget

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

  1. Add @using RazorStyle to your _Imports.cs. (This is necessary for IDE support)

  2. Add the StyleRoot component to any singleton component, for example App.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
    }
}

// ...
  1. 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>

About

A library to allow CSS inside .razor components without duplication

Resources

License

Stars

Watchers

Forks

Packages

No packages published