Skip to content

Commit 284c969

Browse files
authored
Add showBorder prop to Badge and StatusBadge components (#2830)
## Summary: The Figma badge components for the status badges support disabling the border. To match this in code, we add a `showBorder` prop for the `Badge` and `StatusBadge` components When `showBorder=false`, we set the border color to `transparent`. This is so overall, the badge maintains the same size, regardless of if the border is shown or not Note: The Figma badge components now includes a NeutralBadge. I'll create this component in a separate PR! Issue: WB-2028 Design: https://www.figma.com/design/EuFu0U7gqc1koXm8ZhlOLp/%E2%9A%A1%EF%B8%8F-Components?node-id=2151-112&m=dev ## Test plan: 1. Confirm the `showBorder` prop shows/hides the border only on the Badge and StatusBadge components Author: beaesguerra Reviewers: jandrade Required Reviewers: Approved By: jandrade Checks: ✅ 12 checks were successful, ⏭️ 2 checks have been skipped Pull Request URL: #2830
1 parent f171b97 commit 284c969

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

.changeset/dirty-hairs-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/wonder-blocks-badge": minor
3+
---
4+
5+
Add `showBorder` prop (defaults to true) to Badge and StatusBadge components

__docs__/wonder-blocks-badge/badge-testing-snapshots.stories.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,25 @@ export const StateSheetStory: StoryComponentType = {
133133
<View style={{gap: sizing.size_080}}>
134134
<HeadingLarge>Badge</HeadingLarge>
135135
<StateSheet rows={rows} columns={columns} states={states}>
136-
{({props}) => <Badge {...props} />}
136+
{({props}) => (
137+
<View
138+
style={{gap: sizing.size_100, flexDirection: "row"}}
139+
>
140+
<Badge {...props} />
141+
<Badge {...props} showBorder={false} />
142+
</View>
143+
)}
137144
</StateSheet>
138145
<HeadingLarge>Status Badge</HeadingLarge>
139146
<StateSheet rows={statusRows} columns={columns} states={states}>
140-
{({props}) => <StatusBadge {...props} />}
147+
{({props}) => (
148+
<View
149+
style={{gap: sizing.size_100, flexDirection: "row"}}
150+
>
151+
<StatusBadge {...props} />
152+
<StatusBadge {...props} showBorder={false} />
153+
</View>
154+
)}
141155
</StateSheet>
142156
<HeadingLarge>Gem Badge</HeadingLarge>
143157
<StateSheet

__docs__/wonder-blocks-badge/badge.stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ export const Default = {
6868
},
6969
};
7070

71+
/**
72+
* A badge can be used without a border.
73+
*/
74+
export const NoBorder: StoryComponentType = {
75+
args: {
76+
icon: "cookieBold",
77+
label: "Badge",
78+
showBorder: false,
79+
},
80+
};
81+
7182
/**
7283
* A badge can be used with only a label.
7384
*/

__docs__/wonder-blocks-badge/status-badge.stories.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,39 @@ export const Kinds = {
128128
},
129129
};
130130

131+
/**
132+
* A status badge can be used without a border.
133+
*/
134+
export const NoBorder = {
135+
args: {
136+
icon: "cookieBold",
137+
},
138+
render(args: Omit<PropsFor<typeof StatusBadge>, "icon"> & {icon: string}) {
139+
return (
140+
<View style={styles.container}>
141+
{kinds.map((kind) => {
142+
const kindLabel =
143+
kind.charAt(0).toUpperCase() + kind.slice(1);
144+
return (
145+
<StatusBadge
146+
showBorder={false}
147+
key={kind}
148+
kind={kind}
149+
label={kindLabel}
150+
icon={
151+
<PhosphorIcon
152+
icon={args.icon}
153+
aria-label="Cookie"
154+
/>
155+
}
156+
/>
157+
);
158+
})}
159+
</View>
160+
);
161+
},
162+
};
163+
131164
/**
132165
* A badge can be used with only a label.
133166
*/

packages/wonder-blocks-badge/src/components/badge.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import * as React from "react";
1010
import {focusStyles} from "@khanacademy/wonder-blocks-styles";
1111
import {BaseBadgeProps, IconLabelProps} from "../types";
1212

13-
type Props = IconLabelProps & BaseBadgeProps;
13+
type BadgeProps = {
14+
/**
15+
* Whether to show the border. Defaults to `true`.
16+
*/
17+
showBorder?: boolean;
18+
};
19+
20+
type Props = IconLabelProps & BaseBadgeProps & BadgeProps;
1421

1522
const StyledSpan = addStyle("span");
1623

@@ -29,6 +36,7 @@ const Badge = React.forwardRef<HTMLDivElement, Props>(function Badge(
2936
testId,
3037
styles: stylesProp,
3138
tag = "div",
39+
showBorder = true,
3240
...otherProps
3341
} = props;
3442
const StyledTag = React.useMemo(() => addStyle(tag, styles.default), [tag]);
@@ -46,6 +54,10 @@ const Badge = React.forwardRef<HTMLDivElement, Props>(function Badge(
4654
styles.defaultBadgeStyling,
4755
icon && !label ? styles.iconOnly : {},
4856
stylesProp?.root,
57+
// disabling border needs to be after custom styles so it can
58+
// override any border styles that were applied by another badge
59+
// component
60+
!showBorder && styles.transparentBorder,
4961
]}
5062
{...otherProps}
5163
>
@@ -156,4 +168,7 @@ const styles = StyleSheet.create({
156168
iconOnly: {
157169
padding: badgeTokens.root.layout.iconOnly.padding,
158170
},
171+
transparentBorder: {
172+
borderColor: semanticColor.core.transparent,
173+
},
159174
});

packages/wonder-blocks-badge/src/components/status-badge.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ type Props = {
99
* The kind of badge to display. Defaults to `info`.
1010
*/
1111
kind?: "info" | "success" | "warning" | "critical";
12+
/**
13+
* Whether to show the border. Defaults to `true`.
14+
*/
15+
showBorder?: boolean;
1216
} & BaseBadgeProps &
1317
IconLabelProps;
1418

@@ -19,10 +23,11 @@ type Props = {
1923
* for the status kinds. For more details, see the `Badge` docs.
2024
*/
2125
const StatusBadge = React.forwardRef<HTMLDivElement, Props>((props, ref) => {
22-
const {kind = "info", ...otherProps} = props;
26+
const {kind = "info", showBorder, ...otherProps} = props;
2327
return (
2428
<Badge
2529
ref={ref}
30+
showBorder={showBorder}
2631
{...otherProps}
2732
styles={{
2833
...otherProps.styles,

0 commit comments

Comments
 (0)