Link - show custom popover UI with floating-ui
#4097
Unanswered
fbolcic
asked this question in
General Questions
Replies: 3 comments 3 replies
-
|
Bumping this discussion since we are still stuck with this feature. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Any ideas?) I need to implement exactly the same functionality |
Beta Was this translation helpful? Give feedback.
3 replies
-
|
Hi, import {
computePosition,
flip,
shift,
offset,
autoUpdate,
} from "@floating-ui/dom";
export function LinkHoverElement({ editor }: { editor: Editor }) {
const [isOpen, setIsOpen] = useState<boolean>(false);
const [anchorElement, setAnchorElement] = useState<HTMLAnchorElement | null>(
null,
);
const tooltipRef = useRef<HTMLDivElement | null>(null);
const cleanupRef = useRef<(() => void) | null>(null);
useEffect(() => {
const handleMouseClick = (e: Event) => {
const a = (e.target as HTMLElement).closest("a");
if (tooltipRef.current && a) {
if (cleanupRef.current) cleanupRef.current();
setAnchorElement(a);
cleanupRef.current = autoUpdate(a, tooltipRef.current, () => {
computePosition(a, tooltipRef.current as HTMLDivElement, {
placement: "bottom",
middleware: [offset(6), flip(), shift({ padding: 10 })],
}).then(({ x, y }) => {
Object.assign(tooltipRef.current?.style || {}, {
left: `${x}px`,
top: `${y}px`,
});
});
});
setIsOpen(true);
} else {
setIsOpen(false);
if (cleanupRef.current) {
cleanupRef.current();
cleanupRef.current = null;
}
}
};
const dom = editor.view.dom;
dom.addEventListener("click", handleMouseClick);
return () => {
dom.removeEventListener("click", handleMouseClick);
};
});
return (
<div
ref={tooltipRef}
className={`p-4 w-auto absolute gap-4 items-center top-0 left-0 ${isOpen ? "flex" : "hidden"} z-50`}
>
<a href={anchorElement?.href}>
{anchorElement?.href}
</a>
</div> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
First of all, thanks for an amazing library - we have just recently switched to
tiptapand using it is just a breeze!We would like to extend the native link element with a custom popover UI for inputting link
href. We are using thefloating-uilibrary in our app and would like to use it for this problem also.I have seen some implementations that use the
BubbleMenubut it does not seem like a perfect fit for us. We need to show this popover UI below the link component when the user:Here is a mockup of the UI for adding the link:

We also have a variation for editing an existing link:

Currently, we have managed to build the logic which utilises the
window.prompt(as described in the docs). Our main problem here is that for our use case we need to:floating-uito itfloating-uiAnd the approach described in the docs is more of a JS than React approach of doing things.
Do you have any ideas and examples on how to achieve this?
Any help would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions