Last active
May 4, 2020 13:58
-
-
Save rudin/107669713e1684b41c42fa4e1644708e to your computer and use it in GitHub Desktop.
React(Native) Stack Component (Whitespace defined by parent)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { Stack, View } from './components' | |
export default () => ( | |
<Stack gap={2} px={2}> | |
<View ... /> | |
<View ... /> | |
</Stack> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
View is a styled component with at least ${space} from styled-system:
const View = styled.div'${space}'