Skip to content

Commit

Permalink
Keyboard shortcuts help sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeaun committed Sep 6, 2023
1 parent 167fa70 commit 0d090eb
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Compose from './components/compose';
import ComposeButton from './components/compose-button';
import Drafts from './components/drafts';
import { ICONS } from './components/icon';
import KeyboardShortcutsHelp from './components/keyboard-shortcuts-help';
import Loader from './components/loader';
import MediaModal from './components/media-modal';
import Modal from './components/modal';
Expand Down Expand Up @@ -192,7 +193,8 @@ function App() {
snapStates.showAccount ||
snapStates.showDrafts ||
snapStates.showMediaModal ||
snapStates.showShortcutsSettings;
snapStates.showShortcutsSettings ||
snapStates.showKeyboardShortcutsHelp;
useEffect(() => {
if (!showModal) focusDeck();
}, [showModal]);
Expand Down Expand Up @@ -433,6 +435,7 @@ function App() {
<NotificationService />
<BackgroundService isLoggedIn={isLoggedIn} />
<SearchCommand onClose={focusDeck} />
<KeyboardShortcutsHelp />
</>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/icon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const ICONS = {
clipboard: () => import('@iconify-icons/mingcute/clipboard-line'),
'account-edit': () => import('@iconify-icons/mingcute/user-edit-line'),
'account-warning': () => import('@iconify-icons/mingcute/user-warning-line'),
keyboard: () => import('@iconify-icons/mingcute/keyboard-line'),
};

function Icon({
Expand Down
22 changes: 22 additions & 0 deletions src/components/keyboard-shortcuts-help.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#keyboard-shortcuts-help-container {
table {
th {
font-weight: normal;
text-align: start;
padding: 0.25em 0;
line-height: 1;
}
td {
padding: 0.25em 1em;
}
}

kbd {
border-radius: 4px;
display: inline-block;
padding: 0.3em;
line-height: 1;
border: 1px solid var(--outline-color);
background-color: var(--bg-faded-color);
}
}
136 changes: 136 additions & 0 deletions src/components/keyboard-shortcuts-help.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import './keyboard-shortcuts-help.css';

import { useHotkeys } from 'react-hotkeys-hook';
import { useSnapshot } from 'valtio';

import states from '../utils/states';

import Icon from './icon';
import Modal from './modal';

export default function KeyboardShortcutsHelp() {
const snapStates = useSnapshot(states);

function onClose() {
states.showKeyboardShortcutsHelp = false;
}

useHotkeys(
'?, shift+?',
(e) => {
console.log('help');
states.showKeyboardShortcutsHelp = true;
},
{
ignoreEventWhen: (e) => {
const hasModal = !!document.querySelector('#modal-container > *');
return hasModal;
},
},
);

const escRef = useHotkeys('esc', onClose, []);

return (
!!snapStates.showKeyboardShortcutsHelp && (
<Modal
class="light"
onClick={(e) => {
if (e.target === e.currentTarget) {
onClose();
}
}}
>
<div
id="keyboard-shortcuts-help-container"
class="sheet"
tabindex="-1"
ref={escRef}
>
<button type="button" class="sheet-close" onClick={onClose}>
<Icon icon="x" />
</button>
<header>
<h2>Keyboard shortcuts</h2>
</header>
<main>
<table>
{[
{
action: 'Keyboard shortcuts help',
keys: <kbd>?</kbd>,
},
{
action: 'Next post',
keys: <kbd>j</kbd>,
},
{
action: 'Previous post',
keys: <kbd>k</kbd>,
},
{
action: 'Skip carousel to next post',
keys: (
<>
<kbd>Shift</kbd> + <kbd>j</kbd>
</>
),
},
{
action: 'Skip carousel to previous post',
keys: (
<>
<kbd>Shift</kbd> + <kbd>k</kbd>
</>
),
},
{
action: 'Search',
keys: <kbd>/</kbd>,
},
{
action: 'Compose new post',
keys: <kbd>c</kbd>,
},
{
action: 'Send post',
keys: (
<>
<kbd>Ctrl</kbd> + <kbd>Enter</kbd> or <kbd></kbd> +{' '}
<kbd>Enter</kbd>
</>
),
},
{
action: 'Open post details',
keys: (
<>
<kbd>Enter</kbd> or <kbd>o</kbd>
</>
),
},
{
action: 'Toggle expanded/collapsed thread',
keys: <kbd>x</kbd>,
},
{
action: 'Close post or dialogs',
keys: (
<>
<kbd>Esc</kbd> or <kbd>Backspace</kbd>
</>
),
},
].map(({ action, keys }) => (
<tr key={action}>
<th>{action}</th>
<td>{keys}</td>
</tr>
))}
</table>
</main>
</div>
</Modal>
)
);
}
8 changes: 8 additions & 0 deletions src/components/nav-menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ function NavMenu(props) {
>
<Icon icon="group" size="l" /> <span>Accounts&hellip;</span>
</MenuItem>
<MenuItem
onClick={() => {
states.showKeyboardShortcutsHelp = true;
}}
>
<Icon icon="keyboard" size="l" />{' '}
<span>Keyboard shortcuts</span>
</MenuItem>
<MenuItem
onClick={() => {
states.showShortcutsSettings = true;
Expand Down
3 changes: 2 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ pre {
tab-size: 2;
}
pre code,
code {
code,
kbd {
font-size: 90%;
font-family: var(--monospace-font);
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const states = proxy({
showDrafts: false,
showMediaModal: false,
showShortcutsSettings: false,
showKeyboardShortcutsHelp: false,
// Shortcuts
shortcuts: store.account.get('shortcuts') ?? [],
// Settings
Expand Down Expand Up @@ -137,6 +138,7 @@ export function hideAllModals() {
states.showDrafts = false;
states.showMediaModal = false;
states.showShortcutsSettings = false;
states.showKeyboardShortcutsHelp = false;
}

export function statusKey(id, instance) {
Expand Down

0 comments on commit 0d090eb

Please sign in to comment.