ToggleGroup

A set of buttons that can be pressed on or off.

Usage

Use the v-model directive to control the pressed items of the ToggleGroup.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'left',
    icon: 'i-lucide-align-left',
    'aria-label': 'Align left'
  },
  {
    value: 'center',
    icon: 'i-lucide-align-center',
    'aria-label': 'Align center'
  },
  {
    value: 'right',
    icon: 'i-lucide-align-right',
    'aria-label': 'Align right'
  }
])
const value = ref('left')
</script>

<template>
  <PToggleGroup v-model="value" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'left',
    icon: 'i-lucide-align-left',
    'aria-label': 'Align left'
  },
  {
    value: 'center',
    icon: 'i-lucide-align-center',
    'aria-label': 'Align center'
  },
  {
    value: 'right',
    icon: 'i-lucide-align-right',
    'aria-label': 'Align right'
  }
])
const value = ref('left')
</script>

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

Use the default-value prop to set the initial value when you do not need to control its state.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'left',
    icon: 'i-lucide-align-left',
    'aria-label': 'Align left'
  },
  {
    value: 'center',
    icon: 'i-lucide-align-center',
    'aria-label': 'Align center'
  },
  {
    value: 'right',
    icon: 'i-lucide-align-right',
    'aria-label': 'Align right'
  }
])
</script>

<template>
  <PToggleGroup default-value="left" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'left',
    icon: 'i-lucide-align-left',
    'aria-label': 'Align left'
  },
  {
    value: 'center',
    icon: 'i-lucide-align-center',
    'aria-label': 'Align center'
  },
  {
    value: 'right',
    icon: 'i-lucide-align-right',
    'aria-label': 'Align right'
  }
])
</script>

<template>
  <PToggleGroup default-value="left" :items="items" />
</template>

Items

Use the items prop as an array of objects that accept the Button props except the link ones, plus a value that defaults to the index as a string, so the model holds strings too:

  • value?: AcceptableValue
  • label?: string
  • icon?: string
  • disabled?: boolean
  • class?: any
  • ui?: ButtonProps['ui']
<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

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

Multiple

Set the type prop to multiple to allow several items to be pressed at once. The model value is then an array.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'bold',
    icon: 'i-lucide-bold',
    'aria-label': 'Toggle bold'
  },
  {
    value: 'italic',
    icon: 'i-lucide-italic',
    'aria-label': 'Toggle italic'
  },
  {
    value: 'underline',
    icon: 'i-lucide-underline',
    'aria-label': 'Toggle underline'
  }
])
</script>

<template>
  <PToggleGroup type="multiple" :default-value="['bold', 'italic']" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    value: 'bold',
    icon: 'i-lucide-bold',
    'aria-label': 'Toggle bold'
  },
  {
    value: 'italic',
    icon: 'i-lucide-italic',
    'aria-label': 'Toggle italic'
  },
  {
    value: 'underline',
    icon: 'i-lucide-underline',
    'aria-label': 'Toggle underline'
  }
])
</script>

<template>
  <PToggleGroup type="multiple" :default-value="['bold', 'italic']" :items="items" />
</template>

Orientation

Use the orientation prop to change the orientation of the ToggleGroup. Defaults to horizontal.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup orientation="vertical" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

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

Attached

Use the attached prop to attach the items together, like in a FieldGroup.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup attached variant="outline" active-variant="subtle" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup attached variant="outline" active-variant="subtle" default-value="week" :items="items" />
</template>

Color

Use the color and active-color props to set the color of the items when not pressed and when pressed. Defaults to neutral and primary.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup color="neutral" active-color="neutral" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup color="neutral" active-color="neutral" default-value="week" :items="items" />
</template>

Variant

Use the variant and active-variant props to set the variant of the items when not pressed and when pressed. Defaults to ghost and soft.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup variant="outline" active-variant="subtle" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup variant="outline" active-variant="subtle" default-value="week" :items="items" />
</template>

Size

Use the size prop to set the size of the items.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup size="xl" default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

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

Disabled

Use the disabled prop to disable the ToggleGroup.

<script setup lang="ts">
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

<template>
  <PToggleGroup disabled default-value="week" :items="items" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ToggleGroupItem } from 'pohon-ui'

const items = ref<ToggleGroupItem[]>([
  {
    label: 'Day',
    value: 'day'
  },
  {
    label: 'Week',
    value: 'week'
  },
  {
    label: 'Month',
    value: 'month'
  }
])
</script>

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

API

Props

Prop Default Type
as
'div'
any

The element or component this component should render as.

items
T[]
color
'neutral'
"primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral"

The color of the items.

variant
'ghost'
"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"

The variant of the items.

activeColor
'primary'
"primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral"

The color of the pressed items.

activeVariant
'soft'
"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"

The variant of the pressed items.

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

The orientation the items are laid out.

attached
boolean

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" | "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).

disabled
boolean

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

loop
boolean

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

rovingFocus
boolean

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.

required
boolean

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

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

Slots

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

Emits

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

Theme

app.config.ts
export default defineAppConfig({
  ui: {
    toggleGroup: {
      slots: {
        root: 'relative',
        item: ''
      },
      variants: {
        orientation: {
          horizontal: {
            root: 'inline-flex items-center gap-1'
          },
          vertical: {
            root: 'flex flex-col gap-1'
          }
        },
        fieldGroup: {
          horizontal: {
            root: 'gap-0 -space-x-px',
            item: 'first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-e-none not-first:[&:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-s-none not-first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-none focus-visible:z-[1] data-[state=on]:z-[1] focus-visible:z-[2]'
          },
          vertical: {
            root: 'gap-0 -space-y-px',
            item: 'first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-b-none not-first:[&:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-t-none not-first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-none focus-visible:z-[1] data-[state=on]:z-[1] focus-visible:z-[2]'
          }
        }
      }
    }
  }
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from 'pohon-ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      ui: {
        toggleGroup: {
          slots: {
            root: 'relative',
            item: ''
          },
          variants: {
            orientation: {
              horizontal: {
                root: 'inline-flex items-center gap-1'
              },
              vertical: {
                root: 'flex flex-col gap-1'
              }
            },
            fieldGroup: {
              horizontal: {
                root: 'gap-0 -space-x-px',
                item: 'first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-e-none not-first:[&:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-s-none not-first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-none focus-visible:z-[1] data-[state=on]:z-[1] focus-visible:z-[2]'
              },
              vertical: {
                root: 'gap-0 -space-y-px',
                item: 'first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-b-none not-first:[&:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-t-none not-first:not-[:nth-last-child(1_of_:not(input[aria-hidden],select[aria-hidden]))]:rounded-none focus-visible:z-[1] data-[state=on]:z-[1] focus-visible:z-[2]'
              }
            }
          }
        }
      }
    })
  ]
})

Changelog

No recent changes