ScrollableHStack

Use the ScrollableHStack component to align items horizontally in a scrollable container.

Props

xGap

Use this to set the horizontal gap between items.

TypeDefaultDescription
number0Any positive number.

Example

1
2
3
4
1
2
3
4
Source code
<View>
  <UIScrollableHStack
    xGap={16}
  >
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>1</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>2</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>3</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>4</Text></View>
  </UIScrollableHStack>
  <UIScrollableHStack
    xGap={32}
  >
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>1</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>2</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>3</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>4</Text></View>
  </UIScrollableHStack>
</View>

px

Use this to set horizontal padding.

TypeDefaultDescription
number0The most left and right padding. If the page layout has horizontal padding, the items will look like they are cut off while scrolling. In that case, you will have to remove the padding from the page layout and update this value instead.

Example

1
2
3
4
1
2
3
4
Source code
<View>
  <UIScrollableHStack
    xGap={16}
    px={32}
  >
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>1</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>2</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>3</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>4</Text></View>
  </UIScrollableHStack>
  <UIScrollableHStack
    xGap={16}
    px={32}
  >
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>1</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>2</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>3</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>4</Text></View>
  </UIScrollableHStack>
</View>

py

Use this to set vertical padding.

TypeDefaultDescription
number0Useful mostly if you enable showScrollIndicator and set it to true, to make sure the scroll indicator doesn't go over the items.
It's not enabled by default. Make sure to enable it in Editor if you want to use it.

Example

1
2
3
4
1
2
3
4
Source code
<View>
  <UIScrollableHStack
    xGap={16}
    py={16}
  >
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>1</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>2</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>3</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>4</Text></View>
  </UIScrollableHStack>
  <UIScrollableHStack
    xGap={16}
    py={32}
  >
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>1</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>2</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>3</Text></View>
    <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>4</Text></View>
  </UIScrollableHStack>
</View>

showScrollIndicator

Use this to show scroll indicator (scrollbar).

TypeDefaultDescription
booleanfalseIt's false explicitly, because if it was undefined it would be visible by default. The styles and behaviour of scrollbar is native and depends on OS.
It's not enabled by default. Make sure to enable it in Editor if you want to use it.

Example

Source code
<UIScrollableHStack showScrollIndicator>
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#e5ecf1'}} />
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#e5ecf1'}} />
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#e5ecf1'}} />
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#e5ecf1'}} />
</UIScrollableHStack>

reverse

Use this to reverse the order of items.

TypeDefaultDescription
boolean/If true, the order of items will be reversed.
It's not enabled by default. Make sure to enable it in Editor if you want to use it.

Example

6
5
4
3
2
1
Source code
<UIScrollableHStack reverse>
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>6</Text></View>
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>5</Text></View>
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>4</Text></View>
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>3</Text></View>
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>2</Text></View>
  <View style={{width: 110, height: 100, borderRadius: 8, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 16}}>1</Text></View>
</UIScrollableHStack>

children

Use this to set ReactNode items.

TypeDefaultDescription
ReactNode/You need to set width and height on item level.

Example

Source code
<UIHstack>
  {/* Your ReactNode item */}
  {/* Your ReactNode item */}
  {/* Your ReactNode item */}
</UIHstack>

...rest

Use this to spread the rest of the props to the ScrollView element.

TypeDefaultDescription
ScrollViewProps/If you need more control over the ScrollView element inside the ScrollableHStack, you can use any prop from ScrollView this way.

Example

Source code
<View>
  <ScrollView {...rest}>
    {/* ... */}
  </ScrollView>
</View>

Preview

numOfItems

Use this to set the number of items to display.

Tips

View wrapper

Our View wrapper element is essential in ensuring that the ScrollView doesn't take the entire available height. Without it, the ScrollView will take the entire available height.

ScrollView or FlatList

If you have a lot of items, you should consider using FlatList instead of ScrollView to improve performance, but for simple sections with a few items (and maybe "See all" item at the end), you can use ScrollView.

Editor

Source code
import React from 'react';
import {View, ScrollView} from 'react-native';

const UIScrollableHStack = ({
  xGap = 0,
  px = 0,
  children,
  ...rest
}) => {
  const styles = {
    scrollableHStackScroll: {
      flexGrow: 1,
      paddingHorizontal: px,
    },
    scrollableHStackItem: {
      marginLeft: xGap,
      minWidth: 0,
      flexShrink: 0,
    },
    scrollableHStackItemIsFirst: {
      marginLeft: 0,
    }
  };

  return (
    <View>
      <ScrollView
        horizontal
        contentContainerStyle={styles.scrollableHStackScroll}
        showsHorizontalScrollIndicator={false}
        {...rest}
      >
        {React.Children.map(children, (child, index) => {
          const isFirst = index === 0;
          return (
            <View style={[styles.scrollableHStackItem, isFirst && styles.scrollableHStackItemIsFirst]}>
              {React.cloneElement(child)}
            </View>
          )
        })}
      </ScrollView>
    </View>
  );
};

export default UIScrollableHStack;
Usage example
<UIScrollableHStack
  xGap={12}
  px={24}
>
  <View style={{width: 300, height: 260, borderRadius: 16, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 20}}>1</Text></View>
  <View style={{width: 300, height: 260, borderRadius: 16, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 20}}>2</Text></View>
  <View style={{width: 300, height: 260, borderRadius: 16, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 20}}>3</Text></View>
  <View style={{width: 300, height: 260, borderRadius: 16, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 20}}>4</Text></View>
  <View style={{width: 300, height: 260, borderRadius: 16, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 20}}>5</Text></View>
  <View style={{width: 300, height: 260, borderRadius: 16, backgroundColor: '#1bbeb0', justifyContent: 'center', alignItems: 'center'}}><Text style={{color: '#ffffff', fontSize: 20}}>6</Text></View>
</UIScrollableHStack>