mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
27 lines
892 B
TypeScript
27 lines
892 B
TypeScript
import { ConfigsContextShape } from 'containers/ConfigsContext'
|
|
import * as React from 'react'
|
|
import * as keyHelper from 'utils/keyHelper'
|
|
import { SideBarState } from '../../containers/SideBarState'
|
|
|
|
export function useToggleSideBarWithKeyboard(
|
|
state: SideBarState,
|
|
configContext: ConfigsContextShape,
|
|
toggleShowSideBar: () => void,
|
|
) {
|
|
const isDisabled = state === 'disabled'
|
|
React.useEffect(
|
|
function attachKeyDown() {
|
|
if (isDisabled || !configContext.value.shortcut) return
|
|
|
|
function onKeyDown(e: KeyboardEvent) {
|
|
const keys = keyHelper.parseEvent(e)
|
|
if (keys === configContext.value.shortcut) {
|
|
toggleShowSideBar()
|
|
}
|
|
}
|
|
window.addEventListener('keydown', onKeyDown)
|
|
return () => window.removeEventListener('keydown', onKeyDown)
|
|
},
|
|
[toggleShowSideBar, isDisabled, configContext.value.shortcut],
|
|
)
|
|
}
|