Skip to main content

LgButton

LgButton is the Lagoon button component. It handles user actions (click, keyboard shortcut, command), navigation (URI / LgPageLink), form submission, tooltips, authorization policies, and several style and size variants.

<LgButton Text="My button" IconName="@IconNames.All.Check" />
  • Namespace : Lagoon.UI.Components
  • Inherits from : LgAriaComponentBase → LgComponentBase
  • Interfaces : ILgComponentPolicies, IActionComponent, ILgFocusableComponent
  • NuGet Package : Lagoon.UI
Specialized variants

LgButton serves as the base for several ready-to-use components: LgCopyButton (copy to clipboard) and LgPasteButton (paste from clipboard).

Usage​

Demo​

Try this component in the Lagoon showcase.

Simple button​

Use Text to display the translated button label. If no size is specified, the cascading size is used, then ButtonSize.Medium.

OrderListPage.razor
<LgButton Text="#Refresh" OnClick="RefreshAsync" />
OrderListPage.razor.cs
public partial class OrderListPage : LgPage
{
// Refreshes the displayed data: useful for restarting a search after an explicit user action.
private async Task RefreshAsync(ActionEventArgs args)
{
await Task.Delay(100, args.CancellationToken);
ShowSuccess("#OrderListRefreshed");
}
}

Style, size, icon, and tooltip​

Combine Kind, ButtonSize, IconName, and Tooltip to adjust the visual importance of the action. The keyboard shortcut, if present, is automatically added to the tooltip.

OrderActionsPage.razor
<LgButton Text="#ShowDetails"
Kind="ButtonKind.Secondary"
ButtonSize="ButtonSize.Small"
IconName="@IconNames.All.InfoCircle"
Tooltip="#ShowDetailsTooltip"
TooltipPosition="TooltipPosition.Bottom"
OnClick="ShowDetailsAsync" />
OrderActionsPage.razor.cs
public partial class OrderActionsPage : LgPage
{
// Opens the details of the current item: secondary action, therefore styled as a subtle button.
private Task ShowDetailsAsync(ActionEventArgs args)
{
ShowInformation("#OrderDetailsOpened");
return Task.CompletedTask;
}
}

Disabling and preventing double-click​

Disabled immediately disables the button. PreventDoubleClick defaults to true: during the execution of an asynchronous action, the button is temporarily disabled to avoid concurrent processing.

OrderSavePage.razor
<LgButton Text="#Save"
IconName="@IconNames.Save"
Disabled="@IsLocked"
PreventDoubleClick="true"
OnClick="SaveAsync" />
OrderSavePage.razor.cs
public partial class OrderSavePage : LgPage
{
// Indicates whether the screen is locked to prevent any modifications.
private bool IsLocked { get; set; }

// Persists changes: the button remains inactive while waiting to prevent duplicate saves.
private async Task SaveAsync(ActionEventArgs args)
{
await Task.Delay(100, args.CancellationToken);
ShowSuccess("#OrderSaved");
}
}

Click handling​

The OnClick event exposes ActionEventArgs. Use its CancellationToken to propagate cancellation to long-running operations triggered by the button.

OrderDeletePage.razor
<LgButton Text="#Delete"
Kind="ButtonKind.Error"
IconName="@IconNames.Delete"
OnClick="DeleteAsync" />
OrderDeletePage.razor.cs
public partial class OrderDeletePage : LgPage
{
// Deletes the current item: the token allows cleanly stopping processing if the action is canceled.
private async Task DeleteAsync(ActionEventArgs args)
{
await Task.Delay(100, args.CancellationToken);
ShowSuccess("#OrderDeleted");
}
}

Uri opens a raw route. Target allows opening the URI in another window. Link automatically inherits the title and icon from an LgPageLink if Text or IconName are not defined on the button.

OrderNavigationPage.razor
@* Internal navigation. *@
<LgButton Text="#OpenOrders" Uri="orders" />

@* Open in a new window. *@
<LgButton Text="#OpenHelp" Uri="help/orders" Target="_blank" />

@* Typed navigation to a Lagoon page. *@
<LgButton Link="@OrderEditPage.Link(CurrentOrderId)" />
OrderNavigationPage.razor.cs
public partial class OrderNavigationPage : LgPage
{
// Identifier passed to the typed link to avoid manually rebuilding the URL.
private int CurrentOrderId { get; set; } = 42;
}
Action priority

If Uri or Link provides a URI, the click navigates to that destination. Do not add OnClick to execute business logic in the same button: place that logic in the target page or use a button without a URI.

Form submission button​

In an LgEditForm, IsSubmit="true" renders the button as type="submit" and triggers the parent form submission.

