Skip to content
Oruga
Bulma
Bootstrap
Tailwind

Switch ​

The Switch input component (also known as 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

html
<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>
javascript
import { ref } from "vue";

const isSwitched = ref(false);
const isSwitchedCustom = ref("Yes");

Variants ​

Different styles can be achieved with the variant prop.

html
<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.

html
<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".

html
<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>
javascript
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.

html
<o-switch></o-switch>

Props ​

Prop nameDescriptionTypeValuesDefault
aria-activedescendantIdentifies the currently active element when DOM focus is on a composite widget, textbox, group, or application.string-
aria-atomicIndicates whether assistive technologies will present all, or only parts of, the changed region based on the change notifications defined by the aria-relevant attribute.Booleanish-
aria-autocompleteIndicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be
presented if they are made.
"both" | "inline" | "list" | "none"-
aria-busyIndicates an element is being modified and that assistive technologies MAY want to wait until the modifications are complete before exposing them to the user.Booleanish-
aria-checkedIndicates the current "checked" state of checkboxes, radio buttons, and other widgets."mixed" | Booleanish-
aria-colcountDefines the total number of columns in a table, grid, or treegrid.Numberish-
aria-colindexDefines an element's column index or position with respect to the total number of columns within a table, grid, or treegrid.Numberish-
aria-colspanDefines the number of columns spanned by a cell or gridcell within a table, grid, or treegrid.Numberish-
aria-controlsIdentifies the element (or elements) whose contents or presence are controlled by the current element.string-
aria-currentIndicates the element that represents the current item within a container or set of related elements."date" | "location" | "page" | "step" | "time" | Booleanish-
aria-describedbyIdentifies the element (or elements) that describes the object.string-
aria-detailsIdentifies the element that provides a detailed, extended description for the object.string-
aria-disabledIndicates that the element is perceivable but disabled, so it is not editable or otherwise operable.Booleanish-
aria-dropeffect
deprecated - in ARIA 1.1
Indicates what functions can be performed when a dragged object is released on the drop target.
"copy" | "execute" | "link" | "move" | "none" | "popup"-
aria-errormessageIdentifies the element that provides an error message for the object.string-
aria-expandedIndicates whether the element, or another grouping element it controls, is currently expanded or collapsed.Booleanish-
aria-flowtoIdentifies the next element (or elements) in an alternate reading order of content which, at the user's discretion,
allows assistive technology to override the general default of reading in document source order.
string-
aria-grabbed
deprecated - in ARIA 1.1
Indicates an element's "grabbed" state in a drag-and-drop operation.
Booleanish-
aria-haspopupIndicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element."dialog" | "grid" | "listbox" | "menu" | "tree" | Booleanish-
aria-hiddenIndicates whether the element is exposed to an accessibility API.Booleanish-
aria-invalidIndicates the entered value does not conform to the format expected by the application."grammar" | "spelling" | Booleanish-
aria-keyshortcutsIndicates keyboard shortcuts that an author has implemented to activate or give focus to an element.string-
aria-labelDefines a string value that labels the current element.string-
aria-labelledbyIdentifies the element (or elements) that labels the current element.string-
aria-levelDefines the hierarchical level of an element within a structure.Numberish-
aria-liveIndicates that an element will be updated, and describes the types of updates the user agents, assistive technologies, and user can expect from the live region."assertive" | "off" | "polite"-
aria-modalIndicates whether an element is modal when displayed.Booleanish-
aria-multilineIndicates whether a text box accepts multiple lines of input or only a single line.Booleanish-
aria-multiselectableIndicates that the user may select more than one item from the current selectable descendants.Booleanish-
aria-orientationIndicates whether the element's orientation is horizontal, vertical, or unknown/ambiguous."horizontal" | "vertical"-
aria-ownsIdentifies an element (or elements) in order to define a visual, functional, or contextual parent/child relationship
between DOM elements where the DOM hierarchy cannot be used to represent the relationship.
string-
aria-placeholderDefines a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value.
A hint could be a sample value or a brief description of the expected format.
string-
aria-posinsetDefines an element's number or position in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM.Numberish-
aria-pressedIndicates the current "pressed" state of toggle buttons."mixed" | Booleanish-
aria-readonlyIndicates that the element is not editable, but is otherwise operable.Booleanish-
aria-relevantIndicates what notifications the user agent will trigger when the accessibility tree within a live region is modified."additions removals" | "additions text" | "additions" | "all" | "removals additions" | "removals text" | "removals" | "text additions" | "text removals" | "text"-
aria-requiredIndicates that user input is required on the element before a form may be submitted.Booleanish-
aria-roledescriptionDefines a human-readable, author-localized description for the role of an element.string-
aria-rowcountDefines the total number of rows in a table, grid, or treegrid.Numberish-
aria-rowindexDefines an element's row index or position with respect to the total number of rows within a table, grid, or treegrid.Numberish-
aria-rowspanDefines the number of rows spanned by a cell or gridcell within a table, grid, or treegrid.Numberish-
aria-selectedIndicates the current "selected" state of various widgets.Booleanish-
aria-setsizeDefines the number of items in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM.Numberish-
aria-sortIndicates if items in a table or grid are sorted in ascending or descending order."ascending" | "descending" | "none" | "other"-
aria-valuemaxDefines the maximum allowed value for a range widget.Numberish-
aria-valueminDefines the minimum allowed value for a range widget.Numberish-
aria-valuenowDefines the current value for a range widget.Numberish-
aria-valuetextDefines the human readable text alternative of aria-valuenow for a range widget.string-
autocompleteNative options to use in HTML5 validation,
whether the value of the form's controls can be automatically completed by the browser.
string-
From config:
switch: {
  autocomplete: "off"
}
customValidityCustom HTML 5 validation error to set on the form controlstring | ((currentValue: unknown | null , state: ValidityState) => string) | undefined-""
disabledSame as native disabledboolean-false
falseValueOverrides the returned value when it's not checkedunknown-
formSame as native formstring-
idSame as native id. Also pass the id to a wrapping o-field component. Default is a uuid.string-useId()
labelInput label, unnecessary when default slot is usedstring-
v-modelThe input value state, use v-model to make it two-way bindingunknown-
nameSame as native namestring-
nativeValueSame as native valueunknown-
overrideOverride existing theme classes completelyboolean-
passiveVariantColor of the switch when is passivestringprimary, info, success, warning, danger, and any other custom color
From config:
switch: {
  passiveVariant: undefined
}
positionLabel positionstring-
From config:
switch: {
  right: "position"
}
requiredSame as native requiredBooleanish-
roundedEnable rounded styleboolean-
From config:
switch: {
  rounded: true
}
sizeSize of the controlstringsmall, medium, large
From config:
switch: {
  size: undefined
}
trueValueOverrides the returned value when it's checkedunknown-
useHtml5ValidationEnable HTML 5 native validationboolean-
From config:
{
  useHtml5Validation: true
}
variantColor variant of the controlstringprimary, info, success, warning, danger, and any other custom color
From config:
switch: {
  variant: undefined
}

Events ​

Event namePropertiesDescription
update:model-valuevalue unknown - updated modelValue propmodelValue prop two-way binding
inputvalue unknown - input value
event Event - native event
on input change event
focusevent Event - native eventon input focus event
blurevent Event - native eventon input blur event
invalidevent Event - native eventon input invalid event

Slots ​

NameDescriptionBindings
defaultOverride the label, default is label prop

Class Inspector ​

Classes applied to the element:
➜ Want to know how does the Class Inspector work?
Class propDescriptionPropsSuffixes
rootClass
Class of the root element.
checkedClass
Class of the root element when checked.
modelValue
sizeClass
Class of the root element with size.
sizesmall
medium
large
variantClass
Class of the root element with variant.
variantprimary
info
warning
danger
passiveVariantClass
Class of the root element with passive variant.
passiveVariantprimary
info
warning
danger
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 VariableDefault
$switch-spacerh.useVar("control-spacer")
$switch-width2.75em
$switch-height1.5em
$switch-colorh.useVar("font-color")
$switch-font-sizeh.useVar("font-size")
$switch-font-weighth.useVar("font-weight")
$switch-line-heighth.useVar("line-height")
$switch-box-shadowh.useVar("control-box-shadow-inset")
$switch-border-colorh.useVar("grey-lighter")
$switch-border-stylesolid
$switch-border-widthh.useVar("control-border-width")
$switch-border-radiush.useVar("border-radius")
$switch-border-radius-roundedh.useVar("border-radius-rounded")
$switch-background-colorh.useVar("grey-light")
$switch-toggle-background-colorvars.$control-background-color
$switch-checked-background-colorh.useVar("primary")
$switch-checked-border-colorh.useVar("primary")
$switch-disabled-opacityh.useVar("control-disabled-opacity")

See ➜ 📄 SCSS file

Current theme ➜ Bulma

SASS VariableDefault
$switch-width2.75em
$switch-padding0.2em
$switch-colorsdv.$colors
$switch-background-colorcss.getVar("grey-light")
$switch-active-background-colorcss.getVar("primary")
$switch-shadowcss.getVar("shadow")

See ➜ 📄 SCSS file

Current theme ➜ Bootstrap

SASS VariableDefault
$switch-spacer0.5rem

See ➜ 📄 SCSS file

Current theme ➜ Tailwind

SASS VariableDefault

See ➜ 📄 SCSS file

Released under the MIT License.