Skip to content

Commit 1b629e1

Browse files
feat: set ButtonTwoLines component styles for Tailwind
1 parent 59ed0c3 commit 1b629e1

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { BiCircle } from "react-icons/bi"
2+
import { Stack } from "@chakra-ui/react"
3+
import { Meta, StoryObj } from "@storybook/react"
4+
5+
import ButtonTwoLinesComponent from "../buttons/ButtonTwoLines"
6+
7+
const meta = {
8+
title: "Atoms / Form / ShadCn Buttons / ButtonTwoLines",
9+
component: ButtonTwoLinesComponent,
10+
} satisfies Meta<typeof ButtonTwoLinesComponent>
11+
12+
export default meta
13+
14+
type Story = StoryObj<typeof meta>
15+
16+
export const ButtonTwoLines: Story = {
17+
args: {
18+
componentType: "button",
19+
icon: BiCircle,
20+
mainText: "Main Text",
21+
helperText: "Helper Text",
22+
className: "w-[300px]",
23+
},
24+
render: (args) => (
25+
<Stack spacing="8">
26+
<ButtonTwoLinesComponent {...args} />
27+
<ButtonTwoLinesComponent
28+
{...args}
29+
iconAlignment="end"
30+
size="sm"
31+
reverseTextOrder
32+
/>
33+
</Stack>
34+
),
35+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import type { IconType } from "react-icons/lib"
2+
3+
import { cn } from "@/lib/utils/cn"
4+
5+
import { Stack } from "../flex"
6+
7+
import {
8+
Button,
9+
ButtonLink,
10+
type ButtonLinkProps,
11+
type ButtonProps,
12+
} from "./Button"
13+
14+
type CommonProps = {
15+
icon: IconType
16+
iconAlignment?: "left" | "right" | "start" | "end"
17+
/**
18+
* Reduced choices of the button variant.
19+
*
20+
* This component only accepts the `solid` or `outline` variant
21+
*/
22+
variant?: "solid" | "outline"
23+
/**
24+
* Reduced choices of the button size
25+
*
26+
* This component only accepts the `md` or `sm` sizes
27+
*/
28+
size?: "md" | "sm"
29+
mainText: string
30+
helperText: string
31+
/**
32+
* Should the main text be below the helper text instead of ab?
33+
*/
34+
reverseTextOrder?: boolean
35+
}
36+
37+
type OmittedTypes = "variant" | "size" | "children"
38+
39+
type ButtonTypeProps = CommonProps &
40+
Omit<ButtonProps, OmittedTypes> & {
41+
componentType: "button"
42+
}
43+
44+
type ButtonLinkTypeProps = CommonProps &
45+
Omit<ButtonLinkProps, OmittedTypes> & {
46+
componentType: "link"
47+
}
48+
49+
type ButtonTwoLinesProps = ButtonTypeProps | ButtonLinkTypeProps
50+
51+
const ButtonTwoLines = ({
52+
iconAlignment = "start",
53+
className,
54+
...props
55+
}: ButtonTwoLinesProps) => {
56+
const isIconLeft = ["left", "start"].includes(iconAlignment)
57+
58+
const commonClassStyles = cn(
59+
isIconLeft ? "text-start justify-start" : "text-end justify-end",
60+
className
61+
)
62+
63+
if (props.componentType === "link") {
64+
return (
65+
<ButtonLink className={commonClassStyles} {...props}>
66+
<ChildContent {...props} isIconLeft={isIconLeft} />
67+
</ButtonLink>
68+
)
69+
}
70+
return (
71+
<Button className={commonClassStyles} {...props}>
72+
<ChildContent {...props} isIconLeft={isIconLeft} />
73+
</Button>
74+
)
75+
}
76+
77+
export default ButtonTwoLines
78+
79+
const ChildContent = (
80+
props: Omit<ButtonTwoLinesProps, "iconAlignment"> & { isIconLeft: boolean }
81+
) => {
82+
const {
83+
reverseTextOrder = false,
84+
size = "md",
85+
mainText,
86+
helperText,
87+
icon: Icon,
88+
isIconLeft,
89+
isSecondary,
90+
variant,
91+
} = props
92+
93+
const ButtonIcon = () => (
94+
<Icon
95+
// TODO: Text size here should not be marked important after migration
96+
className="!text-2xl"
97+
/>
98+
)
99+
return (
100+
<>
101+
{isIconLeft && <ButtonIcon />}
102+
<Stack
103+
className={cn(
104+
"gap-0",
105+
reverseTextOrder ? "flex-col-reverse" : "flex-col"
106+
)}
107+
>
108+
<span
109+
className={cn("text-md", size === "md" ? "text-bold" : "text-normal")}
110+
>
111+
{mainText}
112+
</span>
113+
<span
114+
className={cn(
115+
"text-xs",
116+
variant === "outline" || isSecondary
117+
? "text-body-medium"
118+
: undefined
119+
)}
120+
>
121+
{helperText}
122+
</span>
123+
</Stack>
124+
{!isIconLeft && <ButtonIcon />}
125+
</>
126+
)
127+
}

0 commit comments

Comments
 (0)