Skip to content
Layout Theme

Theme

Cascades Ui overrides to every component of a type below it, without touching each instance.

On this page

Examples

One component type

Instance Ui and Class still win over the theme. A theme does not cross a render mode boundary: repeat it inside an interactive island, or use AddKlyrooUi.

Razor
@using Klyroo.UI.Theming
<div class="flex flex-wrap items-center gap-3">
    <KlyButton Label="Default" data-testid="theme-default" />
    <KlyTheme For="typeof(KlyButton)" Ui="_bold">
        <KlyButton Label="Themed" data-testid="theme-button" />
        <KlyButton Label="Instance wins" Ui='new ButtonUi { Base = "font-normal normal-case tracking-normal" }' data-testid="theme-instance" />
    </KlyTheme>
</div>

@code {
    private readonly ButtonUi _bold = new() { Base = "font-extrabold uppercase tracking-wider" };
}

Several types, nested

Outer
Razor
@using Klyroo.UI.Theming
@* One KlyTheme can carry several component types, and themes nest: the innermost wins. *@
<KlyTheme Overrides="_outer">
    <div class="flex flex-wrap items-center gap-3" data-testid="theme-outer">
        <KlyButton Label="Outer" />
        <KlyBadge Label="Outer" />
        <KlyTheme For="typeof(KlyButton)" Ui='new ButtonUi { Base = "rounded-none" }'>
            <KlyButton Label="Inner" data-testid="theme-inner" />
        </KlyTheme>
    </div>
</KlyTheme>

@code {
    private readonly Dictionary<Type, ISlotOverrides> _outer = new()
    {
        [typeof(KlyButton)] = new ButtonUi { Base = "rounded-full" },
        [typeof(KlyBadge)] = new BadgeUi { Base = "rounded-full" },
    };
}

API

KlyTheme

Cascades class overrides to every component of a given type below it, merged after the theme and before each instance.

Parameters

Name Type Description
For Type The component type the Ui applies to: typeof(KlyButton) , or typeof(KlyTable<>) for a generic one.
Overrides IReadOnlyDictionary<Type, ISlotOverrides> Overrides for several component types at once. They win over For and Ui on a clash.
Ui ISlotOverrides The overrides for For : any *Ui record, such as new ButtonUi { Base = "font-bold" } . Slots the component does not have are ignored.

Slots

Name Type Description
ChildContent RenderFragment The content whose components receive the overrides.