brohlson / gatsby-plugin-anchor-links

⚓ Gatsby plugin for using smooth scroll anchor links with a Gatsby Link component
https://www.npmjs.com/package/gatsby-plugin-anchor-links
MIT License
68 stars 22 forks source link

add option to provide custom onClick #20

Closed brijesh-pant closed 4 years ago

brijesh-pant commented 4 years ago

It's not possible right now to set a custom onClick event handler on the AnchorLink component, hence become difficult to make changes related to components (such as setting state variables). Would be nice to add such kinda functionality.

ansh commented 4 years ago

Wouldn't this just consist of passing down the onClick prop? @brijesh-pant

brijesh-pant commented 4 years ago

Yes it’d be but as of now there isn’t an option to do that provided the AnchorLink

ansh commented 4 years ago

Would you not want to just pass down all props like ...props ?

brijesh-pant commented 4 years ago

@ansh Yes, it can be done via. ...props as well, it depends what we want to customize. For me, it's just the click handler as of now. https://stackoverflow.com/questions/63119384/gatsbyjs-burger-menu-close-menu-on-anchorlink-click Take a look at this https://github.com/brohlson/gatsby-plugin-anchor-links/blob/master/src/components/AnchorLink.js#L20 We'd not want the existing click handler to be overridden by the onClick prop we send, hence we need this piece of code to be customized so that a custom click handler also runs before we do the URL switching logic.

ansh commented 4 years ago

Makes sense. I see where the problem is. Let me know if you think of an elegant solution and need help implementing.

brijesh-pant commented 4 years ago

@ansh I can just raise a PR for this if the author @brohlson agrees...

rindPHI commented 4 years ago

I'm also interested in this for building a collapsible menu!

brohlson commented 4 years ago

FWIW - I usually write a useEffect function which watches the location in order to run side effects.

Naming this onClick seems confusing to me.

I will add an onAnchorLinkClick prop to the next version. #25