-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ButtonGroup to separate core component
- Loading branch information
1 parent
07d454d
commit 6264717
Showing
5 changed files
with
84 additions
and
54 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
packages/ui/src/components/core/button-group/button-group.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { Button } from "#components/core/button/button.js"; | ||
|
||
import { ButtonGroup } from "./button-group"; | ||
|
||
const meta = { | ||
title: "UI/Core/ButtonGroup", | ||
component: ButtonGroup, | ||
tags: ["autodocs"], | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
} satisfies Meta<typeof ButtonGroup>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
children: [ | ||
<Button key="1">Left</Button>, | ||
<Button key="1">Center</Button>, | ||
<Button key="1">Right</Button>, | ||
], | ||
}, | ||
}; |
53 changes: 53 additions & 0 deletions
53
packages/ui/src/components/core/button-group/button-group.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from "react"; | ||
|
||
import type { ButtonProps } from "#components/core/button/button.js"; | ||
import { cn } from "#lib/utils.js"; | ||
|
||
interface ButtonGroupProps { | ||
className?: string; | ||
orientation?: "horizontal" | "vertical"; | ||
children: React.ReactElement<ButtonProps>[]; | ||
} | ||
|
||
export function ButtonGroup({ | ||
className, | ||
orientation = "horizontal", | ||
children, | ||
}: ButtonGroupProps) { | ||
const totalButtons = React.Children.count(children); | ||
const isHorizontal = orientation === "horizontal"; | ||
const isVertical = orientation === "vertical"; | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
"flex", | ||
{ | ||
"flex-col": isVertical, | ||
"w-fit": isVertical, | ||
}, | ||
className, | ||
)} | ||
> | ||
{React.Children.map(children, (child, index) => { | ||
const isFirst = index === 0; | ||
const isLast = index === totalButtons - 1; | ||
|
||
return React.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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./button-group"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters