Skip to main content

LgToggle

LgToggle is a toggle switch linked to a boolean value. It displays different text for each state (TextOn / TextOff) and an optional label on the left or right of the button.

<LgToggle Text="#Notifications" @bind-Value="_model.NotificationsEnabled" />
  • Namespace: Lagoon.UI.Components
  • Inherits from: LgInputBase<bool> → LgAriaComponentBase → LgComponentBase
  • NuGet package: Lagoon.UI

Usage​

Demonstration​

Try this component in the Lagoon showcase.

Simple toggle​

Text is the label displayed next to the toggle switch; TextPosition places it on the left (default) or right.

SettingsPage.razor
<LgToggle Text="#Notifications"
TextPosition="ToggleTextPosition.Right"
@bind-Value="_model.NotificationsEnabled" />
SettingsPage.razor.cs
public partial class SettingsPage : LgPage
{
private readonly SettingsVm _model = new();

private sealed class SettingsVm
{
public bool NotificationsEnabled { get; set; }
}
}

State text​

TextOn and TextOff are displayed in the toggle switch according to the state. In read-only mode (ReadOnly), the text of the current state is rendered.

PublicationPage.razor
<LgToggle Text="#Visibility"
TextOn="#Published"
TextOff="#DraftState"
@bind-Value="_model.IsPublished" />
PublicationPage.razor.cs
public partial class PublicationPage : LgPage
{
private readonly PublicationVm _model = new();

private sealed class PublicationVm
{
public bool IsPublished { get; set; }
}
}

Cancel or confirm the toggle​

OnChangingAsync is invoked before the change and allows canceling it (args.Cancel); args.Item returns the object passed to the Item parameter, useful when the toggle switch is repeated in a list. ConfirmationMessage (inherited) displays a confirmation prompt before the toggle.

UserListPage.razor
@foreach (UserVm user in _users)
{
<LgToggle Text="@user.Name"
Item="user"
ConfirmationMessage="#ConfirmToggleUser"
OnChangingAsync="OnActiveChangingAsync"
@bind-Value="user.IsActive" />
}
UserListPage.razor.cs
public partial class UserListPage : LgPage
{
// Displayed users: each toggle carries its user via Item.
private readonly List<UserVm> _users = new();

// Called before toggle: prevents disabling the last administrator.
private Task OnActiveChangingAsync(ChangingItemEventArgs<bool> args)
{
if (args.Item is UserVm user && user.IsLastAdmin && !args.NewValue)
{
args.Cancel = true;
ShowWarning("#CannotDisableLastAdmin");
}
return Task.CompletedTask;
}

private sealed class UserVm
{
public string Name { get; set; }
public bool IsActive { get; set; }
public bool IsLastAdmin { get; set; }
}
}

Properties​

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

NameTypeDefaultDeclared inDescription
ItemobjectnullLgToggleData associated with the control, returned in ChangingItemEventArgs.Item.
OnChangingAsyncFunc<ChangingItemEventArgs<bool>, Task>nullLgToggleCallback invoked before the value changes; the change can be canceled there.
TextstringnullLgToggleLabel displayed next to the toggle switch.
TextOffstringnullLgToggleText displayed in the toggle switch in the "off" state.
TextOnstringnullLgToggleText displayed in the toggle switch in the "on" state.
TextPositionToggleTextPositionLeftLgTogglePosition of the Text label relative to the toggle switch.
AdditionalAttributesIReadOnlyDictionary<string, object>nullLgInputBaseAdditional HTML attributes applied to the rendered element.
AutoFocusboolfalseLgInputBaseFocuses the control on first render.
ConfirmationMessagestringnullLgInputBaseConfirmation message displayed before applying the toggle.
ContainerIInputContainer<bool>nullLgInputBaseInternal use: attaches a grid cell to the control.
CssClassstringnullLgInputBaseAdditional CSS classes.
DisabledboolfalseLgInputBaseDisables the control.
DisplayNamestringnullLgInputBaseField name used in error messages.
HideValidationMessageboolfalseLgInputBaseHides the validation message below the control.
LabelstringnullLgInputBaseField label (standard label area for form fields).
LabelContentRenderFragmentnullLgInputBaseCustom label area content.
OnChangeEventCallback<ChangeEventArgs>—LgInputBaseCallback invoked after the value changes.
OnResetEventCallback<ChangeEventArgs>—LgInputBaseCallback invoked when the control is reset.
PolicyEditstringnullLgInputBasePolicy required to modify the control.
PolicyVisiblestringnullLgInputBasePolicy required to display the control.
ReadOnlyboolfalseLgInputBaseDisplays the current state text in read-only mode.
Requiredbool?nullLgInputBaseRequired field. If null, inferred from the [Required] attribute of the bound property.
ResetButtonbool?nullLgInputBaseDisplays the reset button. If null, the application's behavior configuration applies.
ResetTextstringnullLgInputBaseReset button text.
ResetTextAriaLabelstringnullLgInputBaseAccessible label (aria-label) for the reset button.
TooltipstringnullLgInputBaseTooltip (applied to the Text label).
TooltipIsHtmlboolfalseLgInputBaseIndicates whether the Tooltip content is in HTML format (otherwise plain text).
ValueboolfalseLgInputBaseBound value (@bind-Value).
ValueChangedEventCallback<bool>—LgInputBaseCallback for updating the bound value.
ValueExpressionExpression<Func<bool>>—LgInputBaseExpression identifying the bound property.
AriaLabelstringnullLgAriaComponentBaseValue of the aria-label attribute.
AriaLabelledBystringnullLgAriaComponentBaseValue of the aria-labelledby attribute.
Cascading parameters

LgToggle also consumes parameters automatically provided by ancestor components (you do not need to define them): ParentAuthorizeView (LgAuthorizeView) and the editing context of a parent LgEditForm.

Events​

EventTypeDescription
OnChangingAsyncFunc<ChangingItemEventArgs<bool>, Task>Before toggle; args.Cancel = true cancels it, args.Item returns the associated data.
OnChangeEventCallback<ChangeEventArgs>After toggle.
OnResetEventCallback<ChangeEventArgs>On control reset.

Associated enumerations​

ToggleTextPosition​

Position of the Text label relative to the toggle switch.

ValueDescription
LeftLabel on the left (default value).
RightLabel on the right.

Best practices / accessibility​

  • Use LgToggle for an action with immediate effect (enable/disable); for a checkbox in a submitted form, prefer LgCheckBox.
  • Provide AriaLabel if no Text is supplied, to give an accessible label to the toggle switch.
Complete reference

All parameters (including inherited ones): API Reference — LgToggle.