Right click menus now close on 'Escape'

This commit is contained in:
Aria Minaei 2021-08-09 11:30:44 +02:00
parent e6a4f3ae3f
commit 3a9285fa9e
2 changed files with 18 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import useWindowSize from 'react-use/esm/useWindowSize'
import styled from 'styled-components' import styled from 'styled-components'
import Item, {height as itemHeight} from './Item' import Item, {height as itemHeight} from './Item'
import {PortalContext} from 'reakit' import {PortalContext} from 'reakit'
import useOnKeyDown from '@theatre/studio/uiComponents/useOnKeyDown'
const minWidth = 190 const minWidth = 190
@ -94,6 +95,10 @@ const RightClickMenu: React.FC<{
}, [rect, container, props.rightClickPoint, windowSize, props.onRequestClose]) }, [rect, container, props.rightClickPoint, windowSize, props.onRequestClose])
const portalLayer = useContext(PortalContext) const portalLayer = useContext(PortalContext)
useOnKeyDown((ev) => {
if (ev.key === 'Escape') props.onRequestClose()
})
const items = Array.isArray(props.items) ? props.items : props.items() const items = Array.isArray(props.items) ? props.items : props.items()
return createPortal( return createPortal(

View file

@ -0,0 +1,13 @@
import {useEffect, useRef} from 'react'
export default function useOnKeyDown(callback: (ev: KeyboardEvent) => void) {
const ref = useRef(callback)
ref.current = callback
useEffect(() => {
const onKeyDown = (ev: KeyboardEvent) => ref.current(ev)
window.addEventListener('keydown', onKeyDown)
return () => {
window.removeEventListener('keydown', onKeyDown)
}
}, [])
}