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

Buttons that are links #31

Closed
cbenz opened this issue Oct 5, 2019 · 4 comments
Closed

Buttons that are links #31

cbenz opened this issue Oct 5, 2019 · 4 comments

Comments

@cbenz
Copy link

cbenz commented Oct 5, 2019

I'd like to create a link to an URL, rendered as a button.

For now if I create a <Button on:click={() => goto("https://.../")> then the user will not be able to see the target of the link, disabling the possibility of doing ctrl-click to open in a new tab, etc.

So I'd like to have a <Button> that would accept a href (or to) property, and would be rendered as a <a href="...". I'd like also to setup a target="_blank" attribute if needed.

Is this need doable with another approach? Is it something planned from your side @matyunya?

Related work in other libraries:

  • vuetify <Button> accepts a to property which generates links
    • remark: it creates links compatible with vue-router but in sapper this would not be necessary as sapper automatically detects links and triggers navigation events
@BogdanDarius
Copy link
Contributor

BogdanDarius commented Oct 5, 2019

Not the best way but you could do something like this:

<a href="http://website.com">
  <Button>Visit us!</Button>
</a>

There is no way to to create a dynamic element (see sveltejs/svelte#2324).
A solution would be to have another property to the button like type that can be either button or a. That would require changes to the component like:

{#if type === "button"}
...make a button
{:else if type === "a"}
...make an anchor
{/if}

@cbenz
Copy link
Author

cbenz commented Oct 7, 2019

Thanks @BogdanDarius, that worked perfectly.
I'm going to use a <ButtonLink> component in my project.

When dynamic elements will exist, I think that Smelte will make use of them for implementing button links.

@matyunya
Copy link
Owner

matyunya commented Oct 7, 2019

Thanks! Added in 0.1.6.

Addind href now creates a button wrapped in anchor, it receives arbitrary props as well, so you may pass target to it or anything else. Please tell me if you find any issue with it.

@matyunya matyunya closed this as completed Oct 7, 2019
@cbenz
Copy link
Author

cbenz commented Oct 9, 2019

Great @matyunya !

I could use it here: https://jailbreak.frama.io/vitrine/

Just a quick note: if I pass <Button class="xxx"> then the class is applied to the <a> element and the inner <button> element, which is not always wanted. So I ended up by wrapping the <Button> component like that (example with ml-auto class):

<div class="ml-auto">
  <Button href="https://example.com/" target="_blank">Register</Button>
</div>

where it could have been:

<Button class="ml-auto" href="https://example.com/" target="_blank">Register</Button>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants