项目作者: eightyfive

项目描述 :
React Native Flexbox made easy
高级语言: JavaScript
项目地址: git://github.com/eightyfive/react-native-col.git
创建时间: 2019-01-24T06:59:18Z
项目社区:https://github.com/eightyfive/react-native-col

开源协议:MIT License

下载


react-native-col

Flexbox made easy & semantic

  1. ┌─────────────┐
  2. TL T TR
  3. L C R
  4. BL B BR
  5. └─────────────┘

Install

  1. yarn add react-native-col

Usage

Before

  1. import { View } from 'react-native';
  2. <View
  3. style={{
  4. flexDirection: 'row',
  5. justifyContent: 'flex-end',
  6. alignItems: 'flex-start',
  7. }}
  8. ></View>;

After

  1. import { Row } from 'react-native-col';
  2. // "Top Right"
  3. <Row.TR ></Row.TR>;

API

All demos use these 3 RGB squares:

  1. import { View } from 'react-native';
  2. const square = {
  3. minWidth: 50,
  4. minHeight: 50,
  5. };
  6. const $ = {
  7. // Red
  8. r: {
  9. ...square,
  10. backgroundColor: 'red',
  11. },
  12. // Green
  13. g: {
  14. ...square,
  15. backgroundColor: 'green',
  16. },
  17. // Blue
  18. b: {
  19. ...square,
  20. backgroundColor: 'blue',
  21. },
  22. };
  23. const Red = () => <View style={$.r} ></View>;
  24. const Green = () => <View style={$.g} ></View>;
  25. const Blue = () => <View style={$.b} ></View>;

Also assume that all Col and Row containers in demos, are { flex: 1 }. Which are redacted for clarity.

Col

  1. import { Col } from 'react-native-col';

Top Left

  1. // Equivalent to <Col />
  2. <Col.TL>
  3. <Red ></Red>
  4. <Green ></Green>
  5. <Blue ></Blue>
  6. </Col.TL>

Top

  1. <Col.T>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.T>

Top Right

  1. <Col.TR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.TR>

Left

  1. <Col.L>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.L>

Center

  1. <Col.C>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.C>

Right

  1. <Col.R>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.R>

Bottom Left

  1. <Col.BL>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.BL>

Bottom

  1. <Col.B>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.B>

Bottom Right

  1. <Col.BR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.BR>

Top to Bottom, aligned Left

  1. <Col.TBL>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.TBL>

Top to Bottom

  1. <Col.TB>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.TB>

Top to Bottom, aligned Right

  1. <Col.TBR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.TBR>

Bottom to Top, aligned Left

  1. <Col.BTL>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.BTL>

Bottom to Top

  1. <Col.BT>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.BT>

Bottom to Top, aligned Right

  1. <Col.BTR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.BTR>

Top, aligned Left to Right

  1. <Col.LRT>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.LRT>

Center, aligned Left to Right

  1. <Col.LRC>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.LRC>

Bottom, aligned Left to Right

  1. <Col.LRB>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col.LRB>

Row

  1. import { Row } from 'react-native-col';

Top Left

  1. // Equivalent to <Row ></Row>
  2. <Row.TL>
  3. <Red ></Red>
  4. <Green ></Green>
  5. <Blue ></Blue>
  6. </Row.TL>

Top

  1. <Row.T>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.T>

Top Right

  1. <Row.TR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.TR>

Left

  1. <Row.L>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.L>

Center

  1. <Row.C>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.C>

Rigth

  1. <Row.R>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.R>

Bottom Left

  1. <Row.BL>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.BL>

Bottom

  1. <Row.B>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.B>

Bottom Right

  1. <Row.BR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.BR>

Left to Right, aligned Top

  1. <Row.LRT>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.LRT>

Left to Right

  1. <Row.LR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.LR>

Left to Right, aligned Bottom

  1. <Row.LRB>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.LRB>

Right to Left, aligned Top

  1. <Row.RLT>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.RLT>

Right to Left

  1. <Row.RL>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.RL>

