A container that groups related buttons together with consistent styling.
1<script lang="ts">
2import { Button } from "@/components/ui/button"
3import { ButtonGroup } from "@/components/ui/button-group"
4import {
Installation
pnpm dlx sprawlify@latest add button-groupnpx sprawlify@latest add button-groupyarn sprawlify@latest add button-groupbunx --bun sprawlify@latest add button-group
Install the following dependencies:
pnpm add @sprawlify/primitives @sprawlify/sveltenpm install @sprawlify/primitives @sprawlify/svelteyarn add @sprawlify/primitives @sprawlify/sveltebun add @sprawlify/primitives @sprawlify/svelte
Add the following files to your project:
1<script lang="ts">
2import { cn } from "@/lib/utils"
3import type { ComponentProps } from "svelte";
4import { Separator } from "../separator"
5
6interface Props extends ComponentProps<typeof Separator> {}
7
8let { class: className, orientation = "vertical", ...rest }: Props = $props()
9</script>
10
11<Separator
12 data-scope="button-group"
13 data-part="separator"
14 data-slot="button-group-separator"
15 {orientation}
16 class={cn("bg-input relative self-stretch data-horizontal:mx-px data-horizontal:w-auto data-vertical:my-px data-vertical:h-auto", className)}
17 {...rest}
18/>1<script lang="ts">
2import { cn } from "@/lib/utils"
3import { Sprawlify, type PolymorphicProps } from "@sprawlify/svelte"
4import type { HTMLAttributes } from "svelte/elements"
5
6interface Props extends HTMLAttributes<HTMLDivElement>, PolymorphicProps<"div"> { }
7
8let { class: className, children, ...rest }: Props = $props()
9</script>
10
11<Sprawlify
12 as="div"
13 class={cn("bg-muted gap-2 rounded-lg border px-2.5 text-sm font-medium [&_svg:not([class*='size-'])]:size-4 flex items-center [&_svg]:pointer-events-none", className)}
14 {...rest}
15>
16 {@render children?.()}
17</Sprawlify>1<script module lang="ts">
2import { cva } from "class-variance-authority"
3export const buttonGroupVariants = cva(
4 "has-[>[data-slot=button-group]]:gap-2 has-[select[aria-hidden=true]:last-child]:[&>[data-slot=select-trigger]:last-of-type]:rounded-r-lg flex w-fit items-stretch *:focus-visible:z-10 *:focus-visible:relative [&>[data-slot=select-trigger]:not([class*='w-'])]:w-fit [&>input]:flex-1",
5 {
6 variants: {
7 orientation: {
8 horizontal: "[&>[data-slot]:not(:has(~[data-slot]))]:rounded-r-lg! [&>*:not(:first-child)]:rounded-l-none [&>*:not(:first-child)]:border-l-0 [&>*:not(:last-child)]:rounded-r-none",
9 vertical: "[&>[data-slot]:not(:has(~[data-slot]))]:rounded-b-lg! flex-col [&>*:not(:first-child)]:rounded-t-none [&>*:not(:first-child)]:border-t-0 [&>*:not(:last-child)]:rounded-b-none",
10 },
11 },
12 defaultVariants: { orientation: "horizontal" },
13 }
14)
15</script>
16
17<script lang="ts">
18import { cn } from "@/lib/utils"
19import { Sprawlify, type PolymorphicProps } from "@sprawlify/svelte"
20import type { HTMLAttributes } from "svelte/elements"
21import type { VariantProps } from "class-variance-authority"
22
23interface Props extends HTMLAttributes<HTMLDivElement>, VariantProps<typeof buttonGroupVariants>, PolymorphicProps<"div"> {}
24
25let { class: className, orientation = "horizontal", children, ...rest }: Props = $props()
26</script>
27
28<Sprawlify
29 as="div"
30 data-scope="button-group"
31 data-part="root"
32 role="group"
33 data-slot="button-group"
34 data-orientation={orientation}
35 class={cn(buttonGroupVariants({ orientation, className }))}
36 {...rest}
37>
38 {@render children?.()}
39</Sprawlify>1export { default as ButtonGroup, buttonGroupVariants } from "./button-group.svelte";2export { default as ButtonGroupSeparator } from "./button-group-separator.svelte";3export { default as ButtonGroupText } from "./button-group-text.svelte";4Update the import paths to match your project setup.
Usage
1import {
2 ButtonGroup,
3 ButtonGroupSeparator,
4 ButtonGroupText
5} from "@/components/ui/button-group"1<ButtonGroup>
2 <Button>Button 1</Button>
3 <Button>Button 2</Button>
4</ButtonGroup>Examples
Orientation
Set the orientation prop to change the button group layout.
1<script lang="ts">
2import { Button } from "@/components/ui/button"
3import { ButtonGroup } from "@/components/ui/button-group"
4import { MinusIcon, PlusIcon } from "lucide-svelte"
Separator
The ButtonGroupSeparator component visually divides buttons within a group.
1<script lang="ts">
2import { Button } from "@/components/ui/button"
3import {
4 ButtonGroup,
Size
Control the size of buttons using the size prop on individual buttons.
1<script lang="ts">
2import { Button } from "@/components/ui/button"
3import { ButtonGroup } from "@/components/ui/button-group"
4import { PlusIcon } from "lucide-svelte"
Split
Create a split button group by adding two buttons separated by a ButtonGroupSeparator.
1<script lang="ts">
2import { Button } from "@/components/ui/button"
3import {
4 ButtonGroup,
Get PRO
Need premium blocks and templates? Upgrade to PRO and get access.