Class Swipe

Manages swipe interactions:

  • Tracks movement and detects direction
  • Emits events on start, move, and end
  • Supports exponential inertia
  • Optional bounds with rubber-band overflow and bounce-back

Notes:

  • Does not transform elements, only computes coordinates.

Documentation

Hierarchy (View Summary)

Constructors

  • Parameters

    • Optionalprops: ISwipeStaticProps & ISwipeMutableProps & Partial<
          {
              onAbort: (data: undefined, ctx: Swipe) => void;
              onDestroy: (data: undefined, ctx: Swipe) => void;
              onEnd: (data: ISwipeCoords, ctx: Swipe) => void;
              onInertia: (data: undefined, ctx: Swipe) => void;
              onInertiaCancel: (data: undefined, ctx: Swipe) => void;
              onInertiaEnd: (data: undefined, ctx: Swipe) => void;
              onInertiaFail: (data: undefined, ctx: Swipe) => void;
              onInertiaStart: (data: undefined, ctx: Swipe) => void;
              onMousemove: (data: MouseEvent, ctx: Swipe) => void;
              onMove: (data: ISwipeCoords, ctx: Swipe) => void;
              onPointerdown: (
                  data: { event: PointerEvent; pointer: IPointersItem },
                  ctx: Swipe,
              ) => void;
              onPointermove: (
                  data: { event: PointerEvent; pointer: IPointersItem },
                  ctx: Swipe,
              ) => void;
              onPointerup: (data: { pointer: IPointersItem }, ctx: Swipe) => void;
              onPreventEdgeSwipe: (data: undefined, ctx: Swipe) => void;
              onProps: (data: Partial, ctx: Swipe) => void;
              onStart: (data: ISwipeCoords, ctx: Swipe) => void;
              onToBottom: (data: undefined, ctx: Swipe) => void;
              onToLeft: (data: undefined, ctx: Swipe) => void;
              onToRight: (data: undefined, ctx: Swipe) => void;
              onToTop: (data: undefined, ctx: Swipe) => void;
              onTouchmove: (data: TouchEvent, ctx: Swipe) => void;
              onTouchstart: (data: TouchEvent, ctx: Swipe) => void;
          },
      >
    • OptionalonCallbacks: Partial<
          {
              onAbort: (data: undefined, ctx: Swipe) => void;
              onDestroy: (data: undefined, ctx: Swipe) => void;
              onEnd: (data: ISwipeCoords, ctx: Swipe) => void;
              onInertia: (data: undefined, ctx: Swipe) => void;
              onInertiaCancel: (data: undefined, ctx: Swipe) => void;
              onInertiaEnd: (data: undefined, ctx: Swipe) => void;
              onInertiaFail: (data: undefined, ctx: Swipe) => void;
              onInertiaStart: (data: undefined, ctx: Swipe) => void;
              onMousemove: (data: MouseEvent, ctx: Swipe) => void;
              onMove: (data: ISwipeCoords, ctx: Swipe) => void;
              onPointerdown: (
                  data: { event: PointerEvent; pointer: IPointersItem },
                  ctx: Swipe,
              ) => void;
              onPointermove: (
                  data: { event: PointerEvent; pointer: IPointersItem },
                  ctx: Swipe,
              ) => void;
              onPointerup: (data: { pointer: IPointersItem }, ctx: Swipe) => void;
              onPreventEdgeSwipe: (data: undefined, ctx: Swipe) => void;
              onProps: (data: Partial, ctx: Swipe) => void;
              onStart: (data: ISwipeCoords, ctx: Swipe) => void;
              onToBottom: (data: undefined, ctx: Swipe) => void;
              onToLeft: (data: undefined, ctx: Swipe) => void;
              onToRight: (data: undefined, ctx: Swipe) => void;
              onToTop: (data: undefined, ctx: Swipe) => void;
              onTouchmove: (data: TouchEvent, ctx: Swipe) => void;
              onTouchstart: (data: TouchEvent, ctx: Swipe) => void;
          },
      >

    Returns Swipe

Accessors

  • get isDestroyed(): boolean

    Checks if the module has been destroyed.

    Returns boolean

  • get name(): string

    The name of the module, derived from the class name

    Returns string

  • get prefix(): string

    Optional prefix for classnames used by the module

    Returns string

