简体   繁体   English

将方向道具传递给 styled-components 关键帧组件?

[英]Passing a direction prop into styled-components keyframe component?

I'm trying to implement a reusable text animation component where a direction prop can be passed in. I probably close but doesn't seem to be working.我正在尝试实现一个可重用的文本动画组件,可以在其中传递方向道具。我可能关闭但似乎没有工作。 Also open to better ways of implementing it a better way.也愿意以更好的方式实施它。

import React from "react"
import styled from "styled-components"
import { GlobalStyles } from "./global"
import TextAnimations from "./Components/TextAnimations"

const Container = styled.div`
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
`
const NavBar = styled.nav`
  background: #3a3a55;
  padding: 0.25rem;
  width: 100%;
  height: 10vh;
`
const Main = styled.main`
  background: #3a3a55;
  color: white;
  padding: 0.25rem;
  flex: 10 1 auto;
  height: 100vh;
`

const Break = styled.div`
  flex-basis: 100%;
  width: 0;
`

function App() {
  return (
    <>
      <GlobalStyles />
      <Container>
        <NavBar>NavBar</NavBar>
        <Break />
        <Main>
          <TextAnimations text='Sample text from the left' direction='left' />
          <TextAnimations text='Sample text from the right' direction='right' />
        </Main>
      </Container>
    </>
  )
}

export default App

and then the animation component:然后是动画组件:

import { motion } from "framer-motion"
import styled, { keyframes } from "styled-components"

type TextAnimationProps = {
  text: string
  direction: string
}

const Left = keyframes`
  0% { left: -3.125em; }
  100% { left: 3em;}
`

const Right = keyframes`
  0% { Right: -3.125em; }
  100% { Right: 3em;}
`

const HomeHeader = styled.div`
  h1 {
    font-weight: lighter;
  }
  position: relative;
  top: 0;
  animation: ${(props) => (props.defaultValue === "left" ? Left : Right)} // is this right?
  animation-duration: 3s;
  animation-fill-mode: forwards;
`

const TextAnimations = ({ text, direction }: TextAnimationProps) => {
  return (
    <HomeHeader defaultValue={direction}>
      <h1>{text}</h1>
    </HomeHeader>
  )
}

export default TextAnimations

I'd also like to make it more flexible so that I can add 'top', 'bottom', etc.我还想让它更灵活,以便我可以添加“顶部”、“底部”等。

Is this the best way to handle text animations?这是处理文本动画的最佳方式吗?

You can create a separate function to set the animation.您可以创建一个单独的函数来设置动画。 The function will receive the props of the styled component from which the function will access the direction prop.该函数将接收样式组件的道具,该函数将从该组件访问方向道具。

const setHomeHeaderAnimation = ({ direction = "left" }) => {
  const animation = keyframes`
    0% {
      ${direction}: -3.125em; 
    }
    100% { 
      ${direction}: 3em;
    }
  `;
    
  return css`
    position: relative;
    animation: ${animation};
    animation-duration: 3s;
    animation-fill-mode: forwards;
  `;
};

const HomeHeader = styled.div`
  ${setHomeHeaderAnimation}

  h1 {
    font-weight: lighter;
  }
`;

const App = () => (
  <div>
    <HomeHeader>
      <div>Some text</div>
    </HomeHeader>
    <HomeHeader direction="right">
      <div>Some text</div>
    </HomeHeader>
    <HomeHeader direction="top">
      <div>Some text</div>
    </HomeHeader>
    <HomeHeader direction="bottom">
      <div>Some text</div>
    </HomeHeader>
  </div>
);

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

相关问题 在关键帧动画中使用道具的样式化组件 - Styled-components using a prop inside of a keyframe animation 将prop传递给样式组件的TypeScript错误 - TypeScript error passing prop to styled-components Typescript React/styled-components - 将 function 作为组件传递给组件会在缺少类型时引发错误 - Typescript React/ styled-components - passing function as prop to component throws error on missing type Typescript React/Nextjs styled-components - 将函数作为道具传递给组件会在缺少类型时引发错误 - Typescript React/Nextjs styled-components - passing function as prop to component throws error on missing type styled-components:使用额外的样式扩展现有组件 - styled-components: Extend existing component with additional prop for styling Prop TypeScript React 组件错误,使用样式化组件 - Prop TypeScript Error With React Component, Using Styled-Components 在 Emotion 中使用 `css` 属性覆盖样式组件 - Overriding styled-components with `css` prop in Emotion styled-components — 如何在道具上使用:not() 选择器? - styled-components — how to use :not() selector on a prop? 将背景作为道具传递给样式组件 - Pass background as prop in styled-components 使用带有打字稿的样式组件的“as”多态属性 - Using 'as' polymorphic prop of styled-components with typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM