All files / packages/design-system/shared/components DocHeaderAnchor.jsx

14.28% Statements 1/7
0% Branches 0/2
100% Functions 0/0
14.28% Lines 1/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                            1x            
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
import React from 'react';
import PropTypes from 'prop-types';
 
class DocHeaderAnchor extends React.Component {
  constructor(props) {
    super(props);
    this.anchorRef = React.createRef();
  }
 
  componentDidMount() {
    // deep links to doc headers doesn't work like it should because doc blocks are loaded after the DOM
    // to get deep links to work, detect if the hash in the URL matches the id on the anchor
    // if so scroll to this anchor
    if (window.location.hash === `#${this.props.id}`)
      this.anchorRef.current.scrollIntoView(true);
  }
 
  render() {
    const { type, id } = this.props;
    return (
      <a
        ref={this.anchorRef}
        id={id}
        href={`#${id}`}
        aria-label="Jump to section"
        className="doc doc-anchor"
        data-level={type[1]}
        {...this.props}
      >
        #
      </a>
    );
  }
}
 
DocHeaderAnchor.propTypes = {
  is: PropTypes.string,
  type: PropTypes.array
};
 
export default DocHeaderAnchor;