Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | 31x 111x 31x 111x 31x 21x 31x 31x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 31x | // @flow strict
import React, {
useRef,
useEffect,
useContext,
useImperativeHandle,
} from 'react';
import type { ElementType } from 'react';
// $FlowFixMe
import { __RouterContext as RouterContext } from 'react-router'; // eslint-disable-line
// $FlowFixMe
import { createLocation } from 'history'; // eslint-disable-line
export const resolveToLocation = (
to: string | ((currentLocation: string) => string),
currentLocation: string,
) => (typeof to === 'function' ? to(currentLocation) : to);
export const normalizeToLocation = (
to: string | ((currentLocation: string) => string),
currentLocation: string,
) => {
return typeof to === 'string'
? createLocation(to, null, null, currentLocation)
: to;
};
function isModifiedEvent(event) {
// $FlowFixMe
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
}
type LinkAnchorProps = {
navigate: () => void,
onClick: (evt: SyntheticEvent<HTMLAnchorElement>) => void,
};
const LinkAnchor = React.forwardRef(({ ...rest }: LinkAnchorProps, ref) => {
return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions,jsx-a11y/click-events-have-key-events,jsx-a11y/anchor-has-content
<a {...(rest: mixed)} ref={ref} />
);
});
LinkAnchor.displayName = 'LinkAnchor';
type LinkProps = {
component?: ElementType,
replace?: boolean,
to?: string,
};
/**
* The public API for rendering a history-aware <a>.
*/
const Link = React.forwardRef<LinkProps, _>(
(
{
component: Component = LinkAnchor,
replace,
to = undefined,
...rest
}: LinkProps,
ref,
) => {
const rootRef = useRef();
const { history, location } = useContext(RouterContext);
useEffect(() => {
const root = rootRef.current;
const navigate = (event: SyntheticEvent<HTMLAnchorElement>) => {
// $FlowFixMe
const { target } = rest;
if (
!event.defaultPrevented && // onClick prevented default
// $FlowFixMe
event.button === 0 && // ignore everything but left clicks
(!target || target === '_self') && // let browser handle "target=_blank" etc.
!isModifiedEvent(event) // ignore clicks with modifier keys
) {
event.preventDefault();
const toLocation = resolveToLocation(to, location);
const method = replace === true ? history.replace : history.push;
method(toLocation);
}
};
Eif (root) {
root.addEventListener('click', navigate);
}
return () => {
Eif (root) {
root.removeEventListener('click', navigate);
}
};
}, [history.push, history.replace, location, replace, rest, to]);
useImperativeHandle(ref, () => rootRef.current, []);
const normalizedLocation = normalizeToLocation(
resolveToLocation(to, location),
location,
);
const href = normalizedLocation
? history.createHref(normalizedLocation)
: '';
return <Component {...(rest: mixed)} href={href} ref={rootRef} />;
},
);
Link.displayName = 'Link';
export default Link;
|