Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor element specific properties for Button #108

Open
MrSquaare opened this issue Apr 25, 2022 · 3 comments
Open

Anchor element specific properties for Button #108

MrSquaare opened this issue Apr 25, 2022 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@MrSquaare
Copy link

Currently, if the href property is set to a Button component, the anchor element is used.
However, it is not possible to use anchor specific properties like rel or target.

A solution to multi-element components is to propose a property (react-bootstrap named it as, reactstrap named it tag) allowing to specify the type of element (in the case of the Button component, it would be either button or a). It is then possible to determine from the latter the properties specific to an element.

@benjitrosch
Copy link
Collaborator

Hi @MrSquaare,

We previously attempted button/anchor polymorphism (modelled after reactstrap specifically) but I removed it in #72. The reason being that it caused type safety issues.

We may attempt it again in the future, and if you would like to open a PR it would be very welcome!

Personally, I would rather keep the code as clean and type safe as possible, even if it comes with a small loss of flexibility. My thinking when removing it was that a user who uses a <Button /> 9 times out of 10 is looking for a <button> rather than an <a> so I prioritized the button props working correctly. Worst case scenario they can just re-style a <Link /> or <a> with the btn class manually. If there's a type safe solution out there for button/anchor polymorphism that's clean and doesn't give us a "less of both" situation, then I'm all for it!

@MrSquaare
Copy link
Author

Hello @benjitrosch,

I didn't know there was an attempt before.
Indeed, polymorphism is somewhat complicated for React components, especially when it comes to ref forwarding.

I tried in the evening to find some solutions, but I always have compromises.

I found a blog post (that you may have already consulted) that I adapted for the Button component and that seemed to be the best: https://www.benmvp.com/blog/forwarding-refs-polymorphic-react-component-typescript/. The only problem is that TypeScript seems to do a lot of operations to deduce the valid types.

What do you think about it?

@rvramesh
Copy link

rvramesh commented May 7, 2022

My 2 cents. There are 3 usescases I could think of.

  1. Make a button look like link. (eg. Expand All or Collapse All). In this case there is no need for rel or target etc. The current Button I believe handles this case

  2. In second case, you genuinely need a link and may be can we consider Link

  3. For third case of a Link to look like Button. Can a Form be used along with the Button and specify the target in the form?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants