Skip to content

Commit

Permalink
Close GraphiQL Playground on escape button (#4982)
Browse files Browse the repository at this point in the history
* fix

* close playground on escape

* changesets

* add comment
  • Loading branch information
Cloud11PL authored and poulch committed Jun 26, 2024
1 parent 8f98cc5 commit 21a25d2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-horses-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

You can now close GraphiQL Playground with Escape key
2 changes: 1 addition & 1 deletion .changeset/tiny-paws-nail.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"saleor-dashboard": patch
---

GraphQL Playground now no longer uses additional variables when using `shift + cmd + '` keyboard shortcut as the shortcut was removed. You can now only use one shortcut `cmd + '`, simplifying your workflow.
GraphQL Playground now no longer uses additional variables when using `shift + cmd + '` keyboard shortcut as the shortcut was removed. You can now only use `cmd + '`, simplifying your workflow.
4 changes: 2 additions & 2 deletions src/components/DevModePanel/DevModePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createFetch } from "@saleor/sdk";
import React from "react";
import { useIntl } from "react-intl";

import GraphiQL from "../GraphiQLPlain";
import PlainGraphiQL from "../GraphiQLPlain";
import { useDevModeContext } from "./hooks";
import { messages } from "./messages";

Expand Down Expand Up @@ -50,7 +50,7 @@ export const DevModePanel: React.FC = () => {
{intl.formatMessage(messages.title)}
</DialogHeader>
<DialogContent style={{ padding: 0, margin: 1, overflowY: "auto" }}>
<GraphiQL
<PlainGraphiQL
query={devModeContent}
variables={variables}
fetcher={fetcher}
Expand Down
8 changes: 6 additions & 2 deletions src/components/DevModePanel/DevModeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export function DevModeProvider({ children }) {
const [devModeContent, setDevModeContent] = useState("");
const [isDevModeVisible, setDevModeVisibility] = useState(false);

const triggerHandler = () => {
const handleOpen = () => {
setDevModeContent("");
setVariables("");

setDevModeVisibility(!isDevModeVisible);
};

useDevModeKeyTrigger(triggerHandler);
const handleClose = () => {
setDevModeVisibility(false);
};

useDevModeKeyTrigger(handleOpen, handleClose);

return (
<DevModeContext.Provider
Expand Down
28 changes: 21 additions & 7 deletions src/components/DevModePanel/useDevModeKeyTrigger.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import { useEffect } from "react";

type DevModeKeyTriggerCallback = () => void;
type HandleOpen = () => void;
type HandleClose = () => void;

export const useDevModeKeyTrigger = (
callbackHandler: DevModeKeyTriggerCallback,
handleOpen: HandleOpen,
handleClose: HandleClose,
) => {
useEffect(() => {
const handler = (event: KeyboardEvent) => {
const keyDownHandler = (event: KeyboardEvent) => {
if ((event.metaKey || event.ctrlKey) && event.code === "Quote") {
callbackHandler();
handleOpen();
}
};

document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
}, [callbackHandler]);
const keyUpHandler = (event: KeyboardEvent) => {
if (event.code === "Escape") {
handleClose();
}
};

document.addEventListener("keydown", keyDownHandler);
// GraphiQL Playground stops propagation of keydown event for Escape key
document.addEventListener("keyup", keyUpHandler);

return () => {
document.removeEventListener("keydown", keyDownHandler);
document.removeEventListener("keyup", keyUpHandler);
};
}, [handleOpen, handleClose]);
};

0 comments on commit 21a25d2

Please sign in to comment.