Skip to content

Commit 17e1174

Browse files
committed
adds 'count' prop to IconButton
1 parent a9c45f4 commit 17e1174

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

packages/react/src/Button/ButtonBase.module.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@
8585
}
8686
}
8787

88+
&.IconButton--withCount {
89+
width: unset;
90+
min-width: var(--control-medium-size);
91+
padding-inline: var(--control-small-paddingInline-condensed);
92+
93+
&:where([data-size='small']) {
94+
min-width: var(--control-small-size);
95+
}
96+
97+
&:where([data-size='large']) {
98+
min-width: var(--control-large-size);
99+
}
100+
}
101+
88102
/* LinkButton */
89103

90104
&[href] {

packages/react/src/Button/ButtonBase.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,21 @@ const ButtonBase = forwardRef(
123123
) : isElement(Icon) ? (
124124
Icon
125125
) : (
126-
<Icon />
126+
<span data-component="buttonContent" data-align={alignContent} className={classes.ButtonContent}>
127+
<Icon />
128+
{count !== undefined
129+
? renderModuleVisual(
130+
() => (
131+
<CounterLabel className={classes.CounterLabel} data-component="ButtonCounter">
132+
{count}
133+
</CounterLabel>
134+
),
135+
Boolean(loading) && !LeadingVisual,
136+
'trailingVisual',
137+
true,
138+
)
139+
: null}
140+
</span>
127141
)
128142
) : (
129143
<>
@@ -244,7 +258,21 @@ const ButtonBase = forwardRef(
244258
) : isElement(Icon) ? (
245259
Icon
246260
) : (
247-
<Icon />
261+
<span data-component="buttonContent" data-align={alignContent} className={classes.ButtonContent}>
262+
<Icon />
263+
{count !== undefined
264+
? renderModuleVisual(
265+
() => (
266+
<CounterLabel className={classes.CounterLabel} data-component="ButtonCounter">
267+
{count}
268+
</CounterLabel>
269+
),
270+
Boolean(loading) && !LeadingVisual,
271+
'trailingVisual',
272+
true,
273+
)
274+
: null}
275+
</span>
248276
)
249277
) : (
250278
<>

packages/react/src/Button/IconButton.features.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,5 @@ export const KeybindingHintOnDescription = () => (
9494
)
9595

9696
export const KeybindingHint = () => <IconButton icon={BoldIcon} aria-label="Bold" keybindingHint="Mod+B" />
97+
98+
export const TrailingCounter = () => <IconButton icon={HeartIcon} aria-label="Favorite" count={42} />

packages/react/src/Button/IconButton.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const IconButton = forwardRef(
1818
description,
1919
disabled,
2020
tooltipDirection,
21+
count,
2122
// This is planned to be a temporary prop until the default tooltip on icon buttons are fully rolled out.
2223
unsafeDisableTooltip = false,
2324
keyshortcuts,
@@ -47,12 +48,15 @@ const IconButton = forwardRef(
4748
return (
4849
<ButtonBase
4950
icon={Icon}
50-
className={clsx(className, classes.IconButton)}
51+
className={clsx(className, classes.IconButton, {
52+
[classes['IconButton--withCount']]: count === 0 || Boolean(count),
53+
})}
5154
data-component="IconButton"
5255
sx={sxStyles}
5356
type="button"
5457
aria-label={ariaLabel}
5558
disabled={disabled}
59+
count={count}
5660
{...props}
5761
// @ts-expect-error StyledButton wants both Anchor and Button refs
5862
ref={forwardedRef}
@@ -70,10 +74,13 @@ const IconButton = forwardRef(
7074
>
7175
<ButtonBase
7276
icon={Icon}
73-
className={clsx(className, classes.IconButton)}
77+
className={clsx(className, classes.IconButton, {
78+
[classes['IconButton--withCount']]: count === 0 || Boolean(count),
79+
})}
7480
data-component="IconButton"
7581
sx={sxStyles}
7682
type="button"
83+
count={count}
7784
aria-keyshortcuts={keyshortcuts ?? undefined}
7885
// If description is provided, we will use the tooltip to describe the button, so we need to keep the aria-label to label the button.
7986
aria-label={description ? ariaLabel : undefined}

packages/react/src/Button/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export type IconButtonProps = ButtonA11yProps & {
9494
unsafeDisableTooltip?: boolean
9595
description?: string
9696
tooltipDirection?: TooltipDirection
97+
count?: ButtonProps['count']
9798
/** @deprecated Use `keybindingHint` instead. */
9899
keyshortcuts?: string
99100
keybindingHint?: string

0 commit comments

Comments
 (0)