Skip to content

Link creation menu fix #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const LinkToolbarButton = (props: HyperlinkButtonProps) => {
const [creationMenu, setCreationMenu] = useState<any>();
const [creationMenuOpen, setCreationMenuOpen] = useState(false);

const ref = useRef<HTMLButtonElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
const menuRef = useRef<HTMLDivElement>(null);

// TODO: review code; does this pattern still make sense?
const updateCreationMenu = useCallback(() => {
Expand All @@ -33,17 +34,22 @@ export const LinkToolbarButton = (props: HyperlinkButtonProps) => {
props.setHyperlink(url, text);
setCreationMenuOpen(false);
}}
ref={menuRef}
/>
);
}, [props]);

const handleClick = useCallback(
(event: MouseEvent) => {
if (ref.current?.contains(event.target as HTMLElement)) {
if (buttonRef.current?.contains(event.target as HTMLElement)) {
setCreationMenuOpen(!creationMenuOpen);
return;
}

if (menuRef.current?.contains(event.target as HTMLElement)) {
return;
}

setCreationMenuOpen(false);
},
[creationMenuOpen]
Expand All @@ -67,7 +73,7 @@ export const LinkToolbarButton = (props: HyperlinkButtonProps) => {
mainTooltip={props.mainTooltip}
secondaryTooltip={props.secondaryTooltip}
icon={props.icon}
ref={ref}
ref={buttonRef}
/>
</Tippy>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createStyles, Stack } from "@mantine/core";
import { useState } from "react";
import { forwardRef, useState } from "react";
import { RiLink, RiText } from "react-icons/ri";
import { EditHyperlinkMenuItem } from "./EditHyperlinkMenuItem";

Expand All @@ -13,7 +13,10 @@ export type EditHyperlinkMenuProps = {
* Menu which opens when editing an existing hyperlink or creating a new one.
* Provides input fields for setting the hyperlink URL and title.
*/
export const EditHyperlinkMenu = (props: EditHyperlinkMenuProps) => {
export const EditHyperlinkMenu = forwardRef<
HTMLDivElement,
EditHyperlinkMenuProps
>((props, ref) => {
const { classes } = createStyles({ root: {} })(undefined, {
name: "EditHyperlinkMenu",
});
Expand All @@ -22,7 +25,7 @@ export const EditHyperlinkMenu = (props: EditHyperlinkMenuProps) => {
const [title, setTitle] = useState(props.text);

return (
<Stack className={classes.root}>
<Stack className={classes.root} ref={ref}>
<EditHyperlinkMenuItem
icon={RiLink}
mainIconTooltip={"Edit URL"}
Expand All @@ -42,4 +45,4 @@ export const EditHyperlinkMenu = (props: EditHyperlinkMenuProps) => {
/>
</Stack>
);
};
});