question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I am trying to create button with styling attributes,

This is not very conveniant but it work.

const Button = styled.button`
  /* Adapt the colors based on primary prop */
  color: ${props =props.theme.atoms.Button.default.color};
  background: ${
  (props) =>
    // if primary
    props.primary ? props.theme.atoms.Button.primary.background :
    // else if success
    props.success ? props.theme.atoms.Button.success.background :
    // else if success
    props.info ? props.theme.atoms.Button.info.background :
    // else if warning
    props.warning ? props.theme.atoms.Button.warning.background :
    // else if danger
    props.danger ? props.theme.atoms.Button.danger.background :
    // else default
    props.theme.atoms.Button.default.background
  }
  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;

Also eslint is now complaining:

/workspace/github.com/mxstbr/react-boilerplate/app/components/atoms/Button/index.js
   6:12  error  Expected parentheses around arrow function argument            arrow-parens
   8:3   error  Arrow function used ambiguously with a conditional expression  no-confusing-arrow
  10:5   error  Do not nest ternary expressions                                no-nested-ternary
  12:5   error  Do not nest ternary expressions                                no-nested-ternary
  14:5   error  Do not nest ternary expressions                                no-nested-ternary
  16:5   error  Do not nest ternary expressions    

I guess there must be a better way.

Thanks in advance for informing me

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:21 (18 by maintainers)

github_iconTop GitHub Comments

22reactions
mxstbrcommented, Oct 22, 2016

Why not do something like this:

const Button = styled.button`
  background: ${props => props.theme.atoms.Button[props.nature].background};
  /* More styles here */
`;

Then you can use your different button styles like this:

<Button nature="primary" />
<Button nature="warning" />
// etc.

You might now think “but now people can pass wrong values (or even nothing!) into the style prop”, and you’d be right – except we’re talking about a react component, which can have propTypes and defaultProps! 🎉

Button.propTypes = {
  nature: React.PropTypes.oneOf(['primary', 'success', 'info', 'danger', 'warning'])
}

Button.defaultProps = {
  nature: 'default',
}

Boom, done, exact same behaviour as your really long ternary expression except much neater 😊

Does that answer the question?

*Edited to reflect the correct observation by @kopax that style is a reserved property in react, so I changed it to nature as to not confuse future readers.`

2reactions
kopaxcommented, Oct 26, 2016

Your example to create a styled component involve a syntax like this :

const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;

It mean if you need to add method you will have to write something like this :

Title.prototype.render = function(){
        .....
}

Instead of storing in a ES6 class. it would be nice to write the css inside a class syntax.

class Title extends styled.h1 {

          // <- put the css function somewhere inside this
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript if else else if - W3Schools
Use the else if statement to specify a new condition if the first condition is false. Syntax. if (condition1) { // block of...
Read more >
C if...else Statement - Programiz
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be...
Read more >
if...else - JavaScript - MDN Web Docs - Mozilla
The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the ...
Read more >
C - if...else statement - Tutorialspoint
C - if...else statement, An if statement can be followed by an optional else statement, which executes when the Boolean expression is false....
Read more >
C if else statement - Javatpoint
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found