aboutsummaryrefslogtreecommitdiffstats
path: root/packages/react-shared/src/components/link.tsx
blob: 7425b936521a50cdff5687db3e400a31650f8625 (plain) (blame)
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
import * as _ from 'lodash';
import * as React from 'react';
import { Link as ReactRounterLink } from 'react-router-dom';
import { Link as ScrollLink } from 'react-scroll';

import { LinkType } from '../types';
import { constants } from '../utils/constants';

export interface LinkProps {
    to: string;
    type?: LinkType;
    shouldOpenInNewTab?: boolean;
    style?: React.CSSProperties;
    className?: string;
    onMouseOver?: (event: React.MouseEvent<HTMLElement>) => void;
    onMouseLeave?: (event: React.MouseEvent<HTMLElement>) => void;
    onMouseEnter?: (event: React.MouseEvent<HTMLElement>) => void;
    containerId?: string;
}

/**
 * A generic link component which let's the developer render internal & external links, and their associated
 * behaviors with a single link component. Many times we want a menu including both internal & external links
 * and this abstracts away the differences of rendering both types of links.
 */
export const Link: React.StatelessComponent<LinkProps> = ({
    style,
    className,
    type,
    to,
    shouldOpenInNewTab,
    children,
    onMouseOver,
    onMouseLeave,
    onMouseEnter,
    containerId,
}) => {
    const styleWithDefault = {
        textDecoration: 'none',
        ...style,
    };

    switch (type) {
        case LinkType.External:
            return (
                <a
                    target={shouldOpenInNewTab ? '_blank' : ''}
                    className={className}
                    style={styleWithDefault}
                    href={to}
                    onMouseOver={onMouseOver}
                    onMouseEnter={onMouseEnter}
                    onMouseLeave={onMouseLeave}
                >
                    {children}
                </a>
            );
        case LinkType.ReactRoute:
            return (
                <ReactRounterLink
                    to={to}
                    className={className}
                    style={styleWithDefault}
                    target={shouldOpenInNewTab ? '_blank' : ''}
                    onMouseOver={onMouseOver}
                    onMouseEnter={onMouseEnter}
                    onMouseLeave={onMouseLeave}
                >
                    {children}
                </ReactRounterLink>
            );
        case LinkType.ReactScroll:
            return (
                <ScrollLink
                    to={to}
                    offset={0}
                    hashSpy={true}
                    duration={constants.DOCS_SCROLL_DURATION_MS}
                    containerId={containerId}
                >
                    {children}
                </ScrollLink>
            );
        default:
            throw new Error(`Unrecognized LinkType: ${type}`);
    }
};

Link.defaultProps = {
    type: LinkType.ReactRoute,
    shouldOpenInNewTab: false,
    style: {},
    className: '',
    onMouseOver: _.noop.bind(_),
    onMouseLeave: _.noop.bind(_),
    onMouseEnter: _.noop.bind(_),
    containerId: constants.DOCS_CONTAINER_ID,
};

Link.displayName = 'Link';