© V. Keerti Kotaru 2016

V. Keerti Kotaru, Material Design implementation with AngularJS, 10.1007/978-1-4842-2190-7_9

9. Mobile-Friendly Elements

V. Keerti Kotaru

(1)Hyderabad, Andhra Pradesh, India

Some of the controls and elements provided by Angular Material are very relevant on a mobile device. Technically, there is nothing stopping these controls from being used on a bigger screen, like a laptop or a desktop. However, they are designed to look better and more usable on a mobile device. Let us have a look at some of those controls.

Bottom Sheet

This is a directive or control for showing a menu of options. Typically, this is used upon clicking a button. The menu or list of available options pops up at the bottom of the screen. See Figure 9-1.

A416608_1_En_9_Fig1_HTML.jpg
Figure 9-1. A basic bottom sheet for demonstration

A directive md-bottom-sheetand a service $mdBottomSheetare used for this control. Consider the following code. It is basic implementation of bottom sheet. For simplicity in the first sample, the bottom sheet menu does not show any list of options. A better example is demonstrated later in the section to depict real usage.

angular.module("sampleApp", ["ngMaterial"])
     .controller('sampleController',function($scope, $mdBottomSheet ){
         $scope.showBottomSheet = function(){
             $mdBottomSheet.show({
                 template: "<md-bottom-sheet > <strong> Welcome to the Bottom Sheet Sample</strong> </md-bottom-sheet>"
             });
         };
     })

Notice that the $mdBottomSheetservice is being injected in the controller. An API show() on this service draws the bottom sheet. We have a sample button in the HTML template, which invokes showBottomSheet() function on scope.

<md-button ng-click="showBottomSheet()" class="md-primary">Show Bottom Sheet</md-button>

As the user clicks the button, the function on scope, showBottomSheet(), calls show() API on $mdBottomSheet service. We pass a JSON object with a template for the bottom sheet. The bottom sheet template must contain the md-bottom-sheet element/directive.

Let us now make it little more sophisticated. We could show buttons, a list of related actions in the bottom sheet. The component provides two default views for actions: list and grid.

Bottom Sheet—List View

Consider Figure 9-2. It shows the bottom sheet for login options.

A416608_1_En_9_Fig2_HTML.jpg
Figure 9-2. Bottom sheet depicting login options

The following code is used for creating the sample.

angular.module("sampleApp", ["ngMaterial"])
    .controller('sampleController',function($scope, $mdBottomSheet){
       $scope.showBottomSheet = function(){
          $mdBottomSheet.show({
             templateUrl: "/bottom-sheet-template.html"
          });
       };
    })

Unlike the previous sample, here we are using template URL. This is a preferred option because as templates grow in size, it becomes difficult to fit it all in the template field (of the JSON object). Consider the following template .

<md-bottom-sheet class="md-list ">
     <md-subheader>
        Choose Login Mechanism
    </md-subheader>
    <md-list>
        <md-list-item>
            <div>
                <md-button >
                     <md-icon md-svg-src="icons/ic_verified_user_black_24px.svg"></md-icon>
                     <span>UserId & Password</span>
                 </md-button>    
             </div>
         </md-list-item>
         <md-list-item>
             <div>
                 <md-button >
                     <md-icon md-svg-src="icons/ic_fingerprint_black_24px.svg"></md-icon>
                     <spanFingerprint</span>
                 </md-button>    
              </div>
         </md-list-item>
         <md-list-item>
             <div>
                 <md-button >
                     <md-icon md-svg-src="icons/ic_camera_black_24px.svg"></md-icon>
                        <span>Facial Recognition</span>
              </md-button>
          </div>
      </md-list-item>
     </md-list>
    </md-bottom-sheet>

Notice the CSS class md-list on the root element of the template, md-bottom-sheet. It ensures that the bottom sheet options are aligned vertically, like a list.

We are using md-subheader element for the header on the bottom sheet. We could use an h1, h2, or h3 element. Nevertheless, a subheader directive provides proper spacing and font size for the subheader. It is also a personal choice. We could choose to use a different element altogether.

The second element in md-bottom-sheet is the md-list itself. It has a list of options arranged by the list directive. We are using buttons with icons for descriptive information about each option.

Bottom Sheet—Grid View

The other view option with bottom sheet is to arrange actions on it like a grid. See Figure 9-3.

A416608_1_En_9_Fig3_HTML.jpg
Figure 9-3. Bottom sheet aligned to a grid

Changes are straightforward. Compared to the list view, change the CSS class on md-bottom-sheet to md-grid (from md-list). Considering the available space on the bottom sheet, you might choose to remove textual titles below or adjust the font accordingly. The following are the snippets that are different from the preceding sample.

<md-bottom-sheet class="md-grid">            

Here is the list item template in the sample.

<md-list-item>
   <div>
       <md-button >
            <md-icon md-svg-src="icons/ic_verified_user.svg" style="width:48px; height:48px; color:red;"></md-icon> <br/>
             <sup>User Id & Password</sup>
        </md-button>    
    </div>
</md-list-item>

Handle Bottom Sheet Actions

The buttons on bottom sheet are only meaningful if an action occurs on clicking them. We need to code handlers for those events. Review show() function on $mdBottomSheet service. The JSON object passed in as parameter also accepts a controller. Here, we can write handlers for each button on the bottom sheet. Consider the following code.

$mdBottomSheet.show({
   templateUrl: "/bottom-sheet-template.html",
   controller: function($scope){
       $scope.defaultAuthenticationHandler = function(){
           console.log("Take user to login form");
       }
       $scope.fingerprintHandler = function(){
                         console.log("Let's authenticate user by fingerprints");  
      };
      $scope.facialRecognitionHandler = function(){
          console.log("Use camera and identify the user");
               };
           }
        });

