---
+## Responsive Toggle with animation
+
+To use animations from the Motion UI library, include the <code>data-animation="someAnimationIn someAnimationOut"</code> attribute.
+
+<div class="primary callout show-for-medium">
+ <p>Scale your browser down to see the toggle happen.</p>
+</div>
+
+```html_example
+<div class="title-bar" data-responsive-toggle="example-animated-menu" data-hide-for="medium">
+ <button class="menu-icon" type="button" data-toggle></button>
+ <div class="title-bar-title">Menu</div>
+</div>
+
+<div class="top-bar" id="example-animated-menu" data-animate="hinge-in-from-top spin-out">
+ <div class="top-bar-left">
+ <ul class="dropdown menu" data-dropdown-menu>
+ <li class="menu-text">Site Title</li>
+ <li>
+ <a href="#">One</a>
+ <ul class="menu vertical">
+ <li><a href="#">One</a></li>
+ <li><a href="#">Two</a></li>
+ <li><a href="#">Three</a></li>
+ </ul>
+ </li>
+ <li><a href="#">Two</a></li>
+ <li><a href="#">Three</a></li>
+ </ul>
+ </div>
+</div>
+```
+
+---
+
### Preventing FOUC
Before the JavaScript on your page loads, you'll be able to see both the mobile and desktop element at once for a brief second. This is known as a [flash of unstyled content](https://en.wikipedia.org/wiki/Flash_of_unstyled_content). There's not an easy way for the framework to handle this for you, but you can add some extra CSS to account for it.
!function($) {
-/**
- * ResponsiveToggle module.
- * @module foundation.responsiveToggle
- * @requires foundation.util.mediaQuery
- */
-
-class ResponsiveToggle {
/**
- * Creates a new instance of Tab Bar.
- * @class
- * @fires ResponsiveToggle#init
- * @param {jQuery} element - jQuery object to attach tab bar functionality to.
- * @param {Object} options - Overrides to the default plugin settings.
+ * ResponsiveToggle module.
+ * @module foundation.responsiveToggle
+ * @requires foundation.util.mediaQuery, foundation.util.motion
*/
- constructor(element, options) {
- this.$element = $(element);
- this.options = $.extend({}, ResponsiveToggle.defaults, this.$element.data(), options);
-
- this._init();
- this._events();
-
- Foundation.registerPlugin(this, 'ResponsiveToggle');
- }
- /**
- * Initializes the tab bar by finding the target element, toggling element, and running update().
- * @function
- * @private
- */
- _init() {
- var targetID = this.$element.data('responsive-toggle');
- if (!targetID) {
- console.error('Your tab bar needs an ID of a Menu as the value of data-tab-bar.');
+ class ResponsiveToggle {
+ /**
+ * Creates a new instance of Tab Bar.
+ * @class
+ * @fires ResponsiveToggle#init
+ * @param {jQuery} element - jQuery object to attach tab bar functionality to.
+ * @param {Object} options - Overrides to the default plugin settings.
+ */
+ constructor(element, options) {
+ this.$element = $(element);
+ this.options = $.extend({}, ResponsiveToggle.defaults, this.$element.data(), options);
+
+ this._init();
+ this._events();
+
+ Foundation.registerPlugin(this, 'ResponsiveToggle');
}
- this.$targetMenu = $(`#${targetID}`);
- this.$toggler = this.$element.find('[data-toggle]');
-
- this._update();
- }
+ /**
+ * Initializes the tab bar by finding the target element, toggling element, and running update().
+ * @function
+ * @private
+ */
+ _init() {
+ var targetID = this.$element.data('responsive-toggle');
+ if (!targetID) {
+ console.error('Your tab bar needs an ID of a Menu as the value of data-tab-bar.');
+ }
+
+ this.$targetMenu = $(`#${targetID}`);
+ this.$toggler = this.$element.find('[data-toggle]');
+ this.options = $.extend({}, this.options, this.$targetMenu.data());
+
+ // If they were set, parse the animation classes
+ if(this.options.animate) {
+ let input = this.options.animate.split(' ');
+
+ this.animationIn = input[0];
+ this.animationOut = input[1] || null;
+ }
+
+ this._update();
+ }
- /**
- * Adds necessary event handlers for the tab bar to work.
- * @function
- * @private
- */
- _events() {
- var _this = this;
+ /**
+ * Adds necessary event handlers for the tab bar to work.
+ * @function
+ * @private
+ */
+ _events() {
+ var _this = this;
- this._updateMqHandler = this._update.bind(this);
-
- $(window).on('changed.zf.mediaquery', this._updateMqHandler);
+ $(window).on('changed.zf.mediaquery', this._update.bind(this));
- this.$toggler.on('click.zf.responsiveToggle', this.toggleMenu.bind(this));
- }
-
- /**
- * Checks the current media query to determine if the tab bar should be visible or hidden.
- * @function
- * @private
- */
- _update() {
- // Mobile
- if (!Foundation.MediaQuery.atLeast(this.options.hideFor)) {
- this.$element.show();
- this.$targetMenu.hide();
+ this.$toggler.on('click.zf.responsiveToggle', this.toggleMenu.bind(this));
}
- // Desktop
- else {
- this.$element.hide();
- this.$targetMenu.show();
+ /**
+ * Checks the current media query to determine if the tab bar should be visible or hidden.
+ * @function
+ * @private
+ */
+ _update() {
+ // Mobile
+ if (!Foundation.MediaQuery.atLeast(this.options.hideFor)) {
+ this.$element.show();
+ this.$targetMenu.hide();
+ }
+
+ // Desktop
+ else {
+ this.$element.hide();
+ this.$targetMenu.show();
+ }
}
- }
- /**
- * Toggles the element attached to the tab bar. The toggle only happens if the screen is small enough to allow it.
- * @function
- * @fires ResponsiveToggle#toggled
- */
- toggleMenu() {
- if (!Foundation.MediaQuery.atLeast(this.options.hideFor)) {
- this.$targetMenu.toggle(0);
-
- /**
- * Fires when the element attached to the tab bar toggles.
- * @event ResponsiveToggle#toggled
- */
- this.$element.trigger('toggled.zf.responsiveToggle');
+ /**
+ * Toggles the element attached to the tab bar. The toggle only happens if the screen is small enough to allow it.
+ * @function
+ * @fires ResponsiveToggle#toggled
+ */
+ toggleMenu() {
+ if (!Foundation.MediaQuery.atLeast(this.options.hideFor)) {
+
+ if(this.options.animate) {
+ if (this.$targetMenu.is(':hidden')) {
+ Foundation.Motion.animateIn(this.$targetMenu, this.animationIn, () => {
+ /**
+ * Fires when the element attached to the tab bar toggles.
+ * @event ResponsiveToggle#toggled
+ */
+ this.$element.trigger('toggled.zf.responsiveToggle');
+ });
+ }
+ else {
+ Foundation.Motion.animateOut(this.$targetMenu, this.animationOut, () => {
+ /**
+ * Fires when the element attached to the tab bar toggles.
+ * @event ResponsiveToggle#toggled
+ */
+ this.$element.trigger('toggled.zf.responsiveToggle');
+ });
+ }
+ }
+ else {
+ this.$targetMenu.toggle(0);
+
+ /**
+ * Fires when the element attached to the tab bar toggles.
+ * @event ResponsiveToggle#toggled
+ */
+ this.$element.trigger('toggled.zf.responsiveToggle');
+ }
+ }
+ };
+
+ destroy() {
+ //TODO this...
}
- };
-
- destroy() {
- this.$element.off('.zf.responsiveToggle');
- this.$toggler.off('.zf.responsiveToggle');
-
- $(window).off('changed.zf.mediaquery', this._updateMqHandler);
-
- Foundation.unregisterPlugin(this);
}
-}
-ResponsiveToggle.defaults = {
- /**
- * The breakpoint after which the menu is always shown, and the tab bar is hidden.
- * @option
- * @example 'medium'
- */
- hideFor: 'medium'
-};
+ ResponsiveToggle.defaults = {
+ /**
+ * The breakpoint after which the menu is always shown, and the tab bar is hidden.
+ * @option
+ * @example 'medium'
+ */
+ hideFor: 'medium',
+
+ /**
+ * To decide if the toggle should be animated or not.
+ * @option
+ * @example false
+ */
+ animate: false
+ };
// Window exports
-Foundation.plugin(ResponsiveToggle, 'ResponsiveToggle');
+ Foundation.plugin(ResponsiveToggle, 'ResponsiveToggle');
}(jQuery);