Switch
The Switch input component — sometimes called "toggle" — offers a choice between one of two values; it's a great option when you want the user to toggle a feature on or off. This component implements the W3C ARIA APG Switch Pattern. Use it with the Field component to access all the functionalities.
Examples
Base
The default value of a checkbox is false when unchecked and true wenn checked. Use the true-value and false-value properties to specify different value that will be used as the modelValue property when the specific radio is checked and unchecked
Show code
<section>
<o-field>
<o-switch label="Default" />
</o-field>
<o-field>
<o-switch v-model="isSwitched">
{{ isSwitched }}
</o-switch>
</o-field>
<o-field>
<o-switch
v-model="isSwitchedCustom"
true-value="Yes"
false-value="No">
{{ isSwitchedCustom }}
</o-switch>
</o-field>
<o-field>
<o-switch disabled>Disabled</o-switch>
</o-field>
</section>
import { ref } from "vue";
const isSwitched = ref(false);
const isSwitchedCustom = ref("Yes");
Variants
Different styles can be achieved with the variant prop.
Show code
<section>
<o-field>
<o-switch :model-value="true" label="Default" />
</o-field>
<o-field>
<o-switch :model-value="true" variant="primary" label="Primary" />
</o-field>
<o-field>
<o-switch
:model-value="true"
variant="secondary"
label="Secondary" />
</o-field>
<o-field>
<o-switch :model-value="true" variant="info" label="Info" />
</o-field>
<o-field>
<o-switch :model-value="true" variant="success" label="Success" />
</o-field>
<o-field>
<o-switch :model-value="true" variant="warning" label="Warning" />
</o-field>
<o-field>
<o-switch :model-value="true" variant="danger" label="Danger" />
</o-field>
</section>
Sizes
The component can be displayed in different sizes using the size prop.
Show code
<section>
<o-field>
<o-switch size="small" label="Small" />
</o-field>
<o-field>
<o-switch label="Default" />
</o-field>
<o-field>
<o-switch size="medium" label="Medium" />
</o-field>
<o-field>
<o-switch size="large" label="Large" />
</o-field>
</section>
Customise
By default, the component supports a label which is displayed to the right of the toggle unless the position prop is set to "left".
Show code
<section>
<o-field grouped>
<o-switch v-model="isRounded" label="Rounded" />
<o-switch
v-model="position"
true-value="left"
false-value="right"
label="Label on left" />
</o-field>
<o-field grouped>
<o-field label="Variant">
<o-select v-model="variant" expanded placeholder="Variant">
<option :value="undefined">Default</option>
<option value="primary">Primary</option>
<option value="secondary">Secondary</option>
<option value="success">Success</option>
<option value="info">Info</option>
<option value="warning">Warning</option>
<option value="danger">Danger</option>
</o-select>
</o-field>
<o-field label="Passive Variant">
<o-select
v-model="passive"
expanded
placeholder="Passive Variant">
<option :value="undefined">Default</option>
<option value="primary">Primary</option>
<option value="secondary">Secondary</option>
<option value="success">Success</option>
<option value="info">Info</option>
<option value="warning">Warning</option>
<option value="danger">Danger</option>
</o-select>
</o-field>
<o-field label="Size">
<o-select v-model="size" expanded>
<option value="">Default</option>
<option value="small">small</option>
<option value="medium">medium</option>
<option value="large">large</option>
</o-select>
</o-field>
</o-field>
<o-switch
:rounded="isRounded"
:position="position"
:size="size"
:variant="variant"
:passive-variant="passive"
label="Sample" />
</section>
import { ref } from "vue";
const size = ref("");
const variant = ref<string>();
const passive = ref<string>();
const isRounded = ref(false);
const position = ref("right");
Switch Component
Switch between two opposing states.
<o-switch></o-switch>Props
| Prop name | Description | Type | Values | Default |
|---|---|---|---|---|
| autocomplete | Same as native autocomplete options to use in HTML5 validation | string | - | From config: switch: { |
| customValidity | Custom HTML 5 validation error to set on the form control | string | ((currentValue: unknown | null , state: ValidityState) => string) | undefined | - | |
| disabled | Same as native disabled | boolean | - | false |
| falseValue | Overrides the returned value when it's not checked | unknown | - | |
| id | Same as native id. Also pass the id to a wrapping o-field component. Default is a uuid. | string | - | useId() |
| label | Input label, unnecessary when default slot is used | string | - | |
| v-model | The input value state, use v-model to make it two-way binding | unknown | - | |
| name | Name attribute on native checkbox | string | - | |
| nativeValue | Same as native value | unknown | - | |
| override | Override existing theme classes completely | boolean | - | |
| passiveVariant | Color of the switch when is passive | string | primary, info, success, warning, danger, and any other custom color | From config: switch: { |
| position | Label position | string | - | From config: switch: { |
| required | Same as native required | boolean | - | false |
| rounded | Rounded style | boolean | - | From config: switch: { |
| size | Size of the control | string | small, medium, large | From config: switch: { |
| trueValue | Overrides the returned value when it's checked | unknown | - | |
| useHtml5Validation | Enable html 5 native validation | boolean | - | From config: { |
| variant | Color variant of the control | string | primary, info, success, warning, danger, and any other custom color | From config: switch: { |
Events
| Event name | Properties | Description |
|---|---|---|
| update:model-value | value unknown - updated modelValue prop | modelValue prop two-way binding |
| input | value unknown - input valueevent Event - native event | on input change event |
| focus | event Event - native event | on input focus event |
| blur | event Event - native event | on input blur event |
| invalid | event Event - native event | on input invalid event |
Slots
| Name | Description | Bindings |
|---|---|---|
| default | Override the label, default is label prop |
Class Inspector
| Class prop | Description | Props | Suffixes | |
|---|---|---|---|---|
| rootClass | Class of the root element. | |||
| checkedClass | Class of the root element when checked. | modelValue | ||
| sizeClass | Class of the root element with size. | size | small | |
| variantClass | Class of the root element with variant. | variant | primary | |
| passiveVariantClass | Class of the root element with passive variant. | passiveVariant | primary | |
| positionClass | Class of the root element with position. | position | ||
| disabledClass | Class of the root element when disabled. | disabled | ||
| roundedClass | Class of the root element when rounded. | rounded | ||
| inputClass | Class of the native input element. | |||
| labelClass | Class of the switch label element. |
Sass Variables
Current theme ➜ Oruga
| SASS Variable | Default |
|---|---|
| $switch-spacer | h.useVar("control-spacer") |
| $switch-width | 2.75em |
| $switch-height | 1.5em |
| $switch-color | h.useVar("font-color") |
| $switch-font-size | h.useVar("font-size") |
| $switch-font-weight | h.useVar("font-weight") |
| $switch-line-height | h.useVar("line-height") |
| $switch-box-shadow | h.useVar("control-box-shadow-inset") |
| $switch-border-color | h.useVar("grey-lighter") |
| $switch-border-style | solid |
| $switch-border-width | h.useVar("control-border-width") |
| $switch-border-radius | h.useVar("border-radius") |
| $switch-border-radius-rounded | h.useVar("border-radius-rounded") |
| $switch-background-color | h.useVar("grey-light") |
| $switch-toggle-background-color | vars.$control-brackground-color |
| $switch-checked-background-color | h.useVar("primary") |
| $switch-checked-border-color | h.useVar("primary") |
| $switch-disabled-opacity | h.useVar("control-disabled-opacity") |
See ➜ 📄 SCSS file
