dotnet / dotnet/roslyn

Suggestion: @style directive similar to scoped CSS files

Open
#85,553 1 comment 4 reactions 0 assignees View on GitHub
Area-Language Design Area-Razor Area-Razor-Compiler
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

In Blazor we have the ability to create scoped CSS files by ending the file in `.razor.css`.
For example a component named `MyComponent.razor` can have a isolated CSS file `MyComponent.razor.css`.
More details [here](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-8.0).

This feature fits nicely when one creates components with a code-behind file (e.g. `MyComponent.razor.cs`).
But it feels odd when using the `@code` directive, so everything is grouped in a single file.

Therefore, I suggest a complementary `@style` directive which in essence behaves in the same way the isolated CSS files do. Meaning the code within should be bundled the same way and also respect `DisableScopedCssBundling` and `ScopedCssEnabled` project settings.

I have not looked into the inner workings of Razor and do not know how the compilation and bundling works in detail. However a naive solution could be to just emit the CSS in the `@style` directive as scoped files and then just use the existing bundling logic.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.