Animating material icons to transform: Hamburger to X

Viewed 4268

Long time reader, first-time poster. I want to have a material hamburger transform to an "X" when clicked and then transform back when clicked again in an Angular application. The website https://material.io/design/iconography/animated-icons.html#transitions shows there are animations just like what I want, but there are no docs on how to do it.

I tried a CSS approach but it is not showing and it would be good to keep the project consistent with the material icons.

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  <span class="sr-only">Toggle navigation</span>
  <span class="icon-bar top-bar"></span>
  <span class="icon-bar middle-bar"></span>
  <span class="icon-bar bottom-bar"></span>
</button>

CSS

navbar-toggle {
  border: none;
  background: transparent !important;

  &:hover {
    background: transparent !important;
  }

  .icon-bar {
    width: 22px;
    transition: all 0.2s;
  }
  .top-bar {
    transform: rotate(45deg);
    transform-origin: 10% 10%;
  }
  .middle-bar {
    opacity: 0;
  }
  .bottom-bar {
    transform: rotate(-45deg);
    transform-origin: 10% 90%;
  }
}

.navbar-toggle.collapsed {
  .top-bar {
    transform: rotate(0);
  }
  .middle-bar {
    opacity: 1;
  }
  .bottom-bar {
    transform: rotate(0);
  }
}

The above code is a workaround to the material icons. When I run the above code, I get a transparent button with no animations. If anyone knows how to do a transition from a hamburger to an "X" in angular with bootstrap, I would be very grateful.

2 Answers

I made this simple stackblitz using just angular animations (no bootstrap) that still needs some work in the CSS of the states of the animation to make it nice and smooth but you can get the idea.

You can use hambugers library.
I'm using Angular material and this is is the closest I got to the original material menu icon.
stackblitz

Related