Skip to content

Animate the expanding/collapsing of sub-items in navigation #10

@hrishikesh-k

Description

@hrishikesh-k

Hello.

I've been trying to animate the opening/closing of sub-items in the navigation. I've got successful in animating the expand, but, I don't know how I should animate the collapse.

Here's what I did to animate the expand:

  1. In my src\gatsby-theme-document\components\LeftSidebar\NavItem.js, I wrapped the sub-items inside a <div> with overflow-y: hidden like this:
<div style = {{overflowY: "hidden"}}>
  {
    hasChildren && !isCollapsed && (
      <NavItemChild>
        {
          item.items.map(child => (
            <StyledNavItem key = {child.url}>
              <NavItemLink to = {child.url} activeClassName = "is-active">
                {child.title}
              </NavItemLink>
            </StyledNavItem>))
        }
      </NavItemChild>)
  }
</div>
  1. In the same file, I added the following styles to NavItemChild (the const in the end of the file):
animation-name: animExpand;
animation-duration: 0.25s;
animation-timing-function: var(--ease-in-out-quad);
position: relative
  1. Lastly, in src\gatsby-theme-document\styles\global.js, I added the animation:
@keyframes animExpand
  {
    0%
      {
        top: -100px;
      }
    100%
      {
        top: 0px
      }
  }

So, yeah, this works perfectly while expanding, but, the collapsing is still instant. How can I animate that too?

Here's the demo of what I've done:

Demo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions