1 | const React = require('react');
|
2 | const PropTypes = require('prop-types');
|
3 |
|
4 | function generateHeadingId(e) {
|
5 | if (typeof e === 'object') {
|
6 | return generateHeadingId(e.props.children[0]);
|
7 | }
|
8 | return e.toLowerCase().replace(/[^\w]+/g, '-');
|
9 | }
|
10 |
|
11 | function Heading(props) {
|
12 |
|
13 |
|
14 |
|
15 | if (typeof props.children === 'undefined') {
|
16 | return null;
|
17 | }
|
18 |
|
19 | const id = `section-${generateHeadingId(props.children[0])}`;
|
20 | return React.createElement(props.level, { className: 'header-scroll' }, [
|
21 | <div key={`anchor-${id}`} className="anchor waypoint" id={id} />,
|
22 | ...props.children,
|
23 |
|
24 | <a key={`anchor-icon-${id}`} className="fa fa-anchor" href={`#${id}`} />,
|
25 | ]);
|
26 | }
|
27 |
|
28 | function createHeading(level) {
|
29 |
|
30 | return props => <Heading level={level} {...props} />;
|
31 | }
|
32 |
|
33 | Heading.propTypes = {
|
34 | children: PropTypes.arrayOf(PropTypes.string).isRequired,
|
35 | level: PropTypes.string.isRequired,
|
36 | };
|
37 |
|
38 | module.exports = level => createHeading(level);
|
39 |
|
\ | No newline at end of file |