Skip to content

Commit

Permalink
feat(components): adding on progress badge components
Browse files Browse the repository at this point in the history
  • Loading branch information
syauqi committed Nov 15, 2023
1 parent 4a904fb commit 1094bdc
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/badges/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@bootwind/button",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"peerDependencies": {
"react": "^18.2.0",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.14"
}
}
66 changes: 66 additions & 0 deletions packages/badges/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { ReactNode } from 'react';

export interface BadgeProps {
variant: 'primary' | 'neutral' | 'warning' | 'success' | 'error';
size?: 'small' | 'large';
withIcon?: ReactNode;
withDot?: boolean;
}

const DotSvg = () => (
<svg
className="-ml-0.5 mr-1.5 h-2 w-2 text-indigo-400"
fill="currentColor"
viewBox="0 0 8 8"
>
<circle cx={4} cy={4} r={3} />
</svg>
);

const Badge: React.FC<BadgeProps> = ({
variant,
size = 'small',
withIcon = null,
withDot = false,
}) => {
const baseClasses = `inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium`;

const sizeClasses = size === 'large' ? 'text-sm' : '';

const badgeContent = (
<>
{withDot && (
<span className="mr-1.5">
<DotSvg />
</span>
)}
{withIcon && <span className="mr-0.5">{withIcon}</span>}
Badge
</>
);

const getVariantClasses = () => {
switch (variant) {
case 'primary':
return 'bg-blue-500 text-white';
case 'neutral':
return `bg-gray-300 text-gray-800`;
case 'warning':
return 'bg-yellow-500 text-white';
case 'success':
return 'bg-green-500 text-white';
case 'error':
return 'bg-red-500 text-white';
default:
return `bg-gray-300 text-gray-800`;
}
};

return (
<span className={`${baseClasses} ${sizeClasses} ${getVariantClasses()}`}>
{badgeContent}
</span>
);
};

export default Badge;
71 changes: 71 additions & 0 deletions packages/badges/stories/Badges.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Badge.stories.tsx
import React from 'react';
import { Story, Meta } from '@storybook/react';
import Badge, { BadgeProps } from '../src/index';

export default {
title: 'Components/Badge',
component: Badge,
argTypes: {
variant: {
control: {
type: 'select',
options: ['primary', 'neutral', 'warning', 'success', 'error'],
},
},
size: {
control: {
type: 'select',
options: ['small', 'large'],
},
},
},
} as Meta;

const Template: Story<BadgeProps> = args => <Badge {...args} />;

export const Primary = Template.bind({});
Primary.args = {
variant: 'primary',
};

export const Neutral = Template.bind({});
Neutral.args = {
variant: 'neutral',
};

export const Warning = Template.bind({});
Warning.args = {
variant: 'warning',
withDot: true,
};

export const Success = Template.bind({});
Success.args = {
variant: 'success',
};

export const Error = Template.bind({});
Error.args = {
variant: 'error',
withIcon: <span>🚨</span>,
};

export const Large = Template.bind({});
Large.args = {
variant: 'primary',
size: 'large',
};

export const WithIcon = Template.bind({});
WithIcon.args = {
variant: 'primary',
withIcon: <span>🌟</span>,
};

export const WithDotAndIcon = Template.bind({});
WithDotAndIcon.args = {
variant: 'warning',
withDot: true,
withIcon: <span>🔔</span>,
};
4 changes: 4 additions & 0 deletions packages/badges/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["../../tsconfig.json"],
"include": ["src"]
}

0 comments on commit 1094bdc

Please sign in to comment.