Skip to content

Commit

Permalink
feat(back-shortcut): Keyboard shortcut for previous page (#1229)
Browse files Browse the repository at this point in the history
* Added shortcut for Back on settings

* Added shortcut for Back on other pages

* Stop escape key propagation

* Formatting/lint

* Use more accurate prop name

---------

Co-authored-by: Oliver Schwendener <[email protected]>
Co-authored-by: Oliver Schwendener <[email protected]>
  • Loading branch information
3 people authored Nov 4, 2024
1 parent 5cdebe8 commit 948833a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/renderer/Core/BaseLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Divider } from "@fluentui/react-components";
import type { ReactNode, RefObject } from "react";
import type { KeyboardEvent, ReactNode, RefObject } from "react";

type BaseLayoutProps = {
header?: ReactNode;
contentRef?: RefObject<HTMLDivElement>;
content: ReactNode;
footer?: ReactNode;
onKeyDown?: (event: KeyboardEvent) => void;
};

export const BaseLayout = ({ header, content, contentRef, footer }: BaseLayoutProps) => {
export const BaseLayout = ({ header, content, contentRef, footer, onKeyDown }: BaseLayoutProps) => {
return (
<div style={{ display: "flex", flexDirection: "column", height: "100%" }}>
<div style={{ display: "flex", flexDirection: "column", height: "100%" }} onKeyDown={onKeyDown} tabIndex={-1}>
{header}
<Divider appearance="subtle" />
<div ref={contentRef} style={{ height: "100%", overflowX: "auto", overflowY: "auto" }}>
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/Core/Search/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const ConfirmationDialog = ({ action, closeDialog }: ConfirmationDialogPr
return (
<Dialog
open={action !== undefined}
onOpenChange={(_, { open }) => {
onOpenChange={(event, { open }) => {
event.stopPropagation();
if (open === false) {
closeDialog();
}
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/Core/Settings/Pages/Debug/ResetSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export const ResetSettings = () => {

return (
<Field label={t("resetAllSettings", { ns })} hint={t("resetAllSettingsHint", { ns })}>
<Dialog>
<Dialog
onOpenChange={(event) => {
event.stopPropagation();
}}
>
<DialogTrigger disableButtonEnhancement>
<div>
<Button>{t("resetAllSettingsButton", { ns })}</Button>
Expand Down
14 changes: 13 additions & 1 deletion src/renderer/Core/Settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useContextBridge } from "@Core/Hooks";
import type { KeyboardEvent } from "react";
import { Route, Routes, useNavigate } from "react-router";
import { ExtensionSettings } from "./ExtensionSettings";
import { Navigation } from "./Navigation";
Expand All @@ -10,8 +11,19 @@ export const Settings = () => {
const navigate = useNavigate();
const closeSettings = () => navigate({ pathname: "/" });

const handleKeyDownEvent = (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
closeSettings();
}
};

return (
<div style={{ display: "flex", flexDirection: "column", height: "100%" }}>
<div
style={{ display: "flex", flexDirection: "column", height: "100%" }}
onKeyDown={handleKeyDownEvent}
tabIndex={-1}
>
<div style={{ flexShrink: 0 }}>
<SettingsHeader onCloseSettingsClicked={closeSettings} />
</div>
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/Extensions/Base64Conversion/Base64Conversion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Header } from "@Core/Header";
import { getImageUrl } from "@Core/getImageUrl";
import { Button, Text } from "@fluentui/react-components";
import { ArrowLeftFilled, CopyRegular } from "@fluentui/react-icons";
import type { KeyboardEvent } from "react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Converter } from "./Converter";
Expand All @@ -15,6 +16,13 @@ export const Base64Conversion = ({ contextBridge, goBack }: ExtensionProps) => {

const [convertedText, setConvertedText] = useState<string>("");

const handleKeyDownEvent = (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
goBack();
}
};

return (
<BaseLayout
header={
Expand Down Expand Up @@ -75,6 +83,7 @@ export const Base64Conversion = ({ contextBridge, goBack }: ExtensionProps) => {
</Button>
</div>
}
onKeyDown={handleKeyDownEvent}
/>
);
};
9 changes: 9 additions & 0 deletions src/renderer/Extensions/DeeplTranslator/DeeplTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useExtensionSetting } from "@Core/Hooks";
import { getImageUrl } from "@Core/getImageUrl";
import { Button, Text, Tooltip } from "@fluentui/react-components";
import { ArrowLeftFilled, CopyRegular, PersonRegular } from "@fluentui/react-icons";
import type { KeyboardEvent } from "react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { MissingApiKey } from "./MissingApiKey";
Expand All @@ -27,6 +28,13 @@ export const DeeplTranslator = ({ contextBridge, goBack }: ExtensionProps) => {
const openDeeplWebsite = () => contextBridge.openExternal("https://www.deepl.com/signup?cta=free-login-signup");
const openDeeplAccount = () => contextBridge.openExternal("https://www.deepl.com/account");

const handleKeyDownEvent = (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
goBack();
}
};

return (
<BaseLayout
header={
Expand Down Expand Up @@ -102,6 +110,7 @@ export const DeeplTranslator = ({ contextBridge, goBack }: ExtensionProps) => {
</div>
) : null
}
onKeyDown={handleKeyDownEvent}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ExtensionProps } from "@Core/ExtensionProps";
import { Header } from "@Core/Header";
import { Button, Field, Input, Text } from "@fluentui/react-components";
import { ArrowLeftRegular } from "@fluentui/react-icons";
import type { KeyboardEvent } from "react";
import { useState } from "react";
import { useTranslation } from "react-i18next";

Expand All @@ -18,6 +19,13 @@ export const MissingEverythingCliFilePath = ({

const temporaryEsFilePathExists = contextBridge.fileExists(temporaryEsFilePath);

const handleKeyDownEvent = (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
goBack();
}
};

return (
<BaseLayout
header={
Expand Down Expand Up @@ -55,6 +63,7 @@ export const MissingEverythingCliFilePath = ({
</Button>
</div>
}
onKeyDown={handleKeyDownEvent}
/>
);
};
12 changes: 11 additions & 1 deletion src/renderer/Extensions/SimpleFileSearch/EditFolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ export const EditFolder = ({ initialFolderSetting, onSave }: EditFolderProps) =>
setTemporaryFolderSetting({ ...temporaryFolderSetting, searchFor });

return (
<Dialog open={isDialogOpen} onOpenChange={(_, { open }) => (open ? openDialog() : closeDialog())}>
<Dialog
open={isDialogOpen}
onOpenChange={(event, { open }) => {
event.stopPropagation();
if (open) {
openDialog();
} else {
closeDialog();
}
}}
>
<DialogTrigger disableButtonEnhancement>
<Button onClick={openDialog} icon={<AddRegular />}>
{t("addFolder")}
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/Extensions/Workflow/EditWorkflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export const EditWorkflow = ({ trigger, workflow, save }: EditWorkflowProps) =>
const closeModal = () => setOpen(false);

return (
<Dialog open={open} onOpenChange={(_, { open }) => setOpen(open)}>
<Dialog
open={open}
onOpenChange={(event, { open }) => {
event.stopPropagation();
setOpen(open);
}}
>
<DialogTrigger disableButtonEnhancement>{trigger}</DialogTrigger>
<DialogSurface>
<DialogBody>
Expand Down

0 comments on commit 948833a

Please sign in to comment.