Multiple evaluations in template literal... How can I improve my padding calculation for styled-components?

Jefferson

I'm trying to use styled components to style a button with some padding as shown below. Is there a cleaner way of returning two values using a single expression so I can clean this up?

export const Button = styled.button`
  padding: ${props => (props.primary ? rhythm(0.65) : rhythm(0.5))} ${props => (props.primary ? rhythm(1.6) : rhythm(1))};
`



Additionally, Prettier formats the previous code as follows. This is not valid css and so does not work. Is this a bug in prettier or is my code just horrible?

Broken:

export const Button = styled.button`
  padding: ${props => (props.primary ? rhythm(0.65) : rhythm(0.5))}
    ${props => (props.primary ? rhythm(1.6) : rhythm(1))};
`
Ori Drori

You can generate both values with one template string. It would be more readable to create a simple function, that generates the string:

const rhythm = v => `${v}em`; // mock

const getPadding = (a, b) => `${rhythm(a)} ${rhythm(b)}`

const Button = styled.button`
  padding: ${props => (props.primary ? getPadding(0.65, 1.6) : getPadding(0.5, 1))};
`

ReactDOM.render(
  <div>
    <Button primary>Primary</Button>
    <Button>Not primary</Button>
  </div>,
  root
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/styled-components/4.4.0/styled-components.js"></script>

<div id="root"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use multiple props in a single callback defined in a template literal in React styled-components

How can I get syntax highlighting for styled components template literals?

How can i do multiple onClick animations in Styled components?

How do I pass a variable/literal for a value in styled components?

How can I improve my button which uses a Html.ActionLink inside a styled div

How can I improve this Javascript calculation tool?

How can I improve this GPA calculation code?

How can I make multiple styling for only one prop in React Styled Components?

How can I style a Select from Material UI with styled components?

With styled-components, how can I target a child class of a component?

How can I change the css property with styled components onClick?

How can I override ExpansionPanelSummary deep elements with styled-components?

How can I use styled-components CDN build on browser?

How can I import local font in Reactjs with Styled Components?

How can I use custom icon in react-styled components?

How can I focus a button on click with styled-components for React?

How can I use animation in styled-components css``?

How can I implement in styled components and interpolations with emotion theming?

How can I set the same padding left in my template for Outlook & Gmail Emails?

How can I set padding left in my template that works both for Outlook & Gmail Emails?

How can I improve the performance of my script?

How can I improve my android layout

How can I improve my junit tests

How can I improve my paw detection?

How can I improve my linux security?

How can I improve my Laravel performance?

How can I improve my script to make it faster when search from multiple sheets?

How can I implement a CSS styled flag into my topbar?

How can I hover my ::after element in styled component