Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable swipe gesture to move to next tab for tabs module in Angular Material? (looking for a solution that works for more than 2 tabs)

I am using the tabs component in angular material: https://material.angular.io/components/component/tabs

Is there a way to make it such that I can swipe on the content area of the tab to trigger moving to the next tab?

My current template html:

<md-tab-group>
  <md-tab label="Tab 1">Content 1</md-tab>
  <md-tab label="Tab 2">Content 2</md-tab>
</md-tab-group>

I have hammerjs imported, and the documentation appears to not mention anything about this even though I could have sworn I've seen something like this before...

Specifically I want to be able to click and drag my mouse towards the left to have it swipe left to the left tab. And click and drag my mouse towards the right to have it swipe right ot the right tab.

like image 338
Rolando Avatar asked May 03 '17 04:05

Rolando


People also ask

Is one swipe gesture better than two taps on the keyboard?

But still, one swipe gesture is much better than two taps. This gesture probably won’t be useful for those who keep dozens of tabs open though. But if you keep your stuff organized, and only have a few open at a time, then you will likely switch to this swipe gesture when you use Chrome.

What is an active tab in Angular Material?

Angular Material tabs organize content into separate views where only one view can be visible at a time. Each tab's label is shown in the tab header and the active tab's label is designated with the animated ink bar. When the list of tab labels exceeds the width of the header, pagination controls appear to let the user scroll left ...

Can a swipe gesture for Chrome Save you time?

Earlier this week I showed you a swipe gesture for Chrome that let you launch the Tab Manager without needing to tap on the Square icon button to the right of the Address Bar. That one isn’t much of a time saver, but it’s nice to know about nonetheless. Today though, I want to show you a swipe gesture for Chrome that actually can save you time.

How do I Turn Off gestures on my Android tablet?

1 Open Settings, and click/tap on the Devices icon. Selecting Nothing for a gesture will disable it. 3 When finished, you can close Settings if you like. 2 Click/tap on Enable Gestures to toggle it to be (checked - enable) or off (unchecked - disable) for what you want.


1 Answers

here is a simple way to do it:

working plunker: https://plnkr.co/edit/uJ3n8XedvCCdeUHXKpwX?p=preview

first, add hammerjs to your module:

import { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';

provide it in providers

  providers: [{ 
                    provide: HAMMER_GESTURE_CONFIG, 
                    useClass: HammerGestureConfig 
                }]

sample module:

@NgModule({

  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    ReactiveFormsModule,
    MaterialModule,
  ],

  declarations: [TabsOverviewExample],
  bootstrap: [TabsOverviewExample],
  providers: [{ 
                    provide: HAMMER_GESTURE_CONFIG, 
                    useClass: HammerGestureConfig 
                }]
})
export class PlunkerAppModule {}

then build your component like this:

import {Component, AfterViewInit, ViewChild, ViewChildren} from '@angular/core';
import { MdTabGroup, MdTab } from '@angular/material';


@Component({
  selector: 'tabs-overview-example',
  template:`
  <md-tab-group [selectedIndex]="selected"  (swipeleft)="swipe($event.type)" (swiperight)="swipe($event.type)">
    <md-tab label="Tab 1"><div class="content">Content 1</div></md-tab>
    <md-tab label="Tab 2"><div class="content">Content 2</div></md-tab>
    <md-tab label="Tab 3"><div class="content">Content 3</div></md-tab>
    <md-tab label="Tab 4"><div class="content">Content 4</div></md-tab>
    <md-tab label="Tab 5"><div class="content">Content 5</div></md-tab>
  </md-tab-group>`,
  styles:['.content{ height: 500px; background-color: yellow;}']
})
export class TabsOverviewExample implements AfterViewInit{
  @ViewChild(MdTabGroup) group;
  @ViewChildren(MdTab) tabs;
  tab_num = 0;
  selected = 0;
  SWIPE_ACTION = { LEFT: 'swipeleft', RIGHT: 'swiperight' };

  number_tabs
  ngAfterViewInit(){
    this.tab_num = this.tabs.length
    console.log(this.group)
  }
  swipe(eType){
    console.log(eType);
    if(eType === this.SWIPE_ACTION.LEFT && this.selected > 0){
      console.log("movin left")
      this.selected--;
    }
    else if(eType === this.SWIPE_ACTION.RIGHT && this.selected < this.tab_num){
      console.log("movin right")
      this.selected++;
    }
    console.log(this.selected)
  }


}
like image 195
Ahmed Musallam Avatar answered Sep 20 '22 12:09

Ahmed Musallam