---
title: "Toggle"
description: "Кнопка, которую можно нажать и отжать: у неё два состояния."
canonical_url: "https://b24-ui.bx-shef.by/docs/components/toggle"
---
# Toggle

> Кнопка, которую можно нажать и отжать: у неё два состояния.

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

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

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

<template>
  <B24Toggle v-model="value" label="Жирный" />
</template>
```

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

```vue
<template>
  <B24Toggle default-value label="Жирный" />
</template>
```

> [!NOTE]
> Toggle построен на [Button](https://b24-ui.bx-shef.by/raw/docs/components/button.md), поэтому принимает его пропсы: `label`, `icon`, `size` и другие, кроме пропсов ссылки. Нажатое состояние Toggle передаёт в пропс `active` у Button.

### Иконка

Пропс `icon` показывает иконку. Кнопке без подписи нужен `aria-label`.

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

<template>
  <B24Toggle :icon="RocketIcon" aria-label="Запуск" />
</template>
```

### Цвет

Пропс `color` задаёт цвет неактивного Toggle. По умолчанию `air-secondary-no-accent`.

```vue
<template>
  <B24Toggle color="air-tertiary-no-accent" label="Жирный" />
</template>
```

### Цвет нажатого

Пропс `active-color` задаёт цвет нажатого Toggle. По умолчанию `air-primary`.

```vue
<template>
  <B24Toggle active-color="air-primary-success" default-value label="Жирный" />
</template>
```

### Размер

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

```vue
<template>
  <B24Toggle size="xl" label="Жирный" />
</template>
```

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

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

```vue
<template>
  <B24Toggle disabled label="Жирный" />
</template>
```

## Примеры

### Стиль нажатого состояния

В нажатом состоянии Toggle получает `data-state="on"`, и его можно оформить вариантом `data-[state=on]`.

```vue
<template>
  <B24Toggle label="Жирный" class="data-[state=on]:italic" />
</template>
```

## API

### Пропсы

```ts
/**
 * Props for the Toggle component
 */
interface ToggleProps {
  id?: string | undefined;
  /**
   * The color of the toggle when not pressed.
   * @default 'air-secondary-no-accent'
   */
  color?: "air-primary" | "air-primary-success" | "air-primary-alert" | "air-primary-copilot" | "air-secondary" | "air-secondary-alert" | "air-secondary-accent" | "air-secondary-accent-1" | "air-secondary-accent-2" | "air-secondary-no-accent" | "air-tertiary" | "air-tertiary-accent" | "air-tertiary-no-accent" | "air-selection" | "air-boost" | "link";
  /**
   * The color of the toggle when pressed.
   * @default 'air-primary'
   */
  activeColor?: "default" | "air-secondary" | "air-primary" | "air-primary-success" | "air-primary-alert" | "air-primary-copilot" | "danger" | "success" | "warning" | "primary" | "secondary" | "collab" | "ai" | "air-secondary-no-accent" | "air-secondary-alert" | "air-secondary-accent" | "air-secondary-accent-1" | "air-secondary-accent-2" | "air-tertiary" | "air-tertiary-accent" | "air-tertiary-no-accent" | "air-selection" | "air-boost" | "link" | undefined;
  /**
   * The element or component this component should render as when not a link.
   * @default 'button'
   */
  as?: any;
  /**
   * Display an icon on the left side.
   */
  icon?: (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 avatar on the left side.
   */
  avatar?: AvatarProps | undefined;
  b24ui?: { base?: SlotClass; baseLoading?: SlotClass; baseLoadingWaitIcon?: SlotClass; baseLoadingClockIcon?: SlotClass; baseLoadingSpinnerIcon?: SlotClass; baseLine?: SlotClass; label?: SlotClass; labelInner?: SlotClass; leadingIcon?: SlotClass; leadingAvatar?: SlotClass; leadingAvatarSize?: SlotClass; trailingIcon?: SlotClass; safeList?: SlotClass; } | undefined;
  /**
   * The class to apply when the link is active
   * @default ''
   */
  activeClass?: string | undefined;
  /**
   * The class to apply when the link is inactive
   * @default ''
   */
  inactiveClass?: string | undefined;
  /**
   * When `true`, the loading icon will be displayed.
   */
  loading?: boolean | undefined;
  /**
   * Class to apply when the link is exact active
   */
  exactActiveClass?: string | undefined;
  /**
   * Pass the returned promise of `router.push()` to `document.startViewTransition()` if supported.
   */
  viewTransition?: boolean | undefined;
  autofocus?: false | true | "true" | "false" | undefined;
  disabled?: boolean | undefined;
  form?: string | undefined;
  formaction?: string | undefined;
  formenctype?: string | undefined;
  formmethod?: string | undefined;
  formnovalidate?: false | true | "true" | "false" | undefined;
  formtarget?: string | undefined;
  name?: string | undefined;
  onClick?: (event: MouseEvent): void | ((event: MouseEvent) => void)[] | undefined;
  label?: string | undefined;
  /**
   * @default 'md'
   */
  size?: "xl" | "md" | "xs" | "sm" | "lg" | "xss" | undefined;
  /**
   * Rounds the corners of the button
   * @default false
   */
  rounded?: boolean | undefined;
  /**
   * Render the button full width
   * @default false
   */
  block?: boolean | undefined;
  /**
   * Disable uppercase label
   * @default true
   */
  normalCase?: boolean | undefined;
  /**
   * Shows LoaderWaitIcon in loading mode
   * @default false
   */
  useWait?: boolean | undefined;
  /**
   * Shows LoaderClockIcon icon in loading mode
   * @default false
   */
  useClock?: boolean | undefined;
  /**
   * Shows a dropdown chevron on the right side, from the dictionary's
   * `chevronDown` role, so an `app.config` override reaches it.
   * @default false
   */
  useDropdown?: boolean | undefined;
  /**
   * The controlled pressed state of the toggle. Can be bind as `v-model`.
   * @default undefined
   */
  modelValue?: boolean | null | undefined;
  /**
   * The pressed state of the toggle when it is initially rendered. Use when you do not need to control its open state.
   */
  defaultValue?: boolean | undefined;
  /**
   * When `true`, indicates that the user must set the value before the owning form can be submitted.
   */
  required?: boolean | undefined;
}
```

### Слоты

```ts
/**
 * Slots for the Toggle component
 */
interface ToggleSlots {
  leading(): any;
  default(): any;
  trailing(): any;
}
```

### События

```ts
/**
 * Emitted events for the Toggle component
 */
interface ToggleEmits {
  change: (payload: [event: Event]) => void;
  update:modelValue: (payload: [value: boolean]) => void;
}
```

## Тема

```ts [app.config.ts]
export default defineAppConfig({
  b24ui: {
    toggle: {
      base: ''
    }
  }
})
```

## Sitemap

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