Skip to content

Commit d3b2a73

Browse files
authored
refactor: badges (#271)
1 parent 95ed65d commit d3b2a73

28 files changed

+231
-419
lines changed

.storybook/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
"stories": [
33
"../src/**/*/stories.@(ts|tsx)",
44
"../src/**/*.stories.@(js|jsx|ts|tsx)",
5+
"../src/**/stories/index.@(js|jsx|ts|tsx)",
56
],
67
"addons": [
78
"@storybook/addon-links",
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,39 @@
1-
import React, { FC } from 'react';
2-
import { Flex, FlexProps } from 'rebass';
1+
import React from 'react';
2+
import { Flex, FlexProps, SxStyleProp } from 'rebass';
33
import { useTheme } from '../../theme/theme';
44
import { getIcon, IconName } from '../icon/list';
55
import Value from '../typography/value';
6-
import getBorderAnimation from './animation';
7-
import { ITheme } from '../../theme/types';
6+
import { BadgeVariant } from './types';
7+
import { getBorderColor, getBorderAnimation } from './utils';
88

99
type Mode = 'default' | 'bordered';
10-
type Variant =
11-
| 'light'
12-
| 'default'
13-
| 'label'
14-
| 'success'
15-
| 'warning'
16-
| 'notice'
17-
| 'fail';
18-
19-
export interface BadgeProps extends Omit<FlexProps, 'css'> {
10+
export interface Props extends Omit<FlexProps, 'css'> {
11+
/** Value for the badge. E.g. 'pending' or 18 */
2012
value: string | number;
21-
variant?: Variant;
13+
/** One of badge variants. Defined by `BadgeVariant` type. E.g. `light` or `warning`... */
14+
variant?: BadgeVariant;
15+
/** Badge mode: `default` or `bordered` */
2216
mode?: Mode;
17+
/** Whether to show loading spinner */
2318
loading?: boolean;
19+
/** Icon, used as `IconName.nameOfTheIcon` e.g. `IconName.download` */
2420
icon?: IconName;
2521
}
2622

27-
// this is a workaround because styled-system does not support theme colors for `linear-gradient`
28-
const getBorderColor = (theme: ITheme, variant: Variant) => {
29-
switch (variant) {
30-
case 'light':
31-
return theme.colors.grayShade1;
32-
case 'default':
33-
return theme.colors.black;
34-
case 'label':
35-
return theme.colors.black;
36-
case 'success':
37-
return theme.colors.labels.green;
38-
case 'warning':
39-
return theme.colors.labels.orange;
40-
case 'notice':
41-
return theme.colors.labels.yellow;
42-
case 'fail':
43-
return theme.colors.labels.red;
44-
default:
45-
return theme.colors.black;
46-
}
47-
};
48-
4923
const THEME_PATH: Record<Mode, string> = {
5024
default: 'variants.badges.primary',
5125
bordered: 'variants.badges.bordered',
5226
};
5327

54-
const Badge: FC<BadgeProps> = ({
28+
export const Badge = ({
5529
value,
5630
variant = 'light',
5731
mode = 'default',
5832
loading = false,
5933
icon,
34+
sx,
6035
...props
61-
}: BadgeProps) => {
36+
}: Props) => {
6237
const theme = useTheme();
6338

6439
const borderColor = getBorderColor(theme, variant);
@@ -73,7 +48,7 @@ const Badge: FC<BadgeProps> = ({
7348
justifyContent="center"
7449
px="6px"
7550
height="19px"
76-
sx={borderStyle}
51+
sx={{ ...borderStyle, ...sx } as SxStyleProp}
7752
variant={variant}
7853
tx={loading ? THEME_PATH.bordered : THEME_PATH[mode]}
7954
{...props}
@@ -99,5 +74,3 @@ const Badge: FC<BadgeProps> = ({
9974
</Flex>
10075
);
10176
};
102-
103-
export default Badge;

src/components/badge/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { Badge } from './Badge';
2+
export type { Props as BadgeProps } from './Badge';

src/components/badge/stories.tsx

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Story } from '@storybook/react/types-6-0';
2+
import React from 'react';
3+
import { Badge, Props } from './Badge';
4+
import { IconName } from '../icon/list';
5+
6+
export default {
7+
title: 'Quartz/Badges/Default',
8+
component: Badge,
9+
args: {
10+
value: 'arizona',
11+
mode: 'default',
12+
variant: 'default',
13+
loading: false,
14+
},
15+
argTypes: {
16+
icon: {
17+
control: { type: 'select' },
18+
options: ['download', 'card', 'birth', 'lock'],
19+
mapping: {
20+
download: IconName.download,
21+
card: IconName.card,
22+
birth: IconName.birth,
23+
lock: IconName.lock,
24+
},
25+
},
26+
variant: {
27+
control: { type: 'select' },
28+
options: [
29+
'light',
30+
'default',
31+
'fail',
32+
'warning',
33+
'success',
34+
'label',
35+
'notice',
36+
],
37+
},
38+
mode: {
39+
control: { type: 'select' },
40+
options: ['default', 'bordered'],
41+
},
42+
},
43+
};
44+
45+
const Template: Story<Props> = (props) => <Badge {...props} />;
46+
47+
export const Default = Template.bind({});

src/components/badge/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type BadgeVariant =
2+
| 'light'
3+
| 'default'
4+
| 'label'
5+
| 'success'
6+
| 'warning'
7+
| 'notice'
8+
| 'fail';

src/components/badges/animation.ts src/components/badge/utils/get-border-animation.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const rotate = keyframes`
66
}
77
`;
88

9-
const getBorderAnimation = (borderColor: string) => {
9+
export const getBorderAnimation = (borderColor: string) => {
1010
return {
1111
border: 'none',
1212
display: 'inline-block',
@@ -48,5 +48,3 @@ const getBorderAnimation = (borderColor: string) => {
4848
},
4949
};
5050
};
51-
52-
export default getBorderAnimation;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ITheme } from '../../../theme/types';
2+
import { BadgeVariant } from '../types';
3+
4+
// this is a workaround because styled-system does not support theme colors for `linear-gradient`
5+
export const getBorderColor = (theme: ITheme, variant: BadgeVariant) => {
6+
switch (variant) {
7+
case 'light':
8+
return theme.colors.grayShade1;
9+
case 'default':
10+
return theme.colors.black;
11+
case 'label':
12+
return theme.colors.black;
13+
case 'success':
14+
return theme.colors.labels.green;
15+
case 'warning':
16+
return theme.colors.labels.orange;
17+
case 'notice':
18+
return theme.colors.labels.yellow;
19+
case 'fail':
20+
return theme.colors.labels.red;
21+
default:
22+
return theme.colors.black;
23+
}
24+
};

src/components/badge/utils/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { getBorderColor } from './get-border-color';
2+
export { getBorderAnimation } from './get-border-animation';

0 commit comments

Comments
 (0)