Giter VIP home page Giter VIP logo

ng-material-floating-button's Introduction

ng-material-floating-button

Material design floating action button implemented as an Angularjs directive. Shamelessly inspired by action buttons from Google Inbox, Evernote and Path.

Made to be fast and easy to customise. It works out of the box with no other dependency apart from Angular, but plays nicely with the Angular Material bundle, for which it has dedicated templates.

Demo

Head over to the project homepage to see it in action as a standalone component, check the Angular Material integration or just take a look at this awesome gif:

Other versions

How to use

Download/clone the repo or use your favorite package manager:

npm i ng-material-floating-button --save

or:

bower install ng-mfb --save

Then (optionally) run npm install to have access to the configured Grunt tasks.

Look in the demo folder for usage examples and head to the original component docs to see how to customise the styles of the menu.

If you are upgrading check the changelog before doing so in order to prevent breaking changes to bite you.

Basic setup

Download the whole repo or clone it, then reference the directive css file (here is mfb/src/mfb.css)in your <head>:

<link href="../path/to/css/mfb.css" rel="stylesheet"/>

Place a reference to the directive before the closing <body> tag or anywhere after your angular script tag.

<script src="../mfb/src/mfb-directive.js"></script>

Make sure you reference the Mfb module as a dependecy to your app or module like so:

var app = angular.module('your-app', ['ng-mfb']);

Finally, place the correct html structure in your template. As a first example, assuming your example is using Ionicons as icon font:

<nav mfb-menu position="br" effect="zoomin" label="hover here"
     active-icon="ion-edit" resting-icon="ion-plus-round"
     toggling-method="click">
  <button mfb-button icon="paper-airplane" label="menu item"></button>
</nav>

This example shows the two basic components of the directive, a unique mfb-menu element which serves as a wrapper to a variable number of child buttons, defined by the mfb-button attribute. This above code will output a basic button menu on the bottom right corner of the screen with a single menu item. Hardly amazing, so let's see how to customise it.

NOTE: if you want to change the CSS make you sure you understand how it's supposed to be done. Please read here and here.

Element attributes

A number of attributes can be passed to the elements from the template in order to customise both behavior and appearance.

####<mfb-menu> element This can be defined as an attribute or an element. So this is valid:

<ul mfb-menu></ul>

...and this is valid too:

<mfb-menu></mfb-menu>
TemplateUrl

Optional attribute definining the template for the main button. If no template is specified it will fallback to the default ng-mfb-menu-default.tpl.html. If you are using Angular Material in your app you can pass the predefined template for the Angular Material bundle which is ng-mfb-menu-md.tpl.html.

Example:

<ul mfb-menu template-url="ng-mfb-menu-md.tpl.html">
  <!-- this will display the Angular Material optimised template -->
</ul>

By no means you are tied to the default templates, though. See customising templates.

Main Action

Defines a main action that will get fired when the main button is clicked. Works best with toggling-method=hover to put a main action on the base button.

Example:

<ul mfb-menu main-action="fireMainAction()"></ul>
Position

Defines in which corner of the screen the component should be displayed.

value explanation
tl top-left corner
tr top-right corner
br bottom-right corner
bl bottom-left corner

Example:

<ul mfb-menu position="br">
  <!-- this will be displayed on the bottom-right corner of the screen -->
</ul>
Toggling method

Defines how the user will open the menu. Two values are possible:

value explanation
hover hover to open the menu
click click or tap to open the menu

Example:

<ul mfb-menu toggling-method="click">
  <!-- click on the button to open the menu -->
</ul>

NOTE: Using hover will prevent user browsing on modbile/touch devices to properly interact with the menu. The directive provides a fallback for this case.

If you want the menu to work on hover but need support for touch devices you first need to include Modernizr to detect touch support. If you are alreay using it in your project just make sure that the touch detection is enabled.

If you're not using Modernizr already, just include the tiny (<3KB) provided modernizr.touch.js script (look in the mfb/src/lib/ folder) in your <head> or get the latest version of this very script right from here. Note that this is a custom build and will only detect for touch support, it's not the full library.

Menu state

You can programmatically open/close the menu leveraging this attribute at any time after compilation, without any clicking required by the user, or listen to the current state of the menu.

