conditional rendering in styled components

后端 未结 5 2211
失恋的感觉
失恋的感觉 2020-12-07 14:19

How can I use conditional rendering in styled-components to set my button class to active using styled-components in React?

In css I would do it similarly to this:<

5条回答
  •  悲&欢浪女
    2020-12-07 15:04

    Here is an simple example with TypeScript:

    import * as React from 'react';
    import { FunctionComponent } from 'react';
    import styled, { css } from 'styled-components';
    
    interface IProps {
      isProcessing?: boolean;
      isDisabled?: boolean;
      onClick?: () => void;
    }
    
    const StyledButton = styled.button`
      width: 10rem;
      height: 4rem;
      cursor: pointer;
      color: rgb(255, 255, 255);
      background-color: rgb(0, 0, 0);
    
      &:hover {
        background-color: rgba(0, 0, 0, 0.75);
      }
    
      ${({ disabled }) =>
        disabled &&
        css`
          opacity: 0.5;
          cursor: not-allowed;
        `}
    
      ${({ isProcessing }) =>
        isProcessing &&
        css`
          opacity: 0.5;
          cursor: progress;
        `}
    `;
    
    export const Button: FunctionComponent = ({
      children,
      onClick,
      isProcessing,
    }) => {
      return (
        
          {!isProcessing ? children : }
        
      );
    };
    
    
    

提交回复
热议问题