summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-hints/src/index.js
blob: 2ee8a1faa63708cf8831dc3cd72a4ebaff813b79 (plain)
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
const classNames = require('classnames');
const GitBook = require('gitbook-core');
const { React } = GitBook;

const STYLE_TO_ICON = {
    info:    'info-circle',
    tip:     'question',
    success: 'check-circle',
    danger:  'exclamation-circle',
    warning: 'exclamation-triangle'
};

const HintAlert = React.createClass({
    propTypes: {
        icon:     React.PropTypes.string,
        style:    React.PropTypes.string,
        children: React.PropTypes.node
    },

    render() {
        const { children, style, icon } = this.props;
        const className = classNames(
            'HintAlert', `HintAlert-Style-${style}`,
            'alert', `alert-${style}`
        );

        return (
            <div className={className}>
                <GitBook.ImportCSS href="gitbook/hints/plugin.css" />
                <div className="HintAlert-Icon">
                    <GitBook.Icon id={icon || STYLE_TO_ICON[style]} />
                </div>
                <div className="HintAlert-Content">
                    {children}
                </div>
            </div>
        );
    }
});

module.exports = GitBook.createPlugin({
    activate: (dispatch, getState, { Components }) => {
        dispatch(Components.registerComponent(HintAlert, { role: 'block:hint' }));
    }
});