UNPKG

swipe-list-sdk

Version:

Shared SwipeList React SDK

88 lines (78 loc) 4.45 kB
'use strict'; var React = require('react'); var reactSpring = require('react-spring'); var react = require('@use-gesture/react'); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ var __assign = function() { __assign = Object.assign || function __assign(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); }; typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; function SwipeList(_a) { var deck = _a.deck, onAdd = _a.onAdd, onDismiss = _a.onDismiss, _b = _a.cardSize, cardSize = _b === void 0 ? { width: 240, height: 360 } : _b; var _c = React.useState(0), index = _c[0], setIndex = _c[1]; var _d = reactSpring.useSpring(function () { return ({ x: 0, rot: 0, config: { tension: 400, friction: 35 }, }); }), springs = _d[0], api = _d[1]; var bind = react.useDrag(function (_a) { var down = _a.down, mx = _a.movement[0], vx = _a.velocity[0]; if (down) { api.start({ x: mx, rot: mx / 80, immediate: true }); } else { var trigger = Math.abs(mx) > cardSize.width / 2 || vx > 0.5; var dir_1 = mx > 0 ? 1 : -1; if (trigger) { var flyX = dir_1 * (cardSize.width + window.innerWidth); api.start({ x: flyX, rot: dir_1 * 20, immediate: false, onRest: function () { var item = deck[index]; dir_1 > 0 ? onAdd(item) : onDismiss(item); setIndex(function (i) { return i + 1; }); api.set({ x: 0, rot: 0 }); } }); } else { api.start({ x: 0, rot: 0, immediate: false }); } } }, { pointer: { touch: true }, from: function () { return [springs.x.get(), 0]; }, filterTaps: true }); if (index >= deck.length) return React.createElement("div", { style: { color: '#fff' } }, "No more titles."); var current = deck[index]; var next = deck[index + 1]; var x = springs.x, rot = springs.rot; return (React.createElement("div", __assign({}, bind(), { style: { position: 'relative', width: cardSize.width, height: cardSize.height, touchAction: 'none', cursor: 'grab' } }), next && (React.createElement("div", { style: { position: 'absolute', top: 8, left: 8, width: cardSize.width, height: cardSize.height, transform: 'scale(0.95)', borderRadius: 8, overflow: 'hidden', boxShadow: '0 4px 6px rgba(0,0,0,0.3)' } }, React.createElement("img", { src: next.posterUrl, alt: next.title, style: { width: '100%', height: '100%', objectFit: 'cover' } }))), React.createElement(reactSpring.animated.div, { style: { width: cardSize.width, height: cardSize.height, borderRadius: 8, overflow: 'hidden', boxShadow: '0 8px 10px rgba(0,0,0,0.4)', transform: reactSpring.to([x, rot], function (x, r) { return "translate3d(".concat(x, "px,0,0) rotate(").concat(r, "deg)"); }) } }, React.createElement("img", { src: current.posterUrl, alt: current.title, style: { width: '100%', height: '100%', objectFit: 'cover' } })))); } exports.SwipeList = SwipeList;