Skip to main content

Grid

Block component it is a wrapper over the React-Native View component, using the Flexbox algorithm which is designed to provide a consistent layout on different screen sizes. We will enhance this component to support the grid and layout styling using properties that will speed up our development time.

You will normally use a combination of BlockProps to achieve the right layout.

API

import { Block } from '@flexnative/layout';

Component

This component extends React.PureComponent, which optimizes the component by preventing unnecessary re-renders when the props haven't changed.

Block Type: React.PureComponent<BlockProps>

Properties

info

BlockProps extends ViewProps from React Native, so any property that is available in the View component is also available in the Block component.

NameTypeRequiredDefault ValueDescription
flexViewStyle["flex"]false1Optional property to set flex style for block component.
rowbooleanfalsefalseOptional boolean property to determine if the layout should be in a row format. If set to true, it likely indicates that the component's children should be displayed horizontally.
justifyContentViewStyle["justifyContent"]falseundefinedAlternative optional property name for justify content along the main axis.
justifyViewStyle["justifyContent"]falseundefinedA short version for justifyContent style property. Same as justifyContent property.
alignItemsViewStyle["alignItems"]falseundefinedOptional property to set alignment of items along the cross-axis.
alignViewStyle["alignItems"]falseundefinedA short version for alignItems style property. Same as alignItems property.
alignContentViewStyle["alignContent"]falseundefinedDefines the distribution of lines along the cross-axis. This only has effect when items are wrapped to multiple lines using flexWrap.
contentViewStyle["alignContent"]falseundefinedA short version for alignContent style property. Same as alignContent property.
widthViewStyle["width"]falseundefinedOptional property to set the width of the Block component.
heightViewStyle["height"]falseundefinedOptional property to set the height of the Block component.
wrapViewStyle["flexWrap"]falsefalseIt controls whether children can wrap around after they hit the end of a flex container.
gapViewStyle["gap"]falseundefinedOptional property to specify the gap between rows or column in grid layout.
rowGapViewStyle["rowGap"]falseundefinedOptional property to specify the gap between rows in grid layout.
columnGapViewStyle["columnGap"]falseundefinedOptional property to specify the gap between columns in grid layout.
paddingViewStyle["padding"]falseundefinedOptional property to set padding for the Block component.
backgroundColorViewStyle["backgroundColor"]falseundefinedOptional property to set the background color of the Block component.
childrenReactNodefalseundefinedThe children of the Block component.

Use case Examples

The following example demonstrates the Box component in action.

Flex

flex will define how items are going to “fill” over the available space along your main axis. Space will be divided according to each element's flex property.

flex: 0.5
flex: 2
flex: 1.5

Row

row will define the flexDirection. If row is set to true, flexDirection it is set to row and component's children should be displayed horizontally, otherwise flexDirection it is set to column and component's children should be displayed vertically.

Block 1. col 1
Block 1. col 2
Block 2. row 1
Block 2. row 2

Justify Content

justifyContent specifies how to align children within the main axis of their container. For example, you can use this property to center a child horizontally within a container with flexDirection set to row or vertically within a container with flexDirection set to column.

Property justify it is added as a short version for justifyContent style property.

flex-start
flex-end
center
space-around
space-around
space-between
space-between
space-evenly
space-evenly

Align Items

alignItems defines how to align children along the cross axis of their container.

Property align it is added as a short version for alignItems style property.

center
flex-end
flex-start
baseline
stretch

Align Content

alignContent defines the distribution of lines along the cross-axis and it has effect only when items are wrapped to multiple lines using flexWrap.

Property content it is added as a short version for alignContent style property.

center
flex-end
flex-start
space-around
space-around
stretch
stretch
space-between
space-between
space-evenly
space-evenly

Width

Specify the width of the Block component.

width: 150
width: 200
width: '50%'

Height

Specify the height of the Block component.

height: 30
height: 50
height: '50%'

Wrap

wrap controls whether children can wrap around after they hit the end of a flex container.

nowrap
nowrap
nowrap
nowrap
nowrap
nowrap
nowrap
nowrap
nowrap
nowrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap
wrap-reverse: 1
wrap-reverse: 2
wrap-reverse: 3
wrap-reverse: 4
wrap-reverse: 5
wrap-reverse: 6
wrap-reverse: 7
wrap-reverse: 8
wrap-reverse: 9
wrap-reverse: 10
wrap-reverse: 11
wrap-reverse: 12
wrap-reverse: 13
wrap-reverse: 14

Gap

gap defines the size of the gap between the rows and between the columns in flexbox, grid or multi-column layout.

It is a shorthand for the following properties:

  • rowGap
  • columnGap
Row 1 : Clo 1. flex: 1
Row 1 : Clo 2. flex: 2
Row 2 : Clo 1. flex: 1
Row 2 : Clo 2. flex: 2

Row Gap

The rowGap property specifies the gap between the rows in a flexbox or grid layout.

Row 1 : Clo 1. flex: 1
Row 1 : Clo 2. flex: 2
Row 2 : Clo 1. flex: 1
Row 2 : Clo 2. flex: 2

Column Gap

The columnGap property specifies the gap between the columns in a flexbox or grid layout.

Row 1 : Clo 1. flex: 1
Row 1 : Clo 2. flex: 2
Row 2 : Clo 1. flex: 1
Row 2 : Clo 2. flex: 2

Padding

The padding property for padding top, bottom, left, right.

padding: 5
padding: 20
padding: 45

Background Color

crimson
hex: #229954
RGB: 255, 195, 0
HSL: 283, 39%, 53%

Playground

Live Editor
const DEMO_PADDING = 15;

const styles = {
  text: {
    padding: 5,
    borderRadius: 3,
    borderWidth: 1,
    borderColor: '#fff',
    color: '#fff'
  },
};

render(
  <Block>
    <Block row>
      <Block backgroundColor='crimson' padding={DEMO_PADDING}>
        <p style={styles.text}>crimson</p>
      </Block>
      
      <Block backgroundColor='#229954' padding={DEMO_PADDING}>
        <p style={styles.text}>hex: #229954</p>
      </Block>
    </Block>

    <Block>
      <Block backgroundColor='rgb(255, 195, 0)' padding={DEMO_PADDING}>
        <p style={styles.text}>RGB: 255, 195, 0</p>
      </Block>

      <Block backgroundColor='hsl(283, 39%, 53%)' padding={DEMO_PADDING}>
        <p style={styles.text}>HSL: 283, 39%, 53%</p>
      </Block>
    </Block>
  </Block>
);
Result
Loading...