Skip to content

Switch

The Switch component is used as an alternative for the checkbox. The option that the switch controls, as well as the state it's in, should be made clear from the corresponding inline label.

Examples

Switch

Code
vue
<script setup lang="ts">
import { ref } from 'vue'
import { BaseSwitch } from '@point-hub/papp'

const form = ref({
  checked: false
})
</script>

<template>
  <form @submit.prevent="">
    <component :is="BaseSwitch" v-model="form.checked" />
  </form>
</template>

Text

Code
vue
<script setup lang="ts">
import { ref } from 'vue'
import { BaseSwitch } from '@point-hub/papp'

const form = ref({
  checked: false
})
</script>

<template>
  <form @submit.prevent="">
    <component :is="BaseSwitch" v-model="form.checked" text="Label" />
    <component :is="BaseSwitch" v-model="form.checked" text="Label" text-position="right" />
  </form>
</template>

Label

Code
vue
<script setup lang="ts">
import { ref } from 'vue'
import { BaseSwitch } from '@point-hub/papp'

const form = ref({
  checked1: false,
  checked2: false
})
</script>

<template>
  <form @submit.prevent="">
    <component
      :is="BaseSwitch"
      v-model="form.checked1"
      label="Label"
      layout="vertical"
      description="Vertical Layout"
    />
    <component
      :is="BaseSwitch"
      v-model="form.checked2"
      label="Label"
      layout="horizontal"
      description="Horizontal Layout"
    />
  </form>
</template>

Size

Code
vue
<script setup lang="ts">
import { ref } from 'vue'
import { BaseSwitch } from '@point-hub/papp'

const form = ref({
  checked1: false,
  checked2: false,
  checked3: false,
  checked4: false
})
</script>

<template>
  <form @submit.prevent="">
    <component :is="BaseSwitch" v-model="form.checked1" label="sm" size="sm" />
    <component :is="BaseSwitch" v-model="form.checked2" label="md" size="md" />
    <component :is="BaseSwitch" v-model="form.checked3" label="lg" size="lg" />
    <component :is="BaseSwitch" v-model="form.checked4" label="xl" size="xl" />
  </form>
</template>

Helper

Code
vue
<script setup lang="ts">
import { ref } from 'vue'
import { BaseSwitch } from '@point-hub/papp'

const form = ref({
  checked1: false,
  checked2: false,
  checked3: false,
  checked4: false,
  checked5: false
})

const errors = ref(['Error Example'])
</script>

<template>
  <form @submit.prevent="">
    <component
      :is="BaseSwitch"
      v-model="form.checked1"
      label="Label"
      layout="horizontal"
      required
    />
    <component
      :is="BaseSwitch"
      v-model="form.checked2"
      label="Label"
      layout="horizontal"
      description="Description example"
    />
    <component
      :is="BaseSwitch"
      v-model="form.checked3"
      label="Label"
      layout="horizontal"
      :helpers="['Description example']"
    />
    <component
      :is="BaseSwitch"
      v-model="form.checked4"
      label="Label"
      layout="horizontal"
      v-model:errors="errors"
    />
    <component
      :is="BaseSwitch"
      v-model="form.checked5"
      label="Label"
      layout="horizontal"
      description="Disabled"
      disabled
    />
  </form>
</template>

Disabled

Code
vue
<script setup lang="ts">
import { ref } from 'vue'
import { BaseSwitch } from '@point-hub/papp'

const form = ref({
  checked1: true,
  checked2: false
})
</script>

<template>
  <form @submit.prevent="">
    <component :is="BaseSwitch" v-model="form.checked1" disabled />
    <component :is="BaseSwitch" v-model="form.checked2" disabled />
  </form>
</template>

Switch API

Types

ts
export type BaseSwitchTextPosition = 'left' | 'right'
export type BaseSwitchSize = 'sm' | 'md' | 'lg' | 'xl'

Props

NameTypeDefaultDescription
v-modelbooleanv-model is required.
v-model:errorsstring[]Input error message.
labelstringSwitch label.
textstringSwitch text.
text-positionBaseSwitchTextPositionleftSwitch text position.
sizeBaseSwitchSizemdSwitch size.
disabledbooleanfalseif true switch is disabled.

Released under the MIT License.