---
title: "Switch"
description: "Переключатель между двумя состояниями."
canonical_url: "https://b24-ui.bx-shef.by/docs/components/switch"
---
# Switch

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

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

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

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

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

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

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

### Подпись

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

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

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

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

### Описание

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

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

### Иконка

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

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

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

### Загрузка

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

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

### Цвет

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

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

### Размер

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

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

### Отключение

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

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

## API

### Пропсы

```ts
/**
 * Props for the Switch component
 */
interface SwitchProps {
  /**
   * The element or component this component should render as.
   * @default 'div'
   */
  as?: any;
  /**
   * @default 'air-primary'
   */
  color?: "air-primary" | "air-primary-success" | "air-primary-alert" | "air-primary-warning" | "air-primary-copilot";
  /**
   * @default 'md'
   */
  size?: "md" | "xs" | "sm" | "lg" | undefined;
  /**
   * Highlight the ring color like a focus state.
   */
  highlight?: boolean | undefined;
  /**
   * When `true`, the loading icon will be displayed
   * @default false
   */
  loading?: boolean | undefined;
  /**
   * The icon when the `loading` prop is `true`.
   * @default icons.refresh
   */
  loadingIcon?: (props: HTMLAttributes & VNodeProps & {}, ctx: Omit<{ attrs: Attrs; slots: Readonly<InternalSlots>; emit: (event: string, ...args: any[]) => void; expose: <Exposed extends Record<string, any> = Record<...>>(exposed?: Exposed | undefined) => void; }, "expose">): any | undefined;
  /**
   * Display an icon when the switch is checked.
   */
  checkedIcon?: (props: HTMLAttributes & VNodeProps & {}, ctx: Omit<{ attrs: Attrs; slots: Readonly<InternalSlots>; emit: (event: string, ...args: any[]) => void; expose: <Exposed extends Record<string, any> = Record<...>>(exposed?: Exposed | undefined) => void; }, "expose">): any | undefined;
  /**
   * Display an icon when the switch is unchecked.
   */
  uncheckedIcon?: (props: HTMLAttributes & VNodeProps & {}, ctx: Omit<{ attrs: Attrs; slots: Readonly<InternalSlots>; emit: (event: string, ...args: any[]) => void; expose: <Exposed extends Record<string, any> = Record<...>>(exposed?: Exposed | undefined) => void; }, "expose">): any | undefined;
  label?: string | undefined;
  description?: string | undefined;
  b24ui?: { root?: SlotClass; base?: SlotClass; container?: SlotClass; thumb?: SlotClass; icon?: SlotClass; wrapper?: SlotClass; label?: SlotClass; description?: SlotClass; } | undefined;
  /**
   * When `true`, prevents the user from interacting with the switch.
   */
  disabled?: boolean | undefined;
  id?: string | undefined;
  /**
   * The name of the field. Submitted with its owning form as part of a name/value pair.
   */
  name?: string | undefined;
  /**
   * When `true`, indicates that the user must set the value before the owning form can be submitted.
   */
  required?: boolean | undefined;
  /**
   * The value given as data when submitted with a `name`.
   */
  value?: string | undefined;
  /**
   * The state of the switch when it is initially rendered. Use when you do not need to control its state.
   */
  defaultValue?: T | undefined;
  /**
   * The controlled state of the switch. Can be bind as `v-model`.
   */
  modelValue?: null | T | undefined;
  /**
   * The value used when the switch is on. Defaults to `true`.
   */
  trueValue?: T | undefined;
  /**
   * The value used when the switch is off. Defaults to `false`.
   */
  falseValue?: T | undefined;
  autofocus?: false | true | "true" | "false" | undefined;
  form?: string | undefined;
  formaction?: string | undefined;
  formenctype?: string | undefined;
  formmethod?: string | undefined;
  formnovalidate?: false | true | "true" | "false" | undefined;
  formtarget?: string | undefined;
}
```

> [!NOTE]
> See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes
> Компонент поддерживает и все нативные HTML-атрибуты `<button>`.

### Слоты

```ts
/**
 * Slots for the Switch component
 */
interface SwitchSlots {
  label(): any;
  description(): any;
}
```

### События

```ts
/**
 * Emitted events for the Switch component
 */
interface SwitchEmits {
  change: (payload: [event: Event]) => void;
  update:modelValue: (payload: [payload: T]) => void;
}
```

## Тема

```ts [app.config.ts]
export default defineAppConfig({
  b24ui: {
    switch: {
      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'
      }
    }
  }
})
```

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
