UNPKG

@rapido/components

Version:

Library of common primitive components used in Rapido apps.

123 lines (122 loc) 5.12 kB
"use strict"; /** * Copyright (c) 2019-present Verum Technologies * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { 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 __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = __importStar(require("react")); // @ts-ignore var ExecutionEnvironment_1 = require("fbjs/lib/ExecutionEnvironment"); var isEnabled = false; if (ExecutionEnvironment_1.canUseDOM) { /** * Web browsers emulate mouse events (and hover states) after touch events. * This code infers when the currently-in-use modality supports hover * (including for multi-modality devices) and considers "hover" to be enabled * if a mouse movement occurs more than 1 second after the last touch event. * This threshold is long enough to account for longer delays between the * browser firing touch and mouse events on low-powered devices. */ var HOVER_THRESHOLD_MS_1 = 1000; var lastTouchTimestamp_1 = 0; var enableHover = function () { if (isEnabled || Date.now() - lastTouchTimestamp_1 < HOVER_THRESHOLD_MS_1) { return; } isEnabled = true; }; var disableHover = function () { lastTouchTimestamp_1 = Date.now(); if (isEnabled) { isEnabled = false; } }; document.addEventListener('touchstart', disableHover, true); document.addEventListener('touchmove', disableHover, true); document.addEventListener('mousemove', enableHover, true); } var Hoverable = /** @class */ (function (_super) { __extends(Hoverable, _super); function Hoverable(props) { var _this = _super.call(this, props) || this; _this.state = { isHovered: false, showHover: true }; _this._handleMouseEnter = _this._handleMouseEnter.bind(_this); _this._handleMouseLeave = _this._handleMouseLeave.bind(_this); _this._handleGrant = _this._handleGrant.bind(_this); _this._handleRelease = _this._handleRelease.bind(_this); return _this; } Hoverable.prototype._handleMouseEnter = function (_) { if (isEnabled && !this.state.isHovered) { var onHoverIn = this.props.onHoverIn; if (onHoverIn) onHoverIn(); this.setState(function (state) { return (__assign(__assign({}, state), { isHovered: true })); }); } }; Hoverable.prototype._handleMouseLeave = function (_) { if (this.state.isHovered) { var onHoverOut = this.props.onHoverOut; if (onHoverOut) onHoverOut(); this.setState(function (state) { return (__assign(__assign({}, state), { isHovered: false })); }); } }; Hoverable.prototype._handleGrant = function () { this.setState(function (state) { return (__assign(__assign({}, state), { showHover: false })); }); }; Hoverable.prototype._handleRelease = function () { this.setState(function (state) { return (__assign(__assign({}, state), { showHover: true })); }); }; Hoverable.prototype.render = function () { var children = this.props.children; var child = typeof children === 'function' ? children(this.state.showHover && this.state.isHovered) : children; return react_1.default.cloneElement(react_1.default.Children.only(child), { onMouseEnter: this._handleMouseEnter, onMouseLeave: this._handleMouseLeave, // prevent hover showing while responder onResponderGrant: this._handleGrant, onResponderRelease: this._handleRelease, // if child is Touchable onPressIn: this._handleGrant, onPressOut: this._handleRelease, }); }; return Hoverable; }(react_1.Component)); exports.default = Hoverable;