Majorsoft.Blazor.Components.Toggle 1.6.1

dotnet add package Majorsoft.Blazor.Components.Toggle --version 1.6.1
                    
NuGet\Install-Package Majorsoft.Blazor.Components.Toggle -Version 1.6.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Majorsoft.Blazor.Components.Toggle" Version="1.6.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Majorsoft.Blazor.Components.Toggle" Version="1.6.1" />
                    
Directory.Packages.props
<PackageReference Include="Majorsoft.Blazor.Components.Toggle" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Majorsoft.Blazor.Components.Toggle --version 1.6.1
                    
#r "nuget: Majorsoft.Blazor.Components.Toggle, 1.6.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Majorsoft.Blazor.Components.Toggle@1.6.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Majorsoft.Blazor.Components.Toggle&version=1.6.1
                    
Install as a Cake Addin
#tool nuget:?package=Majorsoft.Blazor.Components.Toggle&version=1.6.1
                    
Install as a Cake Tool

Blazor Toggle Components

Build Status Package Version NuGet Downloads License

About

Blazor component that renders customizable Toggle Switch and Toggle Button components. All components work with WebAssembly and Server hosted models. For code examples see usage.

You can try it out by using the demo app.

Components

  • ToggleSwitch: Renders HTML <input> styled as Toggle switch with customizable size and color, etc.
  • ToggleButton: Renders HTML <button> styled as Toggle button with custom content and customizable size, color, etc.
  • ToggleButtonGroup: Renders a container for HTML <button> styled as Toggle button with custom content and customizable size, color, etc. Also allows only one toggle button to be Checked.

ToggleSwitch component (See: demo app)

Blazor component to represent a boolean value as an ON/OFF toggle switch. Toggle switch demo

Properties

  • Checked: bool { get; set; } (default: true) - Required <br /> Represents Toggle switch value: ON: true, OFF: false.
  • OnColor: string { get; set; } (default: "blue") - Required <br /> Sets the style of the HTML <input> background-color when switch is ON (bool value true). Use HTML specified: Color Names, RGB, HEX or with HSL values.
  • OffColor: string { get; set; } (default: "darkgray") - Required <br /> Sets the style of the HTML <input> background-color when switch is OFF (bool value false). Use HTML specified: Color Names, RGB, HEX or with HSL values.
  • Height: int { get; set; } (default: 30) - Required <br /> HTML <input> element height in px.
  • Width: int { get; set; } (default: 80) - Required <br /> HTML <input> element width in px.
  • Disabled: bool { get; set; } (default: false) <br /> Determines whether the rendered HTML <input> element should be disabled or not.
  • HandleStyle: ToggleSwitchStyle { get; set; } (default: ToggleSwitchStyle.Circle) <br /> Renders Toggle switch handle with different shaped styles. { Ellipse, Circle, Square }
  • InnerElementReference: ElementReference { get; } <br /> Exposes a Blazor ElementReference of the wrapped around HTML element. It can be used e.g. for JS interop, etc.

Arbitrary HTML attributes e.g.: tabindex="1" will be passed to the corresponding rendered HTML element <input>.

Events

  • OnToggleChanged: EventCallback<bool> delegate <br /> Callback function called when component toggled. Actual toggle Value is the callback bool parameter.

ToggleButton component (See: demo app)

Blazor component to represent a boolean value as an ON/OFF toggle switch as a button and custom content. Toggle button demo

Properties

  • Content: RenderFragment HTML content - Required Required HTML content to show in Toggle button. It can be any text or image (please use transparent background image).
  • Checked: bool { get; set; } (default: true) - Required <br /> Represents Toggle button value: ON: true, OFF: false.
  • OnColor: string { get; set; } (default: "white") - Required <br /> Sets the style of the HTML <button> background-color when button is ON (bool value true). Use HTML specified: Color Names, RGB, HEX or with HSL values.
  • OffColor: string { get; set; } (default: "lightgray") - Required <br /> Sets the style of the HTML <button> background-color when button is OFF (bool value false). Use HTML specified: Color Names, RGB, HEX or with HSL values.
  • HoverColor: string { get; set; } (default: "whitesmoke") - Required <br /> Sets the style of the HTML <button> background-color when button is hovered over with mouse. Use HTML specified: Color Names, RGB, HEX or with HSL values.
  • Height: int { get; set; } (default: 30) - Required <br /> HTML <button> element height in px.
  • Width: int { get; set; } (default: 80) - Required <br /> HTML <button> element width in px.
  • Disabled: bool { get; set; } (default: false) <br /> Determines whether the rendered HTML <button> element should be disabled or not.
  • InnerElementReference: ElementReference { get; } <br /> Exposes a Blazor ElementReference of the wrapped around HTML element. It can be used e.g. for JS interop, etc.

Events

  • OnToggleChanged: EventCallback<bool> delegate <br /> Callback function called when component toggled. Actual toggle Value is the callback bool parameter.

Arbitrary HTML attributes e.g.: tabindex="1" will be passed to the corresponding rendered HTML element <input>.

ToggleButtonGroup component (See: demo app)

Toggle button group demo

Properties

  • ToggleButtons: RenderFragment HTML content - Required Required list of ToggleButtons components. See usage example.
  • ActiveButton: ToggleButton? { get; set; } (default: NULL) Returns currently active "toggled" button element ref also can be used to set which button should be active "toggled".
  • Buttons: IEnumerable<ToggleButton> { get; } Returns all the ToggleButton reference added to the group. It can be used for activating any of the elements.
  • ButtonCount: int { get; } Returns the number of ToggleButton int the given ToggleButtonGroup.
  • MustToggled: bool { get; set; } (default: false) <br /> Determines whether at least one ToggleButton must be toggled all the time or all buttons can be Off.
  • Disabled: bool { get; set; } (default: false) <br /> Determines whether all the rendered HTML <button> elements should be disabled or not.
  • InnerElementReference: ElementReference { get; } <br /> Exposes a Blazor ElementReference of the wrapped around HTML element. It can be used e.g. for JS interop, etc.

