Skip to content

Commit

Permalink
feat(ButtonIcon): adding support for prop "align" (#498)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit


- **New Features**
- Added an `align` property to the `ButtonIcon` component to customize
alignment with a default of "center".

- **Tests**
- Implemented new test cases to verify the alignment functionality of
the `ButtonIcon` component.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
aversini authored Apr 15, 2024
1 parent e572c76 commit 3a96dbe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/documentation/src/Components/ButtonIcon.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ export default {
noBackground: false,
size: "medium",
label: "some label",
align: "center",
},
argTypes: {
align: {
options: ["center", "left", "right"],
control: { type: "radio" },
},
mode: {
options: ["dark", "light", "system", "alt-system"],
control: { type: "radio" },
Expand Down
2 changes: 2 additions & 0 deletions packages/ui-components/src/components/Button/ButtonIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const ButtonIcon = React.forwardRef<HTMLButtonElement, ButtonIconProps>(
labelLeft,
spacing,
noBackground = false,
align = "center",

...otherProps
},
Expand All @@ -42,6 +43,7 @@ export const ButtonIcon = React.forwardRef<HTMLButtonElement, ButtonIconProps>(
labelLeft,
spacing,
noBackground,
align,
});
const iconClass = clsx({
"text-copy-accent-dark": mode === "light" && !raw,
Expand Down
5 changes: 5 additions & 0 deletions packages/ui-components/src/components/Button/ButtonTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export type ButtonIconProps = {
* The children to render.
*/
children: React.ReactNode;
/**
* Cell content alignment.
* @default "center"
*/
align?: "left" | "right" | "center";
/**
* The label to show next to the icon.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ describe("ButtonIcon modifiers", () => {
expect(button.className).toContain("w-full");
});

it("should render a left-aligned button icon", async () => {
render(
<ButtonIcon align="left">
<IconSettings />
</ButtonIcon>,
);
const button = await screen.findByRole("button");
expect(button.className).toContain("justify-start");
});

it("should render a right-aligned button icon", async () => {
render(
<ButtonIcon align="right">
<IconSettings />
</ButtonIcon>,
);
const button = await screen.findByRole("button");
expect(button.className).toContain("justify-end");
});

it("should render a center-aligned button icon", async () => {
render(
<ButtonIcon align="center">
<IconSettings />
</ButtonIcon>,
);
const button = await screen.findByRole("button");
expect(button.className).toContain("justify-center");
});

it("should render a size small button icon", async () => {
render(
<ButtonIcon size="small">
Expand Down
11 changes: 9 additions & 2 deletions packages/ui-components/src/components/Button/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type getButtonClassesProps = {
size: string;
type: typeof TYPE_BUTTON | typeof TYPE_ICON | typeof TYPE_LINK;

align?: "center" | "left" | "right";
className?: string;
labelLeft?: string;
labelRight?: string;
Expand All @@ -32,9 +33,11 @@ const getButtonSizesClasses = ({
size,
labelRight,
labelLeft,
align,
}: {
size: string;
type: typeof TYPE_BUTTON | typeof TYPE_ICON | typeof TYPE_LINK;
align?: "center" | "left" | "right";
labelLeft?: string;
labelRight?: string;
}) => {
Expand All @@ -58,7 +61,10 @@ const getButtonSizesClasses = ({
});

case TYPE_ICON:
return clsx("inline-flex items-center justify-center", {
return clsx("inline-flex items-center", {
"justify-center": align === "center",
"justify-start": align === "left",
"justify-end": align === "right",
"h-6 w-6 p-0": size === "small" && !(labelRight || labelLeft),
"h-6 px-4 text-sm font-medium":
size === "small" && (labelRight || labelLeft),
Expand Down Expand Up @@ -279,6 +285,7 @@ export const getButtonClasses = ({
noBackground,
variant,
noTruncate,
align,
}: getButtonClassesProps) => {
if (!variant) {
variant = "primary";
Expand All @@ -290,7 +297,7 @@ export const getButtonClasses = ({
className,
getSpacing(spacing),
getButtonBaseClasses({ mode, variant, noBackground, noTruncate }),
getButtonSizesClasses({ type, size, labelRight, labelLeft }),
getButtonSizesClasses({ type, size, labelRight, labelLeft, align }),
getButtonBorderClasses({ mode, variant, noBorder }),
getButtonFocusClasses({ focusMode }),
getButtonHoverClasses({ mode, variant, disabled }),
Expand Down

0 comments on commit 3a96dbe

Please sign in to comment.