Skip to content

Commit

Permalink
New: Badge component
Browse files Browse the repository at this point in the history
  • Loading branch information
tuzkituan committed Jun 25, 2024
1 parent de22bac commit 25a479b
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 21 deletions.
18 changes: 9 additions & 9 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import React from "react";
import { withThemeByDataAttribute } from "@storybook/addon-themes";

const preview: Preview = {
// parameters: {
// actions: { argTypesRegex: "^on[A-Z].*" },
// controls: {
// matchers: {
// color: /(background|color)$/i,
// date: /Date$/,
// },
// },
// },
parameters: {
// actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
},
decorators: [
(Story) => (
<ZeniProvider>
Expand Down
20 changes: 20 additions & 0 deletions lib/components/badge/badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Meta, StoryObj } from "@storybook/react";

import { Badge } from "./badge";

const meta = {
title: "DATA DISPLAY/Badge",
component: Badge,
tags: ["autodocs"],
argTypes: {},
} satisfies Meta<typeof Badge>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Primary: Story = {
args: {
children: "Low",
size: "md",
},
};
51 changes: 51 additions & 0 deletions lib/components/badge/badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useMemo } from "react";
import { twMerge } from "tailwind-merge";
import { useComponentStyle } from "../../customization/styles/theme.context";
import { IBadge } from "./badge.types";

export const Badge = (props: IBadge) => {
const theme = useComponentStyle("Badge");
const {
children,
style,
className = "",
variant = "solid",
size = "md",
color = "",
...restProps
} = props;

const classes = useMemo(() => {
return twMerge(
theme.base({
variant,
size,
}),
className
);
}, [className, theme, variant, size]);

return (
<div
className={classes}
style={{
...(variant === "solid" && {
backgroundColor: color,
borderColor: color,
}),
...(variant === "outline" && {
borderColor: color,
color: color,
}),
...(variant === "subtle" && {
backgroundColor: `${color}15`,
color: color,
}),
...style,
}}
{...restProps}
>
<span>{children}</span>
</div>
);
};
10 changes: 10 additions & 0 deletions lib/components/badge/badge.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DetailedHTMLProps } from "react";
import { HTMLAttributes } from "react";

export interface IBadge
extends DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement> {
children?: React.ReactNode;
variant?: "outline" | "solid" | "subtle";
size?: "sm" | "md" | "lg";
color?: string;
}
4 changes: 4 additions & 0 deletions lib/components/tag/tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const Tag = (props: ITag) => {
borderColor: color,
color: color,
}),
...(variant === "subtle" && {
backgroundColor: `${color}15`,
color: color,
}),
...style,
}}
{...restProps}
Expand Down
2 changes: 1 addition & 1 deletion lib/components/tag/tag.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HTMLAttributes } from "react";
export interface ITag
extends DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement> {
children?: React.ReactNode;
variant?: "outline" | "solid";
variant?: "outline" | "solid" | "subtle";
size?: "sm" | "md" | "lg";
color?: string;
}
39 changes: 39 additions & 0 deletions lib/customization/styles/components/badge.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { cva } from "class-variance-authority";

const base = cva(
[
"zn-rounded-sm",
"zn-w-fit",
"zn-border",
"zn-border-primary-500",
"zn-font-bold",
"zn-uppercase",
],
{
variants: {
variant: {
solid: ["zn-bg-primary-500", "zn-text-white"],
outline: ["zn-bg-transparent", "zn-text-primary-500"],
subtle: [
"zn-bg-primary-100",
"zn-border-transparent",
"zn-text-primary-500",

"dark:zn-bg-primary-800",
"dark:zn-text-primary-300",
],
},
size: {
sm: ["zn-px-1.5", "zn-py-0.5", "zn-text-xs"],
md: ["zn-px-2", "zn-py-0.5", "zn-text-sm"],
lg: ["zn-px-3", "zn-py-1", "zn-text-base"],
},
},
}
);

const badgeStyles = {
base,
};

export { badgeStyles };
3 changes: 2 additions & 1 deletion lib/customization/styles/components/code.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { cva } from "class-variance-authority";

const base = cva([
"!zn-font-mono",
"zn-bg-gray-100",
"zn-bg-gray-200",
"dark:zn-bg-gray-800",
"zn-w-fit",
"zn-px-2",
"zn-py-0.5",
"zn-text-sm",
]);

const codeStyles = {
Expand Down
1 change: 1 addition & 0 deletions lib/customization/styles/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export * from "./carousel.styles";
export * from "./card.styles";
export * from "./tag.styles";
export * from "./code.styles";
export * from "./badge.styles";
22 changes: 12 additions & 10 deletions lib/customization/styles/components/tag.styles.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { cva } from "class-variance-authority";

const base = cva(
[
"zn-rounded-base",
"zn-w-fit",
"zn-border",
"zn-border-primary-500",
"zn-font-medium",
],
["zn-rounded-base", "zn-w-fit", "zn-border", "zn-border-primary-500"],
{
variants: {
variant: {
solid: ["zn-bg-primary-500", "zn-text-white"],
outline: ["zn-bg-transparent", "zn-text-primary-500"],
subtle: [
"zn-bg-primary-100",
"zn-border-transparent",
"zn-text-primary-500",

"dark:zn-bg-primary-800",
"dark:zn-text-primary-300",
],
},
size: {
sm: ["zn-px-1.5", "zn-py-0.5", "zn-text-xs"],
md: ["zn-px-2", "zn-py-0.5", "zn-text-sm"],
lg: ["zn-px-3", "zn-py-1", "zn-text-base"],
sm: ["zn-px-1.5", "zn-py-0.5", "zn-text-xs", "zn-font-medium"],
md: ["zn-px-2", "zn-py-1", "zn-text-sm", "zn-font-medium"],
lg: ["zn-px-2.5", "zn-py-1", "zn-text-lg", "zn-font-semibold"],
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions lib/customization/styles/theme.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface StyleComponents {
Card: typeof styles.cardStyles;
Tag: typeof styles.tagStyles;
Code: typeof styles.codeStyles;
Badge: typeof styles.badgeStyles;
}

const defaultStyle: Style = {
Expand Down Expand Up @@ -72,6 +73,7 @@ const defaultStyle: Style = {
Card: styles.cardStyles,
Tag: styles.tagStyles,
Code: styles.codeStyles,
Badge: styles.badgeStyles,
},
};

Expand Down

0 comments on commit 25a479b

Please sign in to comment.