Events

  • OnToggleChanged: EventCallback<ToggleButton> delegate <br /> Callback function called when component toggled. Actual toggled (selected) button is the callback parameter. When nothing selected value is NULL.

Arbitrary HTML attributes e.g.: tabindex="1" will be passed to the corresponding rendered HTML element <input>.

Configuration

Installation

Majorsoft.Blazor.Components.Toggle is available on NuGet.

dotnet add package Majorsoft.Blazor.Components.Toggle

Use the --version option to specify a preview version to install.

Usage

Add using statement to your Blazor <component/page>.razor file. Or globally reference it into _Imports.razor file.

@using Majorsoft.Blazor.Components.Toggle

ToggleSwitch usage

Following code example shows how to use ToggleSwitch component in your Blazor App.

<ToggleSwitch
	@ref="_toggleSwitch"
	Checked="_value"
	OnColor="@_onColor"
	OffColor="@_offColor"
	Width="_widht"
	Height="_height"
	HandleStyle="_styleType"
	Disabled="_disabled"
	OnToggleChanged="OnToggleSwitched">
</ToggleSwitch>

@code {
	private ToggleSwitch _toggleSwitch;
	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			await _toggleSwitch.InnerElementReference.FocusAsync();
		}
	}

	private string _onColor = "DodgerBlue";
	private string _offColor = "DarkGray";
	private int _widht = 50;
	private int _height = 22;
	private bool _value = true;
	private bool _disabled = false;
	private ToggleSwitchStyle _styleType = ToggleSwitchStyle.Circle;

	private ElementReference log1;
	private string _swithch1Log;

	private async Task OnToggleSwitched(bool val)
	{
		_value = val;
	}
}

ToggleButton usage

Following code example shows how to use ToggleButton component in your Blazor App.

<ToggleButton @ref="_toggleButton"
				Checked="@_isButtonChecked"
				OnColor="@_buttonOnColor"
				OffColor="@_buttonOffColor"
				HoverColor="@_buttonHoverColor"
				Width="@_buttonWidth"
				Height="@_buttonHeight"
				Disabled="@_buttonDisabled"
				OnToggleChanged="OnToggleClicked">
	<Content>
		<img src="https://raw.githubusercontent.com/majorimi/blazor-components/master/src/Blazor.Components.TestApps.Common/wwwroot/place-marker.png" width="@(_buttonWidth - 5)px" height="@(_buttonHeight - 5)px" />
	</Content>
</ToggleButton>

<ToggleButton>
	<Content><strong>B</strong></Content>
</ToggleButton>
<ToggleButton>
	<Content><i>I</i></Content>
</ToggleButton>
<ToggleButton>
	<Content><u>U</u></Content>
</ToggleButton>

@code {
	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			await _toggleButton.InnerElementReference.FocusAsync();
		}
	}

	//Button
	private ToggleButton _toggleButton;

	private string _buttonOnColor = "lightGray";
	private string _buttonOffColor = "white";
	private string _buttonHoverColor = "WhiteSmoke";
	private int _buttonWidth = 30;
	private int _buttonHeight = 30;
	private bool _isButtonChecked = true;
	private bool _buttonDisabled = false;

	private async Task OnToggleClicked(bool val)
	{
		_isButtonChecked = val;
	}
}

ToggleButtonGroup usage

Following code example shows how to use ToggleButtonGroup component in your Blazor App.

@*ToggleButtons can be configured as regular ToggleButton*@
<ToggleButtonGroup @ref="_btnGroup" MustToggled="_mustToggled" OnToggleChanged="OnToggleGroupClicked"
			Disabled="@_buttonGroupDisabled" ActiveButton="@_activeButton">
	<ToggleButtons>
		<ToggleButton @ref="_btn1">
			<Content><strong>1</strong></Content>
		</ToggleButton>
		<ToggleButton @ref="_btn2">
			<Content><strong>2</strong></Content>
		</ToggleButton>
		<ToggleButton @ref="_btn3">
			<Content><strong>3</strong></Content>
		</ToggleButton>
	</ToggleButtons>
</ToggleButtonGroup>

@code {
	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			_activeButton = _btn1;
			_btnCount = _btnGroup.ButtonCount;
			StateHasChanged();
		}
	}

	private ToggleButtonGroup _btnGroup;
	private ToggleButton _activeButton;
	private ToggleButton _btn1;
	private ToggleButton _btn2;
	private ToggleButton _btn3;
	private bool _buttonGroupDisabled = false;
	private bool _mustToggled = false;
	private int _btnCount = 0;

	private ElementReference _log3;
	private string _buttonGroupLog;

	private async Task OnToggleGroupClicked(ToggleButton active)
	{
		_activeButton = active;
		var index = _btnGroup.Buttons.ToList().IndexOf(active);
	}
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Majorsoft.Blazor.Components.Toggle:

Package Downloads
Majorsoft.Blazor.Components.GdprConsent

Blazor Components by Imre Toth

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.6.1 43 3/27/2026
1.6.0 37 3/26/2026
1.5.0 28,529 8/27/2021
1.4.0 903 7/15/2021
1.3.0 1,380 3/10/2021
1.2.0 769 1/5/2021
1.1.0 687 12/18/2020