Methods

  • Adds a class name on an element, and keeps track of it for removal when the module is destroyed.

    Parameters

    • element: Element

      The target DOM element.

    • className: string

      The class name to toggle.

    Returns void

  • Helper function to generate classnames with the module's prefix.

    Parameters

    • ...classNames: string[]

      The class names to generate.

    Returns string

    A string of class names with the module's prefix applied.

  • Returns default mutable properties.

    Returns {
        __mutableProp: true;
        axis: "x" | "y";
        bounceDuration: number;
        bounds: (coords: ISwipeCoords) => null | ISwipeAxes;
        canBounce: () => boolean;
        directionThreshold: number;
        edgeSwipeThreshold: number;
        enabled: boolean;
        grabCursor: boolean;
        inertia: boolean;
        inertiaBounceEase: number;
        inertiaDecay: number;
        inertiaDistanceModifier: (distance: ISwipeVec3) => null | ISwipeVec3;
        inertiaDistanceThreshold: number;
        inertiaDuration: (distance: number) => number;
        inertiaEasing: false | number[] | TEasingFunction;
        inertiaRatio: number;
        inertiaThreshold: number;
        maxVelocity: { angle: number; x: number; y: number };
        minTime: number;
        overflow: () => number;
        preventEdgeSwipe: boolean;
        preventTouchMove: boolean;
        ratio: number;
        recalculateBoundsOnInertia: boolean;
        relative: boolean;
        requireCtrlKey: boolean;
        snap: () => null | ISwipeAxes;
        snapRadius: number;
        threshold: number;
        velocityModifier: false | (velocity: ISwipeState) => ISwipeState;
        willAbort: (props: ISwipeCanMoveArg) => boolean;
    }

    • __mutableProp: true
    • axis: "x" | "y"

      Primary swiping axis.

      null
      
    • bounceDuration: number

      Duration of the bounce-back timeline when movement exceeds bounds and inertia does not run.

      250
      
    • bounds: (coords: ISwipeCoords) => null | ISwipeAxes

      Movement limits per axis. Unset axis is unbounded.

      null
      
    • canBounce: () => boolean

      Determines whether the swipe is allowed to bounce back.

      () => true
      
    • directionThreshold: number

      Minimum swipe distance (px) for directional callbacks.

      50
      
    • edgeSwipeThreshold: number

      Edge swipe threshold (px) from the left/right edge.

      20
      
    • enabled: boolean

      Enables or disables swipe events.

      true
      
    • grabCursor: boolean

      Shows "grab" and "grabbing" cursors during interaction.

      false
      
    • inertia: boolean

      Enables release inertia (RAF-based decay).

      false
      
    • inertiaBounceEase: number

      Easing factor for pulling inertia back inside bounds (per frame, FPS-independent).

      0.3
      
    • inertiaDecay: number

      Inertia velocity decay per frame (higher = stops sooner).

      0.05
      
    • inertiaDistanceModifier: (distance: ISwipeVec3) => null | ISwipeVec3

      Inertia distance modifier. Called when inertia distance is predicted but not yet started.

      null
      
    • inertiaDistanceThreshold: number

      Timelined inertia is not supported anymore.

    • inertiaDuration: (distance: number) => number

      Timelined inertia is not supported anymore.

    • inertiaEasing: false | number[] | TEasingFunction

      Timelined inertia is not supported anymore.

    • inertiaRatio: number

      Multiplier applied to release velocity.

      1
      
    • inertiaThreshold: number

      Minimum release speed to start inertia (px/s for x/y, deg/s for angle).

      1
      
    • maxVelocity: { angle: number; x: number; y: number }

      Max release velocity per axis (coord/ms for x/y, deg/ms for angle). Falsy axis value disables inertia on that axis.

      { x: 7, y: 7, angle: 3 }
      
    • minTime: number

      Minimum duration (ms) to trigger swipe move.

      0
      
    • overflow: () => number

      Rubber-band distance past bounds (px for x/y, degrees for angle).

      () => 50
      
    • preventEdgeSwipe: boolean

      Prevents edge swiping (iOS swipe-back gesture).

      true
      
    • preventTouchMove: boolean

      Prevents touchmove event.

      true
      
    • ratio: number

      Swipe movement scale.

      1
      
    • recalculateBoundsOnInertia: boolean

      Enable bounds recalculation when inertia is active

      true
      
    • relative: boolean

      Calculates coordinates relative to the container.

      false
      
    • requireCtrlKey: boolean

      Requires Ctrl key for swipe (mouse only).

      false
      
    • snap: () => null | ISwipeAxes

      Snap targets per axis in movement space.

      null
      
    • snapRadius: number

      Max distance to a snap target (same units as the axis). Falsy = no radius limit.

      null
      
    • threshold: number

      Minimum swipe distance (px) to trigger swipe start.

      5
      
    • velocityModifier: false | (velocity: ISwipeState) => ISwipeState

      Timelined inertia is not supported anymore.

    • willAbort: (props: ISwipeCanMoveArg) => boolean

      Returns true to abort before the swipe starts (after threshold / axis checks).

      () => false

  • Returns default static properties.

    Returns {
        __staticProp: true;
        buttons: number[] | (type: TPointersType) => number[];
        container: HTMLElement | SVGElement;
        disableUserSelect: boolean;
        pointers: number | (type: TPointersType) => number;
        thumb: HTMLElement | SVGElement;
    }

    • __staticProp: true
    • buttons: number[] | (type: TPointersType) => number[]

      Determines which mouse buttons trigger events.

      • 0: Main button pressed, usually the left button or the un-initialized state
      • 1: Auxiliary button pressed, usually the wheel button or the middle button (if present)
      • 2: Secondary button pressed, usually the right button
      • 3: Fourth button, typically the Browser Back button
      • 4: Fifth button, typically the Browser Forward button

      See MouseEvent.button.

      [0]
      
    • container: HTMLElement | SVGElement

      Event listener container.

    • disableUserSelect: boolean

      Disable user selection on drag.

      true
      
    • pointers: number | (type: TPointersType) => number

      Required pointer count to activate swiping.

      1
      
    • thumb: HTMLElement | SVGElement

      An element that triggers the swipe start. Calculations remain relative to the container regardless of this element.

  • Adds a callback on the module's destruction.

    Parameters

    • action: () => void

      The function to execute during destruction.

    Returns void

  • Apply bounce overflow animation

    Parameters

    • OptionaltargetDuration: number

    Returns void

  • Sets programmatic scale in movement space. Optionally zooms toward an origin point and emits move.

    Parameters

    • value: number
    • origin: MouseEvent | ISwipeVec2 | TouchEvent

    Returns void