Skip to content

Commit

Permalink
Add CircleStat
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKurt committed Oct 24, 2024
1 parent 0f3820e commit 7dc5da0
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/primitives/Indicators/CircleStat/CircleStat.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Meta, Title, Primary, Stories, Controls, Story } from "@storybook/blocks";
import * as CircleStatStories from "./CircleStat.stories";
import { CircleStat } from "./CircleStat";

# CircleStat

<Meta of={CircleStatStories} />

The `CircleStat` component displays a circular statistic with a percentage. It supports customizable size, color, show percentage symbol and font options.

## Example

<Story of={CircleStatStories.Default} />

# Controls

<Controls />

# Other Variations

<Stories />
80 changes: 80 additions & 0 deletions src/primitives/Indicators/CircleStat/CircleStat.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Meta, StoryObj } from "@storybook/react";
import { CircleStat } from "./CircleStat";
import { colors } from "@/tokens/colors";

const meta: Meta<typeof CircleStat> = {
title: "Primitives/CircleStat",
component: CircleStat,
args: {
text: 50,
},
argTypes: {
color: {
control: "color",
},
size: {
control: "number",
},
text: {
control: "number",
},
showPercentageSymbol: {
control: "boolean",
},
},
} satisfies Meta<typeof CircleStat>;

export default meta;

type Story = StoryObj<typeof CircleStat>;

export const Default: Story = {};

export const Lowtext: Story = {
args: {
text: 23,
},
};

export const Midtext: Story = {
args: {
text: 54,
},
};

export const Hightext: Story = {
args: {
text: 60,
},
};

export const CustomColor: Story = {
args: {
color: "#ff00ff",
},
};

export const CustomSize: Story = {
args: {
size: 60,
},
};

export const WithoutPercentageSymbol: Story = {
args: {
text: 42,
showPercentageSymbol: false,
},
};

export const CustomFont: Story = {
args: {
font: {
family: "Arial",
weight: "700",
size: "18px",
lineHeight: "28px",
color: "#8c7373",
},
},
};
71 changes: 71 additions & 0 deletions src/primitives/Indicators/CircleStat/CircleStat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from "react";
import { cn } from "@/lib/utils";
import { colors as col } from "../../../tokens/colors";
import { ColorSet } from "@/tokens/types";

interface CircleStatProps {
text: number;
size?: number;
color?: string;
showPercentageSymbol?: boolean;
font?: {
family?: string;
weight?: string;
size?: string;
lineHeight?: string;
color?: string;
};
}

export const CircleStat: React.FC<CircleStatProps> = ({
text,
size = 48,
color,
showPercentageSymbol = true,
font = {},
}) => {
const defaultColors = {
low: (col.orange as ColorSet)["300"],
mid: (col.yellow as ColorSet)["300"],
high: (col.green as ColorSet)["300"],
};

const defaultFont = {
family: "DM Sans",
weight: "400",
size: "16px",
lineHeight: "24px",
color: col.black,
};

const usedFont = { ...defaultFont, ...font };

const getColor = (text: number) => {
if (color) return color;
if (text <= 30) {
return defaultColors.low;
} else if (text < 60) {
return defaultColors.mid;
} else {
return defaultColors.high;
}
};

return (
<div
className={cn("ui-flex ui-items-center ui-justify-center ui-rounded-full")}
style={{
width: `${size}px`,
height: `${size}px`,
fontFamily: usedFont.family,
fontWeight: usedFont.weight,
fontSize: usedFont.size,
lineHeight: usedFont.lineHeight,
color: usedFont.color as string,
backgroundColor: getColor(text),
}}
>
{`${text}${showPercentageSymbol ? "%" : ""}`}
</div>
);
};
1 change: 1 addition & 0 deletions src/primitives/Indicators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// export * from "./CircleStat/CircleStat";
9 changes: 9 additions & 0 deletions src/tokens/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ export const colors: Colors = {
},
white: "#ffffff",
black: "#000000",
yellow: {
"300": "#ffea7d",
},
orange: {
"300": "#ff9776",
},
green: {
"300": "#c4f092",
},
};

0 comments on commit 7dc5da0

Please sign in to comment.