Skip to content
LIBRARYForms

Switch ​

MSwitch · A preference that takes effect when you turn it on or off.

Next alpha

Available in 0.1.0-alpha.4.

Web preview
Open example ↗

Usage ​

vue
<script setup lang="ts">
import { ref } from 'vue'
const notifications = ref(true)
</script>

<template>
  <MSwitch
    v-model="notifications"
    label="Notifications"
    helperText="Updates from the people you follow."
  />
</template>

API ​

PropTypeDefaultPurpose
modelValue / v-modelbooleanfalseControlled on/off value.
labelstringrequiredVisible label and accessible name.
helperTextstring''Optional explanation below the label.
disabledbooleanfalsePrevents activation from the label and control.
widthnumber, percentage or autoautoWidth of the complete row.
grow / shrinknumber0 / layout contextSpace distribution in the parent layout.

id identifies the control. accessibilityHidden follows the shared component contract.

update:modelValue emits one boolean per user activation. External changes do not emit. Accept the requested value in your event handler, or use v-model; if the parent keeps its old value, the control returns to it. There is no extra change event, internal persistence or optimistic network state.

Interaction and appearance ​

Activate the visible label or the control. The label and helper text wrap as available width or text size changes. Keep them as plain text; links and other controls belong outside the Switch. On the Web, Tab focuses the row and Space or Enter changes the value. Disabled switches cannot be activated or reached with Tab.

NativeScript uses the platform switch for its checked state, keyboard support, thumb gestures and system animation, with Mavis theme colors. The label has Jolt press feedback, released when disabled, unloaded or unmounted. Web uses a themed track and a thumb moved by Jolt: it settles with the theme's spring, stretches while it is held and can be dragged across the track; releasing past the middle requests the other value, and a rejected request returns the thumb. The keyboard focus ring is unchanged. Reduced-motion settings apply to Mavis feedback; native switch transitions follow the operating system. The controls share behavior and theme tokens, not identical platform geometry. Vertical drags do not toggle a preference. The native thumb retains the system's horizontal drag behavior; on iOS, start scrolling from the label or surrounding space, as the thumb can retain the gesture.

Use a Switch for a boolean setting. Use Checkbox for independent selections and indeterminate states.

Full example ​

The same file runs in the NativeScript catalog and Web preview.

View source
vue
<script setup lang="ts">
import { useCatalogLocale } from '../locale'
import { ref } from 'vue'
import { useMavisTheme } from '@getcortexlabs/mavis-core/vue'

const { t } = useCatalogLocale()

withDefaults(defineProps<{ lang?: 'en' | 'pt' }>(), { lang: 'pt' })
const { theme } = useMavisTheme()
const notifications = ref(true)
const digest = ref(false)
const changes = ref(0)
function reset() {
  notifications.value = true
  digest.value = false
}
</script>

<template>
  <m-column gap="lg">
    <m-column gap="sm" padding="lg" radius="lg" :backgroundColor="theme.colors.primarySoft">
      <m-row gap="sm">
        <m-icon name="bell" tone="primary" />
        <m-text variant="overline" tone="primary">{{
          t(lang === 'pt' ? 'NO SEU RITMO' : 'AT YOUR PACE')
        }}</m-text>
      </m-row>
      <m-text variant="heading">{{
        t(lang === 'pt' ? 'Só o que importa.' : 'Just what matters.')
      }}</m-text>
      <m-text variant="caption" tone="muted">{{
        t(lang === 'pt' ? 'Escolha o que chega até você.' : 'Choose what reaches you.')
      }}</m-text>
    </m-column>
    <m-column gap="sm">
      <m-switch
        id="notifications"
        v-model="notifications"
        :label="t(lang === 'pt' ? 'Notificações' : 'Notifications')"
        :helperText="
          t(
            lang === 'pt'
              ? 'Novidades das pessoas que você acompanha.'
              : 'Updates from the people you follow.',
          )
        "
        @update:modelValue="changes++"
      />
      <m-box :height="1" :backgroundColor="theme.colors.border" />
      <m-switch
        id="digest"
        v-model="digest"
        :label="t(lang === 'pt' ? 'Resumo semanal por e-mail' : 'Weekly email digest')"
        :helperText="
          t(
            lang === 'pt'
              ? 'Uma seleção para ler com calma, uma vez por semana.'
              : 'A thoughtful selection to read once a week.',
          )
        "
        @update:modelValue="changes++"
      />
      <m-box :height="1" :backgroundColor="theme.colors.border" />
      <m-switch
        :modelValue="true"
        disabled
        :label="t(lang === 'pt' ? 'Alertas de segurança' : 'Security alerts')"
        :helperText="
          t(
            lang === 'pt'
              ? 'Esta preferência é gerenciada pela sua equipe.'
              : 'Your team manages this preference.',
          )
        "
      />
      <m-switch disabled :label="t(lang === 'pt' ? 'Acesso antecipado' : 'Early access')" />
    </m-column>
    <m-row justify="between" gap="sm" wrap>
      <m-text variant="caption" tone="muted">{{
        t(
          lang === 'pt'
            ? `${changes} ${changes === 1 ? 'alteração' : 'alterações'}`
            : `${changes} ${changes === 1 ? 'change' : 'changes'}`,
        )
      }}</m-text>
      <m-button variant="ghost" size="sm" @click="reset">{{
        t(lang === 'pt' ? 'Restaurar' : 'Reset')
      }}</m-button>
    </m-row>
  </m-column>
</template>

In context ​

Profile combines text fields and notification preferences. Save and Discard belong to that screen; leaving it resets the local demonstration.

Desktop ​

On the desktop the switch is drawn with the colours of the theme, not the switch of the system, and a click or Space turns it on or off while the thumb slides along its track. Dragging the thumb does nothing. The Mac reads it as a switch that is on or off; Windows does not report the state yet (#489); see Desktop components.

Components for NativeScript and Web. Starting with Vue.