Showing posts with label Material Design. Show all posts
Showing posts with label Material Design. Show all posts

Thursday, September 15, 2016

Working with Grid List in Angular Material


This blog describes using Angular Material's Grid List to show an array of data. We use Angular Material directives/elements/attributes for rendering the content.



Angular Material’s Grid List provides a different perspective to the regular list control. Each item in the grid list are laid out as tiles. It provides additional space and an elaborate view. More than anything, it is fun to play with the layout compared to regular list.

For the blog I’m using dinosaur data represented with a grid list. Special thanks Firebase sample dataset that provide readymade JSON objects. (Not using firebase for this sample. Just used the sample dataset). Refer to references section at the end for a link to the dataset.

Getting Started 

 Use md-grid-list and md-grid-tile directive to create a Grid List. Consider following code for md-grid-list.

  • Use md-cols attribute for configuring number of columns on the grid list. 
  • Use md-row-height attribute to set height of each row (and hence the tile). 
<md-grid-list md-cols="4" md-row-height="200px">…</md-grid-list>

Each tile on the grid list is represented by md-grid-tile directive. Use ng-repeat to iterate through array of dinosaur data. 
<md-grid-tile ng-repeat="item in dinosaurs" > ... </md-grid-tile>

Within md-grid-tile, use elements/directives md-grid-tile-header and md-grid-tile-footer elements to add header and footer to each tile. Blog's sample is using a footer. Consider the following code. It shows two elements on the footer for a dinosaur, name and the order.

<md-grid-tile-footer >
          <strong>{{item.name}}</strong>
          <div>{{item.order}}</div>
  </md-grid-tile-footer>

Responsive Attributes

A four column grid looks good on a desktop screen. How about a mobile or tablet screen? The tiles might squeeze and the content might not be legible. Use Angular Material attributes that take advantage of CSS3 Flexbox break points for rendering according to the screen size. Consider following sample.

   <md-grid-list md-cols-gt-sm="4" md-cols-sm="2" md-cols="1" md-row-height="200px">

Use md-cols-sm with a value 2.  It shows grid list with two columns on a small screen. The CSS Flexbox (used underneath by Angular Material) considers screen width between 600px and 960px as a small screen.

Use md-cols-gt-sm with a value 4. It shows grid list with four columns on a screen greater than large. That is medium, large and extra large screens. Anything with screen width greater than 960px is considered greater than small.

That leaves an extra small screen (screen width less than 600px). Use md-cols default value to 1. It shows a grid list with one column on an extra small screen.


Make specific tiles larger

Based on a specific criteria, one or more
tiles could be made larger than others. Consider following sample. It makes the first tile span over two rows. $index represents index of an item in the loop with ng-repeat. If it’s 0, set rowspan  value to 2. Otherwise stick to default value 1.

<md-grid-tile md-rowspan-gt-xs="{{($index===0)?2:1}}" ng-class="item.background" ng-click="null" ng-repeat="item in dinosaurs">

It is a simplistic example. But consider using the rowspan based on tile's content length. For tiles with larger content or images, increase the rowspan.

Also, notice -gt-xs break point has been used on md-rowspan. As detailed out already, on an extra small screen, grid list shows a single column. With nothing next to it in a row, we can set it to default height on an extra small screen.

Complete Sample


References:

