简体   繁体   English

React不是样式样式的样式可重用组件

[英]React style reusable component that is NOT styled-component

I have a reusable component that contains an animation called AnimationLoader. 我有一个可重用的组件,其中包含一个名为AnimationLoader的动画。 This component is a reusable component for loading states. 该组件是用于加载状态的可重用组件。 I'm simply trying to take this component and use it inside of another component, UnpublishBlogBtn as the loader after a button click - that all works fine. 我只是试图将这个组件带到另一个组件UnpublishBlogBtn中,并在单击按钮后将其用作加载器-一切正常。 However, I'd like to change the height and width of the animation inside of UnpublishBlogBtn and cannot for the life of me get that to work properly. 但是,我想更改UnpublishBlogBtn内部动画的高度和宽度,并且无法一生都能够正常工作。

I've tried implementing Object.assign to bring in the CSS from the other file and just change the height and width. 我尝试实现Object.assign来从另一个文件引入CSS并只是更改高度和宽度。 I've also tried just changing the CSS by doing <style={{}}> as well as wrapping the component in a div that has a style added(this appears to just change the button and not the animation itself). 我还尝试过通过执行<style={{}}>来更改CSS,以及将组件包装在已添加样式的div中(这似乎只是更改按钮,而不是动画本身)。

<Button type="main" className="blogBtn">
   {currentlyUnpublishingBlog ? <AnimatedLoader css={{ height: 1, width: 1 }}/> : 
   'Unpublish Blog'}
</Button>
const AnimatedLoader = () => {
  return (
    <div
      css={{
        height: 45,
        width: 45
      }}
    >
      <AnimatedIcon 
        css={{
          animationDelay: '0.7s',
          height: 35,
          left: 10,
          position: 'absolute',
          top: 10,
          width: 35
        }}
      />
      <AnimatedIcon 
        css={{
          animationDelay: '0.7s'
          display: 'none',
          height: 45,
          left: 0,
          top: 0,
          width: 45,
        }}
      />
      <div
        css={{
          borderRadius: 20,
          borderStyle: 'solid',
          borderWidth: 3,
          height: 45,
          position: 'absolute',
          width: 45,
        }}
      />
    </div>
  )
};

export default AnimatedLoader;

After user clicks on Unpublish Blog button, the loader should display as a smaller width and height on top of the button. 用户单击“取消发布博客”按钮后,加载程序应在按钮顶部显示为较小的宽度和高度。 Currently, it maintains the same size as what is listed inside of AnimatedLoader component and I'd like the size to change inside of the UnpublishBlogBtn component. 目前,它的大小与AnimatedLoader组件中列出的大小相同,我希望在UnpublishBlogBtn组件中更改大小。

You can pass your css in as a prop to your AnimatedLoader 您可以将css作为道具传递给AnimatedLoader

 const AnimatedLoader = ({css={
        height: 45,
        width: 45
      }}) => {
  return (
    <div
      {...css}
    >
      <AnimatedIcon 
        css={{
          animationDelay: '0.7s',
          height: 35,
          left: 10,
          position: 'absolute',
          top: 10,
          width: 35
        }}
     // ....

If you need to do more complex things, it's probably more sensible to pass in a prop that describes the different style options as a group. 如果您需要做更复杂的事情,则最好传入一个将不同样式选项作为一组描述的道具。 So isSmallSize as a boolean or different sizes as an enum etc. 所以isSmallSize为布尔值或不同的大小为枚举等。

Then in your component you adjust the styles depending on the prop. 然后,在组件中根据道具调整样式。

const AnimatedLoader = ({ isSmallSize = false }) => {
    const outerSize = isSmallSize ? 
                       { height: 45, width: 45 } : { height: 1, width: 1 }
    const iconSize = isSmallSize ? 
                       { height: 35, width: 35 } : { height: 1, width: 1 }
    return (
       <div css={{ ...outerSize }}>
         <AnimatedIcon
           css={{
             animationDelay: '0.7s',
             left: 10,
             position: 'absolute',
             top: 10,
             ...iconSize
           }}
         />
         <AnimatedIcon
           css={{
             animationDelay: '0.7s',
             display: 'none',
             left: 0,
             top: 0,
             ...iconSize
           }}
         />
         <div
           css={{
             borderRadius: 20,
             borderStyle: 'solid',
             borderWidth: 3,
             position: 'absolute',
             ...iconSize
           }}
      />
    </div>
  )
}

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

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