๐ Feature Proposal
Add a new optional href
prop to Button
components:
/**
* The URL to link to when the button is clicked.
* If specified, will use an `a` element as the root of the component
*/
href?: string;
It would also probably be good to add some prop that gets used as the target
attribute on the underlying a
tag
Motivation
Many of our use cases of the Button
component end up essentially being links, changing the current URL in the onClick
handler:
<Button onClick={() => window.location.href='url'}>Button Text</Button>
// may also use `window.open('url', '_blank')` in some cases
This has downsides in that it doesn't benefit from any of the native browser functionality that actual links get when using the a
tag, such as being able to explicitly open a link in a new tab or window
One workaround to get the benefits of an a
tag, while also getting the style of the Button
component is to wrap the Button
in an a
:
<a href='url' target='_blank'>
<Button>Button Text</Button>
</a>
The downsides to this approach:
- The
a
tag may have styles that need to be suppressed (at the very least, disabling its outline
)
- The
a
and underlying button
elements are both focusable, causing keyboard nav to first focus the a
and then the button
next.
Example
<Button href='http://www.google.com'>Button Text</Button>
// DOM renders as:
<a href='http://www.google.com'>
/* ... children elements */
</a>
// while maintaining the style of the original `button` based implementation.