Angular Material website (https://material.angularjs.org) for

  • Grid List details.
  • Responsive break points.

Firebase dinosaurs sample dataset- https://dinosaur-facts.firebaseio.com/dinosaurs

Saturday, August 20, 2016

Implementing Google Inbox styled FAB buttons using Angular Material


This blog describes Floating Action Buttons and its implementation using Angular Material

What is a FAB (Floating Action Button) control?

Google's Material Design uses a FAB control to promote an action. These are floating buttons, not tied to a container or a control like a menu bar, a nav bar or a side menu. These highlight one more frequently used actions on the page.
As an example, Google Inbox has a button on the right-bottom, which pulls-up frequent actions like compose email, create a reminder etc.

Angular Material

Angular Material is a library for developing Material Design styled applications using AngularJS. In this blog let's explore creating a FAB control using Angular Material.

Create a button

Let's start by creating a simple button and styling it as a FAB. To create an Angular Material button use the directive md-button. Apply following CSS classes
md-fab - provides FAB look and feel to the button.
md-fab-top-right / md-fab-top-left / md-fab-bottom-right / md-fab-bottom-left - Position the button on top right or top left or bottom right or bottom left
Consider following code sample,


<md-button aria-label="An Idea" class="md-fab md-fab-top-right" ng-click="null">      <md-icon md-svg-src="images/ic_lightbulb_outline_white_48px.svg"></md-icon> 
</md-button>


Figure-1: A FAB control on top right of a page.

Notice md-icon element with-in the md-button. A FAB button looks better with an icon describing it's purpose instead of a text title. Refer to figure-1 for the result.


FAB Speed dial

Figure 2 - Speed dial



Google Inbox example described earlier is a Speed Dial. The FAB expands to a series of options. In the sample, let's create a speed dial of settings.  Clicking on the settings buttons shows available settings. Refer to figure 2. It shows settings speed dial trigger. Clicking or hovering over the trigger expands to show available settings.













Consider following code sample,


<md-fab-speed-dial md-open="isOpen" md-direction="up" class="md-fling md-fab-bottom-right md-hover-full" ng-mouseenter="isOpen=true"
            ng-mouseleave="isOpen=false">

...
</md-fab-speed-dial>


Similar to previous example, the CSS class md-fab-bottom-right positions the button on bottom right of the container. In the code sample md-content (directive for workspace in Angular Material) is the container.

Use md-fab-speed-dial directive, which encapsulate all the elements of speed dial.

The md-open attribute takes an expression. We are using a variable on model isOpen. If the value is set to true by default, will show FAB expanded on load.

In the sample, it's set to true by an expression on hovering over the speed dial. Notice the expression for ng-mouseenter sets isOpen to true. Similarly, on moving the mouse pointer out of the speed dial area closes the dial with isOpen set to false by ng-mouseleave

The md-direction accepts up/down/left/right to set the direction the dial expands. For a button on bottom right expanding the dial up is natural.

A CSS class md-fling sets animation while speed dial options show. md-scale is the other animation option available.

Use md-fab-trigger child element (within md-fab-speed-dial) for speed dial's trigger button. Consider following code,

           
 <md-fab-trigger>
     <md-button class="md-fab" aria-label="Settings">
         <md-icon md-svg-src="/images/ic_settings.svg"></md-icon>
     </md-button>
 </md-fab-trigger>


A fab button has been created as the trigger, which expands to show available speed dial options.

Encapsulate speed dial options under md-fab-actions. Each option is another FAB.  

You may consider using md-mini CSS class on child buttons under md-fab-actions. It shows the options as a smaller button than the trigger, indicating a child element. 

Also consider using md-tooltip directive to show tooltip help text for each option on the speed dial.

Consider following code,

 <md-fab-actions>
    <md-button class="md-fab md-primary md-mini" aria-label="Bluetooth Settings">
          <!-- Each component provides descriptive text as tooltip
          Direction tooltip should appear is et by md-direction attribute.
                     -->
         <md-tooltip md-direction="left">Bluetooth</md-tooltip>
         <md-icon md-svg-src="/images/ic_settings_bluetooth.svg"></md-icon>
     </md-button>
     
     <md-button class="md-fab md-primary md-mini" aria-label="Brightness Settings">
           <md-tooltip md-direction="left">Brightness</md-tooltip>
           <md-icon md-svg-src="/images/ic_settings_brightness.svg"></md-icon>
     </md-button>
     
     ...
</md-fab-actions>


Consider consolidated FAB Speed dial code below. Follow this link to Github for complete sample.

        <md-fab-speed-dial md-open="isOpen" md-direction="up" class="md-fling md-fab-bottom-right md-hover-full" ng-mouseenter="isOpen=true"
            ng-mouseleave="isOpen=false">
            <!-- Trigger button for speed dial. Notice it's a FAB button
                ARIA Label - FAB Buttons don't have title for the screen readers to pick
                For accessibility reasons we need ARIA label set. 
                Otherwise it might result in warnings
            -->
            <md-fab-trigger>
                <md-button class="md-fab" aria-label="Settings">
                    <md-icon md-svg-src="/images/ic_settings.svg"></md-icon>
                </md-button>
            </md-fab-trigger>

            <!--  Individual FAB options in the speed dial 
                  Notice these are fab buttons. 
                  md-mini is applied to make it a smaller sized FAB control
            -->
            <md-fab-actions>
                <md-button class="md-fab md-primary md-mini" aria-label="Bluetooth Settings">
                    <!-- Each component provides descriptive text as tooltip
                        Direction tooltip should appear is et by md-direction attribute.
                     -->
                    <md-tooltip md-direction="left">Bluetooth</md-tooltip>
                    <md-icon md-svg-src="/images/ic_settings_bluetooth.svg"></md-icon>
                </md-button>
                <md-button class="md-fab md-primary md-mini" aria-label="Brightness Settings">
                    <md-tooltip md-direction="left">Brightness</md-tooltip>
                    <md-icon md-svg-src="/images/ic_settings_brightness.svg"></md-icon>
                </md-button>
                <md-button class="md-fab md-primary md-mini" aria-label="Display Settings">
                    <md-tooltip md-direction="left">Display Overscan</md-tooltip>
                    <md-icon md-svg-src="/images/ic_settings_overscan.svg"></md-icon>
                </md-button>
                <md-button class="md-fab md-primary md-mini" aria-label="Voice Settings">
                    <md-tooltip md-direction="left">Voice</md-tooltip>
                    <md-icon md-svg-src="/images/ic_settings_voice.svg"></md-icon>
                </md-button>
            </md-fab-actions>
        </md-fab-speed-dial>

References and useful links