Skip to content

Commit

Permalink
Fix account drawer closure on esc
Browse files Browse the repository at this point in the history
  • Loading branch information
serjonya-trili committed Oct 1, 2023
1 parent f17f51a commit 9c60874
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/views/home/AccountListWithDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useDisclosure } from "@chakra-ui/hooks";
import { Drawer, DrawerBody, DrawerContent, DrawerOverlay } from "@chakra-ui/react";
import { useState } from "react";
import { useEffect, useState } from "react";
import AccountCard from "../../components/AccountDrawer";
import { useAllAccounts } from "../../utils/hooks/accountHooks";
import { AccountsList } from "./AccountsList";
import { DrawerTopButtons } from "./DrawerTopButtons";
import { useDynamicModal } from "../../components/DynamicModal";
import colors from "../../style/colors";

const AccountListWithDrawer: React.FC = () => {
const [selected, setSelected] = useState<string | null>(null);
Expand All @@ -14,32 +15,33 @@ const AccountListWithDrawer: React.FC = () => {
const { isOpen, onClose: closeDrawer, onOpen } = useDisclosure();
const { isOpen: isDynamicModalOpen } = useDynamicModal();

const handleClose = () => {
setSelected(null);
closeDrawer();
};
// For some reason the drawer doesn't close on esc for this particular component
// Until we figure out why, we'll have this crutch
useEffect(() => {
const handleEsc = (e: KeyboardEvent) => {
if (e.key === "Escape") {
closeDrawer();
}
};
document.addEventListener("keydown", handleEsc);
return () => document.removeEventListener("keydown", handleEsc);
}, [closeDrawer]);

const account = allAccounts.find(account => account.address.pkh === selected);
return (
<>
<AccountsList
onOpen={onOpen}
selected={selected}
onSelect={pkh => {
setSelected(pkh);
}}
/>
<AccountsList onOpen={onOpen} selected={selected} onSelect={setSelected} />
<Drawer
blockScrollOnMount={!isDynamicModalOpen}
isOpen={isOpen}
placement="right"
onClose={handleClose}
onClose={closeDrawer}
size="md"
autoFocus={false}
>
<DrawerOverlay />
<DrawerContent maxW="594px" bg="umami.gray.900">
<DrawerTopButtons onClose={handleClose} />
<DrawerContent maxW="594px" bg={colors.gray[900]}>
<DrawerTopButtons onClose={closeDrawer} />
<DrawerBody>{account && <AccountCard account={account} />}</DrawerBody>
</DrawerContent>
</Drawer>
Expand Down

0 comments on commit 9c60874

Please sign in to comment.