OrderEditPage.razor
<LgEditForm Model="Model" OnValidSubmit="OnValidSubmitAsync">
<LgButton Text="#Save" IconName="@IconNames.Save" IsSubmit="true" />
</LgEditForm>
OrderEditPage.razor.cs
public partial class OrderEditPage : LgPage
{
// Model edited by the form: it serves as the validation and save context.
private OrderVm Model { get; } = new();

// Triggered only when the form is valid: this is where to persist the model.
private async Task OnValidSubmitAsync(EditContext editContext)
{
await Task.Delay(100);
ShowSuccess("#OrderSaved");
}

private sealed class OrderVm
{
public string Reference { get; set; }
}
}
warning

IsSubmit="true" is incompatible with ConfirmationMessage: the confirmation message must then be carried by the LgEditForm, not the button (an InvalidOperationException is thrown otherwise).

Confirmation message​

ConfirmationMessage displays a confirmation request before executing the action or navigation. Do not use it with CommandName.

OrderDeleteConfirmPage.razor
<LgButton Text="#Delete"
Kind="ButtonKind.Error"
IconName="@IconNames.Delete"
ConfirmationMessage="#ConfirmDeleteOrder"
OnClick="DeleteAsync" />
OrderDeleteConfirmPage.razor.cs
public partial class OrderDeleteConfirmPage : LgPage
{
// Executed only after confirmation: protects destructive actions from accidental clicks.
private async Task DeleteAsync(ActionEventArgs args)
{
await Task.Delay(100, args.CancellationToken);
ShowSuccess("#OrderDeleted");
}
}

Keyboard shortcut​

ShortcutKey associates a key with Ctrl. Prefix the key with @ to associate it with Alt instead.

OrderShortcutsPage.razor
@* Ctrl+S. *@
<LgButton Text="#Save" ShortcutKey="s" OnClick="SaveAsync" />

@* Alt+N. *@
<LgButton Text="#NewOrder" ShortcutKey="@@n" OnClick="CreateAsync" />
OrderShortcutsPage.razor.cs
public partial class OrderShortcutsPage : LgPage
{
// Quick save: the shortcut avoids leaving the keyboard during input.
private Task SaveAsync(ActionEventArgs args)
{
ShowSuccess("#OrderSaved");
return Task.CompletedTask;
}

// Initializes a new record: useful for repetitive input screens.
private Task CreateAsync(ActionEventArgs args)
{
ShowInformation("#OrderCreated");
return Task.CompletedTask;
}
}

DOM event behavior​

PreventDefault cancels the default click behavior and StopPropagation prevents the click from bubbling to parent containers. This is useful in a row or panel that is itself clickable.

OrderRowPage.razor
<LgButton Text="#Select"
PreventDefault="true"
StopPropagation="true"
OnClick="SelectAsync" />
OrderRowPage.razor.cs
public partial class OrderRowPage : LgPage
{
// Selects only the button action, without triggering the parent container click.
private Task SelectAsync(ActionEventArgs args)
{
ShowInformation("#OrderSelected");
return Task.CompletedTask;
}
}

Additional attributes, focus, and accessibility​

Unrecognized attributes are passed to the rendered element. Use them for data-* attributes, and provide AriaLabel when a button is primarily identified by its icon. AutoFocus gives focus on first render.

OrderRefreshPage.razor
<LgButton Text="#Refresh"
IconName="@IconNames.Load"
AutoFocus="true"
AriaLabel="#RefreshOrders"
data-testid="refresh-orders"
OnClick="RefreshAsync" />
OrderRefreshPage.razor.cs
public partial class OrderRefreshPage : LgPage
{
// Restarts loading and receives focus on first display to facilitate keyboard usage.
private Task RefreshAsync(ActionEventArgs args)
{
ShowSuccess("#OrderListRefreshed");
return Task.CompletedTask;
}
}

Commands bubbled to parent​

CommandName and CommandArgument allow bubbling an action to a parent that listens for commands, for example an LgToolbar. The command must not be combined with ConfirmationMessage.

OrderToolbarPage.razor
<LgToolbar OnCommand="OnToolbarCommandAsync">
<LgButton Text="#Archive"
CommandName="archive"
CommandArgument="@CurrentOrderId" />
<LgButton Text="#Duplicate"
CommandName="duplicate"
CommandArgument="@CurrentOrderId" />
</LgToolbar>
OrderToolbarPage.razor.cs
public partial class OrderToolbarPage : LgPage
{
// Identifier passed with the command so the parent knows which item to process.
private int CurrentOrderId { get; set; } = 42;

// Centralizes the toolbar actions: useful when multiple buttons share the same processing flow.
private Task OnToolbarCommandAsync(CommandEventArgs args)
{
args.Handled = true;
ShowInformation("#CommandReceived");
return Task.CompletedTask;
}
}

