ion-toggle
Toggles change the state of a single option. Toggles can be switched on or off by pressing or swiping them. They can also be checked programmatically by setting the checked
property.
#
Customization#
Customizing BackgroundThe background of the toggle track and handle can be customized using CSS variables. There are also variables for setting the background differently when the toggle is checked.
Because these variables set the background
property, which is a shorthand, it can accept any value that the background property accepts.
A more complex case may involve adding an image to the handle background.
Taking it a step further, we could use the ::before
or ::after
pseudo-elements to position text on top of the background.
#
Customizing WidthAdjusting the width of the toggle smaller will result in a narrower track, with the handle remaining the default width. If desired, set --handle-width
to make the handle narrower.
Adjusting the width of the toggle larger will result in a wider track, with the handle remaining the default width. If desired, set --handle-width
to make the handle wider.
#
Customizing HeightAdjusting the height of the toggle smaller than the default will result in the handle height auto-sizing itself to the track. In ios
the handle is shorter than the track (100% - 4px
) and in md
the handle is taller than the track (100% + 6px
).
Note: this does not affect the handle width, width should be set using
--handle-width
.
Adjusting the height of the toggle larger will keep the handle in the center at the default height. This can be modified by setting --handle-height
which can be set to any amount but will not exceed the --handle-max-height
.
Note: this does not affect the handle width, width should be set using
--handle-width
.
#
Customizing SpacingThe spacing refers to the horizontal gap between the handle and the track. By default, the handle has 2px
of spacing around it in ios
only. In md
mode there is no default spacing.
To remove the horizontal spacing, set --handle-spacing
to 0px
.
Due to the handle having a fixed height, to remove the spacing on the top and bottom, set the height to 100%.
#
Customizing Border RadiusThe --handle-border-radius
can be used to change the border-radius
on the handle.
To target the border-radius
only when the toggle is checked, target .toggle-checked
:
#
Customizing Box ShadowThe --handle-box-shadow
can be used to change the box-shadow
on the handle.
To target the box shadow only when the toggle is checked, target .toggle-checked
:
See the section on customizing overflow to allow the box-shadow
to overflow the toggle container.
#
Customizing OverflowSetting overflow
on the toggle will be inherited by the toggle handle. By default, overflow is set to hidden
in ios
only. The box-shadow
will still appear cut off due to the contain
css property. Set contain
to none
in order to overflow the toggle container.
#
Usage- ANGULAR
- JAVASCRIPT
- REACT
- STENCIL
- VUE
#
Properties#
checkedDescription | If true , the toggle is selected. |
Attribute | checked |
Type | boolean |
Default | false |
#
colorDescription | The color to use from your application's color palette. Default options are: "primary" , "secondary" , "tertiary" , "success" , "warning" , "danger" , "light" , "medium" , and "dark" .For more information on colors, see theming. |
Attribute | color |
Type | string \| undefined |
Default | undefined |
#
disabledDescription | If true , the user cannot interact with the toggle. |
Attribute | disabled |
Type | boolean |
Default | false |
#
modeDescription | The mode determines which platform styles to use. |
Attribute | mode |
Type | "ios" \| "md" |
Default | undefined |
#
nameDescription | The name of the control, which is submitted with the form data. |
Attribute | name |
Type | string |
Default | this.inputId |
#
valueDescription | The value of the toggle does not mean if it's checked or not, use the checked property for that. The value of a toggle is analogous to the value of a <input type="checkbox"> ,it's only used when the toggle participates in a native <form> . |
Attribute | value |
Type | null \| string \| undefined |
Default | 'on' |
#
EventsName | Description |
---|---|
ionBlur | Emitted when the toggle loses focus. |
ionChange | Emitted when the value property has changed. |
ionFocus | Emitted when the toggle has focus. |
#
CSS Shadow PartsName | Description |
---|---|
handle | The toggle handle, or knob, used to change the checked state. |
track | The background track of the toggle. |
#
CSS Custom PropertiesName | Description |
---|---|
--background | Background of the toggle |
--background-checked | Background of the toggle when checked |
--border-radius | Border radius of the toggle track |
--handle-background | Background of the toggle handle |
--handle-background-checked | Background of the toggle handle when checked |
--handle-border-radius | Border radius of the toggle handle |
--handle-box-shadow | Box shadow of the toggle handle |
--handle-height | Height of the toggle handle |
--handle-max-height | Maximum height of the toggle handle |
--handle-spacing | Horizontal spacing around the toggle handle |
--handle-transition | Transition of the toggle handle |
--handle-width | Width of the toggle handle |