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

Turn the button into a link when href is provided #2864

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/components/Button/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ button {
</docs>

<template>
<button class="button-vue"
v-bind="$attrs"
<root-element class="button-vue"
v-bind="rootElement"
:class="buttonClassObject"
:aria-label="ariaLabel"
:type="nativeType"
Expand All @@ -220,7 +220,7 @@ button {
<slot />
</span>
</span>
</button>
</root-element>
</template>
<script>

Expand Down Expand Up @@ -281,6 +281,15 @@ export default {
type: String,
default: null,
},

/**
* Providing the href attribute turns the button component into a an `a`
* element
*/
href: {
type: String,
default: null,
},
},

data() {
Expand All @@ -300,6 +309,21 @@ export default {
},

computed: {
// Determines whether the root element is an a or a button
rootElement() {
if (this.href) {
return {
is: 'a',
href: this.href,
...this.$attrs,
}
}
return {
is: 'button',
...this.$attrs,
}
marcoambrosini marked this conversation as resolved.
Show resolved Hide resolved
},

hasText() {
return this.slots?.default !== undefined
&& this.slots?.default[0]?.text
Expand Down