value explanation
open menu is... open (surprise, surprise)
closed menu is...(hold tight) ... closed

Example:

<ul mfb-menu menu-state="myVar">
</ul>
// in your controller
$scope.myVar = 'closed';

NB: currently this value is only updated if using click toggling.

Effect

Defines the effect that is performed when the menu opens up to show its child buttons.

value
zoomin
slidein
slidein-spring
fountain

Test them here.

Example:

<ul mfb-menu effect="zoomin">
</ul>
Label

The text that is displayed when hovering the main button. Example:

<ul mfb-menu label="your text here">
</ul>
Active-icon

The icon that will be displayed by default on the main button. Example:

<ul mfb-menu active-icon="ion-edit">
</ul>
Resting-icon

The icon that will be displayed on the main button when hovering/interacting with the menu. Example:

<ul mfb-menu resting-icon="ion-plus-round">
</ul>

<mfb-button> element

This element represents the child button(s) of the menu and can only "live" inside a wrapper <mfb-menu> element. Like its parent, it can be defined both as an attribute and as an element. So this is valid:

<a mfb-button></a>

...and this is valid too:

<mfb-button></mfb-button>

NOTE: If you are adding more than the default number of buttons supported by the provided CSS (currently 4) you will need to compile your own CSS beforehand to support your requirements. It's easy, here's an example.

TemplateUrl

Optional attribute definining the template for the child buttons. If no template is specified it will fallback to the default ng-mfb-button-default.tpl.html. If you are using Angular Material in your app you can pass the predefined template for the Angular Material bundle which is ng-mfb-button-md.tpl.html.

Example:

<button mfb-button template-url="ng-mfb-button-md.tpl.html">
  <!-- this will display the Angular Material optimised template -->
</button>

Here again customising the template is surely possible, see how here.

Icon

Pass the class of the icon font character that is associated to the menu item: Example:

<a mfb-button icon="ion-paperclip"></a>
Label

The text that is displayed when hovering the button. Example:

<a mfb-button label="About us"></a>
Custom attributes

Due to the nature of the component you'll probably want to associate some actions or use other angular directives such as ng-repeat on the buttons. As these attributes will be copied over to the generated html structure you can simply attach them to the <mfb-element>. A couple of examples, here using ui-router:

<!-- if using ui-router, associate an on-click event to the anchor-->
<a mfb-button ui-sref="yourState"></a>

And here leveraging a basic ng-repeat with buttons defined via js:

