Button Group
View Switcher
A list view switcher on toggle buttons: the state is announced through aria-pressed and the styling is selected by the same attribute, so markup and looks cannot drift apart.
- buttongroup
- toggle
- view
- aria-pressed
Preview
Use it with AI
- 1. Copy the link.
- 2. Write to your agent in your own words and drop the link into the sentence.
- 3. The agent opens the link and installs the component from the registry.
put this in the header: https://vibeui.ru/c/buttongroup-005?lang=en
Show the full instructions
What the link says, spelled out. Use it when your agent cannot open links — paste this text instead.
# Install and place "buttongroup-005" (View Switcher) from VibeUI
## 1. Install first — do not skip, do not recreate
Run this exact command before writing any code:
npx shadcn@latest add https://vibeui.ru/r/buttongroup-005.json
Registry item: https://vibeui.ru/r/buttongroup-005.json
Installs to: components/vibeui/buttongroup-005.tsx (the exact path follows this project's components.json aliases).
npm dependencies: none.
Registry dependencies: none.
Install it from the registry. Do not recreate it from the description,
do not substitute a similar component from another library, and do not
rewrite it to match the project's existing style.
## 2. What it is
A list view switcher on toggle buttons: the state is announced through aria-pressed and the styling is selected by the same attribute, so markup and looks cannot drift apart.
A view switcher on buttons with aria-pressed: the value never reaches a form, it reshapes the list in place. Zero dependencies, one file, a client component.
## 3. How to use it
import { Buttongroup005 } from "@/components/vibeui/buttongroup-005"
<Buttongroup005
views={[{ id: "list", label: "List" }, { id: "grid", label: "Grid" }]}
defaultView="grid"
label="List view"
onChange={(id) => setView(id)}
/>
Read the installed file for the full prop list.
## 4. Where to place it
This is a small inline component. Put it exactly where the user asked,
inside the existing markup. Do not create a new page, section or
wrapper for it. If a similar control already sits in that spot,
replace it instead of adding a second one.
Placement: ___
(The user fills this line in. If it is still blank, ask where to put it
instead of guessing.)
## 5. Keep exactly as installed
- the local --vibeui-buttongroup-005-* palette — do not swap it for your theme tokens
- aria-pressed instead of radios: nothing is submitted, and a radio here would be a lie
- styling picked by [aria-pressed="true"] — the look then cannot drift from the accessible state
- the white surface and shadow on the active button: icon colour alone is too little for state
- the visible captions next to the icons — not everyone tells "grid" from "table" by silhouette
- role="group" with aria-label: unnamed, the three toggles read as unrelated buttons
## 6. You may change
- the set of views through views
- the initial view through defaultView
- the accessible group name through label
- the selection callback through onChange and the accent through accent
## 7. Rules
- The component governs itself only: redrawing the list is your onChange handler's job.
- defaultView is matched against id, not the caption: a typo silently deselects every button.
- Three views are the limit for this layout, beyond that the captions stop fitting on a phone.
- Do not lift the CSS variables into globals.css: the component has to stay a single file.
## 8. Verify
- it renders with no console errors;
- it looks like the preview on the VibeUI page you copied this from;
- if it does not, you changed something listed in section 5 — put it back.For developers
npx shadcn@latest add https://vibeui.ru/r/buttongroup-005.jsonhttps://vibeui.ru/r/buttongroup-005.jsonКомпонент самодостаточен: один файл, без зависимостей, собственная палитра в переменных --vibeui-buttongroup-005-*. Клиентский: useState хранит текущий вид, onChange отдаёт его наружу. Здесь сознательно не radio: значение не отправляется формой, оно меняет представление на месте — это набор кнопок-тумблеров с aria-pressed. Стиль нажатой кнопки выбирается селектором button[aria-pressed="true"], поэтому доступное состояние и внешний вид не могут разойтись. Значки — инлайновые SVG на currentColor, у активной кнопки они перекрашиваются в акцент; активность несут ещё и подложка с тенью, а не только цвет.
Component source
The same file your agent installs. Here in case you would rather copy it by hand.
"use client"
import { useState } from "react"
import type { ComponentPropsWithoutRef, CSSProperties } from "react"
export type Buttongroup005View = {
id: string
label: string
}
export type Buttongroup005Props = Omit<
ComponentPropsWithoutRef<"div">,
"children" | "onChange"
> & {
views?: Buttongroup005View[]
defaultView?: string
label?: string
onChange?: (id: string) => void
accent?: string
}
// Идея компонента: переключатель представления — это не выбор из формы, а
// набор кнопок-тумблеров. Состояние объявляется через aria-pressed, поэтому
// скринридер читает «Сетка, нажато», а не догадывается по цвету. Radio здесь
// был бы неправдой: значение никуда не отправляется, оно меняет вид списка.
const STYLES = `
:where([data-vibeui-block="buttongroup-005"]){
--vibeui-buttongroup-005-surface:oklch(0.97 0.004 265);
--vibeui-buttongroup-005-on:oklch(1 0 0);
--vibeui-buttongroup-005-fg:oklch(0.25 0.016 265);
--vibeui-buttongroup-005-muted:oklch(0.54 0.014 265);
--vibeui-buttongroup-005-border:oklch(0.89 0.008 265);
--vibeui-buttongroup-005-accent:oklch(0.55 0.17 265);
--vibeui-buttongroup-005-radius:0.5rem;
--vibeui-buttongroup-005-font:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
}
[data-vibeui-block="buttongroup-005"]{
box-sizing:border-box;display:inline-flex;align-items:center;gap:0.1875rem;
padding:0.1875rem;
border:1px solid var(--vibeui-buttongroup-005-border);
border-radius:calc(var(--vibeui-buttongroup-005-radius) + 0.1875rem);
background:var(--vibeui-buttongroup-005-surface);
font-family:var(--vibeui-buttongroup-005-font);
}
[data-vibeui-block="buttongroup-005"] *{box-sizing:border-box}
[data-vibeui-block="buttongroup-005"] button{
appearance:none;cursor:pointer;font:inherit;
display:inline-flex;align-items:center;gap:0.375rem;
height:1.875rem;padding:0 0.625rem;
border:0;border-radius:var(--vibeui-buttongroup-005-radius);background:transparent;
color:var(--vibeui-buttongroup-005-muted);
font-size:0.8125rem;font-weight:600;line-height:1;white-space:nowrap;
transition:background-color .16s ease,color .16s ease,box-shadow .16s ease;
}
[data-vibeui-block="buttongroup-005"] button svg{width:0.9375rem;height:0.9375rem;flex:none}
[data-vibeui-block="buttongroup-005"] button:hover{color:var(--vibeui-buttongroup-005-fg)}
[data-vibeui-block="buttongroup-005"] button:focus-visible{
outline:2px solid var(--vibeui-buttongroup-005-accent);outline-offset:1px;
}
/* Нажатое состояние выбирается по самому aria-pressed: разметка и вид
не могут разойтись, потому что источник у них один. */
[data-vibeui-block="buttongroup-005"] button[aria-pressed="true"]{
background:var(--vibeui-buttongroup-005-on);
color:var(--vibeui-buttongroup-005-fg);
box-shadow:0 1px 2px oklch(0.2 0.02 265 / 14%);
}
[data-vibeui-block="buttongroup-005"] button[aria-pressed="true"] svg{color:var(--vibeui-buttongroup-005-accent)}
@media (prefers-reduced-motion:reduce){[data-vibeui-block="buttongroup-005"] *{animation:none!important;transition:none!important}}
`
const DEFAULT_VIEWS: Buttongroup005View[] = [
{ id: "list", label: "Список" },
{ id: "grid", label: "Сетка" },
{ id: "table", label: "Таблица" },
]
const ICONS: Record<string, string> = {
list: "M6.5 4.5h9M6.5 9h9M6.5 13.5h9M3 4.5h.01M3 9h.01M3 13.5h.01",
grid: "M3 3.5h5.5V9H3zM10.5 3.5H16V9h-5.5zM3 10.5h5.5V16H3zM10.5 10.5H16V16h-5.5z",
table: "M2.5 4h14v10h-14zM2.5 7.5h14M7.5 7.5V14",
}
/**
* Переключатель представления списка с состоянием через aria-pressed.
* Один файл, ноль зависимостей, собственная палитра.
*/
export function Buttongroup005({
views = DEFAULT_VIEWS,
defaultView = "grid",
label = "Вид списка",
onChange,
accent,
className,
style,
...props
}: Buttongroup005Props) {
const [current, setCurrent] = useState(defaultView)
const palette = {
...(accent ? { "--vibeui-buttongroup-005-accent": accent } : null),
...style,
} as CSSProperties
return (
<>
<style href="vibeui-buttongroup-005" precedence="medium">
{STYLES}
</style>
<div
{...props}
data-vibeui-block="buttongroup-005"
role="group"
aria-label={label}
className={className}
style={palette}
>
{views.map((view) => (
<button
key={view.id}
type="button"
aria-pressed={current === view.id}
onClick={() => {
setCurrent(view.id)
onChange?.(view.id)
}}
>
<svg viewBox="0 0 19 18" fill="none" aria-hidden="true">
<path
d={ICONS[view.id] ?? ICONS.list}
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
{view.label}
</button>
))}
</div>
</>
)
}