UNPKG

@blueprintjs/core

Version:
54 lines 2.71 kB
/* * Copyright 2021 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var _a; import { __assign, __spreadArrays } from "tslib"; import * as React from "react"; import { HotkeysDialog2 } from "../../components/hotkeys/hotkeysDialog2"; var initialHotkeysState = { hotkeys: [], isDialogOpen: false }; var noOpDispatch = function () { return null; }; // we can remove this guard once Blueprint depends on React 16 export var HotkeysContext = (_a = React.createContext) === null || _a === void 0 ? void 0 : _a.call(React, [ initialHotkeysState, noOpDispatch, ]); var hotkeysReducer = function (state, action) { switch (action.type) { case "ADD_HOTKEYS": return __assign(__assign({}, state), { hotkeys: __spreadArrays(state.hotkeys, action.payload) }); case "REMOVE_HOTKEYS": return __assign(__assign({}, state), { hotkeys: state.hotkeys.filter(function (key) { return action.payload.indexOf(key) === -1; }) }); case "OPEN_DIALOG": return __assign(__assign({}, state), { isDialogOpen: true }); case "CLOSE_DIALOG": return __assign(__assign({}, state), { isDialogOpen: false }); default: return state; } }; /** * Hotkeys context provider, necessary for the `useHotkeys` hook. */ export var HotkeysProvider = function (_a) { var _b; var children = _a.children, dialogProps = _a.dialogProps, renderDialog = _a.renderDialog; var _c = React.useReducer(hotkeysReducer, initialHotkeysState), state = _c[0], dispatch = _c[1]; var handleDialogClose = React.useCallback(function () { return dispatch({ type: "CLOSE_DIALOG" }); }, []); var dialog = (_b = renderDialog === null || renderDialog === void 0 ? void 0 : renderDialog(state, { handleDialogClose: handleDialogClose })) !== null && _b !== void 0 ? _b : (React.createElement(HotkeysDialog2, __assign({}, dialogProps, { isOpen: state.isDialogOpen, hotkeys: state.hotkeys, onClose: handleDialogClose }))); return (React.createElement(HotkeysContext.Provider, { value: [state, dispatch] }, children, dialog)); }; //# sourceMappingURL=hotkeysProvider.js.map