mirror of
https://github.com/NoCrypt/migu.git
synced 2026-03-11 17:45:32 +00:00
38 lines
1 KiB
JavaScript
38 lines
1 KiB
JavaScript
let lastTapElement = null
|
|
|
|
const noop = _ => {}
|
|
|
|
document.addEventListener('pointerup', () => {
|
|
lastTapElement?.dispatchEvent(new Event('custom-pointerleave'))
|
|
lastTapElement = null
|
|
})
|
|
|
|
export function click (node, cb = noop) {
|
|
node.tabIndex = 0
|
|
node.role = 'button'
|
|
node.addEventListener('pointerup', e => {
|
|
e.stopPropagation()
|
|
cb(e)
|
|
})
|
|
node.addEventListener('keydown', e => { if (e.key === 'Enter') cb(e) })
|
|
}
|
|
|
|
export function hoverClick (node, cb = noop) {
|
|
node.tabIndex = 0
|
|
node.role = 'button'
|
|
node.addEventListener('pointerup', e => {
|
|
if (e.pointerType === 'mouse') return cb(e)
|
|
e.stopPropagation()
|
|
lastTapElement?.dispatchEvent(new Event('custom-pointerleave'))
|
|
if (lastTapElement === node) {
|
|
lastTapElement = null
|
|
cb(e)
|
|
} else {
|
|
lastTapElement = node
|
|
}
|
|
})
|
|
node.addEventListener('keydown', e => { if (e.key === 'Enter') cb(e) })
|
|
node.addEventListener('pointerleave', ({ pointerType }) => {
|
|
if (pointerType === 'mouse') node.dispatchEvent(new Event('custom-pointerleave'))
|
|
})
|
|
}
|