├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── Row.js ├── View.js ├── examples ├── example1.jpg ├── example2.jpg ├── example3.jpg └── example4.jpg ├── index.js ├── package-lock.json ├── package.json ├── shorthandStyles.js └── shorthandStyles.test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["env", "react"] 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # webstorm 40 | .idea 41 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # webstorm 40 | .idea 41 | 42 | # examples 43 | *.jpg 44 | examples/ 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Hyr inc 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-row 2 | 3 | A wrapper around the react-native `` component enabling concise assignment of flexbox layout properties. 4 | 5 | The idea is to keep JSX as clean as possible, while removing the need to manage stylesheet declarations for common positioning needs. 6 | 7 | ### Installation 8 | 9 | npm install react-native-row 10 | 11 | 12 | ### Basic Usage 13 | 14 | Use react-native-row as little or as much as you want. For instance, you could just use the `` to replace 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | with: 23 | 24 | import Row from 'react-native-row' 25 | 26 | 27 | 28 | 29 | 30 | But if you are going to the trouble of installing this module consider these other benefits and weigh the option of reducing stylesheet usage in favor of inline shorthands. The first of which is `dial`. 31 | 32 | 33 | ### Dial 34 | 35 | One of the most DRY features of react-native-row is the `dial` prop which allows you to replace 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | with: 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | The child components of `Row` are positioned according to the position of the `dial` number on a phone dial pad. In this case `5` is centered along both axis and, because this is a `Row`, children are horizontally aligned. 55 | 56 | 57 | 58 | 59 | 60 | 61 | Optionally import `View` from this package instead of from `react-native` and all of your `` components can use dial as well. 62 | 63 | import Row, { View } from 'react-native-row' 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | Use `View` without replacing the core RN `View` like so 74 | 75 | import Row, { View as DialView } from 'react-native-row' 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | You can also use `spaceBetween` to override `justifyContent`: 85 | 86 | 87 | 88 | 89 | ... 90 | 91 | 92 | 93 | 94 | 95 | and `spaceAround` 96 | 97 | 98 | 99 | 100 | 101 | 102 | ... 103 | 104 | 105 | 106 | 107 | and use `stretch` to override `alignItems` 108 | 109 | 110 | 111 | 112 | ... 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | ### Flex 122 | 123 | 124 | 125 | does this 126 | 127 | 128 | 129 | or 130 | 131 | 132 | 133 | is 134 | 135 | 136 | 137 | ### Reverse 138 | 139 | Use reverse to change `row` to `row-reverse` or `column` to `column-reverse`: 140 | 141 | 142 | 143 | 144 | ### Margin and Padding shorthand 145 | 146 | Extra benefits are gained by using react-native-view instead of the core `View` in that you can use shorthands for `margin` and `padding` styles that are based on the css shorthand convention 147 | 148 | 149 | 150 | 151 | becomes 152 | 153 | 154 | 155 | #### margin 156 | 157 | Shorthand | Style Result 158 | ------------ | ------------- 159 | `margin={20}` | `{margin: 20}` 160 | `margin={[20]}` | `{marginVertical: 20}` 161 | `margin={[20,15]}` | `{marginVertical: 20, marginHorizontal: 15 }` 162 | `margin={[20,15,10]}` | `{marginTop: 20, marginHorizontal: 15, marginBottom: 10}` 163 | `margin={[20,15,10,5]}` | `{marginTop: 20, marginRight: 15, marginBottom: 10, marginLeft: 5}` 164 | 165 | #### padding 166 | 167 | Shorthand | Style Result 168 | ------------ | ------------- 169 | `padding={20}` | `{padding: 20}` 170 | `padding={[20]}` | `{paddingVertical: 20}` 171 | `padding={[20,15]}` | `{paddingVertical: 20, paddingHorizontal: 15}` 172 | `padding={[20,15,10]}` | `{paddingTop: 20, paddingHorizontal: 15, paddingBottom: 10}` 173 | `padding={[20,15,10,5]}` | `{paddingTop: 20, paddingRight: 15, paddingBottom: 10, paddingLeft: 5}` 174 | 175 | 176 | 177 | Contributions and issues very much welcome! 178 | -------------------------------------------------------------------------------- /Row.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { View } from 'react-native'; 3 | import shorthandStyles from './shorthandStyles' 4 | 5 | const Row = (props) => { 6 | 7 | const { 8 | dial = 0, 9 | flex: _flex, 10 | spaceBetween, 11 | spaceAround, 12 | stretch, 13 | margin, 14 | padding, 15 | style, 16 | reverse, 17 | ...otherProps, 18 | } = props; 19 | 20 | const _dial = dial > 0 && dial < 10 ? dial : 0; 21 | const flex = typeof(_flex) === "number" ? _flex : !_flex ? null : 1 22 | 23 | const _shorthandStyles = shorthandStyles(margin, padding) 24 | 25 | const justifyContent = spaceBetween ? 'space-between' : spaceAround ? 'space-around' : _dial === 0 ? null : _dial % 3 === 0 ? 'flex-end' : 26 | _dial % 3 === 2 ? 'center' : 'flex-start'; 27 | 28 | const alignItems = stretch ? 'stretch' : _dial === 0 ? null : _dial > 6 ? 'flex-end' : 29 | _dial > 3 ? 'center' : 'flex-start'; 30 | 31 | const flexDirection = reverse ? 'row-reverse' : 'row'; 32 | 33 | return ( 34 | 35 | {props.children} 36 | 37 | ); 38 | }; 39 | 40 | export default Row 41 | -------------------------------------------------------------------------------- /View.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import RN from 'react-native'; 3 | import shorthandStyles from './shorthandStyles' 4 | const RNView = RN.View; 5 | 6 | const View = (props) => { 7 | 8 | const { 9 | dial = 0, 10 | flex: _flex, 11 | style, 12 | spaceBetween, 13 | spaceAround, 14 | stretch, 15 | margin, 16 | padding, 17 | reverse, 18 | ...otherProps, 19 | } = props; 20 | 21 | const _dial = dial > 0 && dial < 10 ? dial : 0; 22 | const flex = typeof(_flex) === "number" ? _flex : !_flex ? null : 1 23 | 24 | const _shorthandStyles = shorthandStyles(margin, padding) 25 | 26 | const justifyContent = spaceBetween ? 'space-between' : spaceAround ? 'space-around' : _dial === 0 ? null : _dial > 6 ? 'flex-end' : 27 | _dial > 3 ? 'center' : 'flex-start'; 28 | 29 | const alignItems = stretch ? 'stretch' : _dial === 0 ? null : _dial % 3 === 0 ? 'flex-end' : 30 | _dial % 3 === 2 ? 'center' : 'flex-start'; 31 | 32 | const flexDirection = reverse ? 'column-reverse' : 'column'; 33 | 34 | return ( 35 | 36 | {props.children} 37 | 38 | ); 39 | }; 40 | 41 | export default View 42 | -------------------------------------------------------------------------------- /examples/example1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyrwork/react-native-row/c67adf3846161d0e94b275525bd170a8b76f4290/examples/example1.jpg -------------------------------------------------------------------------------- /examples/example2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyrwork/react-native-row/c67adf3846161d0e94b275525bd170a8b76f4290/examples/example2.jpg -------------------------------------------------------------------------------- /examples/example3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyrwork/react-native-row/c67adf3846161d0e94b275525bd170a8b76f4290/examples/example3.jpg -------------------------------------------------------------------------------- /examples/example4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hyrwork/react-native-row/c67adf3846161d0e94b275525bd170a8b76f4290/examples/example4.jpg -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import View from './View'; 2 | import Row from './Row'; 3 | 4 | export default Row; 5 | export { View }; 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-row", 3 | "version": "1.1.1", 4 | "description": "A wrapper around the react-native View component enabling concise assignment of flexbox properties", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://ssomnoremac@github.com/hyrwork/react-native-row.git" 12 | }, 13 | "keywords": [ 14 | "layout", 15 | "react", 16 | "native", 17 | "flex", 18 | "row" 19 | ], 20 | "author": "Cameron Moss", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/hyrwork/react-native-row/issues" 24 | }, 25 | "homepage": "https://github.com/hyrwork/react-native-row#readme", 26 | "dependencies": {}, 27 | "devDependencies": { 28 | "babel-core": "^6.26.3", 29 | "babel-jest": "^23.6.0", 30 | "babel-preset-env": "^1.7.0", 31 | "babel-preset-react": "^6.24.1", 32 | "jest": "^23.6.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /shorthandStyles.js: -------------------------------------------------------------------------------- 1 | const shorthandStyles = (margin, padding) => Object.assign( 2 | getSpacing('margin', margin), 3 | getSpacing('padding', padding) 4 | ) 5 | 6 | function getSpacing(type, spacing) { 7 | const s = {} 8 | 9 | if (typeof spacing === "number") { s[type] = spacing } 10 | else if (Array.isArray(spacing)){ 11 | switch (spacing.length) { 12 | case 1: 13 | s[`${type}Vertical`] = spacing[0] 14 | break 15 | case 2: 16 | s[`${type}Vertical`] = spacing[0] 17 | s[`${type}Horizontal`] = spacing[1] 18 | break 19 | case 3: 20 | s[`${type}Top`] = spacing[0] 21 | s[`${type}Horizontal`] = spacing[1] 22 | s[`${type}Bottom`] = spacing[2] 23 | break 24 | case 4: 25 | s[`${type}Top`] = spacing[0] 26 | s[`${type}Right`] = spacing[1] 27 | s[`${type}Bottom`] = spacing[2] 28 | s[`${type}Left`] = spacing[3] 29 | break 30 | default: 31 | break 32 | } 33 | } 34 | 35 | return s 36 | } 37 | 38 | export default shorthandStyles -------------------------------------------------------------------------------- /shorthandStyles.test.js: -------------------------------------------------------------------------------- 1 | import getStyles from "./shorthandStyles"; 2 | 3 | test("margin", () => { 4 | expect(getStyles(20)).toEqual({ 5 | margin: 20 6 | }); 7 | }); 8 | 9 | test("margin padding", () => { 10 | expect(getStyles(20, 20)).toEqual({ 11 | margin: 20, 12 | padding: 20 13 | }); 14 | }); 15 | 16 | test("vertical", () => { 17 | expect(getStyles([20])).toEqual({ 18 | marginVertical: 20 19 | }); 20 | }); 21 | 22 | test("vertical horizontal", () => { 23 | expect(getStyles([20, 15])).toEqual({ 24 | marginVertical: 20, 25 | marginHorizontal: 15 26 | }); 27 | }); 28 | 29 | test("top horizontal bottom", () => { 30 | expect(getStyles([20, 15, 10])).toEqual({ 31 | marginTop: 20, 32 | marginHorizontal: 15, 33 | marginBottom: 10 34 | }); 35 | }); 36 | 37 | test("top right bottom left", () => { 38 | expect(getStyles([20, 15, 10, 5])).toEqual({ 39 | marginTop: 20, 40 | marginRight: 15, 41 | marginBottom: 10, 42 | marginLeft: 5 43 | }); 44 | }); 45 | --------------------------------------------------------------------------------