-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HotkeysProvider, HotkeysDialog2, fix examples
- Loading branch information
Showing
22 changed files
with
590 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import React, { createContext, useReducer, Dispatch, useCallback } from "react"; | ||
|
||
import { IHotkeyProps, HotkeysDialog2 } from "../components"; | ||
import { HotkeysDialog2Props } from "../components/hotkeys/hotkeysDialog2"; | ||
|
||
interface HotkeysContextState { | ||
/** List of hotkeys accessible in the current scope, registered by currently mounted components, can be global or local. */ | ||
hotkeys: IHotkeyProps[]; | ||
|
||
/** Whether the global hotkeys dialog is open. */ | ||
isDialogOpen: boolean; | ||
} | ||
|
||
type HotkeysAction = | ||
| { type: "ADD_HOTKEYS" | "REMOVE_HOTKEYS"; payload: IHotkeyProps[] } | ||
| { type: "CLOSE_DIALOG" | "OPEN_DIALOG" }; | ||
|
||
const initialHotkeysState: HotkeysContextState = { hotkeys: [], isDialogOpen: false }; | ||
|
||
export const HotkeysContext = createContext<[HotkeysContextState, Dispatch<HotkeysAction>]>([ | ||
initialHotkeysState, | ||
() => null, | ||
]); | ||
|
||
const hotkeysReducer = (state: HotkeysContextState, action: HotkeysAction) => { | ||
switch (action.type) { | ||
case "ADD_HOTKEYS": | ||
return { | ||
...state, | ||
hotkeys: [...state.hotkeys, ...action.payload], | ||
}; | ||
case "REMOVE_HOTKEYS": | ||
return { | ||
...state, | ||
hotkeys: state.hotkeys.filter(key => action.payload.indexOf(key) === -1), | ||
}; | ||
case "OPEN_DIALOG": | ||
return { ...state, isDialogOpen: true }; | ||
case "CLOSE_DIALOG": | ||
return { ...state, isDialogOpen: false }; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export interface HotkeysProviderProps { | ||
/** The component subtree which will have access to this hotkeys context. */ | ||
children: React.ReactChild; | ||
|
||
/** Optional props to customize the rendered hotkeys dialog. */ | ||
dialogProps?: Partial<Omit<HotkeysDialog2Props, "hotkeys">>; | ||
|
||
/** If provided, this dialog render function will be used in place of the default implementation. */ | ||
renderDialog?: (state: HotkeysContextState, contextActions: { handleDialogClose: () => void }) => JSX.Element; | ||
} | ||
|
||
export const HotkeysProvider = ({ children, dialogProps, renderDialog }: HotkeysProviderProps) => { | ||
const [state, dispatch] = useReducer(hotkeysReducer, initialHotkeysState); | ||
const handleDialogClose = useCallback(() => dispatch({ type: "CLOSE_DIALOG" }), []); | ||
|
||
const dialog = renderDialog?.(state, { handleDialogClose }) ?? ( | ||
<HotkeysDialog2 | ||
{...dialogProps} | ||
isOpen={state.isDialogOpen} | ||
hotkeys={state.hotkeys} | ||
onClose={handleDialogClose} | ||
/> | ||
); | ||
|
||
return ( | ||
<HotkeysContext.Provider value={[state, dispatch]}> | ||
{children} | ||
{dialog} | ||
</HotkeysContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { HotkeysProvider, HotkeysProviderProps } from "./hotkeysProvider"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c702efb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add HotkeysProvider, HotkeysDialog2, fix examples
Previews: documentation | landing | table