+ With toggle switches, users can select the one or off state of an option. +
-A - toggle switch changes on and off states of a single setting.
+Angular Toggle Switch Component
-To create a toggle switch, wrap an input[type="checkbox"]
- element and a label
element in a block-level wrapper
- (like a div
) with the .toggle-switch
- classname. To use a toggle without any label, keep the label
- tag empty. The default toggle is gray in its “off” state and green in
- its “on” state. The user can change states by clicking on either the toggle or its
- accompanying label. You can move the label to the right side by adding the right-label
class.
If you are using Angular, the recommended approach is to always use the ClrToggle
+ directive on your toggle switches to help manage the form control. By using ClrToggle
,
+ you'll automatically be able to leverage built in validation, helper text, and layout features in forms.
Disabled Toggles
+Basic checkbox button
-To disable a toggle switch, add the disabled
attribute to the
- toggle’s checkbox
input. When disabled, the toggle appears
- gray. The user cannot change the state of the toggle. And the present state is reflected
- in both the location of the toggle switch and by the “on” state being a slightly darker
- gray.
While it is possible to create a toggle switch without a label, the cases are rare. However, it is possible
+ by just adding clrToggle
to your checkbox button to wire up the
+ directive. Notice, it is necessary to add type="checkbox"
.
Labels
+ +For anything beyond a standalone checkbox button, you'll need to wrap your toggle switches with the ClrToggleWrapper
+ component when you include a label. This manages the label and display of the toggle switch for you.
+
Helper and error messages
+ +To leverage helper and validation messages, you'll need to wrap all of the toggle switches inside of a ClrToggleContainer
+ component. The toggle switch container tracks the the validations placed on a toggle switch, such as required
+ or even custom built validators in Angular. Under the hood, it looks at the NgControl
+ values to determine the control validity, and display the helper text or error message accordingly.
Note: the validation in this example only displays an error after you focus and leave focus without + selecting a value. The easiest way to do this is to highlight the text in this paragraph, and hit tab a few + times to change the focus.
+ + + +Inline toggle switches
+ +Toggle switches can be placed inline by default by adding the clrInline
directive
+ to your toggle switch container elements. The toggle switches will wrap if there is not enough space.
Disabled toggle switches
+ +A toggle switch can be disabled by simply putting the disabled
attribute on the
+ checkbox input. This does require the toggle switch to be inside of a toggle switch container. Also, for groups of
+ toggle switches you have to put the disabled attribute on the last checkbox input and cannot disable individual ones,
+ as Angular doesn't have support to disable individual checkboxes in a group.
CSS Toggle Switch Component
+ +Toggle switches can be used without Angular, but without some of the automatic interactivity. If you intend to + create + your own components, here are the basic features you'll need to build out.
+ +Basic toggle switch button
+ +This is the minimal markup needed to style the checkbox button control by wrapping it with the an element with
+ the
+ clr-toggle-wrapper
class. You must then put the input first, then the label
+ like you see below (the CSS selectors are specific about this ordering). You'll want to tie the input and
+ label together by providing an id for the label to connect. Ensure you add the clr-toggle
+ class to the checkbox input as well.
Full input display
+ +Each form control has a .clr-form-control
wrapper, as well as a specific DOM
+ structure necessary to layout the form control as desired. This works with all layout options as well
+ without needing to change the DOM structure in any way.
Error state
+ +To signify an error state, you need to add the .clr-error
class to the .clr-control-container
element. This will show the .clr-validate-icon
+ and turn the .clr-subtext
message to red. If you have helper text that needs
+ to change to error content, you'll have to handle the hiding and showing of that content inside of the
+ .clr-subtext
element.
Usage
+ -Use a toggle switch when you need the sole options of “on” and “off.”
+Toggle switches take up less space than an “on/off” radio button group and communicate their intended - purpose better than a checkbox - that toggles functionality.
+Inline toggle switches
-Toggle States
+Toggle switches can be placed inline by default by adding the clr-control-inline
directive
+ to your checkbox container. The toggle switches will wrap if there is not enough space.
Toggle switches do not include “ON” and “OFF” text because these states are clearly implied.
+The subtext content, the icon and subtext messages, also must be wrapped in a clr-subtext-wrapper
+ element so it can properly layout in all layout variations. If you omit this, the validation icon and text may appear in the wrong place.
Label
+ -The label should clearly describe the setting. Although Clarity supports a toggle - switch without a label, use this approach only if the purpose of the control is made - clear elsewhere, for example, in a group label or section header.
+Disabled toggle switches
+ +In order to disable toggle switches, you'll add the disabled
attribute on the checkbox input and
+ putting the clr-form-control-disabled
class onto the form control wrapper. This
+ helps setup the correct styling for the labels and other aspects of the form control when it is disabled.
Usage
+ +Use a toggle switch when you need the sole options of “on” and “off.”
+ +Toggle switches take up less space than an “on/off” radio button group and communicate their intended purpose better than a checkbox that toggles functionality.
+ +Toggle States
+ +Toggle switches do not include “ON” and “OFF” text because these states are clearly implied.
+ +Label
+ +The label should clearly describe the setting. Although Clarity supports a toggle switch without a label, use this approach only if the purpose of the control is made clear elsewhere, for example, in a group label or section header.
+