Right to Left, aligned Bottom

  1. <Row.RLB>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.RLB>

Left, aligned Top to Bottom

  1. <Row.TBL>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.TBL>

Center, aligned Top to Bottom

  1. <Row.TBC>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.TBC>

Right, aligned Top to Bottom

  1. <Row.TBR>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Row.TBR>

Col0, Col1, Row7… (Flex)

The package also exports Col[0-9] and Row[0-9] views with pre-defined flex values.

So instead of writing:

  1. <Col style={{ flex: 1 }}>
  2. <Red ></Red>
  3. <Green ></Green>
  4. <Blue ></Blue>
  5. </Col>

You could make use of Col1:

  1. import { Col1 } from 'react-native-col';
  2. <Col1>
  3. <Red ></Red>
  4. <Green ></Green>
  5. <Blue ></Blue>
  6. </Col1>;

Here are all possible pre-defined flex values:

  1. import {
  2. Col0,
  3. Col1,
  4. Col2,
  5. Col3,
  6. Col4,
  7. Col5,
  8. Col6,
  9. Col7,
  10. Col8,
  11. Col9,
  12. //
  13. Row0,
  14. Row1,
  15. Row2,
  16. Row3,
  17. Row4,
  18. Row5,
  19. Row6,
  20. Row7,
  21. Row8,
  22. Row9,
  23. } from 'react-native-col';

You can also use the positioning shortcuts on them:

  1. <Col6.TL />
  2. <Col0.T />
  3. <Col2.TR />
  4. <Col9.L />
  5. <Col4.C />
  6. // ...
  7. <Row7.TL ></Row7.TL>
  8. <Row4.T ></Row4.T>
  9. // ...

create(Col|Row)

  1. createCol<T extends ViewProps>(BaseComponent: ComponentType<any>)
  1. createRow<T extends ViewProps>(BaseComponent: ComponentType<any>)

Utility functions to generate all positioning shortcuts based on your BaseComponent of choice.

Works great with react-native-themesheet for example:

  1. import { createTheme } from 'react-native-themesheet';
  2. // src/lib/theme.ts
  3. export const { createBox } = createTheme(
  4. { primary: '#ff00dd' },
  5. { s: 4, m: 8, l: 16, xl: 32 }
  6. );
  7. // src/lib/index.ts
  8. import { View } from 'react-native';
  9. import { createCol } from 'react-native-col';
  10. import { createBox } from './theme';
  11. const Box = createBox(View);
  12. export const Col = createCol(Box);
  13. export const Row = createRow(Box);
  14. // src/screens/home.tsx
  15. import { Col, Row } from '../lib';
  16. export function Home() {
  17. return (
  18. <Col.C py="xl" mb="l">
  19. {/* ... */}
  20. </Col.C>
  21. <Row.LR p="m" mb="m">{/* ... */}</Row.LR>
  22. );
  23. }

createDialStyle

  1. type Dial = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  2. createDialStyle(flexDirection: 'row' | 'column', dial: Dial): ViewStyle

Utility function to generate Flex “dial” positioning raw styles.

Think of your Flexbox container as a dial number pad:

  1. ┌─────────────┐
  2. 1 2 3
  3. 4 5 6
  4. 7 8 9
  5. └─────────────┘

You can then align container items according to the “dial” number:

  1. import { createDialStyle as dial } from 'react-native-col';
  2. dial('column', 3); // --> col direction, justified right (flex-end), aligned Top
  3. dial('row', 8); // --> row direction, justified center, aligned bottom
  4. // Etc

(col|row|flex)Styles

All react-native StyleSheet styles used by the library are re-exported:

  1. import { colStyles, rowStyles, flexStyles } from 'react-native-col';
  2. colStyles.col;
  3. colStyles.TR;
  4. colStyles.T;
  5. // Etc...
  6. rowStyles.row;
  7. rowStyles.B;
  8. rowStyles.BR;
  9. // Etc...
  10. flexStyles.f0;
  11. flexStyles.f1;
  12. flexStyles.f2;
  13. // Etc...

Credits