This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Link.js
51 lines (47 loc) · 1.36 KB
/
Link.js
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
47
48
49
50
51
import React from "react";
import {navigate, getBasepath} from "./router";
/**
* Accepts HTML `a`-tag properties, requiring `href` and optionally
* `onClick`, which are appropriately wrapped to allow other
* frameworks to be used for creating `hookrouter` navigatable links.
*
* If `onClick` is supplied, then the navigation will happen before
* the supplied `onClick` action!
*
* @example
*
* <MyFrameworkLink what="ever" {...useLink({ href: '/' })}>
* Link text
* </MyFrameworkLink>
*
* @param {Object} props Requires `href`. `onClick` is optional.
*/
export const setLinkProps = (props) => {
const onClick = (e) => {
if (!e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey && props.target !== "_blank")) {
e.preventDefault(); // prevent the link from actually navigating
navigate(e.currentTarget.href);
}
if (props.onClick) {
props.onClick(e);
}
};
const href =
props.href.substr(0, 1) === '/'
? getBasepath() + props.href
: props.href;
return {...props, href, onClick};
};
/**
* Accepts standard HTML `a`-tag properties. `href` and, optionally,
* `onClick` are used to create links that work with `hookrouter`.
*
* @example
*
* <A href="/" target="_blank">
* Home
* </A>
*
* @param {Object} props Requires `href`. `onClick` is optional
*/
export const A = (props) => <a {...setLinkProps(props)} />;