FluentCss Documentation

Extensibility

FluentCss is a small set of primitives. Because utilities are ordinary C# methods and values are typed, you can grow the API with your own tokens and helper methods.

Primitives, not a fixed API

The library gives you typed values (Color, Length, Size, FontFamily) and builder methods that accept them. Anything project-specific — a spacing scale, a brand palette, a card helper — belongs in your application, not in the library.

Define your own tokens

A token is just a named value. Group related Length values in a static class; the name becomes the class fragment and the value is emitted as-is.

public static class Spacing
{
    public static readonly Length Xs = new("0.25rem", "1"); // p-1
    public static readonly Length Sm = new("0.5rem", "2");   // p-2
    public static readonly Length Md = new("1rem", "4");     // p-4
    public static readonly Length Lg = new("1.5rem", "6");   // p-6
    public static readonly Length Xl = new("2rem", "8");     // p-8
}

// The name is the class token, so these reuse the numeric class names
// that P(1)..P(8) already produce and deduplicate with them.
Css.P(Spacing.Md).Gap(Spacing.Md);   // classes: p-4 gap-4

Add helper methods

Turn repeated chains into extension methods. A helper is a normal static method whose first parameter is the builder; return the same interface so calls keep chaining.

public static class StyleExtensions
{
    // IStyleBuilder<T> is implemented by every builder, so this works on
    // Css, Dark, Theme("...") and inside Hover/Focus/Active.
    public static T Padded<T>(this IStyleBuilder<T> builder, Length length) =>
        builder.P(length);

    // A concrete interface can chain and return the same type.
    public static IFluentCssBuilder SpacingMd(this IFluentCssBuilder builder) =>
        builder.P(Spacing.Md).Gap(Spacing.Md);
}

// usage
Css.Padded(Spacing.Lg);
Css.SpacingMd();

Which interface should I extend?

  • IStyleBuilder<T> — implemented by every builder. Best for helpers that only set values, and the only choice that also works inside Hover, Focus and Active.
  • IFluentCssBuilder — the normal builder returned by Css, Dark and Theme(...). Use it when the helper chains several utilities.
  • IRootFluentCssBuilder — the root only. Required for Min, Max and Media.
  • IOnlyStyleBuilder — the parameter passed to the Hover/Focus/Active callbacks.

Composite helpers

The same pattern works for component-level styles: one method that expands to a full set of utilities. The call site stays readable and the definition lives in one place.

public static class ComponentExtensions
{
    public static IFluentCssBuilder Card(this IFluentCssBuilder builder) =>
        builder
            .P(Spacing.Lg)
            .Gap(Spacing.Md)
            .Flex(FlexDirection.Col)
            .RoundedLg()
            .Border()
            .BorderColor(Color.Slate200)
            .Background(Color.White);
}

// usage
<div class="@Css.Card()">
    ...
</div>

Card

Composed from Length tokens.

<div class="@Css.Card()">
    <p class="@Css.FontBold()">Card</p>
    <p class="@Css.TextSm()">Composed from Length tokens.</p>
</div>

Arbitrary declarations

When no typed helper covers a CSS property, use the Arbitrary escape hatch. It emits one utility with a class name, a property and a value. The class name is the identity used for deduplication, so keep it unique and stable.

// Arbitrary("class-name", "css-property", "css-value")
Css.Arbitrary("font-350", "font-weight", "350");
Css.Arbitrary("aspect-golden", "aspect-ratio", "1.618 / 1");
Css.Arbitrary("grid-dashboard", "grid-template-columns", "240px 1fr");

// It composes with states and media like any other utility:
Css.Hover(h => h.Arbitrary("font-350", "font-weight", "350"));
Css.MinMd(md => md.Arbitrary("text-huge", "font-size", "4rem"));

Prefer typed helpers

Arbitrary is stringly-typed. Prefer Color, Length, Size, FontFamily and the built-in methods when they exist; reach for Arbitrary only for the gaps.

Responsive and state helpers

Root and callback helpers follow the same idea, targeting the interface that has the methods you need.

public static class ResponsiveExtensions
{
    // IRootFluentCssBuilder is required for media and theme helpers.
    public static IRootFluentCssBuilder StackOnMd(this IRootFluentCssBuilder builder) =>
        builder.MinMd(md => md.Flex(FlexDirection.Row));

    // IOnlyStyleBuilder is what Hover/Focus/Active pass to the callback.
    public static IOnlyStyleBuilder CardHover(this IOnlyStyleBuilder builder) =>
        builder.ShadowLg().Background(Color.Blue50);
}

// usage
Css.Card().StackOnMd();
Css.Hover(h => h.CardHover());