Skip to content

Instantly share code, notes, and snippets.

@RafalFilipek
Created September 4, 2017 20:03
Show Gist options
  • Save RafalFilipek/f2a02e95b4c6501ae72509adf09036db to your computer and use it in GitHub Desktop.
Save RafalFilipek/f2a02e95b4c6501ae72509adf09036db to your computer and use it in GitHub Desktop.
import { css } from 'emotion';
import styled from 'react-emotion';
const box = props => css`
composes:
${display(props)}
${flex(props)}
${direction(props)}
${alignItems(props)}
${alignSelf(props)}
${justifyContent(props)}
${alignContent(props)}
${margin(props)}
${padding(props)};`;
export const Box = ({ as, ...props }) => {
const Component = styled(as)`${box};`;
return <Component {...props} />;
};
@tkh44
Copy link

tkh44 commented Sep 4, 2017

I would not create a new styled component on every render. I would do something like this.

const box = props => css`
  ${display(props)}
  ${flex(props)}
  ${direction(props)}
  ${alignItems(props)}
  ${alignSelf(props)}
  ${justifyContent(props)}
  ${alignContent(props)}
  ${margin(props)}
  ${padding(props)};`;
 
const Box = {}
Box.Div = styled('div')`${box}`
Box.A = styled('a')`${box}`
// .. and whatever other elements you need.
//... you could write a loop to do this for you.

export default Box

@tkh44
Copy link

tkh44 commented Sep 4, 2017

const box = props => css`
  ${display(props)}
  ${flex(props)}
  ${direction(props)}
  ${alignItems(props)}
  ${alignSelf(props)}
  ${justifyContent(props)}
  ${alignContent(props)}
  ${margin(props)}
  ${padding(props)};`;

const createBox = (tag) => styled(tag)`${box}`

const A = createBox('a')

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