Skip to content

Commit

Permalink
feat: change tabs with keyboard (#150)
Browse files Browse the repository at this point in the history
closes #147
  • Loading branch information
e-krebs authored May 19, 2024
1 parent d0cba17 commit 25d310b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pile",
"version": "1.9.2",
"version": "1.9.3",
"author": "Emmanuel Krebs <e-krebs@users.noreply.github.com>",
"license": "MIT",
"scripts": {
Expand Down
27 changes: 22 additions & 5 deletions src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cx from 'classnames';
import { FC, PropsWithChildren, useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';

import { ServiceContext } from 'hooks';
import { type ServiceNames } from 'services';
Expand Down Expand Up @@ -29,6 +30,26 @@ export const Tabs: FC<PropsWithChildren<TabsProps>> = ({
const service = isService(selected.tab) ? selected.tab.service : null;
const Content = selected.tab.content;

const selectedTabByIndex = async (index: number) => {
const tab = tabs[index];
setSelected({ index, tab });
if (onTabChange) {
await onTabChange(isService(tab) ? tab.service.name : undefined);
}
};

useHotkeys(
'ctrl+1,ctrl+2,ctrl+3,ctrl+4,ctrl+5,ctrl+6,ctrl+7,ctrl+8,ctrl+9,ctrl+0',
async (e, { keys }) => {
e.preventDefault();
if (!keys || keys.length < 1) return;
let tabIndex = parseInt(keys[0]);
if (tabIndex === 0) tabIndex = 10;
if (isNaN(tabIndex) || tabIndex > tabs.length) return;
await selectedTabByIndex(tabIndex - 1);
}
);

return (
<ServiceContext.Provider value={service}>
<div
Expand All @@ -43,11 +64,7 @@ export const Tabs: FC<PropsWithChildren<TabsProps>> = ({
key={index}
active={selected.index === index}
onClick={async () => {
const tab = tabs[index];
setSelected({ index, tab });
if (onTabChange) {
await onTabChange(isService(tab) ? tab.service.name : undefined);
}
await selectedTabByIndex(index);
}}
/>
))}
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "pile",
"description": "pile",
"version": "1.9.2",
"version": "1.9.3",
"icons": {
"16": "./content/icons/icon-16.png",
"48": "./content/icons/icon-48.png",
Expand Down

0 comments on commit 25d310b

Please sign in to comment.