|
1 |
| -import { useTheme } from "@emotion/react"; |
2 |
| -import type { FC } from "react"; |
3 |
| -import type { ThemeRole } from "theme/roles"; |
| 1 | +import { type VariantProps, cva } from "class-variance-authority"; |
| 2 | +import { type FC, createContext, useContext } from "react"; |
| 3 | +import { cn } from "utils/cn"; |
4 | 4 |
|
5 |
| -interface StatusIndicatorProps { |
6 |
| - color: ThemeRole; |
7 |
| - variant?: "solid" | "outlined"; |
8 |
| -} |
| 5 | +const statusIndicatorVariants = cva( |
| 6 | + "font-medium inline-flex items-center gap-2", |
| 7 | + { |
| 8 | + variants: { |
| 9 | + variant: { |
| 10 | + success: "text-content-success", |
| 11 | + failed: "text-content-destructive", |
| 12 | + inactive: "text-highlight-grey", |
| 13 | + warning: "text-content-warning", |
| 14 | + pending: "text-highlight-sky", |
| 15 | + }, |
| 16 | + size: { |
| 17 | + sm: "text-xs", |
| 18 | + md: "text-sm", |
| 19 | + }, |
| 20 | + }, |
| 21 | + defaultVariants: { |
| 22 | + variant: "success", |
| 23 | + size: "md", |
| 24 | + }, |
| 25 | + }, |
| 26 | +); |
| 27 | + |
| 28 | +type StatusIndicatorContextValue = VariantProps<typeof statusIndicatorVariants>; |
| 29 | + |
| 30 | +const StatusIndicatorContext = createContext<StatusIndicatorContextValue>({}); |
| 31 | + |
| 32 | +export interface StatusIndicatorProps |
| 33 | + extends React.HTMLAttributes<HTMLDivElement>, |
| 34 | + StatusIndicatorContextValue {} |
9 | 35 |
|
10 | 36 | export const StatusIndicator: FC<StatusIndicatorProps> = ({
|
11 |
| - color, |
12 |
| - variant = "solid", |
| 37 | + size, |
| 38 | + variant, |
| 39 | + className, |
| 40 | + ...props |
13 | 41 | }) => {
|
14 |
| - const theme = useTheme(); |
| 42 | + return ( |
| 43 | + <StatusIndicatorContext.Provider value={{ size, variant }}> |
| 44 | + <div |
| 45 | + className={cn(statusIndicatorVariants({ variant, size }), className)} |
| 46 | + {...props} |
| 47 | + /> |
| 48 | + </StatusIndicatorContext.Provider> |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +const dotVariants = cva("rounded-full inline-block border-4 border-solid", { |
| 53 | + variants: { |
| 54 | + variant: { |
| 55 | + success: "bg-content-success border-surface-green", |
| 56 | + failed: "bg-content-destructive border-surface-destructive", |
| 57 | + inactive: "bg-highlight-grey border-surface-grey", |
| 58 | + warning: "bg-content-warning border-surface-orange", |
| 59 | + pending: "bg-highlight-sky border-surface-sky", |
| 60 | + }, |
| 61 | + size: { |
| 62 | + sm: "size-3 border-4", |
| 63 | + md: "size-4 border-4", |
| 64 | + }, |
| 65 | + }, |
| 66 | + defaultVariants: { |
| 67 | + variant: "success", |
| 68 | + size: "md", |
| 69 | + }, |
| 70 | +}); |
| 71 | + |
| 72 | +export interface StatusIndicatorDotProps |
| 73 | + extends React.HTMLAttributes<HTMLDivElement>, |
| 74 | + VariantProps<typeof dotVariants> {} |
| 75 | + |
| 76 | +export const StatusIndicatorDot: FC<StatusIndicatorDotProps> = ({ |
| 77 | + className, |
| 78 | + // We allow the size and variant to be overridden directly by the component. |
| 79 | + // This allows StatusIndicatorDot to be used alone. |
| 80 | + size, |
| 81 | + variant, |
| 82 | + ...props |
| 83 | +}) => { |
| 84 | + const { size: ctxSize, variant: ctxVariant } = useContext( |
| 85 | + StatusIndicatorContext, |
| 86 | + ); |
15 | 87 |
|
16 | 88 | return (
|
17 | 89 | <div
|
18 |
| - css={[ |
19 |
| - { |
20 |
| - height: 8, |
21 |
| - width: 8, |
22 |
| - borderRadius: 4, |
23 |
| - }, |
24 |
| - variant === "solid" && { |
25 |
| - backgroundColor: theme.roles[color].fill.solid, |
26 |
| - }, |
27 |
| - variant === "outlined" && { |
28 |
| - border: `1px solid ${theme.roles[color].outline}`, |
29 |
| - }, |
30 |
| - ]} |
| 90 | + className={cn( |
| 91 | + dotVariants({ variant: variant ?? ctxVariant, size: size ?? ctxSize }), |
| 92 | + className, |
| 93 | + )} |
| 94 | + {...props} |
31 | 95 | />
|
32 | 96 | );
|
33 | 97 | };
|
0 commit comments