|
1 |
| -import { PaletteColor, Theme } from "@mui/material/styles"; |
2 |
| -import { makeStyles } from "@mui/styles"; |
3 |
| -import { FC } from "react"; |
4 |
| -import { PaletteIndex } from "theme/theme"; |
5 |
| -import { combineClasses } from "utils/combineClasses"; |
| 1 | +import { type FC, type ReactNode, useMemo } from "react"; |
| 2 | +import { css, type Interpolation, type Theme, useTheme } from "@emotion/react"; |
| 3 | +import { colors } from "theme/colors"; |
| 4 | + |
| 5 | +export type PillType = |
| 6 | + | "primary" |
| 7 | + | "secondary" |
| 8 | + | "error" |
| 9 | + | "warning" |
| 10 | + | "info" |
| 11 | + | "success" |
| 12 | + | "neutral"; |
6 | 13 |
|
7 | 14 | export interface PillProps {
|
8 | 15 | className?: string;
|
9 |
| - icon?: React.ReactNode; |
10 |
| - text: string; |
11 |
| - type?: PaletteIndex; |
| 16 | + icon?: ReactNode; |
| 17 | + text: ReactNode; |
| 18 | + type?: PillType; |
12 | 19 | lightBorder?: boolean;
|
13 | 20 | title?: string;
|
14 | 21 | }
|
15 | 22 |
|
| 23 | +const themeOverrides = { |
| 24 | + primary: (lightBorder) => ({ |
| 25 | + backgroundColor: colors.blue[13], |
| 26 | + borderColor: lightBorder ? colors.blue[5] : colors.blue[7], |
| 27 | + }), |
| 28 | + secondary: (lightBorder) => ({ |
| 29 | + backgroundColor: colors.indigo[13], |
| 30 | + borderColor: lightBorder ? colors.indigo[6] : colors.indigo[8], |
| 31 | + }), |
| 32 | + neutral: (lightBorder) => ({ |
| 33 | + backgroundColor: colors.gray[13], |
| 34 | + borderColor: lightBorder ? colors.gray[6] : colors.gray[8], |
| 35 | + }), |
| 36 | +} satisfies Record<string, (lightBorder?: boolean) => Interpolation<Theme>>; |
| 37 | + |
| 38 | +const themeStyles = |
| 39 | + (type: PillType, lightBorder?: boolean) => (theme: Theme) => { |
| 40 | + const palette = theme.palette[type]; |
| 41 | + return { |
| 42 | + backgroundColor: palette.dark, |
| 43 | + borderColor: lightBorder ? palette.light : palette.main, |
| 44 | + }; |
| 45 | + }; |
| 46 | + |
16 | 47 | export const Pill: FC<PillProps> = (props) => {
|
17 |
| - const { className, icon, text = false, title } = props; |
18 |
| - const styles = useStyles(props); |
| 48 | + const { lightBorder, icon, text = null, type = "neutral", ...attrs } = props; |
| 49 | + const theme = useTheme(); |
| 50 | + |
| 51 | + const typeStyles = useMemo(() => { |
| 52 | + if (type in themeOverrides) { |
| 53 | + return themeOverrides[type as keyof typeof themeOverrides](lightBorder); |
| 54 | + } |
| 55 | + return themeStyles(type, lightBorder); |
| 56 | + }, [type, lightBorder]); |
| 57 | + |
19 | 58 | return (
|
20 | 59 | <div
|
21 |
| - className={combineClasses([styles.wrapper, styles.pillColor, className])} |
| 60 | + css={[ |
| 61 | + { |
| 62 | + display: "inline-flex", |
| 63 | + alignItems: "center", |
| 64 | + borderWidth: 1, |
| 65 | + borderStyle: "solid", |
| 66 | + borderRadius: 99999, |
| 67 | + fontSize: 12, |
| 68 | + color: "#FFF", |
| 69 | + height: theme.spacing(3), |
| 70 | + paddingLeft: icon ? theme.spacing(0.75) : theme.spacing(1.5), |
| 71 | + paddingRight: theme.spacing(1.5), |
| 72 | + whiteSpace: "nowrap", |
| 73 | + fontWeight: 400, |
| 74 | + }, |
| 75 | + typeStyles, |
| 76 | + ]} |
22 | 77 | role="status"
|
23 |
| - title={title} |
| 78 | + {...attrs} |
24 | 79 | >
|
25 |
| - {icon && <div className={styles.iconWrapper}>{icon}</div>} |
| 80 | + {icon && ( |
| 81 | + <div |
| 82 | + css={css` |
| 83 | + margin-right: ${theme.spacing(0.5)}; |
| 84 | + width: ${theme.spacing(1.75)}; |
| 85 | + height: ${theme.spacing(1.75)}; |
| 86 | + line-height: 0; |
| 87 | + display: flex; |
| 88 | + align-items: center; |
| 89 | + justify-content: center; |
| 90 | +
|
| 91 | + & > img, |
| 92 | + & > svg { |
| 93 | + width: ${theme.spacing(1.75)}; |
| 94 | + height: ${theme.spacing(1.75)}; |
| 95 | + } |
| 96 | + `} |
| 97 | + > |
| 98 | + {icon} |
| 99 | + </div> |
| 100 | + )} |
26 | 101 | {text}
|
27 | 102 | </div>
|
28 | 103 | );
|
29 | 104 | };
|
30 |
| - |
31 |
| -const useStyles = makeStyles<Theme, PillProps>((theme) => ({ |
32 |
| - wrapper: { |
33 |
| - display: "inline-flex", |
34 |
| - alignItems: "center", |
35 |
| - borderWidth: 1, |
36 |
| - borderStyle: "solid", |
37 |
| - borderRadius: 99999, |
38 |
| - fontSize: 12, |
39 |
| - color: "#FFF", |
40 |
| - height: theme.spacing(3), |
41 |
| - paddingLeft: ({ icon }) => |
42 |
| - icon ? theme.spacing(0.75) : theme.spacing(1.5), |
43 |
| - paddingRight: theme.spacing(1.5), |
44 |
| - whiteSpace: "nowrap", |
45 |
| - fontWeight: 400, |
46 |
| - }, |
47 |
| - |
48 |
| - pillColor: { |
49 |
| - backgroundColor: ({ type }) => |
50 |
| - type |
51 |
| - ? (theme.palette[type] as PaletteColor).dark |
52 |
| - : theme.palette.text.secondary, |
53 |
| - borderColor: ({ type, lightBorder }) => |
54 |
| - type |
55 |
| - ? lightBorder |
56 |
| - ? (theme.palette[type] as PaletteColor).light |
57 |
| - : (theme.palette[type] as PaletteColor).main |
58 |
| - : theme.palette.text.secondary, |
59 |
| - }, |
60 |
| - |
61 |
| - iconWrapper: { |
62 |
| - marginRight: theme.spacing(0.5), |
63 |
| - width: theme.spacing(1.75), |
64 |
| - height: theme.spacing(1.75), |
65 |
| - lineHeight: 0, |
66 |
| - display: "flex", |
67 |
| - alignItems: "center", |
68 |
| - justifyContent: "center", |
69 |
| - |
70 |
| - "& > svg": { |
71 |
| - width: theme.spacing(1.75), |
72 |
| - height: theme.spacing(1.75), |
73 |
| - }, |
74 |
| - }, |
75 |
| -})); |
|
0 commit comments