···11-// does nothing in native
22-export const useAuxClick = () => {}
-43
src/lib/hooks/useAuxClick.web.ts
···11-import {useEffect} from 'react'
22-33-// This is the handler for the middle mouse button click on the feed.
44-// Normally, we would do this via `onAuxClick` handler on each link element
55-// However, that handler is not supported on react-native-web and there are some
66-// discrepancies between various browsers (i.e: safari doesn't trigger it and routes through click event)
77-// So, this temporary alternative is meant to bridge the gap in an efficient way until the support improves.
88-export const useAuxClick = () => {
99- const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
1010- useEffect(() => {
1111- // On the web, it should always be there but in case it gets accidentally included in native builds
1212- const wrapperEl = document?.body
1313-1414- // Safari already handles auxclick event as click+metaKey so we need to avoid doing this there in case it becomes recursive
1515- if (wrapperEl && !isSafari) {
1616- const handleAuxClick = (e: MouseEvent & {target: HTMLElement}) => {
1717- // Only handle the middle mouse button click
1818- // Only handle if the clicked element itself or one of its ancestors is a link
1919- if (
2020- e.button !== 1 ||
2121- e.target.closest('a') ||
2222- e.target.tagName === 'A'
2323- ) {
2424- return
2525- }
2626-2727- // On the original element, trigger a click event with metaKey set to true so that it triggers
2828- // the browser's default behavior of opening the link in a new tab
2929- e.target.dispatchEvent(
3030- new MouseEvent('click', {metaKey: true, bubbles: true}),
3131- )
3232- }
3333-3434- // @ts-ignore For web only
3535- wrapperEl.addEventListener('auxclick', handleAuxClick)
3636-3737- return () => {
3838- // @ts-ignore For web only
3939- wrapperEl?.removeEventListener('auxclick', handleAuxClick)
4040- }
4141- }
4242- }, [isSafari])
4343-}