Skip to content

Instantly share code, notes, and snippets.

@rudin
Last active May 4, 2020 13:58
Show Gist options
  • Save rudin/107669713e1684b41c42fa4e1644708e to your computer and use it in GitHub Desktop.
Save rudin/107669713e1684b41c42fa4e1644708e to your computer and use it in GitHub Desktop.
React(Native) Stack Component (Whitespace defined by parent)
import React, { Fragment } from "react"
import { View } from "./"
const Stack = ({ children, gap = 2, ...rest }) => {
const Wrapper = Object.keys(rest).length > 0 ? View : Fragment
const childrenWithProps = React.Children.map(children, (child, index) => {
if (!child) return null
if (index === 0 || index === children.length - 1) {
return child
}
const addProps =
typeof child.props.m !== "undefined" ||
typeof child.props.my !== "undefined"
? null
: {
mt: child.props.mt || gap,
mb: child.props.mb || gap
}
return React.cloneElement(child, addProps)
})
return <Wrapper {...rest}>{childrenWithProps}</Wrapper>
}
export default Stack
import React from 'react'
import { Stack, View } from './components'
export default () => (
<Stack gap={2} px={2}>
<View ... />
<View ... />
</Stack>
)
@rudin
Copy link
Author

rudin commented Jul 1, 2019

View is a styled component with at least ${space} from styled-system:
const View = styled.div'${space}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment