0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Added keyboard shortcut for search modal exit

refs https://github.com/TryGhost/Team/issues/1665

- uses esc to exit search modal
This commit is contained in:
Rishabh 2022-07-06 15:23:33 +02:00
parent 6b08ad5978
commit 1b2f6524cd
2 changed files with 25 additions and 2 deletions

View file

@ -27,6 +27,14 @@ export default class App extends React.Component {
});
}
componentDidUpdate(_prevProps, _prevState) {
if (this.state.showPopup !== _prevState?.showPopup && !this.state.showPopup) {
this.setState({
searchValue: ''
});
}
}
componentWillUnmount() {
/**Clear timeouts and event listeners on unmount */
window.removeEventListener('hashchange', this.hashHandler, false);

View file

@ -128,11 +128,26 @@ class PopupContent extends React.Component {
function SearchBox() {
const {searchValue, dispatch} = useContext(AppContext);
const inputRef = useRef(null);
const containerRef = useRef(null);
useEffect(() => {
setTimeout(() => {
inputRef?.current?.focus();
}, 150);
}, []);
let keyUphandler = (event) => {
if (event.key === 'Escape') {
dispatch('update', {
showPopup: false
});
}
};
const containeRefNode = containerRef?.current;
containeRefNode?.ownerDocument.removeEventListener('keyup', keyUphandler);
containeRefNode?.ownerDocument.addEventListener('keyup', keyUphandler);
return () => {
containeRefNode?.ownerDocument.removeEventListener('keyup', keyUphandler);
};
}, [dispatch]);
let className = 'z-10 relative flex items-center py-5 px-7 mt-10 md:mt-0 bg-white rounded-t-lg shadow';
if (!searchValue) {
@ -140,7 +155,7 @@ function SearchBox() {
}
return (
<div className={className}>
<div className={className} ref={containerRef}>
<SearchIcon className='mr-3 text-neutral-900' alt='Search' />
<input
ref={inputRef}