简体   繁体   English

样式化组件按钮扩展

[英]Styled Component Button Extension

So my aim is to have a base button component and then a variant button which has the same markup as the base button but obviously has different styling, animations.所以我的目标是拥有一个基本按钮组件,然后是一个与基本按钮具有相同标记但显然具有不同样式和动画的变体按钮。

My base file is button.js我的基本文件是 button.js

 import React from 'react';
import styled,{ keyframes, ThemeProvider} from 'styled-components';
import theme from '../../theme/default';

// import {rotatecw, rotateccw} from '../../../theme/keyframes';


const ButtonWrapper = styled.button`
  position: relative;
  color: ${(props) => props.theme.colors.primary};
  width: 256px;
  height: 64px;
  line-height: 64px;
  background: none;
  border: 1px solid ${(props) => props.theme.colors.primary};
    &:hover {
      cursor: pointer;
      color: ${(props) => props.theme.colors.grey};
      border: 1px solid ${(props) => props.theme.colors.grey};
    }
  }

`;

const ButtonText = styled.span`
  // transition: all 0.1s;
  // tranform: scale(1, 1);
`;


function Button(props) {
  return (
    <ThemeProvider theme={theme}>
      <ButtonWrapper>
        <ButtonText>  
          {props.text}
        </ButtonText>
      </ButtonWrapper>
    </ThemeProvider>
  );
}

export default Button;

So far so good.到现在为止还挺好。 My AnimatedButton file is like that我的 AnimatedButton 文件就是这样

    import React from 'react';
import Styled, { keyframes, ThemeProvider} from 'styled-components';
import theme from '../../theme/default';
import Button from '../../components/button/button'

// import {rotatecw, rotateccw} from '../../../theme/keyframes';

const rotatecw = keyframes`
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
`

const rotateccw = keyframes`
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(-360deg);
    }
`



const AnimatedButtonWrapper = Styled(Button)` 
transition: all 0.3s;
  &:before,
  &:after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    bottom: 0;
    left: 0;
    z-index: 1;
    transition: all 0.3s;
    border: 1px solid ${(props) => props.theme.colors.primary};
  }
  &:hover {
    cursor: pointer;
    &:after {
      animation-name: ${rotatecw};
      animation-duration: 2s;
    }
    &:before {
      animation-name: ${rotateccw}; 
      animation-duration: 3s;
    }
    &:before,
    &:after {
      left: 96px;
      width: 64px;
      animation-iteration-count: infinite;
      animation-timing-function: linear;
    }
`;

function AnimatedButton(props) {
  return (
    <ThemeProvider theme={theme}>
      <AnimatedButtonWrapper>



      </AnimatedButtonWrapper>
    </ThemeProvider>
  );
}

export default AnimatedButton;

What confused me is the bottom part.让我困惑的是底部。 seems like a repeat ... How do I ensure it generates the same markup as Button ?似乎是重复......我如何确保它生成与 Button 相同的标记? I want my animated button to extend the markup and the css.我希望我的动画按钮扩展标记和 css。 Eventually, is there a way to call my button this way最终,有没有办法以这种方式调用我的按钮

<Button animatedButton text="test"></Button>

When you extend a Button with styled(Button) , you are essentially creating a more specific version of it and is planning to use the new specific one instead.当您使用styled(Button)扩展Button时,您实际上是在创建它的一个更具体的版本,并计划使用新的特定版本。

But when you want to use the button as:但是当您想将按钮用作:

<Button animatedButton text="test"></Button>

which is a variant passing in animatedButton as a prop, you are looking to incorporate these changes in the ButtonComponent itself.这是一个将animatedButton按钮作为道具传递的变体,您希望将这些更改合并到ButtonComponent本身中。

const ButtonWrapper = styled.button`
  All the normal button stuff

  ${props.animated && `
    All the animation stuff goes here
  `}
`
function Button(props) {
  return (
    <ThemeProvider theme={theme}>
      <ButtonWrapper animated={props.animatedButton}>
        <ButtonText>  
          {props.text}
        </ButtonText>
      </ButtonWrapper>
    </ThemeProvider>
  );
}

If you have lots of variants as such, creating styles for these like this can be excruciating.如果您有很多这样的变体,那么为这些创建样式可能会令人痛苦。 This is why styled-components has a helper library styled-theming that can help out.这就是为什么styled-components有一个辅助库styled-theming可以提供帮助。 (It isn't going be much help for the animated part since that's pretty much adding code rather than changing. (这对animated部分没有多大帮助,因为这几乎是添加代码而不是更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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