[ENG-1663] Prevent middle-click opening new tab (#2168)

Update Link.tsx
This commit is contained in:
ameer2468 2024-03-06 03:37:31 +03:00 committed by GitHub
parent c11ecd2457
commit 55d2ec7a6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,6 @@
import { cva } from 'class-variance-authority';
import clsx from 'clsx';
import { forwardRef, PropsWithChildren } from 'react';
import { forwardRef, PropsWithChildren, useEffect } from 'react';
import { NavLink, NavLinkProps } from 'react-router-dom';
import { useOperatingSystem } from '~/hooks/useOperatingSystem';
import { usePlatform } from '~/util/Platform';
@ -31,6 +31,17 @@ const Link = forwardRef<
const os = useOperatingSystem();
const { platform } = usePlatform();
//prevents middle click from opening new tab
useEffect(() => {
const handleClick = (e: MouseEvent) => {
if (e.button === 1) e.preventDefault();
};
document.addEventListener('auxclick', handleClick);
return () => {
document.removeEventListener('auxclick', handleClick);
};
}, []);
return (
<NavLink
onClick={(e) => {