Skip to content

Commit

Permalink
Create the button component with tailwindcss
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel committed Feb 14, 2024
1 parent 2de9689 commit d2cba4f
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 16 deletions.
48 changes: 47 additions & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,53 @@ const config = {
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/addon-mdx-gfm'
'@storybook/addon-styling-webpack',
({
name: "@storybook/addon-styling-webpack",

options: {
rules: [{
test: /\.css$/,
sideEffects: true,
use: [
require.resolve("style-loader"),
{
loader: require.resolve("css-loader"),
options: {


},
},
],
},],
}
}),
'@storybook/addon-mdx-gfm',
({
name: "@storybook/addon-styling-webpack",

options: {
rules: [{
test: /\.css$/,
sideEffects: true,
use: [
require.resolve("style-loader"),
{
loader: require.resolve("css-loader"),
options: {

importLoaders: 1,
},
},{
loader: require.resolve("postcss-loader"),
options: {
implementation: require.resolve("postcss"),
},
},
],
},],
}
})
],
framework: {
name: '@storybook/react-webpack5',
Expand Down
5 changes: 4 additions & 1 deletion .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from 'react'
import type { Preview } from '@storybook/react'
import React from 'react'
import { ThemeProvider } from 'styled-components'
import '../src/tailwind.css'
import theme from '../src/theme'

import '../src/tailwind.css';

const preview: Preview = {
parameters: {
actions: { argTypesRegex: '^on[A-Z].*' },
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"dependencies": {
"@styled-system/css": "^5.1.5",
"@styled-system/should-forward-prop": "^5.1.5",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"react-select": "^5.8.0",
"react-tag-input-component": "^2.0.2",
"styled-system": "^5.1.5"
},
"devDependencies": {
Expand All @@ -29,6 +32,7 @@
"@storybook/addon-interactions": "^7.6.15",
"@storybook/addon-links": "^7.6.15",
"@storybook/addon-mdx-gfm": "^7.6.15",
"@storybook/addon-styling-webpack": "^0.0.6",
"@storybook/blocks": "^7.6.15",
"@storybook/react": "^7.6.15",
"@storybook/react-webpack5": "^7.6.15",
Expand All @@ -45,9 +49,11 @@
"@types/styled-system__css": "^5.0.21",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"autoprefixer": "^10.4.17",
"babel-plugin-inline-react-svg": "^2.0.2",
"babel-plugin-styled-components": "^2.1.4",
"cheerio": "^1.0.0-rc.12",
"css-loader": "^6.10.0",
"eslint": "^8.46.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.9.0",
Expand All @@ -63,6 +69,8 @@
"jest-environment-jsdom": "^29.7.0",
"jest-styled-components": "^7.1.1",
"jest-svg-transformer": "^1.0.0",
"postcss": "^8.4.35",
"postcss-loader": "^8.1.0",
"prettier": "^3.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -77,7 +85,9 @@
"rollup-plugin-typescript2": "^0.35.0",
"rollup-plugin-visualizer": "^5.9.2",
"storybook": "^7.6.15",
"style-loader": "^3.3.4",
"styled-components": "^6.1.8",
"tailwindcss": "^3.4.1",
"ts-jest": "^29.1.1",
"tslib": "^2.6.2",
"typescript": "^5.3.3"
Expand Down
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
86 changes: 86 additions & 0 deletions src/components/ButtonTW.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { VariantProps, cva } from 'class-variance-authority';
import React from 'react';

const buttonStyles = cva(
'flex items-center justify-center rounded-full font-medium border-2 focus:outline-none focus:ring-2 focus:ring-blue-600 focus:ring-offset-white focus:ring-offset-1 disabled:opacity-60 disabled:pointer-events-none',
{
variants: {
variant: {
primary: 'border-blue-600 hover:bg-blue-800 hover:border-blue-800',
dark: 'border-black hover:bg-gray-600 hover:border-gray-600',
white: '',
danger: 'bg-red-500 text-white focus:ring-red-500',
},
hollow: {
true: 'bg-transparent'
},
transparent: {
true: 'bg-transparent border-0'
},
size: {
s: "px-4 py-1 text-sm",
m: 'px-6 py-2 text-base',
l: 'px-8 py-2 text-2xl',
xl: 'px-16 py-3.5 text-2xl'
}
},
defaultVariants: {
variant: 'primary',
size: 'm',
hollow: false
},
compoundVariants: [
{
variant: 'primary',
hollow: true,
class: 'text-blue-600 hover:text-blue-600 hover:text-white hover:bg-blue-100 hover:border-blue-600'
},
{
variant: 'primary',
hollow: false,
class: 'bg-blue-600 text-white'
},
{
variant: 'dark',
hollow: true,
class: 'bg-transparent text-black hover:text-black hover:text-white hover:bg-gray-100 hover:border-black'
},
{
variant: 'dark',
hollow: false,
class: 'bg-black text-white'
}
],

},
);

export interface Props
extends VariantProps<typeof buttonStyles>, React.HTMLAttributes<HTMLButtonElement> {
type?: 'button' | 'submit'
loading?: boolean
disabled?: boolean
startIcon?: React.ReactNode
endIcon?: React.ReactNode
children: React.ReactNode
}

const Button = ({ variant, hollow, disabled = false, size, type = "button", loading = false, startIcon, endIcon, children, ...props }: Props) => {
return (
<button className={buttonStyles({ variant, size, hollow })} {...props} disabled={disabled} type={type}>
{startIcon && (
<span className="mr-2">
{startIcon}
</span>
)}
{children}
{endIcon && (
<span className="ml-2">
{endIcon}
</span>
)}
</button>
);
}

export default Button
3 changes: 3 additions & 0 deletions src/tailwind.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
71 changes: 71 additions & 0 deletions stories/ButtonTW.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Meta, StoryObj } from '@storybook/react'
import React from 'react'
import { BsTwitter } from 'react-icons/bs'
import Box from '../src/components/Box'
import ButtonTW from '../src/components/ButtonTW'

const meta = {
title: 'Components/ButtonTW',
component: ButtonTW,
tags: ['autodocs'],
argTypes: {
onClick: { action: 'clicked' },
hollow: { control: 'boolean' },
disabled: { control: 'boolean' },
},
} satisfies Meta<typeof ButtonTW>

export default meta
type Story = StoryObj<typeof meta>

export const Primary: Story = {
args: {
children: 'Button',
},
}

export const DisabledButton: Story = {
args: {
children: 'Disabled Button',
disabled: true,
},
}

export const Dark: Story = {
args: {
variant: 'dark',
children: 'Button',
size: 's',
},
}

export const DarkHollow: Story = {
args: {
variant: 'dark',
hollow: true,
children: 'Button',
size: 'l',
},
}

export const WithIcon: Story = {
args: {
children: 'Button',
endIcon: <BsTwitter />,
},
}

export const Hollow: Story = {
args: {
hollow: true,
children: 'Button',
},
}

export const LongButton = () => {
return (
<Box width="300px">
<ButtonTW>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</ButtonTW>
</Box>
)
}
35 changes: 35 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** @type {import('tailwindcss').Config} */
const defaultTheme = require('tailwindcss/defaultTheme');
const colors = require('tailwindcss/colors');

const primaryColor = '#0588cb'

module.exports = {
content: ['./src/**/*.{js,ts,jsx,tsx}', './stories/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {
colors: {
...colors,
blue: {
'100': '#e7f5ff',
'200': '#c9e8ff',
'300': '#8dd5f8',
'400': '#5db8fe',
'500': '#37a6ed',
'600': '#0588cb',
'700': '#0f77b8',
'800': '#056aa6',
'900': '#005f9c',
'1000': '#005a99'
},

},
},
},

// plugins: [
// require('@tailwindcss/typography'),
// require('@tailwindcss/aspect-ratio'),
// require('@tailwindcss/forms'),
// ],
};
Loading

0 comments on commit d2cba4f

Please sign in to comment.