Button Group for Astro
Joined Astro buttons that share a border and corner radius.
Installation
bunx --bun shadcn@latest add @gnostic/button-groupnpx shadcn@latest add @gnostic/button-grouppnpm dlx shadcn@latest add @gnostic/button-groupComplete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/button-group/button-group-separator.astro
---
import type { ComponentProps } from "astro/types"
import { cn } from "cn"
import { Separator } from "../separator"
type Props = ComponentProps<typeof Separator>
const { class: className, orientation = "vertical", ...props } = Astro.props
---
<Separator
orientation={orientation}
data-slot="button-group-separator"
class={cn(
"pointer-events-none relative z-2 bg-input before:absolute before:inset-0 has-[+[data-slot=input]:focus-within,+[data-slot=input-group]:focus-within,+[data-slot=select-trigger]:focus-visible+*,+[data-slot=number-field]:focus-within]:translate-x-px has-[+[data-slot=input]:focus-within,+[data-slot=input-group]:focus-within,+[data-slot=select-trigger]:focus-visible+*,+[data-slot=number-field]:focus-within]:bg-ring dark:before:bg-input/32 [[data-slot=input]:focus-within+&,[data-slot=input-group]:focus-within+&,[data-slot=select-trigger]:focus-visible+*+&,[data-slot=number-field]:focus-within+&,[data-slot=number-field]:focus-within+input+&]:bg-ring [[data-slot=input]:focus-within+&,[data-slot=input-group]:focus-within+&,[data-slot=select-trigger]:focus-visible+*+&,[data-slot=number-field]:focus-within+input+&]:-translate-x-px",
className,
)}
{...props}
/>
src/components/ui/button-group/button-group-text.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="button-group-text"
class={cn(
"relative inline-flex items-center gap-2 whitespace-nowrap rounded-lg border border-input bg-muted not-dark:bg-clip-padding px-[calc(--spacing(3)-1px)] text-base text-muted-foreground shadow-xs/5 outline-none transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] before:shadow-[0_1px_--theme(--color-black/6%)] sm:text-sm dark:bg-input/64 dark:before:shadow-[0_-1px_--theme(--color-white/6%)] [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:-mx-0.5 [&_svg]:shrink-0",
className,
)}
{...props}
>
<slot />
</div>
src/components/ui/button-group/button-group-variants.ts
import { cva, type VariantProps } from 'class-variance-authority'
export const buttonGroupVariants = cva(
'dark:*:[[data-slot=separator]:has(~button:hover):not(:has(~[data-slot=separator]~[data-slot]:hover)),[data-slot=separator]:has(~[data-slot][aria-pressed=true]):not(:has(~[data-slot=separator]~[data-slot][aria-pressed=true]))]:before:bg-input/64 dark:*:[button:hover~[data-slot=separator]:not([data-slot]:hover~[data-slot=separator]~[data-slot=separator]),[data-slot][aria-pressed=true]~[data-slot=separator]:not([data-slot][aria-pressed=true]~[data-slot=separator]~[data-slot=separator])]:before:bg-input/64 flex w-fit *:focus-visible:z-1 *:has-focus-visible:z-1 has-[>[data-slot=button-group]]:gap-2',
{
defaultVariants: {
orientation: 'horizontal',
},
variants: {
orientation: {
horizontal:
'*:data-slot:has-[~[data-slot]]:rounded-e-none *:data-slot:has-[~[data-slot]]:border-e-0 *:data-slot:has-[~[data-slot]]:before:rounded-e-none *:data-slot:not-data-[slot=separator]:has-[~[data-slot]]:before:inset-e-[-0.5px] *:pointer-coarse:after:min-w-auto *:[[data-slot]~[data-slot]]:rounded-s-none *:[[data-slot]~[data-slot]]:border-s-0 *:[[data-slot]~[data-slot]]:before:rounded-s-none *:[[data-slot]~[data-slot]:not([data-slot=separator])]:before:inset-s-[-0.5px]',
vertical:
'flex-col *:data-slot:has-[~[data-slot]]:rounded-b-none *:data-slot:has-[~[data-slot]]:border-b-0 *:data-slot:has-[~[data-slot]]:before:rounded-b-none *:data-slot:not-data-[slot=separator]:has-[~[data-slot]]:before:bottom-[-0.5px] *:data-slot:not-data-[slot=separator]:has-[~[data-slot]]:before:hidden dark:*:first:before:block dark:*:last:before:hidden *:pointer-coarse:after:min-h-auto *:[[data-slot]~[data-slot]]:rounded-t-none *:[[data-slot]~[data-slot]]:border-t-0 *:[[data-slot]~[data-slot]]:before:rounded-t-none *:[[data-slot]~[data-slot]:not([data-slot=separator])]:before:top-[-0.5px]',
},
},
},
)
export type ButtonGroupVariantProps = VariantProps<typeof buttonGroupVariants>
src/components/ui/button-group/button-group.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import { buttonGroupVariants } from "./button-group-variants.ts"
import type { ButtonGroupProps } from "./types.ts"
/** Div attributes pass through. Give the group an accessible name. */
type Props = HTMLAttributes<"div"> & ButtonGroupProps
const { class: className, orientation = "horizontal", ...props } = Astro.props
---
<div
role="group"
data-slot="button-group"
data-orientation={orientation}
class={cn(buttonGroupVariants({ orientation }), className)}
{...props}
>
<slot />
</div>
src/components/ui/button-group/index.ts
export { default as ButtonGroup } from './button-group.astro'
export { default as ButtonGroupSeparator } from './button-group-separator.astro'
export { default as ButtonGroupText } from './button-group-text.astro'
export { buttonGroupVariants } from './button-group-variants.ts'
export type { ButtonGroupProps } from './types.ts'
src/components/ui/button-group/types.ts
/**
* Props for a joined set of buttons.
* Div attributes such as `class`, `id`, and `aria-label` pass through.
* Place each button directly inside the group.
*/
export interface ButtonGroupProps {
/**
* Direction of the joined controls.
*
* @default "horizontal"
*/
orientation?: 'horizontal' | 'vertical'
}
Usage
---
import {
ButtonGroup,
ButtonGroupSeparator,
ButtonGroupText,
} from "@/components/ui/button-group"
import { Button } from "@/components/ui/button"
---
<ButtonGroup aria-label="Document actions">
<Button variant="outline">Save draft</Button>
<ButtonGroupSeparator />
<Button variant="outline">Preview</Button>
</ButtonGroup>
Set orientation="vertical" for a column. ButtonGroupText adds a label. ButtonGroupSeparator adds a divider. A horizontal group uses a vertical separator.
Composition
Place the parts directly inside ButtonGroup:
ButtonGroup
├── Button
├── ButtonGroupSeparator
├── Button
└── ButtonGroupText
ButtonGroupSeparator is optional. Its default orientation is vertical, which fits a horizontal group. Set orientation="horizontal" on the separator when the group is vertical.
API Reference
ButtonGroupProps
orientation?'horizontal' | 'vertical'
Direction of the joined controls.
'horizontal' | 'vertical'"horizontal"Div attributes such as class, id, and aria-label pass through.
ButtonGroupSeparator accepts Separator attributes such as class and orientation. ButtonGroupText accepts div attributes such as class.
Read the installed source for the component source.
When to use Button Group
Use Button Group when several buttons act on the same content and should read as one control. Put the group label in aria-label when nearby text does not name the group. Each button keeps its own label. Give an icon-only button an accessible name.
Keyboard and behavior
The group uses role="group". Each button keeps its own native keyboard behavior. This layout does not add arrow-key navigation or a pressed state. Use Toggle Group when the visitor keeps one option pressed.
Theme and layout
The outer buttons keep rounded corners. The inner edges join. Keep button sizes equal so the shared border stays even. Check visible focus around the shared borders, especially when a button is disabled.