react styled components button that flips over and inverts colors when hovered in typescript

To create a React button component using Styled Components that flips over and inverts colors on hover, you can use the following code:

import React from 'react';
import styled from 'styled-components';

interface ButtonProps { 
  invertColors: boolean;
}

const Button = styled.button<ButtonProps>`
  background-color: ${props => props.invertColors ? 'white' : 'blue'};
  color: ${props => props.invertColors ? 'blue' : 'white'};
  transform: ${props => props.invertColors ? 'rotateY(180deg)' : 'rotateY(0)'};
  transition: all 0.2s ease-in-out;
  
  &:hover {
    background-color: ${props => props.invertColors ? 'blue' : 'white'};
    color: ${props => props.invertColors ? 'white' : 'blue'};
    transform: ${props => props.invertColors ? 'rotateY(0)' : 'rotateY(180deg)'};
  } 
`;

const FlippingButton: React.FC<ButtonProps> = ({ invertColors, children, ...rest }) => (
  <Button invertColors={invertColors} {...rest}>
    {children}
  </Button>
);

export default FlippingButton;
852 chars
28 lines

In this code, we have defined a styled button component that uses the transform CSS property to flip the button over on hover. We have also used the transition property to smoothly animate the flipping animation.

The Button component takes in a ButtonProps interface as a prop, which specifies a boolean invertColors prop that determines whether the button should have inverted colors on hover or not.

We then define a FlippingButton component that uses the Button component and passes in the invertColors prop. This FlippingButton component can be used just like a regular button component, but with the added functionality of flipping over and changing colors on hover.

gistlibby LogSnag