繁体   English   中英

样式化组件按钮扩展

[英]Styled Component Button Extension

所以我的目标是拥有一个基本按钮组件,然后是一个与基本按钮具有相同标记但显然具有不同样式和动画的变体按钮。

我的基本文件是 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;

到现在为止还挺好。 我的 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;

让我困惑的是底部。 似乎是重复......我如何确保它生成与 Button 相同的标记? 我希望我的动画按钮扩展标记和 css。 最终,有没有办法以这种方式调用我的按钮

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

当您使用styled(Button)扩展Button时,您实际上是在创建它的一个更具体的版本,并计划使用新的特定版本。

但是当您想将按钮用作:

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

这是一个将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>
  );
}

如果您有很多这样的变体,那么为这些创建样式可能会令人痛苦。 这就是为什么styled-components有一个辅助库styled-theming可以提供帮助。 (这对animated部分没有多大帮助,因为这几乎是添加代码而不是更改。

暂无
暂无

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

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