TouchSwipeMixin

Map touch events to swipe gestures

Overview

Purpose: maps touch events to swipe gestures.

This mixin works at the beginning of the Elix render pipeline. In response to touch events, the mixin may call methods on the component or call setState directly.

eventsmethods → setState → render DOM → post-render

Expects the component to provide:

  • setState method compatible with ReactiveMixin's setState.
  • swipeLeft and swipeRight methods, or swipeDown and swipeUp methods. If the component defines these, the mixin will call them if the user makes a flick (fast swipe) gesture to the left/right or down/up, respectively.

Provides the component with:

  • [internal.state].swipeAxis member that is either "horizontal" (default) or "vertical", indicating in which axis swipes should be tracked.
  • [internal.state].swipeFraction member expressing how far through a swipe the user is currently is. See below.

Usage

import TouchSwipeMixin from 'elix/src/TouchSwipeMixin.js';
class MyElement extends TouchSwipeMixin(HTMLElement) {}

See also the related TrackpadSwipeMixin, which is like TouchSwipeMixin, but for trackpads instead of touch devices. Both TouchSwipeMixin and TrackpadSwipeMixin are designed to work with SwipeCommandsMixin to reveal commands behind list items when the user swipes.

Examples

This demo shows how a component can use TouchSwipeMixin and TrackpadSwipeMixin to listen to horizontal touch swipes and trackpad swipes, respectively. Swiping will show the current swipe distance as a fraction of the demo width. It will also translate the gray block by that fraction so the user feels like they are directly moving the block.
Demo: Swiping shows the fraction of the demo's width you have swiped
We would love to host you out here in the Sonoma Valley. The guest house is done on our Sonoma farm and we would love to see you. Dear Members, We're looking for a "top-notch" administrative assistant. Any leads you can give us would certainly be appreciated. I met with Peter Veruki at the Jones School of Administration at Rice last week and we had a great meeting. Our timing is a little off (they want someone next spring and I want a position by Jan), but we are still talking. Thank you for your presentation yesterday. As always, you handled very well some tough questions Well done. I want you to know that I am on board and 100% committed to do my best for the company. We'll get pointed questions related to Europe and broadband. Suggest we answer that we feel confident that the first mover advantage will give us superior returns in the long run. I would be happy to participate in the conference and workshop. Could we arrange a time to discuss the details later this week? Flowers have been ordered and will be delivered to Sharon's office on Tuesday morning. I spent a week on the project and my subcontractor was out his mobilization cost, and after more than a year, they finally paid a part of our cost. Per your request, I have attached a photo of the ornament. Please see attached revised travel advisory. I think it does a better job of balancing the competing interests. Below is an organization chart which displays the changes described here. We are confident this new organization will focus our talent and capital appropriately. Please join me in supporting this management team as it strives to maximize return. Stan will call in. I am emailing in the hopes of having an opportunity to talk if you are interested in the following possibility If there's anything you'd like to talk about, at any point, please do get in touch. Today I am receiving my 15 Years of Service Award and having my Anniversary lunch. Would you like to attend? Don't forget... Our Board of Trustees Meeting has been cancelled. (Please call me if you have questions regarding this matter.) With your permission, Harry and I will plan to bring our golf clubs on the airplane on Tuesday. I plan to buy one of those little traveling golf bags (very small) and carry only about 5 or 6 of my clubs with me. Attached is the second part of the email for the Development Committee. Please note that these documents should be opened in Word. Please call if you should have questions. The NYSE has standard criteria for listing stocks, which can be found on their website at www.nyse.com.
Demo: TouchSwipeMixin reveals commands behind these list items on a touch swipe

[internal.state].swipeFraction

This mixin calculates a state member [internal.state].swipeFraction to track the state of a touch swipe operation. If no swipe is in progress, this value is null. If a swipe is in progress, then swipeFraction holds a real number expressing the distance the user has swiped, divided by the width of the element being swiped (which, by default is the element itself). This value is negative for swipes to the left or up, and positive for swipes to the right or down.

Example: If the user touches an element with TouchSwipeMixin, and drags to the left one half of the element's width, then swipeFraction will be equal to -0.5.

The related TrackpadSwipeMixin may also set [internal.state].swipeFraction, adhering to an identical definition.

Completing a swipe

When the user lifts their finger to complete a swipe, the mixin inspects the current value of [internal.state].swipeFraction:

  • If the drag is over halfway to the left/up (swipeFraction less than -0.5), the mixin will invoke the component's swipeLeft/swipeUp method (if defined).
  • Conversely, if the drag ends over halfway to the right/down (swipeFraction greater than 0.5), the mixin will invoke swipeRight/swipeDown (if defined).

In all cases, when the drag completes, the mixin will set [internal.state].swipeFraction to null.

Flick gestures

If the user ends a swipe at high speed, the gesture is interpreted as a flick. If the flick is to the left, the mixin will invoke the component's swipeLeft method (if defined). Conversely, if the flick is to the right, the mixin will invoke swipeRight (if defined).

You can use TouchSwipeMixin with SwipeDirectionMixin to map these swipeLeft and swipeRight calls to directional navigations. You can additionally map directional navigation operations to selection calls using DirectionSelectionMixin. Elix's carousel components like SlidingPages use this chain of mixins to turn flick gestures into changes in the carousel's selection:

  • internal.swipeLeftinternal.goRightselectNext
  • internal.swipeRightinternal.goLeftselectPrevious