Skip to content

[API Proposal]: CSS Class List String Syntax #119519

@Peter-Juhasz

Description

@Peter-Juhasz

Background and motivation

It would help to support many web development scenarios (especially Blazor) and unlock potential cross-language editor features if we had a StringSyntax for CSS Class Lists:

  • syntax error checking
  • reference highlight of CSS class names
  • code completion for class names

API Proposal

namespace System.Diagnostics.CodeAnalysis;

public sealed class StringSyntaxAttribute : Attribute
{
    public const string CssClassList = "CssClassList";
}

API Usage

Customers could annotate that Blazor components which accept additional CSS classes:

using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;

public class MyComponent : ComponentBase
{
	[Parameter]
	[StringSyntax(StringSyntaxAttribute.CssClassList)]
   	public string? CssClass { get; set; }
}
<MyComponent CssClass="first second" />

Or other helper methods:

using System.Diagnostics.CodeAnalysis;

public static class CssBuilder
{
	public static string? Combine([StringSyntax(StringSyntaxAttribute.CssClassList)] string? class1, [StringSyntax(StringSyntaxAttribute.CssClassList)] string? class2) => (class1, class2) switch
	{
		(not null, null) => class1,
		(not null, not null) => $"{class1} {class2}",
		(null, null) => null,
		(null, not null) => class2,
	};
}
<div class="@CssBuilder.Combine("first", "second")"></div>

Alternative Designs

No response

Risks

It is a simple feature with a very simple grammar, which is broadly used everywhere, I see no risks with adding it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions