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

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

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

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

export interface AnchorTitleState {}

const styles: Styles = {
    h1: {
        fontSize: '1.875em',
    },
    h2: {
        fontSize: '1.5em',
        fontWeight: 400,
    },
    h3: {
        fontSize: '1.17em',
    },
};

interface AnchorIconProps {
    shouldShowAnchor: boolean;
}

const AnchorIcon =
    styled.i <
    AnchorIconProps >
    `
            opacity: ${props => (props.shouldShowAnchor ? 1 : 0)};
            &:hover {
                opacity: ${props => (props.shouldShowAnchor ? 0.6 : 0)};
            }
            font-size: 20px;
            transform: rotate(45deg);
            cursor: pointer;
        `;

export class AnchorTitle extends React.Component<AnchorTitleProps, AnchorTitleState> {
    public static defaultProps: Partial<AnchorTitleProps> = {
        isDisabled: false,
    };
    public render(): React.ReactNode {
        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, color: colors.darkestGrey }}>
                    {this.props.title}
                </div>
                {!this.props.isDisabled && (
                    <ScrollLink
                        to={this.props.id}
                        hashSpy={true}
                        offset={headerSizeToScrollOffset[this.props.headerSize]}
                        duration={constants.DOCS_SCROLL_DURATION_MS}
                        containerId={constants.SCROLL_CONTAINER_ID}
                    >
                        <AnchorIcon className="zmdi zmdi-link" shouldShowAnchor={this.props.shouldShowAnchor} />
                    </ScrollLink>
                )}
            </div>
        );
    }
}