about summary refs log tree commit diff
path: root/src/view/com/util/WebAuxClickWrapper.tsx
blob: 8105a8518fa358d975ee5042dae570854097ac02 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import React from 'react'
import {Platform} from 'react-native'

const onMouseUp = (e: React.MouseEvent & {target: HTMLElement}) => {
  // Only handle whenever it is the middle button
  if (e.button !== 1 || e.target.closest('a') || e.target.tagName === 'A') {
    return
  }

  e.target.dispatchEvent(
    new MouseEvent('click', {metaKey: true, bubbles: true}),
  )
}

const onMouseDown = (e: React.MouseEvent) => {
  // Prevents the middle click scroll from enabling
  if (e.button !== 1) return
  e.preventDefault()
}

export function WebAuxClickWrapper({children}: React.PropsWithChildren<{}>) {
  if (Platform.OS !== 'web') return children

  return (
    // @ts-ignore web only
    <div onMouseDown={onMouseDown} onMouseUp={onMouseUp}>
      {children}
    </div>
  )
}