v2.15.0

Switch

Переключатель между двумя состояниями.

Использование

Директива v-model управляет состоянием Switch.

<script setup lang="ts">
const value = ref(true)
</script>

<template>
  <B24Switch v-model="value" />
</template>
<script setup lang="ts">
import { ref } from 'vue'

const value = ref(true)
</script>

<template>
  <B24Switch v-model="value" />
</template>

Пропс default-value задаёт начальное значение, когда управлять состоянием не нужно.

<template>
  <B24Switch default-value />
</template>

Подпись

Пропс label задаёт подпись Switch.

<template>
  <B24Switch label="Включить уведомления" />
</template>

При использовании пропса required рядом с подписью появляется звёздочка.

<template>
  <B24Switch required label="Включить уведомления" />
</template>

Описание

Пропс description задаёт описание Switch.

Это переключатель.

<template>
  <B24Switch label="Включить уведомления" description="Это переключатель." />
</template>

Иконка

Пропсы checked-icon и unchecked-icon задают иконки Switch во включённом и выключенном состоянии.

<script setup lang="ts">
import RocketIcon from '@bitrix24/b24icons-vue/main/RocketIcon'
</script>

<template>
  <B24Switch :checked-icon="RocketIcon" default-value label="Включить уведомления" />
</template>

Загрузка

Пропс loading показывает иконку загрузки в Switch.

<template>
  <B24Switch loading default-value label="Включить уведомления" />
</template>

Цвет

Пропс color меняет цвет Switch.

<template>
  <B24Switch color="air-primary-copilot" default-value label="Включить уведомления" />
</template>

Размер

Пропс size меняет размер Switch.

<template>
  <B24Switch size="lg" default-value label="Включить уведомления" />
</template>

Отключение

Пропс disabled отключает Switch.

<template>
  <B24Switch disabled label="Включить уведомления" />
</template>

API

Пропсы

Prop Default Type
as'div'any

The element or component this component should render as.

color'air-primary'"air-primary" | "air-primary-success" | "air-primary-alert" | "air-primary-warning" | "air-primary-copilot"
size'md' "md" | "xs" | "sm" | "lg"
highlightboolean

Highlight the ring color like a focus state.

loadingfalseboolean

When true, the loading icon will be displayed

loadingIconicons.refreshIconComponent

The icon when the loading prop is true.

checkedIconIconComponent

Display an icon when the switch is checked.

uncheckedIconIconComponent

Display an icon when the switch is unchecked.

label string
description string
disabledboolean

When true, prevents the user from interacting with the switch.

id string
name string

The name of the field. Submitted with its owning form as part of a name/value pair.

requiredboolean

When true, indicates that the user must set the value before the owning form can be submitted.

value string

The value given as data when submitted with a name.

defaultValue T

The state of the switch when it is initially rendered. Use when you do not need to control its state.

modelValue null | T

The controlled state of the switch. Can be bind as v-model.

trueValue T

The value used when the switch is on. Defaults to true.

falseValue T

The value used when the switch is off. Defaults to false.

autofocus false | true | "true" | "false"
b24ui { root?: SlotClass; base?: SlotClass; container?: SlotClass; thumb?: SlotClass; icon?: SlotClass; wrapper?: SlotClass; label?: SlotClass; description?: SlotClass; }
Компонент поддерживает и все нативные HTML-атрибуты <button>.

Слоты

Slot Type
label{ label: string | undefined; }
description{ description: string | undefined; }

События

Event Type
change[event: Event]
update:modelValue[payload: T]

Тема

