Skip to content

Commit

Permalink
Create first component
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgudzevskyi committed Mar 25, 2021
1 parent bb9cda3 commit 1e755e9
Show file tree
Hide file tree
Showing 43 changed files with 36,756 additions and 110 deletions.
12 changes: 1 addition & 11 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@
/node_modules
/.pnp
.pnp.js
package-lock.json
/package-lock.json
yarn.lock

# testing
/coverage

# production
/build
.nyc_output

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.sass-cache
.eslintcache
/.eslintcache
.nyc_output
/.nyc_output/processinfo/index.json
.nyc_output/processinfo/index.json

npm-debug.log*
yarn-debug.log*
yarn-error.log*
debug.log
11 changes: 11 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/preset-create-react-app"
]
}
4 changes: 4 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
}
Empty file added README.md
Empty file.
20,921 changes: 20,921 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

69 changes: 46 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
{
"name": "react-storybook-archref",
"version": "1.0.0",
"description": "This is a template/starter to create a React UI library using Storybook and TypeScript",
"main": "dist/index.js",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/styled-components": "^5.1.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"styled-components": "^5.2.1",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook -s public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/michaelgudzevskyi/react-storybook-archref.git"
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"keywords": [
"react",
"storybook",
"typescript",
"ui",
"template",
"starter"
],
"author": "Michael Gudzevskyi",
"license": "ISC",
"bugs": {
"url": "https://github.com/michaelgudzevskyi/react-storybook-archref/issues"
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"homepage": "https://github.com/michaelgudzevskyi/react-storybook-archref#readme",
"devDependencies": {
"@types/react": "^17.0.3",
"react": "^17.0.2",
"typescript": "^4.2.3"
"@storybook/addon-actions": "^6.1.21",
"@storybook/addon-essentials": "^6.1.21",
"@storybook/addon-links": "^6.1.21",
"@storybook/node-logger": "^6.1.21",
"@storybook/preset-create-react-app": "^3.1.7",
"@storybook/react": "^6.1.21"
}
}
Binary file added public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
5 changes: 0 additions & 5 deletions src/components/Button/index.tsx

This file was deleted.

100 changes: 100 additions & 0 deletions src/components/button/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { ElementType, ReactNode, MouseEventHandler, ForwardRefRenderFunction, forwardRef, MutableRefObject } from "react";
import { StyledButton } from "./styled"

export type ButtonType = 'default' | 'danger' | 'ghost' | 'secondary';

export type ComponentSize = 'default' | 'large' | 'small';

interface BaseButtonProps {
type?: ButtonType;
icon?: ElementType;
size?: ComponentSize;
className?: string;
children?: ReactNode;
disabled?: boolean;
loading?: boolean;
}

type HTMLButtonProps = {
onClick?: MouseEventHandler<HTMLButtonElement>;
} & BaseButtonProps;

type HTMLAnchorProps = {
href?: string;
} & BaseButtonProps;

/**
* For React Router Link
*/
type CustomNodeProps = {
as?: ElementType
to?: string
} & BaseButtonProps

export type ButtonProps = HTMLButtonProps & HTMLAnchorProps & CustomNodeProps

const Button: ForwardRefRenderFunction<unknown, ButtonProps> = (props, ref) => {
const {
type = 'default',
icon,
size = 'default',
className,
children,
disabled = false,
loading,
onClick,
href,
as,
to,
} = props;

const styles = {
innerType: type,
size,
disabled,
withText: children !== null
}

if (as) {
return (
<StyledButton
as={as}
to={to}
ref={ref}
className={className}
{...styles}
>
{loading? 'Loading...' : children}
</StyledButton>
)
}

if (href) {
return (
<StyledButton
as='a'
href={href}
ref={ref as MutableRefObject<HTMLAnchorElement>}
className={className}
{...styles}
>
{loading? 'Loading...' : children}
</StyledButton>
)
}

return (
<StyledButton
as='button'
type='button'
onClick={onClick}
ref={ref as MutableRefObject<HTMLButtonElement>}
className={className}
{...styles}
>
{loading? 'Loading...' : children}
</StyledButton>
)
}

export default forwardRef<unknown, ButtonProps>(Button)
5 changes: 5 additions & 0 deletions src/components/button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Button from './button';

export * from './button'

export default Button
75 changes: 75 additions & 0 deletions src/components/button/styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import styled from "styled-components";
import { ComponentSize, heights, sidePaddings } from "../../config/sizes";
import { ButtonType } from "./button";

type StateColors = {
regular: string;
hover: string;
}

const typeColors: {[key in ButtonType]: StateColors} = {
default: {
regular: '#0018cf',
hover: '#2e27cc',
},
danger: {
regular: '#d93848',
hover: '#eb4d5d',
},
ghost: {
regular: 'transparent',
hover: '#dbdbdb',
},
secondary: {
regular: '#000',
hover: '#3d3d3d',
}
};

interface StyledButtonProps {
innerType: ButtonType;
size: ComponentSize;
withText: boolean;
}

/* Real tag is assigned dynamically */
export const StyledButton = styled.button<StyledButtonProps>`
display: flex;
align-items: center;
justify-content: center;
/* Add margin in case of loading or icon */
& > *:nth-child(1) {
margin-left: ${pr => pr.withText ? 7 : 0}px;
}
font-size: 15px;
border: none;
cursor: pointer;
background-color: ${ (pr) => typeColors[pr.innerType].regular };
padding: 0 ${ pr => sidePaddings[pr.size]}px;
height: ${ pr => heights[pr.size]}px;
color: ${ pr => pr.innerType === 'ghost'
? typeColors['default'].regular
: '#fff'
};
${ pr => pr.disabled ? `
background-color: #a6a6a6;
color: #5e5e5e;
cursor: not-allowed;
&:hover {
background-color: #a6a6a6 !important;
color: #5e5e5e !important;
}
` : ''}
border-radius: 0;
outline: none;
&:focus {
box-shadow: 0 0 0 1px #fff, 0 0 0 2px ${ (pr) => typeColors[pr.innerType].regular };
}
&:hover {
background-color: ${ (pr) => typeColors[pr.innerType].hover };
}
`;

export const StyledIcon = styled.div`
height: 20px;
`;
Loading

0 comments on commit 1e755e9

Please sign in to comment.