File size: 1,587 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { MouseEvent, MouseEventHandler } from 'react';
import React, { useMemo, forwardRef } from 'react';
import { Link as DumiLink, useLocation, useAppData, useNavigate } from 'dumi';

export interface LinkProps {
  to: string | { pathname?: string; search?: string; hash?: string };
  style?: React.CSSProperties;
  className?: string;
  onClick?: MouseEventHandler;
  component?: React.ComponentType<any>;
  children?: React.ReactNode;
}

const Link = forwardRef<HTMLAnchorElement, React.PropsWithChildren<LinkProps>>(
  ({ component, children, to, ...rest }, ref) => {
    const { pathname } = useLocation();
    const { preloadRoute } = useAppData();
    const navigate = useNavigate();
    const href = useMemo<string>(() => {
      if (typeof to === 'object') {
        return `${to.pathname || pathname}${to.search || ''}${to.hash || ''}`;
      }
      return to;
    }, [to]);
    const onClick = (e: MouseEvent<HTMLAnchorElement>) => {
      rest.onClick?.(e);
      if (!href?.startsWith('http')) {
        // Should support open in new tab
        if (!e.metaKey && !e.ctrlKey && !e.shiftKey) {
          e.preventDefault();
          navigate(href);
        }
      }
    };
    if (component) {
      return React.createElement(
        component,
        {
          ...rest,
          ref,
          href,
          onClick,
          onMouseEnter: () => preloadRoute?.(href),
        },
        children,
      );
    }
    return (
      <DumiLink ref={ref} {...rest} to={href} prefetch>
        {children}
      </DumiLink>
    );
  },
);

export default Link;