UNPKG

react-interaction-hooks

Version:

react interaction component like toast confirm alert...

580 lines (490 loc) 25.4 kB
module.exports = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 3); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = require("react"); /***/ }), /* 1 */ /***/ (function(module, exports) { module.exports = require("react-dom"); /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { // extracted by mini-css-extract-plugin /***/ }), /* 3 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); // EXTERNAL MODULE: external {"commonjs":"react","commonjs2":"react","amd":"React","root":"React"} var external_commonjs_react_commonjs2_react_amd_React_root_React_ = __webpack_require__(0); var external_commonjs_react_commonjs2_react_amd_React_root_React_default = /*#__PURE__*/__webpack_require__.n(external_commonjs_react_commonjs2_react_amd_React_root_React_); // EXTERNAL MODULE: external {"commonjs":"react-dom","commonjs2":"react-dom","amd":"ReactDOM","root":"ReactDOM"} var external_commonjs_react_dom_commonjs2_react_dom_amd_ReactDOM_root_ReactDOM_ = __webpack_require__(1); var external_commonjs_react_dom_commonjs2_react_dom_amd_ReactDOM_root_ReactDOM_default = /*#__PURE__*/__webpack_require__.n(external_commonjs_react_dom_commonjs2_react_dom_amd_ReactDOM_root_ReactDOM_); // CONCATENATED MODULE: ./src/Modals/Toast.jsx function Toast(_ref) { var _ref$title = _ref.title, title = _ref$title === void 0 ? '' : _ref$title; return external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__toast" }, title); } /* harmony default export */ var Modals_Toast = (Toast); // CONCATENATED MODULE: ./src/Modals/Loading.jsx function Loading(_ref) { var title = _ref.title; return external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__loading" }, title ? external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("span", { className: "rih_loading_desc" }, title) : null); } /* harmony default export */ var Modals_Loading = (Loading); // CONCATENATED MODULE: ./src/Modals/Alert.jsx function Alert(_ref) { var title = _ref.title, content = _ref.content, onCancel = _ref.onCancel, _ref$buttonText = _ref.buttonText, buttonText = _ref$buttonText === void 0 ? 'OK' : _ref$buttonText; return external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.Fragment, null, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__backdrop" }), external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__alert", role: "alert" }, title ? external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("h3", { className: "rih--title" }, title) : null, content ? external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih--content" }, content) : null, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__footer" }, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("span", { className: "rih_button", tabIndex: 0, role: "button", onClick: onCancel }, buttonText)))); } /* harmony default export */ var Modals_Alert = (Alert); // CONCATENATED MODULE: ./src/Modals/Confirm.jsx function Confirm(_ref) { var title = _ref.title, content = _ref.content, onCancel = _ref.onCancel, onSure = _ref.onSure, _ref$cancelIndex = _ref.cancelIndex, cancelIndex = _ref$cancelIndex === void 0 ? 0 : _ref$cancelIndex, _ref$buttonTexts = _ref.buttonTexts, buttonTexts = _ref$buttonTexts === void 0 ? ['Cancel', 'OK'] : _ref$buttonTexts; return external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.Fragment, null, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__backdrop" }), external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__confirm", role: "dialog", "aria-labelledby": "confirm dialog", "aria-describedby": "confirm dialog" }, title ? external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("h3", { className: "rih--title" }, title) : null, content ? external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih--content" }, content) : null, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__footer" }, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("span", { className: "rih_button".concat(cancelIndex === 0 ? ' rih_button_cancel' : ''), tabIndex: 0, role: "button", onClick: function onClick() { return cancelIndex === 0 ? onCancel() : onSure(0); } }, buttonTexts[0]), external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("span", { className: "rih_button".concat(cancelIndex === 1 ? ' rih_button_cancel' : ''), tabIndex: 0, role: "button", onClick: function onClick() { return cancelIndex === 1 ? onCancel() : onSure(1); } }, buttonTexts[1])))); } /* harmony default export */ var Modals_Confirm = (Confirm); // CONCATENATED MODULE: ./src/Modals/ActionSheet.jsx function ActionSheet(_ref) { var title = _ref.title, content = _ref.content, _ref$cancelIndex = _ref.cancelIndex, cancelIndex = _ref$cancelIndex === void 0 ? 0 : _ref$cancelIndex, _ref$options = _ref.options, options = _ref$options === void 0 ? [] : _ref$options, onSelect = _ref.onSelect; return external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.Fragment, null, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__backdrop" }), external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__action-sheet", role: "dialog", "aria-labelledby": "action sheet", "aria-describedby": "action sheet" }, title ? external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("h3", { className: "rih--title" }, title) : null, content ? external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih--content" }, content) : null, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("div", { className: "rih__footer" }, options.map(function (option, index) { return external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement("span", { key: index, className: "rih_button".concat(cancelIndex === index ? ' rih_button_cancel' : ''), tabIndex: 0, role: "button", onClick: function onClick() { return onSelect(index, option); } }, option); })))); } /* harmony default export */ var Modals_ActionSheet = (ActionSheet); // EXTERNAL MODULE: ./src/Modals/index.css var Modals = __webpack_require__(2); // CONCATENATED MODULE: ./src/Modals/index.jsx function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function Modals_Modals(props) { var modalsRoot = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useRef"])(); var alert = props.alert, confirm = props.confirm, actionSheet = props.actionSheet, toast = props.toast, loading = props.loading; var hasContent = alert.active || confirm.active || actionSheet.active || toast.active || loading.active; if (hasContent && !modalsRoot.current) { modalsRoot.current = document.createElement('div'); modalsRoot.current.className = 'react-interaction-h5__wrapper'; document.body.appendChild(modalsRoot.current); } if (!hasContent) { return null; } var modals = external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.Fragment, null, props.alert.active && external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(Modals_Alert, _extends({}, props.alert, { onCancel: function onCancel() { if (typeof props.alert.onClose === 'function') { props.alert.onClose(); } props.destroy(props.alert.id); } })), props.confirm.active && external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(Modals_Confirm, _extends({}, props.confirm, { onCancel: function onCancel() { if (typeof props.confirm.onCancel === 'function') { props.confirm.onCancel(); } props.destroy(props.confirm.id); }, onSure: function onSure(index) { if (typeof props.confirm.onSure === 'function') { props.confirm.onSure(index); } props.destroy(props.confirm.id); } })), props.actionSheet.active && external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(Modals_ActionSheet, _extends({}, props.actionSheet, { onSelect: function onSelect(index, option) { if (typeof props.actionSheet.onSelect === 'function') { props.actionSheet.onSelect(index, option); } props.destroy(props.actionSheet.id); } })), props.toast.active && external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(Modals_Toast, props.toast), props.loading.active && external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(Modals_Loading, props.loading)); return external_commonjs_react_dom_commonjs2_react_dom_amd_ReactDOM_root_ReactDOM_default.a.createPortal(modals, modalsRoot.current); } /* harmony default export */ var src_Modals = (Modals_Modals); // CONCATENATED MODULE: ./src/types/index.js var CREATE_CONFIRM = '@interaction/CREATE_CONFIRM'; var CREATE_LOADING = '@interaction/CREATE_LOADING'; var CREATE_ACTION_SHEET = '@interaction/CREATE_ACTION_SHEET'; var CREATE_TOAST = '@interaction/CREATE_TOAST'; var CREATE_ALERT = '@interaction/CREATE_ALERT'; var DESTROY_ITEM = '@interaction/DESTROY_ITEM'; // CONCATENATED MODULE: ./src/reducer/index.js var _hash; function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function r(prefix) { return "".concat(prefix, "-").concat(Math.random().toString(16).substr(2, 4)); } var INITIAL_STATE = { duration: 1500, actionSheet: { id: r('action-sheet'), active: false, count: 0 }, alert: { id: r('alert'), active: false, count: 0 }, confirm: { id: r('confirm'), active: false, count: 0 }, loading: { id: r('loading'), active: false, count: 0 }, toast: { id: r('toast'), active: false, count: 0 } }; var hash = (_hash = {}, _defineProperty(_hash, INITIAL_STATE.actionSheet.id, 'actionSheet'), _defineProperty(_hash, INITIAL_STATE.alert.id, 'alert'), _defineProperty(_hash, INITIAL_STATE.confirm.id, 'confirm'), _defineProperty(_hash, INITIAL_STATE.loading.id, 'loading'), _defineProperty(_hash, INITIAL_STATE.toast.id, 'toast'), _hash); function queueReducer() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _objectSpread({}, INITIAL_STATE); var action = arguments.length > 1 ? arguments[1] : undefined; var type = action.type, payload = _objectWithoutProperties(action, ["type"]); switch (action.type) { case CREATE_ACTION_SHEET: return _objectSpread({}, state, { actionSheet: _objectSpread({}, state.actionSheet, payload, { active: true, count: state.actionSheet.count + 1 }) }); case CREATE_ALERT: return _objectSpread({}, state, { alert: _objectSpread({}, state.alert, payload, { active: true, count: state.alert.count + 1 }) }); case CREATE_CONFIRM: return _objectSpread({}, state, { confirm: _objectSpread({}, state.confirm, payload, { active: true, count: state.confirm.count + 1 }) }); case CREATE_LOADING: return _objectSpread({}, state, { loading: _objectSpread({}, state.loading, payload, { active: true, count: state.loading.count + 1 }) }); case CREATE_TOAST: return _objectSpread({}, state, { toast: _objectSpread({}, state.toast, payload, { active: true, count: state.toast.count + 1 }) }); case DESTROY_ITEM: return _objectSpread({}, state, _defineProperty({}, hash[action.id], { id: action.id, active: false, count: state[hash[action.id]].count })); default: return state; } } /* harmony default export */ var reducer = (queueReducer); // CONCATENATED MODULE: ./src/Context/index.jsx function Context_extends() { Context_extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return Context_extends.apply(this, arguments); } function Context_objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = Context_objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function Context_objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function Context_objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { Context_defineProperty(target, key, source[key]); }); } return target; } function Context_defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var InteractionContext = external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createContext(); function useInteraction() { var context = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useContext"])(InteractionContext); if (context === undefined) { throw new Error('useInteraction must be used within a InteractionContext.Provider'); } return context; } function useListener(listener, dispatch, ACTION_TYPE) { var duration = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 2000; Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useEffect"])(function () { var timer; if (listener && listener.active) { timer = setTimeout(function () { dispatch({ type: DESTROY_ITEM, id: listener.id }); }, listener.duration || duration); } return function () { return clearTimeout(timer); }; }, [listener, dispatch, duration]); return Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useCallback"])(function (config) { dispatch(Context_objectSpread({ type: ACTION_TYPE }, config)); }, [dispatch, ACTION_TYPE]); } function useQueue(_ref) { var duration = _ref.duration; var _useReducer = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useReducer"])(reducer, Context_objectSpread({}, INITIAL_STATE, { duration: duration })), _useReducer2 = _slicedToArray(_useReducer, 2), state = _useReducer2[0], dispatch = _useReducer2[1]; var toast = useListener(state.toast, dispatch, CREATE_TOAST, state.duration); var alert = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useCallback"])(function (config) { return dispatch(Context_objectSpread({ type: CREATE_ALERT }, config)); }, [dispatch]); var confirm = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useCallback"])(function (config) { return dispatch(Context_objectSpread({ type: CREATE_CONFIRM }, config)); }, [dispatch]); var actionSheet = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useCallback"])(function (config) { return dispatch(Context_objectSpread({ type: CREATE_ACTION_SHEET }, config)); }, [dispatch]); var loading = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useCallback"])(function (config) { return dispatch(Context_objectSpread({ type: CREATE_LOADING }, config)); }, [dispatch]); var loaded = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useCallback"])(function () { return dispatch({ type: DESTROY_ITEM, id: state.loading.id }); }, [dispatch, state.loading.id]); var destroy = Object(external_commonjs_react_commonjs2_react_amd_React_root_React_["useCallback"])(function (modalId) { return dispatch({ type: DESTROY_ITEM, id: modalId }); }, [dispatch]); return { state: state, alert: alert, confirm: confirm, actionSheet: actionSheet, toast: toast, loading: loading, loaded: loaded, destroy: destroy }; } function InteractionProvider(props) { var duration = props.duration; var _useQueue = useQueue({ duration: duration }), state = _useQueue.state, destroy = _useQueue.destroy, actions = Context_objectWithoutProperties(_useQueue, ["state", "destroy"]); return external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(InteractionContext.Provider, { value: actions }, props.children, external_commonjs_react_commonjs2_react_amd_React_root_React_default.a.createElement(src_Modals, Context_extends({}, state, { destroy: destroy }))); } /* harmony default export */ var Context = (InteractionProvider); // CONCATENATED MODULE: ./src/index.jsx /* concated harmony reexport useInteraction */__webpack_require__.d(__webpack_exports__, "useInteraction", function() { return useInteraction; }); /* harmony default export */ var src = __webpack_exports__["default"] = (Context); /***/ }) /******/ ]);