Skip to content

Commit

Permalink
Merge pull request #65 from joswhite/master
Browse files Browse the repository at this point in the history
Forward refs
  • Loading branch information
rafgraph authored Jul 14, 2020
2 parents 0682234 + 42191df commit 64b6abb
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,44 @@ function hashLinkScroll() {
}, 0);
}

export function genericHashLink(props, As) {
function handleClick(e) {
reset();
if (props.onClick) props.onClick(e);
if (typeof props.to === 'string') {
hashFragment = props.to
.split('#')
.slice(1)
.join('#');
} else if (
typeof props.to === 'object' &&
typeof props.to.hash === 'string'
) {
hashFragment = props.to.hash.replace('#', '');
}
if (hashFragment !== '') {
scrollFunction =
props.scroll ||
(el =>
props.smooth
? el.scrollIntoView({ behavior: "smooth" })
: el.scrollIntoView());
hashLinkScroll();
export function genericHashLink(As) {
return React.forwardRef((props, ref) => {
function handleClick(e) {
reset();
if (props.onClick) props.onClick(e);
if (typeof props.to === 'string') {
hashFragment = props.to
.split('#')
.slice(1)
.join('#');
} else if (
typeof props.to === 'object' &&
typeof props.to.hash === 'string'
) {
hashFragment = props.to.hash.replace('#', '');
}
if (hashFragment !== '') {
scrollFunction =
props.scroll ||
(el =>
props.smooth
? el.scrollIntoView({ behavior: "smooth" })
: el.scrollIntoView());
hashLinkScroll();
}
}
}
const { scroll, smooth, ...filteredProps } = props;
return (
<As {...filteredProps} onClick={handleClick}>
{props.children}
</As>
);
const { scroll, smooth, ...filteredProps } = props;
return (
<As {...filteredProps} onClick={handleClick} ref={ref}>
{props.children}
</As>
);
});
}

export function HashLink(props) {
return genericHashLink(props, Link);
}
export const HashLink = genericHashLink(Link);

export function NavHashLink(props) {
return genericHashLink(props, NavLink);
}
export const NavHashLink = genericHashLink(NavLink);

const propTypes = {
onClick: PropTypes.func,
Expand Down

0 comments on commit 64b6abb

Please sign in to comment.