Skip to content

Commit 642ad96

Browse files
sxsx1xsxsYi Zhang
andauthored
[CAP-theme] Fix small icon only badge padding (#576)
Co-authored-by: Yi Zhang <yizhang9@microsoft.com>
1 parent 3b741bd commit 642ad96

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "[CAP-theme] fix icon only small badge margin",
4+
"packageName": "@fluentui-contrib/react-cap-theme",
5+
"email": "yizhang9@microsoft.com",
6+
"dependentChangeType": "none"
7+
}

packages/react-cap-theme/src/components/Badge/Badge.styles.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ import {
66
type BadgeState,
77
useBadgeStyles_unstable,
88
} from '@fluentui/react-components';
9+
import * as React from 'react';
910

1011
import { CAP_TOKENS } from '../../theme/CAPTheme';
1112

13+
// Copied from Fluent's badge.styles.ts: we are only overriding a few styles, we don't want to break the styles we don't override
14+
// so we follow the existing pattern here
15+
// The text content of the badge has additional horizontal padding, but there is no `text` slot to add that padding to.
16+
// Instead, add extra padding to the root, and a negative margin on the icon to "remove" the extra padding on the icon.
1217
const textPadding = tokens.spacingHorizontalXXS;
1318

1419
export const useBadgeStyles = makeStyles({
@@ -142,16 +147,19 @@ export function useBadgeStylesHook(state: BadgeState): BadgeState {
142147
styles[`${state.appearance}-${state.color}` as keyof typeof styles]
143148
);
144149

145-
// Override icon spacing for small size
146-
if (state.icon && state.size === 'small') {
147-
const iconPositionClass =
148-
state.iconPosition === 'after'
149-
? iconStyles.afterTextSmall
150-
: iconStyles.beforeTextSmall;
151-
state.icon.className = mergeClasses(
152-
state.icon.className,
153-
iconPositionClass
154-
);
150+
// Copied from Fluent: Handle the edge case where children is 0 (a falsy value that should still render text and have margin)
151+
if (React.Children.toArray(state.root.children).length > 0) {
152+
// Override icon spacing for small size
153+
if (state.icon && state.size === 'small') {
154+
const iconPositionClass =
155+
state.iconPosition === 'after'
156+
? iconStyles.afterTextSmall
157+
: iconStyles.beforeTextSmall;
158+
state.icon.className = mergeClasses(
159+
state.icon.className,
160+
iconPositionClass
161+
);
162+
}
155163
}
156164

157165
return state;

packages/react-cap-theme/stories/components/Badge.stories.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,20 @@ export const CAPBadgeStory = () => {
411411
);
412412
},
413413
},
414+
{
415+
title: 'Small Icon only badge',
416+
render() {
417+
return (
418+
<Badge
419+
size="small"
420+
color="informative"
421+
shape="rounded"
422+
appearance="filled"
423+
icon={<CircleRegular />}
424+
/>
425+
);
426+
},
427+
},
414428
]}
415429
/>
416430
);

0 commit comments

Comments
 (0)