We have three handler functions written on scope, namely, defaultAuthenticationHandler, fingerprintHandler, and facialRecognitionHandler.

Next, we bind them on the HTML template on ng-click. As the user clicks one of the three buttons, the appropriate handler is called. Consider the following HTML template code. For the sample, as the user clicks one of the buttons, the log statement is printed on the browser’s console. In a full-fledged app, we can navigate to the appropriate login screen or invoke hardware functionality (for example, switch on camera for facial recognition).

<md-list >
    <md-list-item>
        <div>
            <md-button ng-click="defaultAuthenticationHandler()">
                 <md-icon md-svg-src="icons/ic_verified_user.svg" style="width:48px; height:48px; color:red;"></md-icon> <br/>
                <sup>User Id & Password</sup>
            </md-button>    
        </div>
    </md-list-item>
    <md-list-item>
        <div>
            <md-button ng-click="fingerprintHandler()">
                      <md-icon md-svg-src="icons/ic_fingerprint.svg" style="width:48px; height:48px; color:red;"></md-icon><br/>
                 <sup>Fingerprint</sup>
            </md-button>    
        </div>
    </md-list-item>
    <md-list-item>
        <div>
            <md-button ng-click="facialRecognitionHandler()">
              <md-icon md-svg-src="icons/ic_camera.svg" style="width:48px; height:48px; color:red;"></md-icon><br/>
                 <sup>Facial Recognition</sup>
            </md-button>
        </div>
    </md-list-item>
</md-list>

The show() API returns a promise. The promise is resolved or rejected on calling hide() or cancel() API, respectively. That is, $mdBottomSheet.hide() resolves the promise, whereas $mdBottomSheet.cancel() rejects the promise. Hence, in the handler we can invoke the appropriate API. Consider the following code.

$scope.fingerprintHandler = function(){
   console.log("Let's authenticate user by fingerprints");  
   $mdBottomSheet.hide();                              
};

The fingerprint handler can call hide() API, indicating that the bottom sheet’s job is done. If we have a cancel button, to recognize withdrawing the bottom sheet, use cancel() API . It rejects the promise. Consider the following code.

$mdBottomSheet.show({
   ... // show function definition
}).then(function(){
    console.log("Promise resolved"); // promise returned by show is resolved.
}, function(){
    console.log("Promise rejected"); // promise returned by show is rejected.
});

The following are additional options while using $mdBottomSheet.

  1. Scope: Use it to pass scope values between parent controller and bottom sheet directive. If no value is provided, it will create an isolated scope for the bottom sheet.

  2. preserveScope: true: Scope value is retained between multiple instances of show/hide of the bottom sheet.

  3. clickOutsideToClose: false: Default value is true. When set to false, the bottom sheet will not be closed on losing focus or on clicking outside the control.

  4. disableBackdrop: true: Default value is false. When set to true, does not fade the backdrop. See Figure 9-4.

    A416608_1_En_9_Fig4_HTML.jpg
    Figure 9-4. Backdrop enabled on the left and disabled on the right
  5. escapeToClose: false: Default value is true. If set to false, does not close the bottom sheet on Esc key press on the keyboard.

  6. disableParentScroll: false: Default value is true. If set to false, allows backdrop to scroll even when bottom sheet is open.

Swipe

Swipe is a common gesture with mobile devices. It in fact makes sense on any touch screen, even on a laptop with touch screen.

Angular material supports four directives that make it easy to support swipe in an application. Consider the following code.

<any md-swipe-left="swipeHandler()"></any>

It is an attribute directive. Use it on any element (for example div). It supports swipe gesture and calls the handler function attached on the $scope.

The following are the available directives .

  1. md-swipe-left

  2. md-swipe-right

  3. md-swipe-up

  4. md-swipe-down

Consider the following code snippet for usage.

<div md-swipe-right            ="swipeHandler()" class = "md-raised md-primary"> Swipe Right </div>
<div md-swipe-left ="swipeHandler()" > Swipe Left </div>
<div md-swipe-up ="swipeHandler()" > Swipe Up </div>
<div md-swipe-down ="swipeHandler()" > <sub> </sub> Swipe Down  <sub> </sub> </div>

The sample is simplistic and just prints a console statement .

angular.module("sampleApp", ["ngMaterial"])
    .controller('swipeSampleController', function($scope){
       $scope.swipeHandler = function(){
           console.log("Swiped !");
       }
    });

However, in the real world, on swipe, we could perform actions like navigate to a new view/page (possibly swipe left for forward and swipe right for backward navigation), refresh view with new content (possibly swipe down), and so on. Figure 9-5 depicts swipe directions.

A416608_1_En_9_Fig5_HTML.jpg
Figure 9-5. Swipe directions

Summary

Many Angular Material controls and features help build a responsive UI. The screen adjusts content based on the screen size. However, certain features provide mobile-specific behavior and functionality. These help to create a better mobile experience and easier use. It functionally works on bigger screens as well. However, they are designed for mobile views and touch screens.

This chapter elaborates mobile-specific services and directives initially. It details usage and features of md-bottom-sheet directive. We may perceive bottom sheet as a mobile-friendly menu. It has a set of actions or buttons that the user could tap on.

A bottom sheet supports two layouts: grid and list. Use CSS class md-list or md-grid to apply respective layout.

Then we delved into swipe functionality for touch screens. Angular Material provides four directives, which could be used as attributes on an HTML element. The directives are md-swipe-left, md-swipe-right, md-swipe-up, and md-swipe-down. On a touch screen, they have intuitive behaviour for moving forward, going back, pulling actions, and refreshing a screen, respectively. Implementing such gestures will be very effective for a touch screen.

References

For Angular Material documentation use https://material.angularjs.org

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.81.33