Visibility and activation by policy​

PolicyVisible hides the button if the policy is not satisfied. PolicyEdit leaves it visible but non-editable if the user does not have permission to act.

AdminActionPage.razor
<LgButton Text="#Admin"
PolicyVisible="@Policies.IsAdmin"
PolicyEdit="@Policies.CanEdit"
OnClick="OpenAdminAsync" />
AdminActionPage.razor.cs
public partial class AdminActionPage : LgPage
{
// Opens the administration action only for users authorized by policies.
private Task OpenAdminAsync(ActionEventArgs args)
{
ShowInformation("#AdminOpened");
return Task.CompletedTask;
}
}

Properties​

All [Parameter] properties of the component, including inherited ones.

NameTypeDefaultDeclared inDescription
AdditionalAttributesIReadOnlyDictionary<string, object>nullLgButtonAdditional HTML attributes applied to the rendered element (captures unmapped values).
AutoFocusboolfalseLgButtonGives focus to the button on first render.
ButtonSizeButtonSize?nullLgButtonButton size. If not defined, the cascading value is used, otherwise the default size.
ChildContentRenderFragmentnullLgButtonCustom button content.
CommandArgumentobjectnullLgButtonArgument associated with the command sent to the handler. Use primitive types or string to optimize rendering.
CommandNamestringnullLgButtonName of the command sent to the parent command handler (ICommandListener).
ConfirmationMessagestringnullLgButtonConfirmation message displayed before the action. Incompatible with CommandName and IsSubmit.
CssClassstringnullLgButtonAdditional CSS classes.
DisabledboolfalseLgButtonDisables the button.
IconNamestringnullLgButtonDisplayed icon. Used via IconName="@IconNames.All.MyIcon".
IsSubmitboolfalseLgButtonIf true, the button is of type submit and triggers the parent LgEditForm.
KindButtonKindButtonKind.PrimaryLgButtonStyle / variant of the button.
LinkLgPageLinknullLgButtonLink information to a page. The Text and IconName from the link are used if not defined.
OnClickEventCallback<ActionEventArgs>—LgButtonCallback triggered on button click.
PolicyEditstringnullLgButtonPolicy required to enable (edit) the button.
PolicyVisiblestringnullLgButtonPolicy required to display the button.
PreventDefaultboolfalseLgButtonCancels the default behavior of the click event (@onclick:preventDefault).
PreventDoubleClickbooltrueLgButtonDisables the button during action execution to prevent double-click.
ShortcutKeystringnullLgButtonCharacter of the keyboard shortcut. Associated with Ctrl, or with Alt if prefixed by @.
StopPropagationbooltrueLgButtonStops the propagation of the click event (@onclick:stopPropagation).
TargetstringnullLgButtonName of the browser window in which to open the Uri.
TextstringnullLgButtonButton label.
TooltipstringnullLgButtonTooltip.
TooltipIsHtmlboolfalseLgButtonIndicates whether the Tooltip content is in HTML format (otherwise plain text).
TooltipPositionTooltipPositionTooltipPosition.NoneLgButtonTooltip position.
UristringnullLgButtonURI / route to open on click.
AriaLabelstringnullLgAriaComponentBaseValue of the aria-label attribute.
AriaLabelledBystringnullLgAriaComponentBaseValue of the aria-labelledby attribute.
Cascading parameters

LgButton also consumes parameters automatically provided by ancestor components (you do not have to define them): ParentAuthorizeView (LgAuthorizeView), EditForm (parent LgEditForm, used by IsSubmit), and ButtonSizeCascading (ButtonSize?, default size inherited from an ancestor).

Events​

EventTypeDescription
OnClickEventCallback<ActionEventArgs>Triggered on click (or keyboard shortcut). ActionEventArgs allows canceling or tracking action execution.

Associated enumerations​

ButtonKind​

Visual style of the button (Bootstrap variants).

ValueDescription
PrimaryPrimary style (default value).
SecondarySecondary style.
SuccessSuccess style.
ErrorError style (danger).
WarningWarning style.
InfoInfo style.
LinkLink appearance.
GhostGhost style (transparent background).
CustomCustom style (no variant class imposed).

ButtonSize​

ValueDescription
MediumStandard size (default value).
SmallSmall size.
LargeLarge size.

TooltipPosition​

ValueDescription
NoneUndefined position (default value).
TopAbove.
RightTo the right.
BottomBelow.
LeftTo the left.

Accessibility​

  • Provide AriaLabel (or AriaLabelledBy) when the button displays only an icon to provide an accessible label.
  • The keyboard shortcut configured via ShortcutKey is automatically added to the tooltip.