v2.15.0

ToggleGroup Soon

Набор кнопок, каждую из которых можно нажать и отжать.

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

Директива v-model управляет нажатыми элементами ToggleGroup.

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
const value = ref('day')
</script>

<template>
  <B24ToggleGroup v-model="value" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
const value = ref('day')
</script>

<template>
  <B24ToggleGroup v-model="value" :items="items" />
</template>

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

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup default-value="week" :items="items" />
</template>

Элементы

Пропс items — массив объектов. Они принимают пропсы Button, кроме пропсов ссылки, и поле value: по умолчанию это индекс элемента строкой, поэтому модель тоже хранит строки.

  • value?: AcceptableValue
  • label?: string
  • icon?: IconComponent
  • disabled?: boolean
  • class?: any
  • b24ui?: ButtonProps['b24ui']
<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup default-value="week" :items="items" />
</template>
Все ключи элемента items, включая href, to и обработчики, попадают в кнопку. Не собирайте items из недоверенных данных.

Иконки

Элементы с icon и без подписи требуют aria-label: по нему кнопку находят скринридеры.

<script setup lang="ts">
import { RocketIcon } from '@bitrix24/b24icons-vue/main/RocketIcon'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'day',
    'aria-label': 'День'
  },
  {
    value: 'week',
    'aria-label': 'Неделя'
  },
  {
    value: 'month',
    'aria-label': 'Месяц'
  }
])
</script>

<template>
  <B24ToggleGroup default-value="day" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { RocketIcon } from '@bitrix24/b24icons-vue/main/RocketIcon'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'day',
    'aria-label': 'День'
  },
  {
    value: 'week',
    'aria-label': 'Неделя'
  },
  {
    value: 'month',
    'aria-label': 'Месяц'
  }
])
</script>

<template>
  <B24ToggleGroup default-value="day" :items="items" />
</template>

Несколько значений

Значение multiple в пропсе type разрешает нажимать сразу несколько элементов. Модель тогда — массив.

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Жирный',
    value: 'bold'
  },
  {
    label: 'Курсив',
    value: 'italic'
  },
  {
    label: 'Подчёркнутый',
    value: 'underline'
  }
])
</script>

<template>
  <B24ToggleGroup
    type="multiple"
    :default-value="{
      '0': 'bold',
      '1': 'italic'
    }"
    :items="items"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Жирный',
    value: 'bold'
  },
  {
    label: 'Курсив',
    value: 'italic'
  },
  {
    label: 'Подчёркнутый',
    value: 'underline'
  }
])
</script>

<template>
  <B24ToggleGroup
    type="multiple"
    :default-value="{
      '0': 'bold',
      '1': 'italic'
    }"
    :items="items"
  />
</template>

Ориентация

Пропс orientation меняет направление раскладки. По умолчанию horizontal.

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup orientation="vertical" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup orientation="vertical" default-value="week" :items="items" />
</template>

Соединение

Пропс attached соединяет элементы в одну группу, как в FieldGroup.

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup attached default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup attached default-value="week" :items="items" />
</template>
Крайние кнопки всегда скругляются по --ui-btn-radius: пропс rounded у элемента на них не действует. Если в группе есть и кнопки, и ссылки (to превращает элемент в <a>), скругление получится неверным: первая и последняя кнопки определяются отдельно для каждого тега.

Цвет

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

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup color="air-tertiary-no-accent" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup color="air-tertiary-no-accent" default-value="week" :items="items" />
</template>

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

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

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup active-color="air-primary-success" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup active-color="air-primary-success" default-value="week" :items="items" />
</template>

Размер

Пропс size меняет размер элементов.

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup size="xl" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup size="xl" default-value="week" :items="items" />
</template>

Отключение

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

<script setup lang="ts">
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup disabled default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from '@bxshefby/b24ui-nuxt'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'День',
    value: 'day'
  },
  {
    label: 'Неделя',
    value: 'week'
  },
  {
    label: 'Месяц',
    value: 'month'
  }
])
</script>

<template>
  <B24ToggleGroup disabled default-value="week" :items="items" />
</template>

API

Пропсы

Prop Default Type
as'div'any

The element or component this component should render as.

items T[]
color'air-secondary-no-accent'"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 items when not pressed.

activeColor'air-primary'"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 pressed items.

size "xs" | "md" | "xss" | "sm" | "lg" | "xl"
orientation'horizontal' "horizontal" | "vertical"

The orientation the items are laid out.

attachedfalseboolean

Attach the items together, like in a FieldGroup.

valueKey'value' keyof Extract<NestedItem<T>, object> & string | DotPathKeys<Extract<NestedItem<T>, object>>

The key used to get the value from the item.

labelKey'label' keyof Extract<NestedItem<T>, object> & string | DotPathKeys<Extract<NestedItem<T>, object>>

The key used to get the label from the item.

type'single' "single" | "multiple"

Determines whether a "single" or "multiple" items can be selected at a time.

This prop will overwrite the inferred type from modelValue and defaultValue.

modelValue null | string | number | bigint | Record<string, any> | AcceptableValue[]

The controlled value of the active item(s).

Use this when you need to control the state of the items. Can be binded with v-model

defaultValue null | string | number | bigint | Record<string, any> | AcceptableValue[]

The default active value of the item(s).

Use when you do not need to control the state of the item(s).

disabledboolean

When true, prevents the user from interacting with the toggle group and all its items.

looptrueboolean

When loop and rovingFocus is true, keyboard navigation will loop from last item to first, and vice versa.

rovingFocustrueboolean

When false, navigating through the items using arrow keys will be disabled.

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.

b24ui { root?: SlotClass; item?: SlotClass; }

Слоты

Slot Type
leading{ item: T; index: number; pressed: boolean; b24ui: object; }
default{ item: T; index: number; pressed: boolean; b24ui: object; }
trailing{ item: T; index: number; pressed: boolean; b24ui: object; }

События

Event Type
change[event: Event]
update:modelValue[payload: AcceptableValue | AcceptableValue[]]

Тема

https://github.com/bx-shef/b24-ui/tree/main/src/theme/toggle-group.ts
export default {
  slots: {
    root: 'relative',
    item: ''
  },
  variants: {
    orientation: {
      horizontal: {
        root: 'inline-flex items-center gap-1'
      },
      vertical: {
        root: 'flex flex-col gap-1'
      }
    },
    attached: {
      true: {
        item: 'rounded-none data-[state=on]:z-[1] focus-visible:z-[2]'
      }
    }
  },
  compoundVariants: [
    {
      orientation: 'horizontal',
      attached: true,
      class: {
        root: 'gap-0',
        item: 'not-first-of-type:-ms-px first-of-type:rounded-s-(--ui-btn-radius) last-of-type:rounded-e-(--ui-btn-radius)'
      }
    },
    {
      orientation: 'vertical',
      attached: true,
      class: {
        root: 'gap-0',
        item: 'not-first-of-type:-mt-px first-of-type:rounded-t-(--ui-btn-radius) last-of-type:rounded-b-(--ui-btn-radius)'
      }
    }
  ],
  defaultVariants: {
    orientation: 'horizontal'
  }
}