Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Complex react inline styles such as hover, active on react components such as button

I have the following styles in css for my buttons. I am also using bootstrap.

.btn-primary {     background-color: #229ac8;     background-image: linear-gradient(to bottom, #23a1d1, #1f90bb);     background-repeat: repeat-x;     border-color: #1f90bb #1f90bb #145e7a;     color: #ffffff;     text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); } .btn-primary:hover, .btn-primary:active, .btn-primary.active, .btn-primary.disabled, .btn-primary[disabled] {     background-color: #1f90bb;     background-position: 0 -15px; } 

I have defined a button as a component in react.

const MyButton = ({children, onClick, classNames, ...rest }) => (     <button         onClick   = {onClick}         className = {`${classNames}`}         {...rest}     >         {children}     </button> ); 

Based on some value fetched from the server I want to change the complete color of the button.

Question 1:

How can I set the button's complete style as inline style?

Question 2:

Also, can I use some scss features like mixins in react to generate button styles dynamically passing color as variable ?

Question 3:

Should I use inline styles or classnames using css in js?

For a generic component such as a button should we use a global class which can be reused in all places where button is defined or use a local inline style and create inline styles in all places?

like image 757
WitVault Avatar asked Aug 01 '16 12:08

WitVault


People also ask

How do you add a hover style in React?

Set the onMouseEnter and onMouseLeave props on the element. When the user hovers over or out of the element, update a state variable. Conditionally set inline styles on the element.

How do I change inline style in React?

Setting Inline Styles in a React ComponentSpecify the style object inside the constructor as follows. Notice here that the text-align CSS property got camel-cased to textAlign . Pass this styles object to the <H1 /> component, as shown below. Combine multiple style objects using the spread operator.

How do you add multiple styles in React JS?

Use the spread syntax to combine multiple inline style objects in React, e.g. style={{...style1, ... style2}} . The spread syntax will unpack the key-value pairs of the objects into a final object and the styles will get applied to the element.

Should you use inline styles in React?

The inline styling concept might not help you to build the best React components in your app. If you're planning to build a very performant, scalable, and rich application inline styling is not the right option for you.


2 Answers

CSS in JS (with pseudo classes & MQ support)

There are lots of libs to write CSS with React that supports pseudo classes but all, if not all of them requires you to inline or write your CSS in JS Which I highly recommend

CSS Modules (Write your CSS as normal, but in a better way)

There is also CSS modules which if you are already using Webpack should be simple to set it up, which let you import/require your CSS as an object use it your component like so:

import styles from './Button.css'  const MyButton = ({children, onClick, type='primary', ...rest }) => (     <button         onClick   = {onClick}         className = {styles.primary}         {...rest}     >         {children}     </button> ); 

Decoupling your components

I'd also add that you shouldn't pass classes to the <Button /> and deal with the conditions inside the component itself. For example using classnames lib you can do something like the following.

import classnames from 'classnames'  const MyButton = ({children, onClick, type='primary', ...rest }) => (     <button         onClick   = {onClick}         {/*            depends on the type prop, you will get the relevent button            so no need for consumers to care about the internals of the component         */}         className = {classnames('.btn', { [`btn-${type}`]: true })}         {...rest}     >         {children}     </button> ); 

You can even combine CSS Modules & classnames lib to create some powerful combinations.

like image 85
ahmedelgabri Avatar answered Sep 28 '22 18:09

ahmedelgabri


Personally, I would use global CSS and wire it up with Webpack. It will keep your React much cleaner and of course more modular and easily edited.

To the best of my knowledge, SCSS features cannot be used inline with your React.

If you need to set inline styles in React it's done like this;

var buttonStyle = {     backgroundColor: "#229ac8",     backgroundImage: "linear-gradient(to bottom, #23a1d1, #1f90bb)",     backgroundRepeat: "repeat-x",     borderColor: "#1f90bb #1f90bb #145e7a",     color: "#ffffff",     textShadow: "0 -1px 0 rgba(0, 0, 0, 0.25)" } 
<button style={buttonStyle}>Button</button> 
like image 32
David Meents Avatar answered Sep 28 '22 17:09

David Meents