Skip to content

Commit 97a9b4d

Browse files
authored
Merge pull request #59 from buildo/3193003-make_chip_colors_configurable
2 parents 35ea373 + d7df20e commit 97a9b4d

File tree

5 files changed

+77
-54
lines changed

5 files changed

+77
-54
lines changed

Diff for: src/Chip/Chip.css.ts

+7-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
import { bentoSprinkles } from "../internal";
2-
import { strictRecipe } from "../util/strictRecipe";
32

4-
export const chipRecipe = strictRecipe({
5-
base: bentoSprinkles({
6-
color: "textPrimary",
7-
borderRadius: "circledX",
8-
textTransform: "uppercase",
9-
display: "flex",
10-
alignItems: "center",
11-
justifyContent: "center",
12-
}),
13-
variants: {
14-
color: {
15-
grey: bentoSprinkles({ background: "softGrey" }),
16-
red: bentoSprinkles({ background: "softRed" }),
17-
orange: bentoSprinkles({ background: "softOrange" }),
18-
yellow: bentoSprinkles({ background: "softYellow" }),
19-
green: bentoSprinkles({ background: "softGreen" }),
20-
jade: bentoSprinkles({ background: "softJade" }),
21-
blue: bentoSprinkles({ background: "softBlue" }),
22-
indigo: bentoSprinkles({ background: "softIndigo" }),
23-
violet: bentoSprinkles({ background: "softViolet" }),
24-
pink: bentoSprinkles({ background: "softPink" }),
25-
},
26-
},
3+
export const chip = bentoSprinkles({
4+
color: "textPrimary",
5+
borderRadius: "circledX",
6+
textTransform: "uppercase",
7+
display: "flex",
8+
alignItems: "center",
9+
justifyContent: "center",
2710
});

Diff for: src/Chip/createChip.tsx

+53-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ComponentProps, FunctionComponent } from "react";
22
import { IconProps } from "../Icons/IconProps";
3-
import { Label, LocalizedString, IconButton, IconClose } from "..";
4-
import { Box, Columns, Column, BentoSprinkles } from "../internal";
5-
import { chipRecipe } from "./Chip.css";
3+
import { Label, LocalizedString, IconButton, BoxProps, BoxType } from "..";
4+
import { IconClose } from "../Icons/IconClose";
5+
import { Column, Columns, BentoSprinkles, bentoSprinkles } from "../internal";
6+
import { chip } from "./Chip.css";
67
import { useDefaultMessages } from "../util/useDefaultMessages";
78

89
type DismissProps =
@@ -15,51 +16,64 @@ type DismissProps =
1516
onDismiss?: never;
1617
};
1718