// in your controller...
$scope.buttons = [{
  label: 'a link text',
  icon: 'ion-paper-airplane'
},{
  label: 'another link',
  icon: 'ion-plus'
},{
  label: 'a third link',
  icon: 'ion-paperclip'
};
<!-- in your template -->
<!-- this will output 3 buttons with different icon, label and so on-->
<a mfb-button label="{{button.label}}" icon="{{button.icon}}" ng-repeat="button in buttons"></a>

mfb-button-close attribute

When using the toggling method click <ul mfb-menu toggling-method="click"> only the main button toggles the menu. If you want your secondary buttons to close the menu as well you can use the mfb-button-close attribute on your mfb-button.

That way if your mfb-button opens a modal or something else that loses focus, your menu will close.

<button mfb-button mfb-button-close>Closes the menu</button>

Customising templates

Custom templates can be passed as a attributes to the directive. Just pass either the url of your own template or the ID of the script containing your template. Refer to the default templates provided to have a working base to build upon.

More customisations

The component have plenty more customisations available and they are all handled by the CSS. The CSS and its SCSS source files are found in the mfb/ folder (which is actually a subtree that pulls from this repo).

For a thorough overview of what and how to customise the look of the component through css make sure you read these docs, especially if you plan to keep your copy in sync with this repo by pulling in changes in the future.

Unit tests

To run the tests you need Jasmine and Karma runner. They can be run from the console with either grunt karma or karma start test/karma.conf.js commands.

Contributing and issues

Contributions are very welcome, as well as opening issues if you find any bugs. If an issue or pull request is not specifically related to the Angularjs version (i.e. it's a layout/css bug/feature) please open it on the original component repo rather than here.

Todos

  • add "click to open" functionality and option
  • add to bower
  • add to npm

Credits

Thanks to these contributors. Demo icons are courtesy of Ionicons

ng-material-floating-button's People

Contributors

jgodi avatar nobitagit avatar rubenlagus avatar shprink avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ng-material-floating-button's Issues

How to make child button label clickable?

Hi,First of all I want to thank U for this great directive..I have implemented this on my new ionic project. how to give ng-click event to the child button 'label'?Currently the button 'labels' are not clickable.The ng-click event is only applicable to buttons.Not labels. Can anyone please help me?

feat: button colors

With angular-material, it would be nice to set custom classes on the menu and buttons. So each button could be a different color.

Q: When will package be available on NPM

Hello,

We would love to use ng-material-floating-button in our application. It's looking really nice.

But we are using only NPM for dependency management in our application, not Bower. I noticed everything is prepared for publishing the floating-button component on NPM, but I could not find it in the NPM registry.
Did I miss it? Or do you have short term plans for publishing the component on NPM? If so, for when would this be scheduled?

Kind regards,

Luc Feys

Making use of md-icon directive of ngMaterial

Hello,

Just found your really nice directive, looking awesome! Do you plan to allow the customization of the use of active|restingicons? I would like to use the md-icon directive from the ngMaterial module with svg icons. Or can you point me how to adjust it?

Thanks! Dominic

Draggable

Is there a way to make the button draggable ?

Thanks,
Yohana

Issues with angular-material

Hello!

I'm not good enough in css, but there are some problems in work of this component together with angular-material.
In triggered state it looks like this:
_012

And in not triggered:
_013

As you can see here is 2 issues:

  • Icon is on the top of the button, not in center
  • Menu item has no icon at all

My code:

<nav mfb-menu position="br" effect="zoomin" label="hover here" active-icon="ion-edit" resting-icon="ion-plus-round"  toggling-method="hover">
    <a mfb-button icon="paper-airplane" label="text here"></a>
</nav>

Looks like code from example for me.

Directive does not work for more than 4 buttons

As I found it is the way css is used that limits the number of buttons that could be used.
Using nth-child this way really seems to be very weird, WET and fragile way to achieve what this directive does.
A better and more robust approach could be using ngAnimate staggering animations and calculating offset in directive itself (using JS).

PS
I didnt actually intend showing more than 4 buttons simultaneously but hiding buttons does not help (using ng-show, ng-hide) and using ng-if is not an option since it triggers some weird animation.

Mutliple buttons

Is it possible to display multiple menu buttons on the same screen? E.g: Bottom left menu has certain buttons rating to requirement A, Bottom right menu has certain buttons relating to requirement B but neither menu must display it's buttons when the other is clicked.

Cannot close menu programmatically

I am binding menu-state to a property of my controller like in the docs:

    <nav mfb-menu position="tr" effect="zoomin" toggling-method="click"
         menu-state="vm.FabModel.state"
         active-icon="clear" resting-icon="add" template-url="account/fab/menu.html">
      <button mfb-button aria-label="Comment" icon="comment" label="Comment"
              template-url="account/fab/button.html" mfb-button-close></button>
    </nav>

When I open\close fab - the property get's updated, however when I update a property the fab does not close\open until you hover a mouse over it.

ng-material template

Can you support using the material-icons font library?

I use the litagure method.

<md-icon>person</md-icon>
Outputs the person icon.

Instruction for initial setup not working

I just installed this via bower, and it doesn't seem to work (angular 1.4).

I installed it, added the css and js file into my html, and added 'ng-mfb' into my app (app.module).
Is there something else I am missing?

Child icons font-size

It's a very small thing, but I thought I should mention it.

There is no nice way to customise the size of the icons on child buttons using SASS without changing the whole button size, as it overwrites $icons-font-size used in .mfb-component__icon

.mfb-component__child-icon,
.mfb-component__child-icon{
  @extend .mfb-component__icon;
  line-height: $child_button_size;
  font-size: 18 / 56 * $child_button_size;
}

I just removed the font-size line to make it work for me.

Mobile incompatibility

Hi Guys, first of all thanks for this amazing solution.
I'm developing an Angular/Ionic/Leaflet app and I use the mfb-button to show some actions on the map layer...so my code now is this...

       <nav  mfb-menu position="br" effect="zoomin" active-icon="ion-plus-circled" resting-icon="ion-plus-round" menu-state="mfbState" toggling-method="click">

        <button mfb-button label="add" icon="ion-edit" ng-click=mfbAction(1)></button>
        <button mfb-button label="locate" icon="ion-android-locate" ng-click=locate(null)></button>
        ....
        <button mfb-button label="filter map (GRID)" icon="ion-levels" ng-click=show($event)></button>
    </nav>

well...I have now 2 problem and I really cannot understand the reason:

  1. on web the menu works really good...but when you go via mobile browser or from the build app on android it doesn't work (the menu doesn't show the buttons if u click on it)

  2. after click on action the menu is still opened until u didn't click on it to close it: there is a solution to close the menu automatically after click on a button?

Thanks in advance

Icons don't show up in Chrome

The icons are not showing up in Chrome. I am using grunt to start app locally. When I switch to font awesome, which does show up, the icons are not centered.

mfb-directive.js causes error when using ng-strict-di

In version 0.6.0, there are two injections that cause an application to fail when using ng-strict-di.

One is on line 65

mfb.directive('mfbButtonClose', function($log) {
    ...
});

Another is on line 95 (in mfbMenu directive)

controller: function($scope, $attrs) {
   ...
}

Anyway to "disable" the mfb?

Wondering if there is currently anyway to "disable" the mfb? I need it to be disabled until a certain view state is valid and to then be "enabled". Thinking of something like ng-disabled for buttons. Thanks.

Use the offical material-design-icons with $mdIconProvider?

Is it possible to use the material-design-icons via the $mdIconProvider ?

I implemented like this, but it does not work.

$mdIconProvider
      .icon('account-circle', 'bower_components/material-design-icons/action/svg/production/ic_account_circle_24px.svg')
      .icon('more-vert', 'bower_components/material-design-icons/navigation/svg/production/ic_more_vert_24px.svg');
<nav mfb-menu position="br" effect="zoomin" label="hover here"
     active-icon="more-vert" resting-icon="more-vert"
     toggling-method="hover">
  <button mfb-button icon="account-circle" label="menu item"></button>
</nav>

Use with google icon fonts

Is it possible to use with Google Icon Fonts, as follows:
<i class="material-icons">menu</i>

in order to show for example, menu (hamburguer) icon?

$location on click

Hi, first of all want to thank you for this directive, i've implementing on a new project, but i can't make work with ng-href or ng-click to make a $location, this is what i have 'till now:

<nav mfb-menu position="tl" effect="slidein" label="Menú"
         active-icon="ion-edit" resting-icon="ion-grid" toggling-method="hover">
          <mfb-button ng-click="go({{button.link}})" icon="{{button.icon}}" label="{{button.label}}" ng-repeat="button in buttons"></mfb-button>
      </nav>
$scope.go = function ( path ) {
     $location.path( path );
};

or with the html ng-href="/#/foo"

No touch support

Currently depends on :hover and when tested with iPad2 on the demonstration page it does not work.

angular-material button animates into initial closed state

Compare the angular-material demo against the original demo. In the original the button slides in gracefully, already in a closed state. In the angular-material demo, before the button slides in, you see the faint animation of the menu in an open state rushing to close itself in time.

Tested on OSX Safari and Chrome.

How to access menu state?

Hi there,

I can't seem to figure out how to access and modify a menu button's state from within a controller in an angular/ionic app.
I'm trying to change the opacity of the page whenever the button's menu is open.

Cheers

Issue with more than 4 buttons

Hi, this is a great library. I'm facing a strange problem - it just does not seem possible to generate more than 4 buttons - no matter how much I try it only displays 4 (+the root button). I've tried this on a desktop browser and my iPhone.

<nav mfb-menu position="br" effect="zoomin" label = "collapse"
     active-icon="ion-chevron-down" resting-icon="ion-chevron-up"
     toggling-method="click">
        <button mfb-button icon="ion-close" label="close" ng-click="closeModal()"></button>

           <a mfb-button icon="ion-play" label="Play" ng-click="nextEvent(2)"></a>

        <a mfb-button icon="ion-pause" label="Pause" ng-click="nextEvent(1)"></a>

        <a mfb-button icon="ion-skip-backward" label="previous" ng-click="nextEvent(12)"></a>

        <a mfb-button icon="ion-skip-forward" label="next" ng-click="nextEvent(13)"></a>

         <a mfb-button icon="ion-skip-forward" label="next" ng-click="nextEvent(13)"></a>


</nav>

Animation finished event [Enhancement]

With my app the menu frequently appear over content (visually confusing) so my workaround is to fade the main content (opacity: 20%, say) while the menu is active via main-action().

This works great for the menu open, but ideally I'd like a way to un-fade the content after the menu close animation is complete. Something like an onAnimationFinished event, perhaps (though a full suite of onMenuOpenStart/ onMenuOpenFinished/ onMenuClosedStart/ onMenuClosedFinished events would be even cooler)

Thanks for a great widget.

How to associate callbacks to the buttons?

What's the best way to associate callbacks to be called when the buttons are clicked?

I've tried using ng-click, but it doesn't work as expected on mobile devices when main button is clicked. Instead of opening the menu, it fires the callback associated to the main button.

I've found a way to circumvent the issue. I'm going to fork the project and open a pull request, so you can evaluate it.

Thanks for making the component available.

[]s
Ivam

No way to close menu after click unless user moves mouse out of the way

Because this is entirely CSS, it appears there is no way to close the menu when someone clicks an item, am I missing something?

I assigned an ng-click to the mfb-button item which I use to launch an action, but the user's mouse is still over the button, and it remains displayed open by CSS.

Is there a way to work around this?

-webkit- prefix missing in _zoomin.scss

A prefix is missing - that is preventing button from being shown in Safari:

-webkit-transform: translateY...

@mixin effects-zoomin{

/**
 * ZOOM-IN
 * When hovering the main button, the child buttons grow
 * from zero to normal size.
 *
 */

  .mfb-component--tl.mfb-zoomin,
  .mfb-component--tr.mfb-zoomin{
    .mfb-component__list{
      li{
        transform: scale(0);
      }
      @for $i from 1 through $number-of-child-buttons {
        $distance: 70px * $i;
        li:nth-child( #{$i} ) { 
          -webkit-transform: translateY($distance) scale(0);
          transform: translateY($distance) scale(0);
          transition: all $slide-speed;
          // this is the delay at which the buttons start disappearing
          transition-delay: ( $number-of-child-buttons - $i ) * 0.05s;        
        }          
      }     
    }
    &:hover{
      .mfb-component__list{
        @for $i from 1 through $number-of-child-buttons {
          $distance: 70px * $i;
          li:nth-child( #{$i} ) { 
            -webkit-transform: translateY($distance) scale(1);
            transform: translateY($distance) scale(1); 
            // this is the delay at which the buttons appear          
            transition-delay: $i * 0.05s;
          }          
        }                
      }
    }
  }

  .mfb-component--bl.mfb-zoomin,
  .mfb-component--br.mfb-zoomin{
    .mfb-component__list{
      li{
        transform: scale(0);
      }
      @for $i from 1 through $number-of-child-buttons {
        $distance: -70px * $i;
        li:nth-child( #{$i} ) { 
          -webkit-transform: translateY($distance) scale(0);
          transform: translateY($distance) scale(0);
          transition: all $slide-speed;
          // this is the delay at which the buttons start disappearing
          transition-delay: ( $number-of-child-buttons - $i ) * 0.05s;        
        }          
      }     
    }
    &:hover{
      .mfb-component__list{
        @for $i from 1 through $number-of-child-buttons {
          $distance: -70px * $i;
          li:nth-child( #{$i} ) { 
            -webkit-transform: translateY($distance) scale(1); 
            transform: translateY($distance) scale(1); 
            // this is the delay at which the buttons appear          
            transition-delay: $i * 0.05s;
          }          
        }                
      }
    }
  }

}

``

Smaller child action buttons

Is it possible to have smaller child action buttons, just like Google's Inbox?

I've tried to change buttons height and width, but I'm facing a hard time to adjust the spacing between the buttons after that:

selection_046

Could you please provide any advice on this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.