or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

FastClick

FastClick is a simple, easy-to-use polyfill library that eliminates the 300ms delay between a physical tap and the firing of a click event on mobile browsers. It makes web applications feel more responsive on touch devices by detecting touch events and immediately firing synthetic click events, then suppressing the browser's delayed click events.

Package Information

  • Package Name: fastclick
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install fastclick

Core Imports

// Note: ES6 import may not work directly with this UMD module
// Use CommonJS or AMD patterns instead
const FastClick = require("fastclick").FastClick;

For CommonJS:

const FastClick = require("fastclick");

Alternative CommonJS pattern (recommended):

const attachFastClick = require("fastclick");

For AMD:

require(["fastclick"], function(FastClick) {
  // Use FastClick constructor
});

Basic Usage

// Recommended approach using factory method
document.addEventListener('DOMContentLoaded', function() {
  FastClick.attach(document.body);
}, false);

// With jQuery
$(function() {
  FastClick.attach(document.body);
});

// CommonJS shorthand pattern
const attachFastClick = require("fastclick");
attachFastClick(document.body);

// With options
FastClick.attach(document.body, {
  touchBoundary: 20,
  tapDelay: 100
});

Capabilities

FastClick Constructor

Creates a new FastClick instance and attaches event listeners to the specified DOM element.

/**
 * Instantiate fast-clicking listeners on the specified layer
 * @param {Element} layer - The layer to listen on
 * @param {FastClickOptions} [options] - Optional configuration
 */
function FastClick(layer, options);

Factory Method (Recommended)

Factory method for creating FastClick instances. This is the recommended way to use FastClick.

/**
 * Factory method for creating a FastClick object
 * @param {Element} layer - The layer to listen on
 * @param {FastClickOptions} [options] - Optional configuration
 * @returns {FastClick} FastClick instance
 */
FastClick.attach(layer, options);

Compatibility Check

Determines whether FastClick is needed for the current browser and element.

/**
 * Check whether FastClick is needed
 * @param {Element} layer - The layer to check
 * @returns {boolean} True if FastClick is not needed
 */
FastClick.notNeeded(layer);

Instance Methods

Destroy FastClick Instance

Removes all FastClick event listeners from the layer.

/**
 * Remove all FastClick's event listeners
 * @returns {void}
 */
destroy();

Element Click Requirements

Determines whether a given element requires a native click event.

/**
 * Determine whether a given element requires a native click
 * @param {EventTarget|Element} target - Target DOM element
 * @returns {boolean} True if the element needs a native click
 */
needsClick(target);

Element Focus Requirements

Determines whether a given element requires focus to simulate a click.

/**
 * Determine whether a given element requires focus for click simulation
 * @param {EventTarget|Element} target - Target DOM element  
 * @returns {boolean} True if the element requires focus
 */
needsFocus(target);

Send Synthetic Click

Sends a synthetic click event to the specified element.

/**
 * Send a click event to the specified element
 * @param {EventTarget|Element} targetElement - Target element
 * @param {Event} event - Original touch event
 * @returns {void}
 */
sendClick(targetElement, event);

Focus Element

Focuses the target element with mobile-specific handling.

/**
 * Focus the target element with mobile-specific handling
 * @param {EventTarget|Element} targetElement - Element to focus
 * @returns {void}
 */
focus(targetElement);

Update Scroll Parent

Updates scroll parent tracking for the given target element.

/**
 * Check and update the scroll parent for a target element
 * @param {EventTarget|Element} targetElement - Element to check
 * @returns {void}
 */
updateScrollParent(targetElement);

Get Target Element from Event Target

Resolves the actual target element from an event target, handling text nodes.

/**
 * Get target element from event target, handling text nodes
 * @param {EventTarget} eventTarget - Event target from touch event
 * @returns {Element|EventTarget} Resolved target element
 */
getTargetElementFromEventTarget(eventTarget);

Touch Has Moved

Checks whether the touch has moved past the boundary since it started.

/**
 * Check if touch has moved beyond the boundary
 * @param {Event} event - Touch event
 * @returns {boolean} True if touch moved past boundary
 */
touchHasMoved(event);

Find Control

Finds the associated control element for a given label element.

/**
 * Find the labelled control for a given label element
 * @param {EventTarget|HTMLLabelElement} labelElement - Label element
 * @returns {Element|null} Associated control element or null
 */
findControl(labelElement);

Determine Event Type

Determines the appropriate event type for synthetic events based on the target.

/**
 * Determine event type for synthetic events
 * @param {EventTarget|Element} targetElement - Target element
 * @returns {string} Event type ('click' or 'mousedown')
 */
determineEventType(targetElement);

Event Handler Methods

