diff --git a/.changeset/twelve-mugs-yell.md b/.changeset/twelve-mugs-yell.md new file mode 100644 index 0000000..e2c0755 --- /dev/null +++ b/.changeset/twelve-mugs-yell.md @@ -0,0 +1,5 @@ +--- +"@openfun/cunningham-react": patch +--- + +Make Button handle icon only diff --git a/packages/react/src/components/Button/index.scss b/packages/react/src/components/Button/index.scss index 6da857c..cd9449e 100644 --- a/packages/react/src/components/Button/index.scss +++ b/packages/react/src/components/Button/index.scss @@ -12,6 +12,12 @@ font-size: var(--c--components--button--font-size); font-weight: var(--c--components--button--font-weight); + &--icon-only { + padding: 0; + width: var(--c--components--button--height); + justify-content: center; + } + &--with-icon--left, &--with-icon--right { gap: var(--c--theme--spacings--t); } diff --git a/packages/react/src/components/Button/index.spec.tsx b/packages/react/src/components/Button/index.spec.tsx index c322f08..919fe6a 100644 --- a/packages/react/src/components/Button/index.spec.tsx +++ b/packages/react/src/components/Button/index.spec.tsx @@ -9,14 +9,25 @@ describe("); const button = screen.getByRole("button", { name: "Test button" }); - expect(button.classList.contains("c__button")).toBe(true); + expect(Array.from(button.classList)).toContain("c__button"); }); it("renders with custom class when using left icon", () => { render(); const button = screen.getByText("Test button"); - expect(button.classList.contains("c__button")).toBe(true); - expect(button.classList.contains("c__button--with-icon--left")).toBe(true); + const classes = Array.from(button.classList); + expect(classes).toContain("c__button"); + expect(classes).toContain("c__button--with-icon--left"); + }); + + it("renders with modifier class --icon-only when only icon is defined", () => { + render( ); const button = screen.getByText("Test button"); - expect(button.classList.contains("c__button")).toBe(true); - expect(button.classList.contains("c__button--with-icon--right")).toBe(true); + const classes = Array.from(button.classList); + expect(classes).toContain("c__button"); + expect(classes).toContain("c__button--with-icon--right"); }); it("call onClick when click occurs", async () => { diff --git a/packages/react/src/components/Button/index.stories.mdx b/packages/react/src/components/Button/index.stories.mdx index 5ee9b7b..4a0f604 100644 --- a/packages/react/src/components/Button/index.stories.mdx +++ b/packages/react/src/components/Button/index.stories.mdx @@ -37,6 +37,12 @@ You can use icons within the button by passing the icon name as a prop. +You can also use button with only an icon. + + + + + ## Disabled The button can be disabled. The disabled button will render the same no matter what color is used. diff --git a/packages/react/src/components/Button/index.stories.tsx b/packages/react/src/components/Button/index.stories.tsx index d258a00..9001f62 100644 --- a/packages/react/src/components/Button/index.stories.tsx +++ b/packages/react/src/components/Button/index.stories.tsx @@ -84,3 +84,24 @@ IconRight.args = { ), color: "primary", }; + +export const IconOnly = Template.bind({}); +IconOnly.args = { + icon: ( + + + + ), + color: "primary", +}; diff --git a/packages/react/src/components/Button/index.tsx b/packages/react/src/components/Button/index.tsx index 4af5f16..482a472 100644 --- a/packages/react/src/components/Button/index.tsx +++ b/packages/react/src/components/Button/index.tsx @@ -14,9 +14,12 @@ export const Button = ({ ...props }: Props) => { const classes = ["c__button", "c__button--" + color]; - if (icon) { + if (icon && children) { classes.push("c__button--with-icon--" + iconPosition); } + if (icon && !children) { + classes.push("c__button--icon-only"); + } return (