Button group component in shandcn #4283
Replies: 9 comments 6 replies
-
That is a necessary feature, one more over here. |
Beta Was this translation helpful? Give feedback.
-
Thanks for showing interest in this feature request @gsi-chao ❤️ Would you like to write and propose your solution to this one? |
Beta Was this translation helpful? Give feedback.
-
here is my little hack
|
Beta Was this translation helpful? Give feedback.
-
Here is what I did for my project, keeps things more declarative. import { Children, ReactElement, cloneElement } from 'react';
import { ButtonProps } from '@/components/ui/button';
import { cn } from '@/lib/utils';
interface ButtonGroupProps {
className?: string;
orientation?: 'horizontal' | 'vertical';
children: ReactElement<ButtonProps>[];
}
export const ButtonGroup = ({
className,
orientation = 'horizontal',
children,
}: ButtonGroupProps) => {
const totalButtons = Children.count(children);
const isHorizontal = orientation === 'horizontal';
const isVertical = orientation === 'vertical';
return (
<div
className={cn(
'flex',
{
'flex-col': isVertical,
'w-fit': isVertical,
},
className
)}
>
{Children.map(children, (child, index) => {
const isFirst = index === 0;
const isLast = index === totalButtons - 1;
return cloneElement(child, {
className: cn(
{
'rounded-l-none': isHorizontal && !isFirst,
'rounded-r-none': isHorizontal && !isLast,
'border-l-0': isHorizontal && !isFirst,
'rounded-t-none': isVertical && !isFirst,
'rounded-b-none': isVertical && !isLast,
'border-t-0': isVertical && !isFirst,
},
child.props.className
),
});
})}
</div>
);
}; Usage: <ButtonGroup>
<Button variant="outline">Button 1</Button>
<Button variant="outline">Button 2</Button>
<Button variant="outline">Button 3</Button>
</ButtonGroup>
<ButtonGroup orientation="vertical">
<Button variant="outline">A</Button>
<Button variant="outline">B</Button>
<Button variant="outline">C</Button>
</ButtonGroup> Screenshot: |
Beta Was this translation helpful? Give feedback.
-
Has this feature already been added (not in doc)? |
Beta Was this translation helpful? Give feedback.
-
Have to do it yourself. But cursor helps a lot :)
…On Sat, Mar 1, 2025 at 5:45 PM ZhaoMA ***@***.***> wrote:
Has this feature already been added (not in doc)?
Or users are supposed to do their own version?
—
Reply to this email directly, view it on GitHub
<#4283 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABM6IQMJKMNWPWC2SODOVVT2SIZ63AVCNFSM6AAAAABK2526DWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMZWGI3DSOA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I'm a bit new to shadcn/ui, and are recently comparing it with MUI. Is there any design guidelines for what should (not) be included? |
Beta Was this translation helpful? Give feedback.
-
This might be a good case for creating a block:
https://ui.shadcn.com/docs/blocks
…On Sun, Mar 2, 2025 at 7:43 AM ZhaoMA ***@***.***> wrote:
I'm a bit new to shadcn/ui, and are recently comparing it with MUI.
I understand that shadcn is aiming for flexibility and doesn't provide as
many as re-made components as other UI libraries, but for things like this,
is there any reason that we don't make it into the official release?
Is there any design guidelines for what should (not) be included?
—
Reply to this email directly, view it on GitHub
<#4283 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABM6IQJWFXEMW47BNTKNH4D2SL4FTAVCNFSM6AAAAABK2526DWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMZWGU3TGMA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
If you have tailwind 4 this is a simpler code import { cn } from '@/lib/utils'
import { type VariantProps, cva } from 'class-variance-authority'
import type * as React from 'react'
const buttonGroupVariants = cva('flex items-center *:rounded-none', {
variants: {
orientation: {
horizontal: 'flex-row *:first:rounded-s-md *:last:rounded-e-md',
vertical: 'flex-col *:first:rounded-t-md *:last:rounded-b-md',
},
},
defaultVariants: {
orientation: 'horizontal',
},
})
export const ButtonGroup = ({
className,
orientation,
...props
}: React.ComponentProps<'div'> & VariantProps<typeof buttonGroupVariants>) => {
return <div className={cn(buttonGroupVariants({ orientation, className }))} {...props} />
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello team, I am having a requirement of ButtonGroup component.
Attaching a demo screenshot for reference[1]
I am not sure if this component will going to be there in the upcoming updates, or still in development process. Would love to know if there's any help required here.
As of now, I am using my own solution to use a ButtonGroup for wrapping shadcn buttons, in one of my projects.
[1] - The attached ButtonGroup example is from MantineUI. Would love to have something similar in shadcn-ui as well.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions