UNPKG

2.71 kBJavaScriptView Raw
1/*
2 * Copyright 2021 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16var _a;
17import { __assign, __spreadArrays } from "tslib";
18import * as React from "react";
19import { HotkeysDialog2 } from "../../components/hotkeys/hotkeysDialog2";
20var initialHotkeysState = { hotkeys: [], isDialogOpen: false };
21var noOpDispatch = function () { return null; };
22// we can remove this guard once Blueprint depends on React 16
23export var HotkeysContext = (_a = React.createContext) === null || _a === void 0 ? void 0 : _a.call(React, [
24 initialHotkeysState,
25 noOpDispatch,
26]);
27var hotkeysReducer = function (state, action) {
28 switch (action.type) {
29 case "ADD_HOTKEYS":
30 return __assign(__assign({}, state), { hotkeys: __spreadArrays(state.hotkeys, action.payload) });
31 case "REMOVE_HOTKEYS":
32 return __assign(__assign({}, state), { hotkeys: state.hotkeys.filter(function (key) { return action.payload.indexOf(key) === -1; }) });
33 case "OPEN_DIALOG":
34 return __assign(__assign({}, state), { isDialogOpen: true });
35 case "CLOSE_DIALOG":
36 return __assign(__assign({}, state), { isDialogOpen: false });
37 default:
38 return state;
39 }
40};
41/**
42 * Hotkeys context provider, necessary for the `useHotkeys` hook.
43 */
44export var HotkeysProvider = function (_a) {
45 var _b;
46 var children = _a.children, dialogProps = _a.dialogProps, renderDialog = _a.renderDialog;
47 var _c = React.useReducer(hotkeysReducer, initialHotkeysState), state = _c[0], dispatch = _c[1];
48 var handleDialogClose = React.useCallback(function () { return dispatch({ type: "CLOSE_DIALOG" }); }, []);
49 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 })));
50 return (React.createElement(HotkeysContext.Provider, { value: [state, dispatch] },
51 children,
52 dialog));
53};
54//# sourceMappingURL=hotkeysProvider.js.map
\No newline at end of file