Skip to content

Instantly share code, notes, and snippets.

@carl0zen
Last active September 2, 2020 10:24
Show Gist options
  • Save carl0zen/bcb7d275546ab344805a032e9d659bbc to your computer and use it in GitHub Desktop.
Save carl0zen/bcb7d275546ab344805a032e9d659bbc to your computer and use it in GitHub Desktop.
// Prop passing Shorthands for Styled-components
export const borderProps = props => css`
${props.borderBottom && `border-bottom: ${props.borderWidth || "1px"} solid ${color.border}`};
${props.borderTop && `border-top: ${props.borderWidth || "1px"} solid ${color.border}`};
${props.borderLeft && `border-left: ${props.borderWidth || "1px"} solid ${color.border}`};
${props.borderRight && `border-right: ${props.borderWidth || "1px"} solid ${color.border}`};
`;
export const marginProps = props => css`
${props.marginBottom && `margin-bottom: ${typeof (props.marginBottom) === "string" ? props.marginBottom : "1em"}`};
${props.marginTop && `margin-top: ${typeof (props.marginTop) === "string" ? props.marginTop : "1em"}`};
${props.marginLeft && `margin-left: ${typeof (props.marginLeft) === "string" ? props.marginLeft : "1em"}`};
${props.marginRight && `margin-right: ${typeof (props.marginRight) === "string" ? props.marginRight : "1em"}`};
${props.margin && `margin: ${typeof (props.margin) === "string" ? props.margin : "1em"}`};
${props.marginVertical && `
margin-top: ${typeof (props.marginVertical) === "string" ? props.marginVertical : "1em"}
margin-bottom: ${typeof (props.marginVertical) === "string" ? props.marginVertical : "1em"}
`};
${props.marginHorizontal && `
margin-left: ${typeof (props.marginHorizontal) === "string" ? props.marginHorizontal : "1em"}
margin-right: ${typeof (props.marginHorizontal) === "string" ? props.marginHorizontal : "1em"}
`};
`;
// An example of how you can use it with your components
const SomeDiv = styled.div`
${borderProps}
${marginProps}
`
// This lets you pass all borderProps to the component like so:
<SomeDiv borderTop borderBottom borderLeft borderRight marginVertical>
@joezimjs
Copy link

Where is the css template function coming from? Also you're only passing in the booleans to the tag at the end (eg borderTop) but the CSS expects a borderWidth prop too... Is there a default for that somewhere.

@vjefri
Copy link

vjefri commented Feb 6, 2017

Same question as Joe. How are you specifying the size of the border. I know there is a default set, but how would u specify a specific amount. Thanks.

@MichaelDM
Copy link

Hey Joezimjs, the css template function is coming from styled-components.
import { css } from 'styled-components';
styled-components/styled-components#390

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