简体   繁体   English

如何将 animation 添加到 React 中的可折叠组件?

[英]How to add animation to a collapsible component in React?

I have a collapsible component in my index.js file which is functional.我的 index.js 文件中有一个可折叠的组件,它可以正常工作。 The problem is that I am not able to make it animated so that it would collapse smoothly like in these examples of the link below.问题是我无法使其动画化,因此它会像下面链接的这些示例中那样平滑地折叠。 This is my index.js file:这是我的 index.js 文件:

import { FaArrowDown } from "react-icons/fa";
import { CollapsibleContainer, ArrowContainer } from "./CollapsibleElements";

const Collapsible = (props) => {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <CollapsibleContainer>
      <div className="collapsible">
        <ArrowContainer>
          <FaArrowDown onClick={() => setIsOpen(!isOpen)} style={{fontSize: '1.5rem', marginBottom: 15}} />
        </ArrowContainer>
        {isOpen && (
          <div className={isOpen ? "content show" : "content"}>
            <p>Content inside here</p>
          </div>
        )}
      </div>
    </CollapsibleContainer>
  );
};

export default Collapsible;

Possible help would be really appreciated.可能的帮助将不胜感激。 Examples: https://react-bootstrap.github.io/utilities/transitions/示例: https://react-bootstrap.github.io/utilities/transitions/

You can achieve that effect using css or sass with transition property.您可以使用具有过渡属性的 css 或 sass 来实现该效果。

for example having this component:例如有这个组件:

 export const SidebarOptions = ({ isExpanded = false, iconSize = '16px', color = 'gray', children }) => { const [expanded, setExpanded] = useState(isExpanded); const iconStyles = { fontSize: iconSize, color }; const expandedClassName = expanded? '--expanded': '' const iconDirection = expanded? <DoubleRightOutlined style={iconStyles} />: <DoubleLeftOutlined style={iconStyles} />; return ( <div className={`sidebar-options${expandedClassName}`}> <div className="icon" onClick={() => setExpanded(!expanded)}> {iconDirection} </div> <div className={`sidebar-options__content${expandedClassName}`}> {expanded && children} </div> </div > ) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
When click at icon, an --expanded will be added to clasName so now with sass we can do something like this: 当点击图标时,一个 --expanded 将被添加到 clasName 所以现在我们可以使用 sass 做这样的事情:

 .sidebar-options { width: 60px; height: 100%; transition: width 0.3s ease-in; display: flex; flex-direction: column; align-items: center; border-left: 1px solid var(--dark-border); background-color: $background-light-2; &--expanded { width: 35%; display: flex; flex-direction: column; align-items: center; transition: width 0.3s ease-out; background-color: $background-light-2; border-left: 1px solid var(--dark-border); } &__content { width: 100%; visibility: hidden; opacity: 0; transition: visibility 0.2s, opacity 0.5s linear; &--expanded { visibility: visible; opacity: 1; transition: visibility 0.5s, opacity 0.5s linear; padding: $spacing-l; width: 100%; } } }

if you get a bit confuse with code, just try to play with width and height, and transition property如果您对代码有点困惑,只需尝试使用宽度和高度以及过渡属性

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

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