Open History to see the last commit
1import { cva } from 'class-variance-authority'
2import { ark } from '@ark-ui/solid/factory'
3import { splitProps } from 'solid-js'
4import type { VariantProps } from 'class-variance-authority'
5import type { HTMLArkProps } from '@ark-ui/solid/factory'
6import type { JSX } from 'solid-js'
7import type { SeparatorProps } from '@/components/ui/separator'
8import { Separator } from '@/components/ui/separator'
9import { cn } from '@/utils'
10β
11// Ported from the original shadcn/ui + Radix UI + React component.
12// This wraps the Ark UI + SolidJS primitive and preserves shadcn's Tailwind-based
13// design-system API, variants, and styling.
14// https://ui.shadcn.com/
15β
16const buttonGroupVariants = cva(
17 "group/button-group flex w-fit items-stretch *:focus-visible:relative *:focus-visible:z-10 has-[>[data-slot=button-group]]:gap-2 has-[select[aria-hidden=true]:last-child]:[&>[data-slot=select-trigger]:last-of-type]:rounded-r-lg [&>[data-slot=select-trigger]:not([class*='w-'])]:w-fit [&>input]:flex-1",
18 {
19 variants: {
20 orientation: {
21 horizontal:
22 '[&>*:not(:first-child)]:rounded-l-none [&>*:not(:first-child)]:border-l-0 [&>*:not(:last-child)]:rounded-r-none [&>[data-slot]:not(:has(~[data-slot]))]:rounded-r-lg!',
23 vertical:
24 'flex-col [&>*:not(:first-child)]:rounded-t-none [&>*:not(:first-child)]:border-t-0 [&>*:not(:last-child)]:rounded-b-none [&>[data-slot]:not(:has(~[data-slot]))]:rounded-b-lg!',
25 },
26 },
27 defaultVariants: {
28 orientation: 'horizontal',
29 },
30 },
31)
32β
33type ButtonGroupProps = JSX.HTMLElementTags['div'] &
34 VariantProps<typeof buttonGroupVariants>
35β
36/**
37 * A grouped container that visually joins adjacent buttons and controls.
38 *
39 * @example
40 * ```tsx
41 * <ButtonGroup>
42 * <ButtonGroupText>Status</ButtonGroupText>
43 * <Button variant="outline">Open</Button>
44 * <ButtonGroupSeparator />
45 * <Button variant="outline">Closed</Button>
46 * </ButtonGroup>
47 * ```
48 */
49function ButtonGroup(props: ButtonGroupProps) {
50 const [local, others] = splitProps(props, ['class', 'orientation'])
51β
52 const orientation = () => local.orientation ?? 'horizontal'
53β
54 return (
55 <div
56 {...others}
57 role="group"
58 data-slot="button-group"
59 data-orientation={orientation()}
60 class={cn(
61 buttonGroupVariants({ orientation: orientation() }),
62 local.class,
63 )}
64 />
65 )
66}
67β
68/**
69 * Static text or icon content styled to align with buttons inside a ButtonGroup.
70 *
71 * @example
72 * ```tsx
73 * <ButtonGroup>
74 * <ButtonGroupText>Status</ButtonGroupText>
75 * <Button variant="outline">Open</Button>
76 * <ButtonGroupSeparator />
77 * <Button variant="outline">Closed</Button>
78 * </ButtonGroup>
79 * ```
80 */
81function ButtonGroupText(props: HTMLArkProps<'div'>) {
82 const [local, others] = splitProps(props, ['class'])
83β
84 return (
85 <ark.div
86 {...others}
87 class={cn(
88 "flex items-center gap-2 rounded-lg border bg-muted px-2.5 text-sm font-medium [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4",
89 local.class,
90 )}
91 />
92 )
93}
94β
95/**
96 * A divider that separates adjacent controls inside a ButtonGroup.
97 *
98 * @example
99 * ```tsx
100 * <ButtonGroup>
101 * <ButtonGroupText>Status</ButtonGroupText>
102 * <Button variant="outline">Open</Button>
103 * <ButtonGroupSeparator />
104 * <Button variant="outline">Closed</Button>
105 * </ButtonGroup>
106 * ```
107 */
108function ButtonGroupSeparator(props: SeparatorProps) {
109 const [local, others] = splitProps(props, ['class', 'orientation'])
110β
111 const orientation = () => local.orientation ?? 'vertical'
112β
113 return (
114 <Separator
115 {...others}
116 data-slot="button-group-separator"
117 orientation={orientation()}
118 class={cn(
119 'relative self-stretch bg-input data-horizontal:mx-px data-horizontal:w-auto data-vertical:my-px data-vertical:h-auto',
120 local.class,
121 )}
122 />
123 )
124}
125β
126export { ButtonGroup, ButtonGroupSeparator, ButtonGroupText }