Skip to content

Instantly share code, notes, and snippets.

@ryonakae
Last active January 4, 2022 14:58
Show Gist options
  • Save ryonakae/29f0f320ac577f2af5723986f6067b4e to your computer and use it in GitHub Desktop.
Save ryonakae/29f0f320ac577f2af5723986f6067b4e to your computer and use it in GitHub Desktop.
HStack / VStack / Spacer component on React Native
import React from 'react'
import { StyleSheet, StyleProp, ViewStyle, View, FlexAlignType, ViewProps } from 'react-native'
type HStackProps = ViewProps & {
align?: FlexAlignType
style?: StyleProp<ViewStyle>
}
const HStack: React.FC<HStackProps> = ({ align = 'stretch', style, children, ...delegated }) => {
return (
<View style={[styles.hStack, { alignItems: align }, style]} {...delegated}>
{children}
</View>
)
}
const styles = StyleSheet.create({
hStack: {
flexDirection: 'row'
}
})
export default HStack
import React from 'react'
import { StyleSheet, View, ViewProps } from 'react-native'
type SpacerProps = ViewProps & {
x?: number
y?: number
}
const Spacer: React.FC<SpacerProps> = ({ x, y, style, ...delegated }) => {
const width = x || 1
const height = y || 1
return (
<View
style={[
{
width,
minWidth: width,
height,
minHeight: height
},
styles.spacer,
style
]}
{...delegated}
/>
)
}
const styles = StyleSheet.create({
spacer: {}
})
export default Spacer
import React from 'react'
import { StyleSheet, StyleProp, ViewStyle, View, FlexAlignType, ViewProps } from 'react-native'
type VStackProps = ViewProps & {
align?: FlexAlignType
style?: StyleProp<ViewStyle>
}
const VStack: React.FC<VStackProps> = ({ align = 'stretch', style, children, ...delegated }) => {
return (
<View style={[styles.vStack, { alignItems: align }, style]} {...delegated}>
{children}
</View>
)
}
const styles = StyleSheet.create({
vStack: {
flexDirection: 'column'
}
})
export default VStack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment