= ({ theme, children }) => (
8 |
12 |
Test Component 😢
13 | {children}
14 |
15 | );
16 |
--------------------------------------------------------------------------------
/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | roots: ['./src'],
3 | setupFilesAfterEnv: ['./jest.setup.ts'],
4 | moduleFileExtensions: ['ts', 'tsx', 'js'],
5 | testPathIgnorePatterns: ['node_modules/'],
6 | transform: {
7 | '^.+\\.tsx?$': 'ts-jest',
8 | },
9 | testMatch: ['**/*.test.(ts|tsx)'],
10 | moduleNameMapper: {
11 | // Mocks out all these file formats when tests are run
12 | '\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
13 | 'identity-obj-proxy',
14 | '\\.(css|less|scss|sass)$': 'identity-obj-proxy',
15 | },
16 | };
17 |
--------------------------------------------------------------------------------
/example/src/index.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import ReactDOM from "react-dom";
3 | import "react-component-lib/dist/styles.css";
4 | import App from "./App";
5 | import reportWebVitals from "./reportWebVitals";
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById("root")
12 | );
13 |
14 | // If you want to start measuring performance in your app, pass a function
15 | // to log results (for example: reportWebVitals(console.log))
16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17 | reportWebVitals();
18 |
--------------------------------------------------------------------------------
/example/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/src/components/Button/Button.test.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { render, screen } from '@testing-library/react';
3 | import userEvent from '@testing-library/user-event';
4 |
5 | import { Button } from './Button';
6 |
7 | describe('', () => {
8 | it('Should handle onClick as props', () => {
9 | const mockedFn = jest.fn();
10 | render(
11 | ,
14 | );
15 | const button = screen.getByRole('button', { name: /click me/i });
16 | userEvent.click(button);
17 | expect(mockedFn).toBeCalledTimes(1);
18 | });
19 | });
20 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "declaration": true,
4 | "declarationDir": "dist",
5 | "module": "esnext",
6 | "target": "es5",
7 | "lib": ["DOM", "DOM.Iterable", "ESNext"],
8 | "jsx": "react",
9 | "moduleResolution": "node",
10 | "allowSyntheticDefaultImports": true,
11 | "esModuleInterop": true,
12 | "noImplicitReturns": true,
13 | "noUnusedParameters": true,
14 | "resolveJsonModule": true,
15 | "skipLibCheck": true
16 | },
17 | "include": ["src/**/*"],
18 | "exclude": [
19 | "node_modules",
20 | "build",
21 | "src/**/*.stories.tsx",
22 | "src/**/*.test.tsx",
23 | "src/utils"
24 | ]
25 | }
26 |
--------------------------------------------------------------------------------
/src/components/Button/Button.stories.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Story, Meta } from '@storybook/react';
3 |
4 | import Button from '.';
5 | import { ButtonProps } from './Button.types';
6 |
7 | export default {
8 | title: 'Button',
9 | component: Button,
10 | } as Meta;
11 |
12 | const Template: Story = (args) => ;
13 |
14 | export const Default = Template.bind({}) as Story;
15 | Default.args = {
16 | children: 'Primary',
17 | variant: 'primary',
18 | };
19 |
20 | export const Secondary = Template.bind({}) as Story;
21 | Secondary.args = {
22 | children: 'Secondary',
23 | variant: 'secondary',
24 | };
25 |
--------------------------------------------------------------------------------
/src/components/TestComponent/TestComponent.stories.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Story, Meta } from '@storybook/react';
3 |
4 | import TestComponent from '.';
5 | import { TestComponentProps } from './TestComponent.types';
6 |
7 | export default {
8 | title: 'TestComponent',
9 | component: TestComponent,
10 | } as Meta;
11 |
12 | const Template: Story = (args) => ;
13 |
14 | export const Default = Template.bind({}) as Story;
15 | Default.args = {
16 | theme: 'primary',
17 | };
18 |
19 | export const Secondary = Template.bind({});
20 | Secondary.args = {
21 | theme: 'secondary',
22 | children: 'Mama',
23 | };
24 |
--------------------------------------------------------------------------------
/src/components/TestComponent/TestComponent.test.tsx:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { render, screen } from '@testing-library/react';
3 |
4 | import TestComponent from '.';
5 |
6 | describe('', () => {
7 | it('should have primary className with default props', () => {
8 | render();
9 | const testComponent = screen.getByTestId('test-component');
10 | expect(testComponent).toHaveClass('test-component-primary');
11 | });
12 |
13 | it('should have secondary className with theme set as secondary', () => {
14 | render();
15 | const testComponent = screen.getByTestId('test-component');
16 | expect(testComponent).toHaveClass('test-component-secondary');
17 | });
18 | });
19 |
--------------------------------------------------------------------------------
/.storybook/main.ts:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 |
3 | module.exports = {
4 | stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
5 | addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
6 | webpackFinal: async (config, { configType }) => {
7 | // `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
8 | // You can change the configuration based on that.
9 | // 'PRODUCTION' is used when building the static version of storybook.
10 |
11 | // Make whatever fine-grained changes you need
12 | config.module.rules.push({
13 | test: /\.scss$/,
14 | use: ['style-loader', 'css-loader', 'sass-loader'],
15 | include: path.resolve(__dirname, '../'),
16 | });
17 |
18 | // Return the altered config
19 | return config;
20 | },
21 | };
22 |
--------------------------------------------------------------------------------
/example/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "example",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.11.4",
7 | "@testing-library/react": "^11.1.0",
8 | "@testing-library/user-event": "^12.1.10",
9 | "react": "^17.0.2",
10 | "react-component-lib": "file:..",
11 | "react-dom": "^17.0.2",
12 | "react-scripts": "4.0.3",
13 | "web-vitals": "^1.0.1"
14 | },
15 | "scripts": {
16 | "start": "react-scripts start",
17 | "build": "react-scripts build",
18 | "test": "react-scripts test",
19 | "eject": "react-scripts eject"
20 | },
21 | "eslintConfig": {
22 | "extends": [
23 | "react-app",
24 | "react-app/jest"
25 | ]
26 | },
27 | "browserslist": {
28 | "production": [
29 | ">0.2%",
30 | "not dead",
31 | "not op_mini all"
32 | ],
33 | "development": [
34 | "last 1 chrome version",
35 | "last 1 firefox version",
36 | "last 1 safari version"
37 | ]
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "browser": true,
4 | "es2021": true,
5 | "jest": true
6 | },
7 | "extends": ["plugin:react/recommended", "airbnb"],
8 | "parser": "@typescript-eslint/parser",
9 | "settings": {
10 | "import/resolver": {
11 | "node": { "extensions": [".js", ".jsx", ".ts", ".tsx"] }
12 | }
13 | },
14 | "parserOptions": {
15 | "ecmaFeatures": {
16 | "jsx": true
17 | },
18 | "ecmaVersion": 12,
19 | "sourceType": "module"
20 | },
21 | "plugins": ["react", "@typescript-eslint"],
22 | "rules": {
23 | "no-unused-vars": "warn",
24 | "import/extensions": "off",
25 | "no-use-before-define": "off",
26 | "react/prop-types": "off",
27 | "react/jsx-props-no-spreading": "off",
28 | "@typescript-eslint/no-use-before-define": "error",
29 | "react/react-in-jsx-scope": "off",
30 | "react/jsx-filename-extension": [
31 | 1,
32 | { "extensions": [".js", ".jsx", ".ts", ".tsx"] }
33 | ],
34 | "import/prefer-default-export": "off",
35 | "import/no-extraneous-dependencies": "off"
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/rollup.config.js:
--------------------------------------------------------------------------------
1 | import peerDepsExternal from 'rollup-plugin-peer-deps-external';
2 | import multiInput from 'rollup-plugin-multi-input';
3 | import resolve from '@rollup/plugin-node-resolve';
4 | import commonjs from '@rollup/plugin-commonjs';
5 | import typescript from 'rollup-plugin-typescript2';
6 | import { terser } from 'rollup-plugin-terser';
7 | import postcss from 'rollup-plugin-postcss';
8 | import autoprefixer from 'autoprefixer';
9 | import cssnano from 'cssnano';
10 | import copy from 'rollup-plugin-copy';
11 |
12 | export default [
13 | {
14 | input: [
15 | 'src/components/**/*.tsx',
16 | 'src/components/**/index.ts',
17 | '!src/components/**/*.test.tsx',
18 | '!src/components/**/*.stories.tsx',
19 | ],
20 | output: [
21 | {
22 | dir: 'dist',
23 | format: 'esm',
24 | sourcemap: true,
25 | },
26 | ],
27 | plugins: [
28 | multiInput({
29 | relative: 'src/components',
30 | }),
31 | peerDepsExternal(),
32 | resolve(),
33 | commonjs(),
34 | postcss({
35 | modules: true,
36 | plugins: [autoprefixer(), cssnano()],
37 | }),
38 | typescript({
39 | useTsconfigDeclarationDir: true,
40 | }),
41 | terser(),
42 | ],
43 | },
44 | {
45 | input: 'src/styles.scss',
46 | output: {
47 | file: 'dist/styles.css',
48 | },
49 | plugins: [
50 | postcss({
51 | extract: true,
52 | plugins: [autoprefixer(), cssnano()],
53 | }),
54 | copy({
55 | targets: [
56 | {
57 | src: ['src/_variables.scss'],
58 | dest: 'dist',
59 | },
60 | ],
61 | }),
62 | ],
63 | },
64 | ];
65 |
--------------------------------------------------------------------------------
/example/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-component-lib",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "dist/index.js",
6 | "module": "dist/index.js",
7 | "style": "dist/styles.scss",
8 | "files": [
9 | "dist"
10 | ],
11 | "sideEffects": false,
12 | "scripts": {
13 | "setup": "npm i && npm run build && cd example && npm i",
14 | "dev": "concurrently \" npm run watch \" \" npm run start --prefix example \" ",
15 | "prebuild": "rm -rf dist",
16 | "build": "rollup -c",
17 | "watch": "rollup -c -w",
18 | "test": "jest",
19 | "test:watch": "jest --watch",
20 | "storybook": "start-storybook -p 6006",
21 | "build:storybook": "build-storybook",
22 | "lint:fix": "eslint . --fix"
23 | },
24 | "author": "Momen Sherif",
25 | "license": "ISC",
26 | "devDependencies": {
27 | "@babel/core": "^7.13.10",
28 | "@rollup/plugin-commonjs": "^17.1.0",
29 | "@rollup/plugin-node-resolve": "^11.2.0",
30 | "@storybook/addon-actions": "^6.1.21",
31 | "@storybook/addon-essentials": "^6.1.21",
32 | "@storybook/addon-links": "^6.1.21",
33 | "@storybook/react": "^6.1.21",
34 | "@testing-library/jest-dom": "^5.11.9",
35 | "@testing-library/react": "^11.2.5",
36 | "@testing-library/user-event": "^13.0.7",
37 | "@types/jest": "^26.0.21",
38 | "@types/react": "^17.0.3",
39 | "@typescript-eslint/eslint-plugin": "^4.18.0",
40 | "@typescript-eslint/parser": "^4.18.0",
41 | "autoprefixer": "^10.2.5",
42 | "babel-loader": "^8.2.2",
43 | "babel-preset-react-app": "^10.0.0",
44 | "concurrently": "^6.0.0",
45 | "css-loader": "^5.1.3",
46 | "cssnano": "^4.1.10",
47 | "eslint": "^7.22.0",
48 | "eslint-config-airbnb": "^18.2.1",
49 | "eslint-plugin-import": "^2.22.1",
50 | "eslint-plugin-jsx-a11y": "^6.4.1",
51 | "eslint-plugin-react": "^7.22.0",
52 | "eslint-plugin-react-hooks": "^4.2.0",
53 | "identity-obj-proxy": "^3.0.0",
54 | "jest": "^26.6.3",
55 | "node-sass": "^5.0.0",
56 | "postcss": "^8.2.8",
57 | "react": "^17.0.1",
58 | "react-dom": "^17.0.1",
59 | "rollup": "^2.42.1",
60 | "rollup-plugin-copy": "^3.4.0",
61 | "rollup-plugin-multi-input": "^1.2.0",
62 | "rollup-plugin-peer-deps-external": "^2.2.4",
63 | "rollup-plugin-postcss": "^4.0.0",
64 | "rollup-plugin-terser": "^7.0.2",
65 | "rollup-plugin-typescript2": "^0.30.0",
66 | "sass-loader": "^10.1.1",
67 | "storybook-css-modules-preset": "^1.0.7",
68 | "style-loader": "^2.0.0",
69 | "ts-jest": "^26.5.4",
70 | "typescript": "^4.2.3"
71 | },
72 | "peerDependencies": {
73 | "react": ">=16.8.0",
74 | "react-dom": ">=16.8.0"
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/_normalize.scss:
--------------------------------------------------------------------------------
1 | /**
2 | * Minified by jsDelivr using clean-css v4.2.3.
3 | * Original file: /npm/modern-normalize@1.0.0/modern-normalize.css
4 | *
5 | * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
6 | */
7 | /*! modern-normalize v1.0.0 | MIT License | https://github.com/sindresorhus/modern-normalize */
8 | *,
9 | ::after,
10 | ::before {
11 | box-sizing: border-box;
12 | }
13 | :root {
14 | -moz-tab-size: 4;
15 | tab-size: 4;
16 | }
17 | html {
18 | line-height: 1.15;
19 | -webkit-text-size-adjust: 100%;
20 | }
21 | body {
22 | margin: 0;
23 | }
24 | body {
25 | font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial,
26 | sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji';
27 | }
28 | hr {
29 | height: 0;
30 | color: inherit;
31 | }
32 | abbr[title] {
33 | text-decoration: underline dotted;
34 | }
35 | b,
36 | strong {
37 | font-weight: bolder;
38 | }
39 | code,
40 | kbd,
41 | pre,
42 | samp {
43 | font-family: ui-monospace, SFMono-Regular, Consolas, 'Liberation Mono', Menlo,
44 | monospace;
45 | font-size: 1em;
46 | }
47 | small {
48 | font-size: 80%;
49 | }
50 | sub,
51 | sup {
52 | font-size: 75%;
53 | line-height: 0;
54 | position: relative;
55 | vertical-align: baseline;
56 | }
57 | sub {
58 | bottom: -0.25em;
59 | }
60 | sup {
61 | top: -0.5em;
62 | }
63 | table {
64 | text-indent: 0;
65 | border-color: inherit;
66 | }
67 | button,
68 | input,
69 | optgroup,
70 | select,
71 | textarea {
72 | font-family: inherit;
73 | font-size: 100%;
74 | line-height: 1.15;
75 | margin: 0;
76 | }
77 | button,
78 | select {
79 | text-transform: none;
80 | }
81 | [type='button'],
82 | [type='reset'],
83 | [type='submit'],
84 | button {
85 | -webkit-appearance: button;
86 | }
87 | ::-moz-focus-inner {
88 | border-style: none;
89 | padding: 0;
90 | }
91 | :-moz-focusring {
92 | outline: 1px dotted ButtonText;
93 | }
94 | :-moz-ui-invalid {
95 | box-shadow: none;
96 | }
97 | legend {
98 | padding: 0;
99 | }
100 | progress {
101 | vertical-align: baseline;
102 | }
103 | ::-webkit-inner-spin-button,
104 | ::-webkit-outer-spin-button {
105 | height: auto;
106 | }
107 | [type='search'] {
108 | -webkit-appearance: textfield;
109 | outline-offset: -2px;
110 | }
111 | ::-webkit-search-decoration {
112 | -webkit-appearance: none;
113 | }
114 | ::-webkit-file-upload-button {
115 | -webkit-appearance: button;
116 | font: inherit;
117 | }
118 | summary {
119 | display: list-item;
120 | }
121 | /*# sourceMappingURL=/sm/3c8a72540b353e8066a63580fff1b7fbff35925789e51ac52bb6794c81144ab3.map */
122 |
--------------------------------------------------------------------------------
/example/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `yarn start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will also see any lint errors in the console.
16 |
17 | ### `yarn test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `yarn build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `yarn eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35 |
36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39 |
40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `yarn build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------