Skip to content

Commit

Permalink
fix: click away listener for add-node-handle's popper(#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihebi committed Aug 4, 2023
1 parent cc917b4 commit d452eba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 58 deletions.
15 changes: 12 additions & 3 deletions ui/src/components/nodes/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ const WrappedHandle = ({ position, children }) => {
const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(anchorEl ? null : event.currentTarget);
};
const store = useContext(RepoContext)!;
const moved = useStore(store, (state) => state.moved);
const open = Boolean(anchorEl);
const handler = React.useCallback((e) => {
if (e.key === "Escape") {
Expand All @@ -351,6 +353,10 @@ const WrappedHandle = ({ position, children }) => {
document.removeEventListener("keydown", handler);
};
}, []);
// Remove the popper when the Canvas is moved.
React.useEffect(() => {
setAnchorEl(null);
}, [moved]);
return (
<>
<Handle
Expand All @@ -366,9 +372,12 @@ const WrappedHandle = ({ position, children }) => {
setAnchorEl(null);
}}
>
<ClickAwayContext.Provider value={() => setAnchorEl(null)}>
{children}
</ClickAwayContext.Provider>
<Box>
{/* Must wrap the ClickAwayContext.Provider in this <Box> elem for the click away functionality to work. */}
<ClickAwayContext.Provider value={() => setAnchorEl(null)}>
{children}
</ClickAwayContext.Provider>
</Box>
</ClickAwayListener>
</Popper>
</>
Expand Down
56 changes: 1 addition & 55 deletions ui/src/pages/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ import Divider from "@mui/material/Divider";
import Typography from "@mui/material/Typography";

import Paper from "@mui/material/Paper";
import {
Button,
ClickAwayListener,
Container,
Popper,
Stack,
} from "@mui/material";
import { Container, Stack } from "@mui/material";

import useMe from "../lib/me";
import React from "react";

export default function Profile() {
const { loading, me } = useMe();
Expand Down Expand Up @@ -41,8 +34,6 @@ export default function Profile() {
Name {me.firstname} {me.lastname}
</Box>
<Box> Email: {me.email}</Box>
<Box>CodePod version 0.4.6</Box>
<HandleButton />
</Stack>
</Paper>
<Divider />
Expand All @@ -51,48 +42,3 @@ export default function Profile() {
</Container>
);
}

const HandleButton = ({}) => {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(anchorEl ? null : event.currentTarget);
};
const open = Boolean(anchorEl);

const handler = React.useCallback((e) => {
console.log("keydown", e.key);
if (e.key === "Escape") {
setAnchorEl(null);
}
}, []);

React.useEffect(() => {
document.addEventListener("keydown", handler);

return () => {
document.removeEventListener("keydown", handler);
};
}, []);
return (
<>
<Button onClick={handleClick}>Hello</Button>

<Popper open={open} anchorEl={anchorEl} placement={"right"}>
<ClickAwayListener
onClickAway={() => {
console.log("click away!");
setAnchorEl(null);
}}
>
<Stack
sx={{ border: 1, p: 1, bgcolor: "background.paper" }}
spacing={1}
>
<Button variant="contained">Code</Button>
<Button variant="contained">Rich</Button>
</Stack>
</ClickAwayListener>
</Popper>
</>
);
};

0 comments on commit d452eba

Please sign in to comment.