adopted-ember-addons / ember-burger-menu

An off-canvas sidebar component with a collection of animations and styles using CSS transitions
https://adopted-ember-addons.github.io/ember-burger-menu/
MIT License
279 stars 47 forks source link
animations burger-menu ember ember-addon menu menu-animation

ember-burger-menu

CI npm version

An off-canvas sidebar component with a collection of animations and styles using CSS transitions

Features

Compatibility

Installation

ember install ember-burger-menu

Sass

Installing ember-burger-menu should also install ember-cli-sass and automatically create a scss file under app/styles/app.scss with

// app/styles/app.scss

@import 'ember-burger-menu';

Overriding Variables

Using sass, you can override default variables and easily change the default behavior of ember-burger-menu. See variables.scss for a list of variables you can change.

// app/styles/app.scss

// Burger Menu Overrides
$bm-transition-duration: 0.3s;
$bm-overlay-background: rgba(0, 0, 0, 0.7);

// Import all the styles!
@import 'ember-burger-menu';

Import Only What You Need

Using sass, you can import only the styles you need for the animations you use.

// Core Styles
@import 'ember-burger-menu/variables';
@import 'ember-burger-menu/structure';

// Animations
@import 'ember-burger-menu/animations/push';
@import 'ember-burger-menu/animations/menu-item/stack';

Helpful Links

Looking for help?

If it is a bug please open an issue on GitHub.

Animations

Menu Animations

Menu Item Animations

Usage

This addon utilizes contextual components to be able to correctly control and animate necessary elements.

{{#burger-menu as |burger|}}
  {{#burger.menu itemTagName="li" as |menu|}}
    <button {{on "click" burger.state.closeMenu}}>Close</button>

    <ul>
      {{#menu.item}}
        <LinkTo @route="features">
          Features
        </LinkTo>
      {{/menu.item}}

      {{#menu.item}}
        <LinkTo @route="about">
          About
        </LinkTo>
      {{/menu.item}}

      {{#menu.item}}
        <LinkTo @route="contact">
          Contact Us
        </LinkTo>
      {{/menu.item}}
    </ul>
  {{/burger.menu}}

  {{#burger.outlet}}
    <button {{on "click" burger.state.toggleMenu}}>Menu</button>
    {{outlet}}
  {{/burger.outlet}}
{{/burger-menu}}

Components

{{burger-menu}}

Options

{{burger.outlet}}

This component is where all your content should be placed.

{{burger.menu}}

Everything rendered here will be inside the menu.

Options

Actions

{{menu.item}}

The individual menu item. This is required if you have specified an itemAnimation.

Options

The Menu State

The {{burger-menu}} component exposes multiple contextual components, but it also exposes a state object.

You can use the menu state object to modify pretty much any property.

The state object also exposes some actions:

Custom Animations

If you're not impressed with the in-house animations and want to create your own, all you have to do is pass the following class to the customAnimation property in the {{burger-menu}} component. If you think your animation would be a good addition to the existing collection, feel free to open a PR with it!

import Animation from 'ember-burger-menu/animations/base';

export default Animation.extend({
  // CSS class names to be able to target our menu
  animation: 'my-custom-animation',
  itemAnimation: 'my-custom-item-animation',

  container(open, width, right) {
    return {};
  },

  outlet(open, width, right) {
    return {
      transform: open ? right ? `translate3d(-${width}px, 0, 0)` : `translate3d(${width}px, 0, 0)` : ''
    };
  },

  menu(open, width, right) {
    return {};
  },

  menuItem(open, width, right, index) {
    return {
      transform: open ? '' : `translate3d(0, ${(index + 1) * 500}px, 0)`
    };
  }
});

Note: You don't need to worry about prefixing your CSS attributes as it will be done for you.

If you need to add some base CSS to your animation, you can target the menu as such:

.ember-burger-menu.bm--my-custom-animation {
  #{$bm-menu} {}
  > .bm-outlet {}

  &.is-open {
    #{$bm-menu} {}
    > .bm-outlet {}
  }
}

And the menu items as such:

.ember-burger-menu.bm-item--my-custom-item-animation {
  #{$bm-menu} {
    .bm-menu-item {}
  }

  &.is-open {
    #{$bm-menu} {
      .bm-menu-item {}
    }
  }
}

To use our new custom animation, all we have to do is pass the class to the customAnimation option in the {{burger-menu}} component.

import MyCustomAnimation from 'path/to/my-custom-animation';

export default Ember.Component.extend({
  MyCustomAnimation
});
{{#burger-menu customAnimation=MyCustomAnimation}}
  ...
{{/burger-menu}}

Contributing

See the Contributing guide for details.