-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathEuiCustomLink.jsx
More file actions
46 lines (36 loc) · 1.17 KB
/
EuiCustomLink.jsx
File metadata and controls
46 lines (36 loc) · 1.17 KB
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
44
45
46
// File name: "EuiCustomLink.js".
import React from "react";
import { EuiLink } from "@elastic/eui";
import { useNavigate, useHref } from "react-router-dom";
const isModifiedEvent = (event) =>
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
const isLeftClickEvent = (event) => event.button === 0;
const isTargetBlank = (event) => {
const target = event.target.getAttribute("target");
return target && target !== "_self";
};
export default function EuiCustomLink({ to, ...rest }) {
// This is the key!
const navigate = useNavigate();
function onClick(event) {
if (event.defaultPrevented) {
return;
}
// Let the browser handle links that open new tabs/windows
if (
isModifiedEvent(event) ||
!isLeftClickEvent(event) ||
isTargetBlank(event)
) {
return;
}
// Prevent regular link behavior, which causes a browser refresh.
event.preventDefault();
// Push the route to the history.
navigate(to);
}
// Generate the correct link href (with basename accounted for)
const href = useHref({ pathname: to });
const props = { ...rest, href, onClick };
return <EuiLink {...props} />;
}