aboutsummaryrefslogtreecommitdiffstats
path: root/packages/react-shared/src/components/anchor_title.tsx
blob: 451ba172956aad6b831bc0df2882d8bf78a8c6ae (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
import * as React from 'react';
import { Link as ScrollLink } from 'react-scroll';

import { HeaderSizes, Styles } from '../types';
import { constants } from '../utils/constants';
import { utils } from '../utils/utils';

const headerSizeToScrollOffset: { [headerSize: string]: number } = {
    h2: -20,
    h3: 0,
};

export interface AnchorTitleProps {
    title: string | React.ReactNode;
    id: string;
    headerSize: HeaderSizes;
    shouldShowAnchor: boolean;
}

export interface AnchorTitleState {
    isHovering: boolean;
}

const styles: Styles = {
    anchor: {
        fontSize: 20,
        transform: 'rotate(45deg)',
        cursor: 'pointer',
    },
    h1: {
        fontSize: '1.8em',
    },
    h2: {
        fontSize: '1.5em',
        fontWeight: 400,
    },
    h3: {
        fontSize: '1.17em',
    },
};

export class AnchorTitle extends React.Component<AnchorTitleProps, AnchorTitleState> {
    constructor(props: AnchorTitleProps) {
        super(props);
        this.state = {
            isHovering: false,
        };
    }
    public render(): React.ReactNode {
        let opacity = 0;
        if (this.props.shouldShowAnchor) {
            opacity = this.state.isHovering ? 0.6 : 1;
        }
        return (
            <div
                className="relative flex"
                style={
                    {
                        ...styles[this.props.headerSize],
                        fontWeight: 'bold',
                        display: 'block',
                        WebkitMarginStart: 0,
                        WebkitMarginEnd: 0,
                    } as any
                }
            >
                <div className="inline-block" style={{ paddingRight: 4 }}>
                    {this.props.title}
                </div>
                <ScrollLink
                    to={this.props.id}
                    hashSpy={true}
                    offset={headerSizeToScrollOffset[this.props.headerSize]}
                    duration={constants.DOCS_SCROLL_DURATION_MS}
                    containerId={constants.DOCS_CONTAINER_ID}
                >
                    <i
                        className="zmdi zmdi-link"
                        style={{ ...styles.anchor, opacity }}
                        onMouseOver={this._setHoverState.bind(this, true)}
                        onMouseOut={this._setHoverState.bind(this, false)}
                    />
                </ScrollLink>
            </div>
        );
    }
    private _setHoverState(isHovering: boolean): void {
        this.setState({
            isHovering,
        });
    }
}