18-
export type ChipProps = {
19+
type DefaultColor =
20+
| "grey"
21+
| "red"
22+
| "orange"
23+
| "yellow"
24+
| "green"
25+
| "jade"
26+
| "blue"
27+
| "indigo"
28+
| "violet"
29+
| "pink";
30+
31+
export type ChipProps<CustomColor extends string> = {
1932
label: LocalizedString;
20-
color:
21-
| "grey"
22-
| "red"
23-
| "orange"
24-
| "yellow"
25-
| "green"
26-
| "jade"
27-
| "blue"
28-
| "indigo"
29-
| "violet"
30-
| "pink";
33+
color: DefaultColor | CustomColor;
3134
} & DismissProps;
3235

33-
type ChipConfig = {
36+
type ChipConfig<AtomsFn extends typeof bentoSprinkles, CustomColor extends string> = {
3437
paddingX: BentoSprinkles["paddingX"];
3538
paddingY: BentoSprinkles["paddingY"];
3639
labelSize: ComponentProps<typeof Label>["size"];
3740
closeIcon: FunctionComponent<IconProps>;
3841
closeIconSize: IconProps["size"];
3942
internalSpacing: BentoSprinkles["gap"];
43+
customColors: {
44+
[k in CustomColor]: BoxProps<AtomsFn>["background"];
45+
};
46+
};
47+
48+
const defaultColorsMapping: Record<DefaultColor, BentoSprinkles["background"]> = {
49+
grey: "softGrey",
50+
red: "softRed",
51+
orange: "softOrange",
52+
yellow: "softYellow",
53+
green: "softGreen",
54+
jade: "softJade",
55+
blue: "softBlue",
56+
indigo: "softIndigo",
57+
violet: "softViolet",
58+
pink: "softPink",
4059
};
4160

42-
export function createChip(
43-
config: ChipConfig = {
44-
paddingX: 8,
45-
paddingY: 4,
46-
labelSize: "small",
47-
closeIcon: IconClose,
48-
closeIconSize: 8,
49-
internalSpacing: 8,
50-
}
61+
export function createChip<AtomsFn extends typeof bentoSprinkles, CustomColors extends string>(
62+
Box: BoxType<AtomsFn>,
63+
config: ChipConfig<AtomsFn, CustomColors>
5164
) {
52-
return function Chip({ color, label, ...dismissProps }: ChipProps) {
65+
const colorsMapping = { ...defaultColorsMapping, ...config.customColors };
66+
67+
return function Chip({ color, label, ...dismissProps }: ChipProps<CustomColors>) {
5368
const { defaultMessages } = useDefaultMessages();
5469

5570
return (
5671
<Box display="flex">
5772
<Box
5873
paddingX={config.paddingX}
5974
paddingY={config.paddingY}
60-
className={chipRecipe({
61-
color,
62-
})}
75+
className={chip}
76+
background={colorsMapping[color]}
6377
>
6478
<Columns space={config.internalSpacing} align="center" alignY="center">
6579
<Label size={config.labelSize}>{label}</Label>
@@ -79,3 +93,13 @@ export function createChip(
7993
);
8094
};
8195
}
96+
97+
export const defaultChipConfig: ChipConfig<typeof bentoSprinkles, string> = {
98+
paddingX: 8,
99+
paddingY: 4,
100+
labelSize: "small",
101+
closeIcon: IconClose,
102+
closeIconSize: 8,
103+
internalSpacing: 8,
104+
customColors: {},
105+
};

Diff for: stories/Components/Chip.stories.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ export const NonDismissable = createStory(
2323
export const Dismissable = createStory({
2424
color: "blue",
2525
});
26+
27+
export const CustomColor = createStory({
28+
color: "custom",
29+
});

Diff for: stories/atoms.ts

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const color = {
1111
...vars.color,
1212
};
1313

14+
const background = {
15+
...bentoAtoms.statusProperties.background,
16+
...vars.color,
17+
};
18+
1419
const fill = {
1520
...bentoAtoms.statusProperties.fill,
1621
...vars.color,
@@ -35,6 +40,7 @@ export const responsiveProperties = {
3540

3641
export const statusProperties = {
3742
...bentoAtoms.statusProperties,
43+
background,
3844
color,
3945
fill,
4046
stroke: color,

Diff for: stories/index.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
createFeedback,
2727
IllustrationNegative,
2828
IllustrationPositive,
29+
defaultChipConfig,
2930
} from "../src";
3031
import { sprinkles } from "./sprinkles.css";
3132

@@ -50,7 +51,12 @@ export const Card = createCard<24 | 32 | 40>({});
5051
export const Link = createLink();
5152
export const Breadcrumb = createBreadcrumb(Link);
5253
export const Modal = createModal(Actions);
53-
export const Chip = createChip();
54+
export const Chip = createChip(Box, {
55+
...defaultChipConfig,
56+
customColors: {
57+
custom: "customColor1",
58+
},
59+
});
5460
export const { List } = createListComponents();
5561
export const Disclosure = createDisclosure();
5662
export const DisclosureGroup = createDisclosureGroup(Disclosure);

0 commit comments

Comments
 (0)