https://github.com/bx-shef/b24-ui/tree/main/src/theme/switch.ts
export default {
  slots: {
    root: 'relative flex items-start',
    base: 'group/switch cursor-pointer inline-flex items-center shrink-0 rounded-(--ui-border-radius-pill) outline-(--ui-color-background-transparent) focus-visible:outline-2 focus-visible:outline-offset-2 transition-[background] duration-200 border-2 border-(--ui-color-background-transparent) data-[state=unchecked]:bg-(--ui-color-base-5) data-[state=checked]:bg-(--b24ui-background) focus-visible:outline-(--b24ui-border-color)',
    container: 'flex items-center',
    thumb: 'group pointer-events-none flex items-center justify-center rounded-(--ui-border-radius-pill) bg-(--ui-color-base-white-fixed) shadow-lg ring-0 transition-[transform,translate,scale,rotate,width] duration-200 motion-reduce:transition-none data-[state=unchecked]:translate-x-0 data-[state=unchecked]:rtl:-translate-x-0',
    icon: 'absolute shrink-0 group-data-[state=unchecked]:text-(--ui-color-base-5) edge-dark:group-data-[state=unchecked]:text-(--ui-color-gray-60) opacity-0 p-0.5 size-full transition-[color,opacity] duration-200 group-data-[state=checked]:text-(--b24ui-background)',
    wrapper: 'font-[family-name:var(--ui-font-family-primary)] font-(--ui-font-weight-regular) ms-2',
    label: 'cursor-pointer block text-label',
    description: 'text-description'
  },
  variants: {
    color: {
      'air-primary': {
        base: 'style-filled'
      },
      'air-primary-success': {
        base: 'style-filled-success'
      },
      'air-primary-alert': {
        base: 'style-filled-alert'
      },
      'air-primary-copilot': {
        base: 'style-filled-copilot'
      },
      'air-primary-warning': {
        base: 'style-filled-warning'
      },
      default: {
        base: 'style-old-default'
      },
      danger: {
        base: 'style-old-danger'
      },
      success: {
        base: 'style-old-success'
      },
      warning: {
        base: 'style-old-warning'
      },
      primary: {
        base: 'style-old-primary'
      },
      secondary: {
        base: 'style-old-secondary'
      },
      collab: {
        base: 'style-old-collab'
      },
      ai: {
        base: 'style-old-ai'
      }
    },
    size: {
      xs: {
        base: 'w-[28px]',
        container: 'h-[16px]',
        thumb: 'size-[12px] group-active/switch:w-[14px] data-[state=checked]:translate-x-3 data-[state=checked]:group-active/switch:translate-x-2.5 data-[state=checked]:rtl:-translate-x-3 data-[state=checked]:rtl:group-active/switch:-translate-x-2.5',
        wrapper: 'text-(length:--ui-font-size-xs)',
        label: 'leading-[16px]'
      },
      sm: {
        base: 'w-[32px]',
        container: 'h-[16px]',
        thumb: 'size-[14px] group-active/switch:w-[16px] data-[state=checked]:translate-x-3.5 data-[state=checked]:group-active/switch:translate-x-3 data-[state=checked]:rtl:-translate-x-3.5 data-[state=checked]:rtl:group-active/switch:-translate-x-3',
        wrapper: 'text-(length:--ui-font-size-sm)',
        label: 'leading-[16px]'
      },
      md: {
        base: 'w-[36px]',
        container: 'h-[20px]',
        thumb: 'size-[16px] group-active/switch:w-[20px] data-[state=checked]:translate-x-4 data-[state=checked]:group-active/switch:translate-x-3 data-[state=checked]:rtl:-translate-x-4 data-[state=checked]:rtl:group-active/switch:-translate-x-3',
        wrapper: 'text-(length:--ui-font-size-md)',
        label: 'leading-[20px]'
      },
      lg: {
        base: 'w-[40px]',
        container: 'h-[20px]',
        thumb: 'size-[20px] group-active/switch:w-[24px] data-[state=checked]:translate-x-4 data-[state=checked]:group-active/switch:translate-x-3 data-[state=checked]:rtl:-translate-x-4 data-[state=checked]:rtl:group-active/switch:-translate-x-3',
        wrapper: 'text-(length:--ui-font-size-xl)',
        label: 'leading-[20px]'
      }
    },
    checked: {
      true: {
        icon: 'group-data-[state=checked]:opacity-100'
      }
    },
    unchecked: {
      true: {
        icon: 'group-data-[state=unchecked]:opacity-100'
      }
    },
    loading: {
      true: {
        icon: 'animate-spin'
      }
    },
    required: {
      true: {
        label: "after:content-['*'] after:ms-0.5 after:text-(--ui-color-accent-main-alert)"
      }
    },
    disabled: {
      true: {
        root: 'opacity-30',
        base: 'cursor-not-allowed',
        label: 'cursor-not-allowed',
        description: 'cursor-not-allowed'
      }
    },
    highlight: {
      true: {
        base: 'ring ring-inset ring-(--b24ui-border-color)'
      }
    }
  },
  defaultVariants: {
    color: 'air-primary',
    size: 'md'
  }
}