Sitecore / jss

Software development kit for JavaScript developers building web applications with Sitecore Experience Platform
https://jss.sitecore.com
Apache License 2.0
261 stars 275 forks source link

Link component should forward ref #1053

Closed pzi closed 2 years ago

pzi commented 2 years ago

Is your feature request related to a problem? Please describe.

As JSS exports a shared Link component it should accept a ref prop in order to access their DOM nodes and for managing focus, selection, or animations.

[...] it can be useful for some kinds of components, especially in reusable component libraries.

Source: https://reactjs.org/docs/forwarding-refs.html

Describe the solution you'd like

Set up the Link component using React.forwardRef to allow refs to be passed to the underlying anchor. This change would have to be made in nextjs and react jss packages.

https://github.com/Sitecore/jss/blob/v20.0.3/packages/sitecore-jss-nextjs/src/components/Link.tsx#L20 https://github.com/Sitecore/jss/blob/v20.0.3/packages/sitecore-jss-react/src/components/Link.tsx#L41

Additional information

Not having a ref prop means consumers don't have access to the underlying DOM components.

Unsure if you would be happy to release it as a minor version given this note in the React docs:

Note for component library maintainers

When you start using forwardRef in a component library, you should treat it as a breaking change and release a new major version of your library. This is because your library likely has an observably different behavior (such as what refs get assigned to, and what types are exported), and this can break apps and other libraries that depend on the old behavior.

Additionally, I assume there will be other components that would benefit from having a ref prop.

illiakovalenko commented 2 years ago

Added in #1080, will be released in the next major version