generated from alpine-collective/plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinks.ts
43 lines (38 loc) · 1 KB
/
links.ts
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
31
32
33
34
35
36
37
38
39
40
41
42
43
import { type PineconeRouter } from './router'
import { settings } from './settings'
/**
* Add a handler to click events on valid links
*/
export const handleClicks = (Router: PineconeRouter) => {
window.document.body.addEventListener('click', (e: MouseEvent) => {
// ignore modified clicks or non-primary buttons
if (
e.ctrlKey ||
e.metaKey ||
e.altKey ||
e.shiftKey ||
e.button ||
e.defaultPrevented
) {
return
}
// find closest anchor element
const node = (e.target as HTMLElement).closest('a')
if (!node) return
// skip if link shouldn't be intercepted
if (
(settings.handleClicks === false && !node.hasAttribute('x-link')) ||
node.hasAttribute('data-native') ||
node.hasAttribute('native')
) {
return
}
const href = node.getAttribute('href')
const target = node.getAttribute('target')
// only handle internal links without special targets
if (href && (!target || /^_?self$/i.test(target))) {
Router.navigate(href)
e.preventDefault()
}
})
}