Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: badge component from scratch #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
'*.{js,jsx,ts,tsx}': ['eslint --fix', 'prettier --write'],
'*.{json,md,yml}': ['prettier --write'],
};
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
node_modules
dist
build
.turbo
node_modules
public
build
dist
1 change: 1 addition & 0 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint": "eslint ./stories/*.stories.tsx --max-warnings 0"
},
"dependencies": {
"@signozhq/badge": "workspace:*",
"@signozhq/button": "workspace:*",
"@signozhq/input": "workspace:*",
"@signozhq/tailwind-config": "workspace:*",
Expand Down
73 changes: 73 additions & 0 deletions apps/docs/stories/badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Badge from '@signozhq/badge';
import type { Meta, StoryObj } from '@storybook/react';

const meta: Meta<typeof Badge> = {
title: 'Components/Badge',
component: Badge,
argTypes: {
variant: {
control: 'select',
options: ['primary', 'secondary', 'danger', 'outline'],
},
size: {
control: 'select',
options: ['default', 'sm', 'lg', 'icon'],
},
children: { control: 'text' },
},
};

export default meta;

type Story = StoryObj<typeof Badge>;

export const Default: Story = {
args: {
children: 'Badge',
variant: 'primary',
size: 'default',
},
};

export const Secondary: Story = {
args: {
...Default.args,
variant: 'secondary',
},
};

export const Danger: Story = {
args: {
...Default.args,
variant: 'danger',
},
};

export const Outline: Story = {
args: {
...Default.args,
variant: 'outline',
},
};

export const Small: Story = {
args: {
...Default.args,
size: 'sm',
},
};

export const Large: Story = {
args: {
...Default.args,
size: 'lg',
},
};

export const Icon: Story = {
args: {
...Default.args,
size: 'icon',
children: '🚀',
},
};
4 changes: 4 additions & 0 deletions packages/badge/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("eslint").Linter.Config} */
export default {
extends: ['@repo/eslint-config/react.js'],
};
19 changes: 19 additions & 0 deletions packages/badge/components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"style": "new-york",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "@signozhq/tailwind-config/tailwind.config.js",
"css": "src/index.css",
"baseColor": "zinc",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "src",
"lib": "@/lib",
"hooks": "@/hooks"
}
}
57 changes: 57 additions & 0 deletions packages/badge/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@signozhq/badge",
"version": "0.0.0",
"sideEffects": false,
"license": "MIT",
"type": "module",
"exports": {
".": {
"types": "./dist/badge.d.ts",
"import": "./dist/badge.js"
}
},
"main": "./dist/badge.js",
"module": "./dist/badge.js",
"types": "./dist/badge.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "vite build",
"dev": "vite build --watch",
"lint": "eslint . --ext .ts,.tsx,.js,.jsx --fix && prettier --write .",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@signozhq/tailwind-config": "workspace:*",
"@types/node": "^22.5.5",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
"@vitejs/plugin-react": "^4.2.1",
"autoprefixer": "^10.4.20",
"eslint": "^9.11.0",
"postcss": "^8.4.47",
"react-dom": "^18.2.0",
"tailwindcss": "^3.4.12",
"typescript": "^5.3.3",
"vite": "^5.0.0",
"vite-plugin-lib-inject-css": "^2.1.1",
"vite-plugin-dts": "^4.2.1"
},
"dependencies": {
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-slot": "^1.1.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"lucide-react": "^0.445.0",
"react": "^18.2.0",
"tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7"
},
"publishConfig": {
"access": "public"
},
"description": "a badge component from scratch"
}
6 changes: 6 additions & 0 deletions packages/badge/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
39 changes: 39 additions & 0 deletions packages/badge/src/badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { cva, type VariantProps } from 'class-variance-authority';
import React from 'react';

const badgeBaseClass =
'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50';

const badgeVariants = cva(badgeBaseClass, {
variants: {
variant: {
primary: 'bg-primary text-primary-foreground hover:bg-blue-600',
secondary:
'bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80',
danger:
'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90',
outline:
'text-gray-700 border border-gray-300 hover:bg-gray-100 hover:text-gray-900',
},
size: {
default: 'px-3 py-0.5',
sm: 'px-2 py-0.25 text-xs',
lg: 'px-4 py-1',
icon: 'p-0.5',
},
},
defaultVariants: {
variant: 'primary',
size: 'default',
},
});

export interface BadgeProps
extends React.HTMLAttributes<HTMLDivElement>,
VariantProps<typeof badgeVariants> {}

const Badge = ({ className, variant, ...props }: BadgeProps) => {
return <div className={badgeVariants({ variant, className })} {...props} />;
};

export default Badge;
3 changes: 3 additions & 0 deletions packages/badge/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
6 changes: 6 additions & 0 deletions packages/badge/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { clsx, type ClassValue } from 'clsx';
import { twMerge } from 'tailwind-merge';

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
7 changes: 7 additions & 0 deletions packages/badge/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sharedConfig from '@signozhq/tailwind-config/tailwind.config.js';

/** @type {import('tailwindcss').Config} */
export default {
...sharedConfig,
content: [...sharedConfig.content, './src/**/*.{js,ts,jsx,tsx}'],
};
13 changes: 13 additions & 0 deletions packages/badge/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@repo/typescript-config/react-library.json",
"include": ["src"],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"jsx": "react",
"outDir": "dist",
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
31 changes: 31 additions & 0 deletions packages/badge/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import path from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';
import { libInjectCss } from 'vite-plugin-lib-inject-css';

export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/badge.tsx'),
name: 'Button',
fileName: 'badge',
},
rollupOptions: {
external: ['react'],
output: {
globals: {
react: 'React',
},
},
treeshake: true,
},
},
plugins: [dts(), react(), libInjectCss()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
});
Loading