Toggle
Description
The Toggle component represents a physical switch that allows users to turn a setting on or off. It is ideal for binary operations that take effect immediately when toggled, such as enabling or disabling services or hardware features. As part of the Components group in Tesserae, Toggle provides a fluent interface for configuring its appearance and behavior while automatically updating its on/off label based on its state.
Usage
The Toggle is instantiated through the Tesserae.UI static helper. You can configure text labels, the checked state, and the enabled/disabled state using its fluent methods or public properties.
Below is an example that demonstrates the basic usage of the Toggle:
Public Properties
Text
- Type: string
- Description: Gets or sets the custom text displayed on the Toggle. When set, it overrides the default on/off labels by hiding the automatic label container if a non-empty string is provided.
IsEnabled
- Type: bool
- Description: Indicates whether the Toggle is enabled. A disabled Toggle will visually appear inactive and will not respond to click events.
IsChecked
- Type: bool
- Description: Represents the current checked state of the Toggle. Changing this value updates the visual indicator and the underlying observable that tracks this state.
API reference
public class Toggle : ComponentBase<Toggle, HTMLInputElement>, IObservableComponent<bool>, IRoundedStyle, ITextFormatingA Toggle component that allows users to switch between two states (on/off).
Constructors
Properties
public bool IsEnabled { get ; set ; }Gets or sets whenever Toggle is enabled
public bool IsChecked { get ; set ; }Gets or sets whenever Toggle is checked
Methods
public override HTMLElement Render()Renders the toggle component.
Returns
The rendered HTMLElement.
public Toggle SetText(string text)Sets the text of the toggle.
Parameters
- text
- The text.
Returns
The current instance of the type.
public Toggle Disabled(bool value = true)Sets whether the toggle is disabled.
Parameters
- value
- Whether to disable the toggle.
Returns
The current instance of the type.
public Toggle Checked(bool value = true)Sets whether the toggle is checked.
Parameters
- value
- Whether to check the toggle.
Returns
The current instance of the type.
Samples
Basic Toggle Usage
This sample demonstrates how to instantiate and configure multiple Toggle states including checked, unchecked, enabled, and disabled toggles. It also shows how to use inline labels.