UNPKG

vevet

Version:

Vevet is a JavaScript library for creative development that simplifies crafting rich interactions like split text animations, carousels, marquees, preloading, and more.

430 lines 15.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Swipe = void 0; var base_1 = require("../../base"); var initVevet_1 = require("../../global/initVevet"); var utils_1 = require("../../utils"); var Pointers_1 = require("../Pointers"); var Coords_1 = require("./Coords"); var Inertia_1 = require("./Inertia"); var props_1 = require("./props"); var Styles_1 = require("./Styles"); __exportStar(require("./types"), exports); __exportStar(require("./global"), exports); /** * Manages swipe interactions: * - Tracks movement and detects direction * - Emits events on start, move, and end * - Supports inertia-based movement * * Notes: * - Does not transform elements, only computes coordinates. * - Does not persist state after swipe completion. * * [Documentation](https://vevetjs.com/docs/Swipe) * * @group Components */ var Swipe = /** @class */ (function (_super) { __extends(Swipe, _super); function Swipe(props, onCallbacks) { var _this = _super.call(this, props, onCallbacks) || this; /** If swiping has started */ _this._isSwiping = false; /** If swiping has been aborted */ _this._isAborted = false; var _a = _this.props, container = _a.container, thumb = _a.thumb, buttons = _a.buttons, pointers = _a.pointers; _this._coords = new Coords_1.SwipeCoords(_this); _this._inertia = new Inertia_1.SwipeInertia(_this); _this._styles = new Styles_1.SwipeStyles(_this); // create pointers _this._pointers = new Pointers_1.Pointers({ container: thumb || container, buttons: buttons, minPointers: pointers, maxPointers: pointers, enabled: _this.props.enabled, disableUserSelect: _this.props.disableUserSelect, }); // Set Events _this._setEvents(); return _this; } /** * Returns default static properties. */ Swipe.prototype._getStatic = function () { return __assign(__assign({}, _super.prototype._getStatic.call(this)), props_1.STATIC_PROPS); }; /** * Returns default mutable properties. */ Swipe.prototype._getMutable = function () { return __assign(__assign({}, _super.prototype._getMutable.call(this)), props_1.MUTABLE_PROPS); }; Object.defineProperty(Swipe.prototype, "coords", { /** Returns current swipe coordinates */ get: function () { return this._coords.coords; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "container", { /** Event target element */ get: function () { return this.props.container; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "hasInertia", { /** Indicates if inertia is active */ get: function () { return this._inertia.has; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "isSwiping", { /** Indicates if a swipe is active */ get: function () { return this._isSwiping; }, enumerable: false, configurable: true }); /** Handles property updates */ Swipe.prototype._handleProps = function (props) { _super.prototype._handleProps.call(this, props); this._pointers.updateProps({ enabled: this.props.enabled }); this._styles.setInline(); }; /** Sets event listeners */ Swipe.prototype._setEvents = function () { var _this = this; var callbacks = this.callbacks; var container = this.props.container; this._pointers.on('start', function () { return _this._handlePointersStart(); }); this._pointers.on('pointerdown', function (data) { return callbacks.emit('pointerdown', data); }); this._pointers.on('pointermove', function (data) { return callbacks.emit('pointermove', data); }); this._pointers.on('pointerup', function (data) { return callbacks.emit('pointerup', data); }); var touchstart = (0, utils_1.addEventListener)(container, 'touchstart', function (event) { return _this._handleTouchStart(event); }, { passive: false }); this.onDestroy(function () { return touchstart(); }); }; /** Handles `touchstart` events */ Swipe.prototype._handleTouchStart = function (event) { if (!this.props.enabled) { return; } this.callbacks.emit('touchstart', event); this._preventEdgeSwipe(event); }; /** Prevents edge swipes if enabled */ Swipe.prototype._preventEdgeSwipe = function (event) { var props = this.props; if (!props.preventEdgeSwipe) { return; } var threshold = props.edgeSwipeThreshold; var x = event.targetTouches[0].pageX; var shouldPrevent = x <= threshold || x >= (0, initVevet_1.initVevet)().width - threshold; if (event.cancelable && shouldPrevent) { event.preventDefault(); this.callbacks.emit('preventEdgeSwipe', undefined); } }; /** Handles swipe start and tracking */ Swipe.prototype._handlePointersStart = function () { var _this = this; var touchmove = (0, utils_1.addEventListener)(window, 'touchmove', this._handleTouchMove.bind(this), { passive: false }); var mousemove = (0, utils_1.addEventListener)(window, 'mousemove', this._handleMouseMove.bind(this)); var end = this._pointers.on('end', function () { _this._handleEnd(); end(); touchmove(); mousemove(); }); this.onDestroy(function () { end(); touchmove(); mousemove(); }); }; /** Handles `touchmove` event */ Swipe.prototype._handleTouchMove = function (event) { this.callbacks.emit('touchmove', event); if (this._isSwiping && this.props.preventTouchMove && event.cancelable) { event.preventDefault(); } this._handleMove(event, 'touch'); }; /** Handles `mousemove` event */ Swipe.prototype._handleMouseMove = function (event) { if (this.props.requireCtrlKey && !event.ctrlKey) { return; } this.callbacks.emit('mousemove', event); this._handleMove(event, 'mouse'); }; /** Handles move events */ Swipe.prototype._handleMove = function (event, type) { var data = this._coords; var matrix = data.decode(event); if (this._isAborted) { return; } // Save start coordinates if (!this._startCoord) { this._startCoord = __assign({}, matrix); } // Update start time if (!this._startTime) { this._startTime = +Date.now(); } // check if can start if (!this._isSwiping && !this._canStart(matrix, type)) { return; } // start if (!this._isSwiping) { this._inertia.cancel(); this._isSwiping = true; this._startCoord = __assign({}, matrix); data.setStart(matrix); this.callbacks.emit('start', this.coords); this._styles.append(); } // move this._move(matrix); }; /** Checks if swipe can start */ Swipe.prototype._canStart = function (matrix, type) { var startCoord = this._startCoord; var startTime = this._startTime; if (!startCoord || !startTime) { return false; } var _a = this.props, threshold = _a.threshold, ratio = _a.ratio, minTime = _a.minTime, axis = _a.axis, willAbort = _a.willAbort; var speed = Math.abs(ratio); var diff = { x: matrix.x - startCoord.x, y: matrix.y - startCoord.y, }; // check threshold var distX = diff.x / speed; var distY = diff.y / speed; var dist = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2)); if (dist < threshold) { return false; } // check time if (+new Date() - startTime < minTime) { return false; } // check axis if (axis) { var rawAngle = (Math.atan2(Math.abs(diff.y), Math.abs(diff.x)) * 180) / Math.PI; var normalizedAngle = axis === 'x' ? rawAngle : 90 - rawAngle; if (normalizedAngle > 45) { this._reset(); this._isAborted = true; this.callbacks.emit('abort', undefined); return false; } } // Check if should abort var shouldAbort = willAbort({ type: type, matrix: matrix, start: startCoord, diff: diff, }); if (shouldAbort) { this._reset(); this._isAborted = true; this.callbacks.emit('abort', undefined); return false; } return true; }; /** Handles move events */ Swipe.prototype._move = function (matrix) { var coords = this._coords; // Update coords coords.update(matrix); // Update velocity this._inertia.addVelocity(__assign(__assign({}, coords.current), { timestamp: coords.timestamp })); // trigger callbacks this.callbacks.emit('move', this.coords); }; /** Handles swipe end */ Swipe.prototype._handleEnd = function () { // reset this._startTime = undefined; this._isAborted = false; // check swiping if (!this.isSwiping) { return; } // reset this._reset(); // reset styles this._styles.remove(); // calculate direction var _a = this._coords.diff, diffX = _a.x, diffY = _a.y; var absDiffX = Math.abs(diffX); var absDiffY = Math.abs(diffY); var directionThreshold = this.props.directionThreshold; var endAxis = absDiffX > absDiffY ? 'x' : 'y'; if (endAxis === 'x' && absDiffX > directionThreshold) { if (diffX > 0) { this.callbacks.emit('toRight', undefined); } else if (diffX < 0) { this.callbacks.emit('toLeft', undefined); } } if (endAxis === 'y' && absDiffY > directionThreshold) { if (diffY > 0) { this.callbacks.emit('toBottom', undefined); } else if (diffY < 0) { this.callbacks.emit('toTop', undefined); } } // end callback this.callbacks.emit('end', this.coords); // modifiy last velocity time this._inertia.updateLastTimestamp(); // end with inertia if (this.props.inertia) { this._releaseInertia(); } }; /** Reset swipe states */ Swipe.prototype._reset = function () { this._startCoord = undefined; this._isSwiping = false; }; /** Apply inertia-based movement */ Swipe.prototype._releaseInertia = function () { var _this = this; var startMatrix = __assign({}, this.coords.current); this._inertia.release(function (addMatrix) { _this._move({ x: startMatrix.x + addMatrix.x, y: startMatrix.y + addMatrix.y, angle: startMatrix.angle + addMatrix.angle, }); }); }; /** Cancel inertia */ Swipe.prototype.cancelInertia = function () { this._inertia.cancel(); }; Object.defineProperty(Swipe.prototype, "start", { /** Start coordinate */ get: function () { return this._coords.start; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "prev", { /** Previous coordinate */ get: function () { return this._coords.prev; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "current", { /** Current coordinate */ get: function () { return this._coords.current; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "diff", { /** Difference between start and current coordinates */ get: function () { return this._coords.diff; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "step", { /** Difference between start and previous coordinates */ get: function () { return this._coords.step; }, enumerable: false, configurable: true }); Object.defineProperty(Swipe.prototype, "accum", { /** Accumulated movement */ get: function () { return this._coords.accum; }, enumerable: false, configurable: true }); /** * Destroys the component */ Swipe.prototype._destroy = function () { _super.prototype._destroy.call(this); this._pointers.destroy(); this._inertia.destroy(); this._styles.remove(); }; return Swipe; }(base_1.Module)); exports.Swipe = Swipe; //# sourceMappingURL=index.js.map