Button group component in shandcn #4283
yashsehgal
started this conversation in
General
Replies: 4 comments 4 replies
-
That is a necessary feature, one more over here. |
Beta Was this translation helpful? Give feedback.
0 replies
-
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.
2 replies
-
here is my little hack
|
Beta Was this translation helpful? Give feedback.
1 reply
-
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.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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