handle offset attribute and children

This commit is contained in:
Ron Rennick 2019-08-23 15:34:44 -03:00
parent fadb20c1bc
commit 9d2d085aee
2 changed files with 27 additions and 6 deletions

View File

@ -190,8 +190,13 @@ class Settings extends Component {
</Button> </Button>
</div> </div>
</div> </div>
{ query.import === 'true' ? <ScrollTo /> : '' } { query.import === 'true' ? (
<HistoricalData createNotice={ createNotice } /> <ScrollTo offset="-56">
<HistoricalData createNotice={ createNotice } />
</ScrollTo>
) : (
<HistoricalData createNotice={ createNotice } />
) }
</Fragment> </Fragment>
); );
} }

View File

@ -3,10 +3,11 @@
* External dependencies * External dependencies
*/ */
import { Component, createRef } from '@wordpress/element'; import { Component, createRef } from '@wordpress/element';
import PropTypes from 'prop-types';
class ScrollTo extends Component { class ScrollTo extends Component {
constructor() { constructor( props ) {
super(); super( props );
this.scrollTo = this.scrollTo.bind( this ); this.scrollTo = this.scrollTo.bind( this );
} }
@ -15,19 +16,34 @@ class ScrollTo extends Component {
} }
scrollTo() { scrollTo() {
const { offset } = this.props;
if ( this.ref.current && this.ref.current.offsetTop ) { if ( this.ref.current && this.ref.current.offsetTop ) {
window.scrollTo( 0, this.ref.current.offsetTop ); window.scrollTo( 0, this.ref.current.offsetTop + parseInt( offset ) );
} else { } else {
setTimeout( this.scrollTo, 250 ); setTimeout( this.scrollTo, 250 );
} }
} }
render() { render() {
const { children } = this.props;
this.ref = createRef(); this.ref = createRef();
return ( return (
<span ref={ this.ref }></span> <span ref={ this.ref }>
{ children }
</span>
); );
} }
} }
ScrollTo.propTypes = {
/**
* The offset from the top of the component.
*/
offset: PropTypes.string,
};
ScrollTo.defaultProps = {
offset: '0',
};
export default ScrollTo; export default ScrollTo;