Basic keyboard shortcuts

Esc for carousel modal, Ctrl/Cmd+Enter for Compose field
This commit is contained in:
Lim Chee Aun 2022-12-30 21:36:14 +08:00
parent d605fc6ebe
commit 724be17d3e
4 changed files with 28 additions and 0 deletions

16
package-lock.json generated
View file

@ -18,6 +18,7 @@
"mem": "~9.0.2", "mem": "~9.0.2",
"preact": "~10.11.3", "preact": "~10.11.3",
"preact-router": "~4.1.0", "preact-router": "~4.1.0",
"react-hotkeys-hook": "~4.3.2",
"react-intersection-observer": "~9.4.1", "react-intersection-observer": "~9.4.1",
"string-length": "~5.0.1", "string-length": "~5.0.1",
"swiped-events": "~1.1.7", "swiped-events": "~1.1.7",
@ -4615,6 +4616,15 @@
"react": "^18.2.0" "react": "^18.2.0"
} }
}, },
"node_modules/react-hotkeys-hook": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/react-hotkeys-hook/-/react-hotkeys-hook-4.3.2.tgz",
"integrity": "sha512-ZA/li3kBDHuRTtJIf7Td41UU87bPtnt9xV4r+PlEzpnFoYRDVspk3B+mlaX75zowyQygMVmoaWnM4B88lkyExQ==",
"peerDependencies": {
"react": ">=16.8.1",
"react-dom": ">=16.8.1"
}
},
"node_modules/react-intersection-observer": { "node_modules/react-intersection-observer": {
"version": "9.4.1", "version": "9.4.1",
"resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.4.1.tgz", "resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.4.1.tgz",
@ -9091,6 +9101,12 @@
"scheduler": "^0.23.0" "scheduler": "^0.23.0"
} }
}, },
"react-hotkeys-hook": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/react-hotkeys-hook/-/react-hotkeys-hook-4.3.2.tgz",
"integrity": "sha512-ZA/li3kBDHuRTtJIf7Td41UU87bPtnt9xV4r+PlEzpnFoYRDVspk3B+mlaX75zowyQygMVmoaWnM4B88lkyExQ==",
"requires": {}
},
"react-intersection-observer": { "react-intersection-observer": {
"version": "9.4.1", "version": "9.4.1",
"resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.4.1.tgz", "resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.4.1.tgz",

View file

@ -20,6 +20,7 @@
"mem": "~9.0.2", "mem": "~9.0.2",
"preact": "~10.11.3", "preact": "~10.11.3",
"preact-router": "~4.1.0", "preact-router": "~4.1.0",
"react-hotkeys-hook": "~4.3.2",
"react-intersection-observer": "~9.4.1", "react-intersection-observer": "~9.4.1",
"string-length": "~5.0.1", "string-length": "~5.0.1",
"swiped-events": "~1.1.7", "swiped-events": "~1.1.7",

View file

@ -2,6 +2,7 @@ import './compose.css';
import '@github/text-expander-element'; import '@github/text-expander-element';
import { useEffect, useMemo, useRef, useState } from 'preact/hooks'; import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
import { useHotkeys } from 'react-hotkeys-hook';
import stringLength from 'string-length'; import stringLength from 'string-length';
import supportedLanguages from '../data/status-supported-languages'; import supportedLanguages from '../data/status-supported-languages';
@ -597,6 +598,13 @@ function Compose({
pointerEvents: uiState === 'loading' ? 'none' : 'auto', pointerEvents: uiState === 'loading' ? 'none' : 'auto',
opacity: uiState === 'loading' ? 0.5 : 1, opacity: uiState === 'loading' ? 0.5 : 1,
}} }}
onKeyDown={(e) => {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
formRef.current.dispatchEvent(
new Event('submit', { cancelable: true }),
);
}
}}
onSubmit={(e) => { onSubmit={(e) => {
e.preventDefault(); e.preventDefault();

View file

@ -9,6 +9,7 @@ import {
useRef, useRef,
useState, useState,
} from 'preact/hooks'; } from 'preact/hooks';
import { useHotkeys } from 'react-hotkeys-hook';
import { InView } from 'react-intersection-observer'; import { InView } from 'react-intersection-observer';
import 'swiped-events'; import 'swiped-events';
import useResizeObserver from 'use-resize-observer'; import useResizeObserver from 'use-resize-observer';
@ -1298,6 +1299,8 @@ function Carousel({ mediaAttachments, index = 0, onClose = () => {} }) {
}; };
}, []); }, []);
useHotkeys('esc', onClose, [onClose]);
return ( return (
<> <>
<div <div