简体   繁体   中英

adding two classes (active class) using styled-components

I am migrating from css to styled-components .

My react component looks like the following:

class Example extends React.Component {

  ........code here

  render() {
    return (
      <div 
        className={ this.state.active ? "button active" : "button" }
        onClick={ this.pressNumber }
       >
          <Number>{ this.props.number }</Number>
      </div>
    )
  }
}

const Number = styled.div`
  color: #fff;
  font-size: 26px;
  font-weight: 300;
`;

and my css looks like this:

.button {
  height: 60px;
  width: 60px;
}

.active {
  animation-duration: 0.5s;
  animation-name: highlightButton;
}


@keyframes highlightButton {
  0%   {
    background-color: transparent;
  }
  50%  {
    background-color: #fff;
  }
  100%  {
    background-color: transparent;
  }
}

Does anyone know how I can add the active class/add two classes to an element using styled-components ? Nothing is jumping out at me from the docs.

If anything is unclear or further information is required please let me know.

The template literals in styled components can access props:

const Button = styled.button`
  height: 60px;
  width: 60px;
  animation: ${
      props => props.active ?
          `${activeAnim} 0.5s linear` :
          "none"
  };
`;

 ...
 <Button active={this.state.active} />
 ...

Reference here

And to add the keyframes animation, you'll need to use the keyframes import:

import { keyframes } from "styled-components";

const activeAnim = keyframes`
    0%   {
        background-color: transparent;
    }
    50%  {
        background-color: #fff;
    }
    100%  {
        background-color: transparent;
    }
`;

Reference here

You can extend the style to overwrite certain properties and keep the others untouched:

render() {
    // The main Button styles
    const Button = styled.button`
        height: 60px;
        width: 60px;
    `;

    // We're extending Button with some extra styles
    const ActiveButton = styled(Button)`
        animation-duration: 0.5s;
        animation-name: highlightButton;
    `;

    const MyButton = this.state.active ? ActiveButton : Button;
    return (
        <MyButton onClick={this.pressNumber}>
            <Number>{ this.props.number }</Number>
        </MyButton>
    )
}

You need to pass additional className from props:

Documentation for Styling Normal React Components

const StyledDiv = sytled.div`
  &.active {
    border: blue;
  }
`

class Example extends React.Component {
  constructor(props) {
    super(props)
  }

  render() {
    const isActive = true; // you can dynamically switch isActive between true and false
    return (
      <StyledDiv 
        className={`button ${isActive ? 'active': ''} ${this.props.className}`}
        onClick={ this.pressNumber }
       >
          <Number>{ this.props.number }</Number>
      </StyledDiv>
    )
  }
}

const Number = styled.div`
  color: #fff;
  font-size: 26px;
  font-weight: 300;
`;

Good Luck...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM