Skip to content
Elements Button

Button

A button, or a link styled as one, in every color and variant.

On this page

Examples

Variants

Razor
<div class="flex flex-col gap-3">
    @foreach (var color in Colors)
    {
        <div class="flex flex-wrap gap-2">
            @foreach (var variant in Enum.GetValues<ButtonVariant>())
            {
                <KlyButton Color="color" Variant="variant" Label="@($"{color} {variant}")" />
            }
        </div>
    }
</div>

@code {
    private static readonly KlyColor[] Colors =
        [KlyColor.Primary, KlyColor.Secondary, KlyColor.Success, KlyColor.Info, KlyColor.Warning, KlyColor.Error, KlyColor.Neutral];
}

Sizes and icons

Razor
<div class="flex flex-wrap items-center gap-2">
    @foreach (var size in Enum.GetValues<KlySize>())
    {
        <KlyButton Size="size" Label="@size.ToString()" />
    }
    <KlyButton Label="Download" Variant="ButtonVariant.Outline">
        <LeadingIcon><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 3v12m0 0-4-4m4 4 4-4M5 21h14" /></svg></LeadingIcon>
    </KlyButton>
    <KlyButton Square aria-label="Add" Color="KlyColor.Neutral">
        <LeadingIcon><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 5v14M5 12h14" /></svg></LeadingIcon>
    </KlyButton>
</div>

States

A button with OnClick stays disabled until the page is interactive.

Read the docs
Razor
<div class="flex w-full max-w-md flex-col gap-3">
    <div class="flex flex-wrap items-center gap-2">
        <KlyButton Label="@($"Clicked {_clicks} times")" OnClick="() => _clicks++" data-testid="counter" />
        <KlyButton Label="Disabled" Disabled />
        <KlyButton Label="Saving" Loading />
        <KlyButton Label="Read the docs" Href="/docs" Variant="ButtonVariant.Link" />
    </div>
    <KlyButton Label="Full width" Block Variant="ButtonVariant.Soft" />
</div>

@code {
    private int _clicks;
}

API

KlyButton

Button, rendered as a <button> , or as a link when Href is set. Works in static server rendering: a submit button or a link needs no interactivity.

Parameters

Name Type Default Description
Block bool false Full width.
Class string Classes added to the root element, merged last so they win over the theme.
Color KlyColor "primary" Semantic color of the button: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else primary. Values: primary, secondary, success, info, warning, error, neutral.
Disabled bool false Disables the button.
Href string Address to link to. When set the button renders as a link.
Label string Text of the button. Use ChildContent for markup.
Loading bool false Shows a spinner in place of the leading icon and disables the button.
Size KlySize KlySize.Md Size of the button, from Xs to Xl . When not set, Size applies, else medium. Values: Xs, Sm, Md, Lg, Xl.
Square bool false Equal padding on every side, for icon-only buttons (give them an aria-label ).
Target KlyLinkTarget? The target attribute of the link, such as Blank . It applies when Href is set.
Type ButtonType ButtonType.Button The type attribute of the button: Button (the default), Submit or Reset . Values: Button, Submit, Reset.
Ui ButtonUi Per-instance class overrides of the slots of the button, see ButtonUi .
Variant ButtonVariant ButtonVariant.Solid Look of the button: Solid , Outline , Soft , Subtle , Ghost or Link . Values: Solid, Outline, Soft, Subtle, Ghost, Link.

Events

Name Type Description
OnClick EventCallback<MouseEventArgs> Raised when the button is clicked. The button stays disabled until the page is interactive when this is set.

Slots

Name Type Description
LeadingIcon RenderFragment Icon before the label, as markup such as a KlyIcon . Replaced by a spinner while Loading .
TrailingIcon RenderFragment Icon after the label, as markup such as a KlyIcon .
ChildContent RenderFragment Content of the button, instead of Label .

Ui slots

Ui takes a ButtonUi with one class string per slot, merged after the theme.

Slot Styles
Base Classes merged into the base slot of KlyButton , after the theme and the app configuration.
Label Classes merged into the label slot of KlyButton , after the theme and the app configuration.
LeadingIcon Classes merged into the leadingIcon slot of KlyButton , after the theme and the app configuration.
TrailingIcon Classes merged into the trailingIcon slot of KlyButton , after the theme and the app configuration.