├── .eslintrc.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── jest.config.js ├── package-lock.json ├── package.json ├── src ├── __snapshots__ │ └── index.test.tsx.snap ├── index.test.tsx └── index.tsx └── tsconfig.json /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | extends: airbnb 2 | parser: '@typescript-eslint/parser' 3 | plugins: 4 | - '@typescript-eslint' 5 | settings: 6 | import/resolver: 7 | node: 8 | extensions: 9 | - .ts 10 | - .tsx 11 | rules: 12 | import/extensions: off 13 | react/jsx-filename-extension: [error, { extensions: ['.tsx']}] 14 | '@typescript-eslint/no-unused-vars': error 15 | overrides: 16 | - files: ["src/**/*.test.ts", "src/**/*.test.tsx"] 17 | env: 18 | jest: true 19 | rules: 20 | import/no-extraneous-dependencies: off 21 | - files: ["src/**/*.ts", "src/**/*.tsx"] 22 | rules: 23 | no-undef: off 24 | no-restricted-globals: off 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '11' 4 | - '10' 5 | - '9' 6 | - '8' 7 | - '7' 8 | - 'stable' 9 | before_install: 10 | - npm i -g npm@5 11 | - npm i -g greenkeeper-lockfile@1 12 | before_script: greenkeeper-lockfile-update 13 | after_script: greenkeeper-lockfile-upload 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Kenneth Powers (https://knpw.rs) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-compose-components 2 | 3 | [![Dependency Status](https://img.shields.io/david/knpwrs/react-compose-components.svg)](https://david-dm.org/knpwrs/react-compose-components) 4 | [![devDependency Status](https://img.shields.io/david/dev/knpwrs/react-compose-components.svg)](https://david-dm.org/knpwrs/react-compose-components#info=devDependencies) 5 | [![Greenkeeper badge](https://badges.greenkeeper.io/knpwrs/react-compose-components.svg)](https://greenkeeper.io/) 6 | [![Build Status](https://img.shields.io/travis/knpwrs/react-compose-components.svg)](https://travis-ci.org/knpwrs/react-compose-components) 7 | [![Npm Version](https://img.shields.io/npm/v/react-compose-components.svg)](https://www.npmjs.com/package/react-compose-components) 8 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) 9 | [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) 10 | [![Badges](https://img.shields.io/badge/badges-8-orange.svg)](http://shields.io/) 11 | 12 | A utility to flatten component pyramids in React. 13 | 14 | ## Installation 15 | 16 | ``` 17 | npm i react-compose-components 18 | ``` 19 | 20 | ## Inspiration and Usage 21 | 22 | If you have ever worked on a large-scale React application you are probably 23 | familiar with what I refer to as a "provider pyramid." Consider an application 24 | that uses [`react`], [`redux`], [`glamorous`], [`react-i18next`], 25 | [`react-instantsearch`] (Algolia), [`react-router`], and perhaps some internal 26 | providers. You could have a root component that looks something like this: 27 | 28 | ```jsx 29 | // ... 30 | export default () => ( 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | ); 45 | ``` 46 | 47 | With this library, you can do the following: 48 | 49 | ```jsx 50 | import Compose from 'react-compose-components'; 51 | //.. 52 | export default () => ( 53 | 63 | ); 64 | ``` 65 | 66 | This flattens the pyramid leading to better maintainability and cleaner VCS diffs. 67 | 68 | The `Compose` component also accepts children: 69 | 70 | ```jsx 71 | import Compose from 'react-compose-components'; 72 | //.. 73 | export default () => ( 74 | 83 | 84 | 85 | ); 86 | ``` 87 | 88 | ## API 89 | 90 | This package has one default export, a component called `Compose`. `Compose` 91 | requires a single prop, `components`. `components` is an array of any of the 92 | following: 93 | 94 | * A React component. 95 | * A string (tag name such as `'div'`). 96 | * A two-element array where the first element is either a React component or a 97 | string, and the second element is an object representing props to pass to the 98 | component. 99 | 100 | ## TypeScript 101 | 102 | This package is written in TypeScript and ships with built-in typings. 103 | 104 | ## License 105 | 106 | **MIT** 107 | 108 | [`react`]: https://reactjs.org/ "React - A JavaScript Library for Building User Interfaces" 109 | [`redux`]: https://redux.js.org/ "A Predictable State Container for JavaScript Apps" 110 | [`glamorous`]: https://glamorous.rocks/ "glamorous - React Component Styling Solved 💄" 111 | [`react-i18next`]: https://react.i18next.com/ "An Internationalization Addon for React.js" 112 | [`react-instantsearch`]: https://community.algolia.com/react-instantsearch/ "Lightning-Fast Search for React Apps" 113 | [`react-router`]: https://reacttraining.com/react-router/ "Declarative Routing for React.js" 114 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | globals: { 3 | 'ts-jest': { 4 | tsConfigFile: 'tsconfig.json', 5 | }, 6 | }, 7 | moduleFileExtensions: [ 8 | 'js', 9 | 'ts', 10 | 'tsx', 11 | ], 12 | transform: { 13 | '^.+\\.(ts|tsx)$': './node_modules/ts-jest/preprocessor.js', 14 | }, 15 | testMatch: [ 16 | '**/*.test.(js|ts|tsx)', 17 | ], 18 | testEnvironment: 'jsdom', 19 | }; 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-compose-components", 3 | "version": "1.0.0", 4 | "description": "A utility to flatten component pyramids in React.", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "scripts": { 8 | "commitmsg": "commitlint -E HUSKY_GIT_PARAMS", 9 | "prebuild": "npm run lint", 10 | "build": "tsc", 11 | "prepack": "npm run build", 12 | "lint": "eslint src --ext .ts,.tsx", 13 | "test": "jest", 14 | "posttest": "npm run lint" 15 | }, 16 | "config": { 17 | "commitizen": { 18 | "path": "./node_modules/cz-conventional-changelog" 19 | } 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "git+https://github.com/knpwrs/react-compose-components.git" 24 | }, 25 | "keywords": [ 26 | "react", 27 | "compose", 28 | "components", 29 | "pyramid" 30 | ], 31 | "author": "Kenneth Powers (https://knpw.rs)", 32 | "license": "MIT", 33 | "bugs": { 34 | "url": "https://github.com/knpwrs/react-compose-components/issues" 35 | }, 36 | "homepage": "https://github.com/knpwrs/react-compose-components#readme", 37 | "peerDependencies": { 38 | "react": "^16.8.1" 39 | }, 40 | "devDependencies": { 41 | "@commitlint/cli": "^7.2.1", 42 | "@commitlint/config-conventional": "^7.1.2", 43 | "@types/jest": "^24.0.6", 44 | "@types/react": "^16.8.2", 45 | "@types/react-dom": "^16.0.5", 46 | "@typescript-eslint/eslint-plugin": "^1.3.0", 47 | "@typescript-eslint/parser": "^1.3.0", 48 | "eslint": "^5.9.0", 49 | "eslint-config-airbnb": "^17.1.0", 50 | "eslint-plugin-import": "^2.14.0", 51 | "eslint-plugin-jsx-a11y": "^6.1.2", 52 | "eslint-plugin-react": "^7.11.1", 53 | "husky": "^1.2.0", 54 | "jest": "^24.1.0", 55 | "react": "^16.8.1", 56 | "react-dom": "^16.8.1", 57 | "react-testing-library": "^6.0.0", 58 | "ts-jest": "^24.0.0", 59 | "typescript": "^3.3.3333" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/__snapshots__/index.test.tsx.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`should compose components and attach props with children 1`] = ` 4 |
7 |
    8 |
  • 9 | Bar 10 |
  • 11 |
12 |
13 | `; 14 | 15 | exports[`should compose components without children 1`] = ` 16 |
19 | 22 |
23 | `; 24 | -------------------------------------------------------------------------------- /src/index.test.tsx: -------------------------------------------------------------------------------- 1 | import React, { ReactChildren } from 'react'; 2 | import { render } from 'react-testing-library'; 3 | import Compose from './index'; 4 | 5 | interface Props { 6 | children: ReactChildren; 7 | } 8 | 9 | const Div = ({ children, ...rest }: Props) =>
{children}
; 10 | const Li = ({ children }: Props) =>
  • {children}
  • ; 11 | 12 | it('should compose components and attach props with children', () => { 13 | const { container } = render(( 14 | 21 | Bar 22 | 23 | )); 24 | // Explitly manual testing 25 | const div = container.children[0]; 26 | expect(div.tagName).toBe('DIV'); 27 | expect(div.className).toBe('foo'); 28 | expect(div.children).toHaveLength(1); 29 | const ul = div.children[0]; 30 | expect(ul.tagName).toBe('UL'); 31 | expect(ul.className).toBe(''); 32 | expect(ul.children).toHaveLength(1); 33 | const li = ul.children[0]; 34 | expect(li.tagName).toBe('LI'); 35 | expect(li.className).toBe(''); 36 | expect(li.children).toHaveLength(0); 37 | expect(li.textContent).toBe('Bar'); 38 | // Extra safety 39 | expect(div).toMatchSnapshot(); 40 | }); 41 | 42 | it('should compose components without children', () => { 43 | const { container } = render(( 44 | 50 | )); 51 | // Explicitly manual testing 52 | const div = container.children[0]; 53 | expect(div.tagName).toBe('DIV'); 54 | expect(div.className).toBe('foo'); 55 | expect(div.children).toHaveLength(1); 56 | const i = div.children[0]; 57 | expect(i.tagName).toBe('I'); 58 | expect(i.className).toBe('icon-bar'); 59 | expect(i.children).toHaveLength(0); 60 | // Extra safety 61 | expect(div).toMatchSnapshot(); 62 | }); 63 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { 2 | ComponentType, 3 | ReactElement, 4 | } from 'react'; 5 | 6 | export type InputType = string 7 | | ComponentType 8 | | [ComponentType, { [name: string]: any }] 9 | | [string, { [name: string ]: any }]; 10 | 11 | export interface Props { 12 | components: InputType[]; 13 | children?: any; 14 | } 15 | 16 | const Compose: ComponentType = ({ components, children }: Props) => ( 17 | components.reduceRight>((prev, curr) => { 18 | if (Array.isArray(curr)) { 19 | const [Comp, props] = curr; 20 | return ( 21 | {prev} 22 | ); 23 | } 24 | const Comp = curr; 25 | return ( 26 | {prev} 27 | ); 28 | }, children) 29 | ); 30 | 31 | export default Compose; 32 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "esModuleInterop": true, 5 | "jsx": "react", 6 | "target": "es5", 7 | "lib": ["es6", "dom"], 8 | "noImplicitAny": true, 9 | "strict": true, 10 | "strictFunctionTypes": false, 11 | "moduleResolution": "node", 12 | "sourceMap": true, 13 | "outDir": "dist", 14 | "baseUrl": ".", 15 | "declaration": true 16 | }, 17 | "include": [ 18 | "src/**/*" 19 | ] 20 | } 21 | --------------------------------------------------------------------------------