Inline CSS styles in React: how to implement a:hover?

前端 未结 18 833
星月不相逢
星月不相逢 2020-11-28 21:51

I quite like the inline CSS pattern in React and decided to use it.

However, you can\'t use the :hover and similar selectors. So what\'s the best way to

相关标签:
18条回答
  • 2020-11-28 22:03

    Here's my solution using React Hooks. It combines the spread operator and the ternary operator.

    style.js

    export default {
      normal:{
        background: 'purple',
        color: '#ffffff'
      },
      hover: {
        background: 'red'
      }
    }
    

    Button.js

    import React, {useState} from 'react';
    import style from './style.js'
    
    function Button(){
    
      const [hover, setHover] = useState(false);
    
      return(
        <button
          onMouseEnter={()=>{
            setHover(true);
          }}
          onMouseLeave={()=>{
            setHover(false);
          }}
          style={{
            ...style.normal,
            ...(hover ? style.hover : null)
          }}>
    
            MyButtonText
    
        </button>
      )
    }
    
    0 讨论(0)
  • 2020-11-28 22:03

    With a using of the hooks:

    const useFade = () => {
      const [ fade, setFade ] = useState(false);
    
      const onMouseEnter = () => {
        setFade(true);
      };
    
      const onMouseLeave = () => {
        setFade(false);
      };
    
      const fadeStyle = !fade ? {
        opacity: 1, transition: 'all .2s ease-in-out',
      } : {
        opacity: .5, transition: 'all .2s ease-in-out',
      };
    
      return { fadeStyle, onMouseEnter, onMouseLeave };
    };
    
    const ListItem = ({ style }) => {
      const { fadeStyle, ...fadeProps } = useFade();
    
      return (
        <Paper
          style={{...fadeStyle, ...style}}
          {...fadeProps}
        >
          {...}
        </Paper>
      );
    };
    
    0 讨论(0)
  • 2020-11-28 22:04

    The simple way is using ternary operator

    var Link = React.createClass({
      getInitialState: function(){
        return {hover: false}
      },
      toggleHover: function(){
        this.setState({hover: !this.state.hover})
      },
      render: function() {
        var linkStyle;
        if (this.state.hover) {
          linkStyle = {backgroundColor: 'red'}
        } else {
          linkStyle = {backgroundColor: 'blue'}
        }
        return(
          <div>
            <a style={this.state.hover ? {"backgroundColor": 'red'}: {"backgroundColor": 'blue'}} onMouseEnter={this.toggleHover} onMouseLeave={this.toggleHover}>Link</a>
          </div>
        )
      }
    
    0 讨论(0)
  • 2020-11-28 22:05

    I think onMouseEnter and onMouseLeave are the ways to go, but I don't see the need for an additional wrapper component. Here is how I implemented it:

    var Link = React.createClass({
      getInitialState: function(){
        return {hover: false}
      },
      toggleHover: function(){
        this.setState({hover: !this.state.hover})
      },
      render: function() {
        var linkStyle;
        if (this.state.hover) {
          linkStyle = {backgroundColor: 'red'}
        } else {
          linkStyle = {backgroundColor: 'blue'}
        }
        return(
          <div>
            <a style={linkStyle} onMouseEnter={this.toggleHover} onMouseLeave={this.toggleHover}>Link</a>
          </div>
        )
    }
    

    You can then use the state of hover (true/false) to change the style of the link.

    0 讨论(0)
  • 2020-11-28 22:06

    Late to party but come with solution. You can use "&" to defines styles for hover nth Child etc:

    day: {
        display: "flex",
        flex: "1",
        justifyContent: "center",
        alignItems: "center",
        width: "50px",
        height: "50px",
        transition: "all 0.2s",
        borderLeft: "solid 1px #cccccc",
    
        "&:hover": {
          background: "#efefef"
        },
        "&:last-child": {
          borderRight: "solid 1px #cccccc"
        }
    },
    
    0 讨论(0)
  • 2020-11-28 22:06

    Made Style It -- in part -- because of this reason (others being disagreements with implementation of other libs / syntax and inline stylings lack of support for prefixing property values). Believe we should be able to simply write CSS in JavaScript and have fully self contained components HTML-CSS-JS. With ES5 / ES6 template strings we now can and it can be pretty too! :)

    npm install style-it --save

    Functional Syntax (JSFIDDLE)

    import React from 'react';
    import Style from 'style-it';
    
    class Intro extends React.Component {
      render() {
        return Style.it(`
          .intro:hover {
            color: red;
          }
        `,
          <p className="intro">CSS-in-JS made simple -- just Style It.</p>
        );
      }
    }
    
    export default Intro;
    

    JSX Syntax (JSFIDDLE)

    import React from 'react';
    import Style from 'style-it';
    
    class Intro extends React.Component {
      render() {
        return (
          <Style>
          {`
            .intro:hover {
              color: red;
            }
          `}
    
            <p className="intro">CSS-in-JS made simple -- just Style It.</p>
          </Style>
        );
      }
    }
    
    export default Intro;
    
    0 讨论(0)
提交回复
热议问题