Skip to content

Commit 425dac9

Browse files
authored
Button: add isIconOnly support to render button with a single icon (#657)
1 parent 6ddcad8 commit 425dac9

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

.changeset/grumpy-pans-design.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@obosbbl/grunnmuren-react': minor
3+
---
4+
5+
Button: add support for `isIconOnly` to render a button with a single icon

packages/react/src/button/Button.stories.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cx } from 'cva';
22
import type { Meta, StoryObj } from '@storybook/react';
3-
import { Edit } from '@obosbbl/grunnmuren-icons-react';
3+
import { Edit, Search } from '@obosbbl/grunnmuren-icons-react';
44

55
import { Button } from './Button';
66

@@ -58,7 +58,7 @@ export const Tertiary: Story = {
5858
},
5959
};
6060

61-
export const WithIcon = () => {
61+
export const WithIconAndText = () => {
6262
return (
6363
<div className="flex gap-8 p-8">
6464
<Button>
@@ -71,6 +71,16 @@ export const WithIcon = () => {
7171
);
7272
};
7373

74+
export const IconOnly = () => {
75+
return (
76+
<div className="p-8">
77+
<Button isIconOnly aria-label="Søk">
78+
<Search />
79+
</Button>
80+
</div>
81+
);
82+
};
83+
7484
export const ButtonSandbox = () => {
7585
return (
7686
<div className="flex flex-col">

packages/react/src/button/Button.tsx

+23-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import { useClientLayoutEffect } from '../utils/useClientLayoutEffect';
1010

1111
const buttonVariants = cva({
1212
base: [
13-
'inline-flex min-h-[44px] cursor-pointer items-center justify-center whitespace-nowrap rounded-lg px-4 py-2 font-medium transition-all duration-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2',
14-
// Spaccing when using the button with icons
15-
'[&>svg]:first-of-type:mr-2.5 [&>svg]:last-of-type:ml-2.5',
13+
'inline-flex min-h-[44px] cursor-pointer items-center justify-center whitespace-nowrap rounded-lg font-medium transition-all duration-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2',
1614
],
1715
variants: {
1816
/**
@@ -34,6 +32,16 @@ const buttonVariants = cva({
3432
mint: 'focus-visible:ring-mint focus-visible:ring-offset-green-dark',
3533
white: 'focus-visible:ring-white focus-visible:ring-offset-blue',
3634
},
35+
/**
36+
* When the button is without text, but with a single icon.
37+
* @default false
38+
*/
39+
isIconOnly: {
40+
true: 'p-2 [&>svg]:h-7 [&>svg]:w-7',
41+
false:
42+
// The of-type classes takes care to add spacing when the button is used with icons
43+
'px-4 py-2 [&>svg]:first-of-type:mr-2.5 [&>svg]:last-of-type:ml-2.5',
44+
},
3745
},
3846
compoundVariants: [
3947
{
@@ -83,6 +91,7 @@ const buttonVariants = cva({
8391
defaultVariants: {
8492
variant: 'primary',
8593
color: 'green',
94+
isIconOnly: false,
8695
},
8796
});
8897

@@ -106,8 +115,16 @@ type ButtonProps = VariantProps<typeof buttonVariants> & {
106115
} & ButtonOrLinkProps;
107116

108117
function Button(props: ButtonProps) {
109-
const { children, className, color, loading, variant, style, ...restProps } =
110-
props;
118+
const {
119+
children,
120+
className,
121+
color,
122+
isIconOnly,
123+
loading,
124+
variant,
125+
style,
126+
...restProps
127+
} = props;
111128

112129
// TODO: Merge refs when we use RAC
113130
const buttonRef = useRef<HTMLButtonElement | HTMLAnchorElement>(null);
@@ -136,7 +153,7 @@ function Button(props: ButtonProps) {
136153
// @ts-expect-error TS doesn't agree here taht restProps is safe to spread, because restProps for anchors aren't type compatible with restProps for buttons, but that should be okay here
137154
<Component
138155
aria-busy={loading ? true : undefined}
139-
className={buttonVariants({ className, color, variant })}
156+
className={buttonVariants({ className, color, isIconOnly, variant })}
140157
ref={buttonRef as never}
141158
style={{
142159
...style,

0 commit comments

Comments
 (0)