FastClick instances include several event handler methods that are automatically bound:

/**
 * Handle touch start events
 * @param {Event} event - Touch start event
 * @returns {boolean} Whether to continue event processing
 */
onTouchStart(event);

/**
 * Handle touch move events
 * @param {Event} event - Touch move event
 * @returns {boolean} Whether to continue event processing
 */
onTouchMove(event);

/**
 * Handle touch end events
 * @param {Event} event - Touch end event
 * @returns {boolean} Whether to continue event processing
 */
onTouchEnd(event);

/**
 * Handle touch cancel events
 * @returns {void}
 */
onTouchCancel();

/**
 * Handle mouse events and determine if they should be permitted
 * @param {Event} event - Mouse event
 * @returns {boolean} Whether to permit the event
 */
onMouse(event);

/**
 * Handle click events and determine if they should be permitted
 * @param {Event} event - Click event
 * @returns {boolean} Whether to permit the event
 */
onClick(event);

CSS Classes for Control

FastClick respects certain CSS classes to control its behavior:

  • needsclick: Add this class to elements that require native clicks instead of synthetic clicks
  • needsfocus: Add this class to elements that require focus behavior
<!-- This element will receive native clicks -->
<a class="needsclick" href="/example">Native Click Link</a>

<!-- This element will receive focus handling -->
<input class="needsfocus" type="text" placeholder="Focus handling">

Types

/**
 * Configuration options for FastClick
 */
interface FastClickOptions {
  /** Distance in pixels beyond which a click will be cancelled (default: 10) */
  touchBoundary?: number;
  /** Minimum time in milliseconds between tap events (default: 200) */
  tapDelay?: number;
  /** Maximum time in milliseconds for a tap gesture (default: 700) */
  tapTimeout?: number;
}

/**
 * FastClick instance with all methods and properties
 */
interface FastClick {
  // Instance properties
  /** Whether a click is currently being tracked */
  trackingClick: boolean;
  /** Timestamp for when click tracking started */
  trackingClickStart: number;
  /** The element being tracked for a click */
  targetElement: EventTarget | null;
  /** X-coordinate of touch start event */
  touchStartX: number;
  /** Y-coordinate of touch start event */
  touchStartY: number;
  /** ID of the last touch */
  lastTouchIdentifier: number;
  /** Touch movement boundary */
  touchBoundary: number;
  /** The FastClick layer element */
  layer: Element;
  /** Minimum time between tap events */
  tapDelay: number;
  /** Maximum time for a tap */
  tapTimeout: number;
  /** Timestamp of last click event */
  lastClickTime: number;
  /** Flag to cancel the next click */
  cancelNextClick: boolean;
  
  // Methods (as documented above)
  destroy(): void;
  needsClick(target: EventTarget | Element): boolean;
  needsFocus(target: EventTarget | Element): boolean;
  sendClick(targetElement: EventTarget | Element, event: Event): void;
  focus(targetElement: EventTarget | Element): void;
  updateScrollParent(targetElement: EventTarget | Element): void;
  getTargetElementFromEventTarget(eventTarget: EventTarget): Element | EventTarget;
  touchHasMoved(event: Event): boolean;
  findControl(labelElement: EventTarget | HTMLLabelElement): Element | null;
  determineEventType(targetElement: EventTarget | Element): string;
  onTouchStart(event: Event): boolean;
  onTouchMove(event: Event): boolean;
  onTouchEnd(event: Event): boolean;
  onTouchCancel(): void;
  onMouse(event: Event): boolean;
  onClick(event: Event): boolean;
}

Browser Compatibility

FastClick automatically detects when it's not needed and avoids attaching listeners in these cases:

  • Desktop browsers (no touch support)
  • Chrome 32+ on Android with width=device-width viewport meta tag
  • Chrome on Android with user-scalable=no viewport meta tag
  • BlackBerry 10.3+ with proper viewport configuration
  • Firefox 27+ with non-zoomable content
  • IE10+ with touch-action: manipulation CSS property

Module Export Patterns

FastClick supports multiple module systems:

CommonJS

  • module.exports is set to FastClick.attach function
  • module.exports.FastClick provides access to the constructor

AMD

  • Returns the FastClick constructor (not the attach function)

Global

  • Attaches FastClick constructor to window.FastClick

Error Handling

FastClick is designed to fail gracefully. If any errors occur during touch event processing, the library will typically fall back to allowing normal browser behavior rather than blocking user interactions.

Performance Considerations

  • FastClick only attaches listeners when needed (touch-capable devices)
  • Automatically detects modern browsers that don't need the polyfill
  • Uses efficient event delegation on a single layer element
  • Minimal overhead when not actively processing touch events