Flexbox made easy & semantic
┌─────────────┐
│ TL T TR │
│ │
│ L C R │
│ │
│ BL B BR │
└─────────────┘
yarn add react-native-col
import { View } from 'react-native';
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'flex-start',
}}
/>;
import { Row } from 'react-native-col';
// "Top Right"
<Row.TR />;
All demos use these 3 RGB squares:
import { View } from 'react-native';
const square = {
minWidth: 50,
minHeight: 50,
};
const $ = {
// Red
r: {
...square,
backgroundColor: 'red',
},
// Green
g: {
...square,
backgroundColor: 'green',
},
// Blue
b: {
...square,
backgroundColor: 'blue',
},
};
const Red = () => <View style={$.r} />;
const Green = () => <View style={$.g} />;
const Blue = () => <View style={$.b} />;
Also assume that all Col
and Row
containers in demos, are { flex: 1 }
. Which are redacted for clarity.
import { Col } from 'react-native-col';
// Equivalent to <Col />
<Col.TL>
<Red />
<Green />
<Blue />
</Col.TL>
<Col.T>
<Red />
<Green />
<Blue />
</Col.T>
<Col.TR>
<Red />
<Green />
<Blue />
</Col.TR>
<Col.L>
<Red />
<Green />
<Blue />
</Col.L>
<Col.C>
<Red />
<Green />
<Blue />
</Col.C>
<Col.R>
<Red />
<Green />
<Blue />
</Col.R>
<Col.BL>
<Red />
<Green />
<Blue />
</Col.BL>
<Col.B>
<Red />
<Green />
<Blue />
</Col.B>
<Col.BR>
<Red />
<Green />
<Blue />
</Col.BR>
<Col.TBL>
<Red />
<Green />
<Blue />
</Col.TBL>
<Col.TB>
<Red />
<Green />
<Blue />
</Col.TB>
<Col.TBR>
<Red />
<Green />
<Blue />
</Col.TBR>
<Col.BTL>
<Red />
<Green />
<Blue />
</Col.BTL>
<Col.BT>
<Red />
<Green />
<Blue />
</Col.BT>
<Col.BTR>
<Red />
<Green />
<Blue />
</Col.BTR>
<Col.LRT>
<Red />
<Green />
<Blue />
</Col.LRT>
<Col.LRC>
<Red />
<Green />
<Blue />
</Col.LRC>
<Col.LRB>
<Red />
<Green />
<Blue />
</Col.LRB>
import { Row } from 'react-native-col';
// Equivalent to <Row />
<Row.TL>
<Red />
<Green />
<Blue />
</Row.TL>
<Row.T>
<Red />
<Green />
<Blue />
</Row.T>
<Row.TR>
<Red />
<Green />
<Blue />
</Row.TR>
<Row.L>
<Red />
<Green />
<Blue />
</Row.L>
<Row.C>
<Red />
<Green />
<Blue />
</Row.C>
<Row.R>
<Red />
<Green />
<Blue />
</Row.R>
<Row.BL>
<Red />
<Green />
<Blue />
</Row.BL>
<Row.B>
<Red />
<Green />
<Blue />
</Row.B>
<Row.BR>
<Red />
<Green />
<Blue />
</Row.BR>
<Row.LRT>
<Red />
<Green />
<Blue />
</Row.LRT>
<Row.LR>
<Red />
<Green />
<Blue />
</Row.LR>
<Row.LRB>
<Red />
<Green />
<Blue />
</Row.LRB>
<Row.RLT>
<Red />
<Green />
<Blue />
</Row.RLT>
<Row.RL>
<Red />
<Green />
<Blue />
</Row.RL>
<Row.RLB>
<Red />
<Green />
<Blue />
</Row.RLB>
<Row.TBL>
<Red />
<Green />
<Blue />
</Row.TBL>
<Row.TBC>
<Red />
<Green />
<Blue />
</Row.TBC>
<Row.TBR>
<Red />
<Green />
<Blue />
</Row.TBR>
The package also exports Col[0-9]
and Row[0-9]
views with pre-defined flex
values.
So instead of writing:
<Col style={{ flex: 1 }}>
<Red />
<Green />
<Blue />
</Col>
You could make use of Col1
:
import { Col1 } from 'react-native-col';
<Col1>
<Red />
<Green />
<Blue />
</Col1>;
Here are all possible pre-defined flex
values:
import {
Col0,
Col1,
Col2,
Col3,
Col4,
Col5,
Col6,
Col7,
Col8,
Col9,
//
Row0,
Row1,
Row2,
Row3,
Row4,
Row5,
Row6,
Row7,
Row8,
Row9,
} from 'react-native-col';
You can also use the positioning shortcuts on them:
<Col6.TL />
<Col0.T />
<Col2.TR />
<Col9.L />
<Col4.C />
// ...
<Row7.TL />
<Row4.T />
// ...
createCol<T extends ViewProps>(BaseComponent: ComponentType<any>)
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:
import { createTheme } from 'react-native-themesheet';
// src/lib/theme.ts
export const { createBox } = createTheme(
{ primary: '#ff00dd' },
{ s: 4, m: 8, l: 16, xl: 32 }
);
// src/lib/index.ts
import { View } from 'react-native';
import { createCol } from 'react-native-col';
import { createBox } from './theme';
const Box = createBox(View);
export const Col = createCol(Box);
export const Row = createRow(Box);
// src/screens/home.tsx
import { Col, Row } from '../lib';
export function Home() {
return (
<Col.C py="xl" mb="l">
{/* ... */}
</Col.C>
<Row.LR p="m" mb="m">{/* ... */}</Row.LR>
);
}
type Dial = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
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 3 │
│ │
│ 4 5 6 │
│ │
│ 7 8 9 │
└─────────────┘
You can then align container items according to the "dial" number:
import { createDialStyle as dial } from 'react-native-col';
dial('column', 3); // --> col direction, justified right (flex-end), aligned Top
dial('row', 8); // --> row direction, justified center, aligned bottom
// Etc
All react-native StyleSheet
styles used by the library are re-exported:
import { colStyles, rowStyles, flexStyles } from 'react-native-col';
colStyles.col;
colStyles.TR;
colStyles.T;
// Etc...
rowStyles.row;
rowStyles.B;
rowStyles.BR;
// Etc...
flexStyles.f0;
flexStyles.f1;
flexStyles.f2;
// Etc...
react-native-row
for the initialdial
idea