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

Add option to render button as link #2378

Merged
merged 2 commits into from
Sep 8, 2023
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
31 changes: 13 additions & 18 deletions web/src/components/atomic/Button.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<button
type="button"
<component
:is="to === null ? 'button' : httpLink ? 'a' : 'router-link'"
v-bind="btnAttrs"
class="relative flex items-center py-1 px-2 rounded-md border shadow-sm cursor-pointer transition-all duration-150 overflow-hidden disabled:opacity-50 disabled:cursor-not-allowed"
:class="{
'bg-wp-control-neutral-100 hover:bg-wp-control-neutral-200 border-wp-control-neutral-300 text-wp-text-100':
Expand All @@ -12,7 +13,6 @@
}"
:title="title"
:disabled="disabled"
@click="doClick"
>
<slot>
<Icon v-if="startIcon" :name="startIcon" class="!w-6 !h-6" :class="{ invisible: isLoading, 'mr-1': text }" />
Expand All @@ -32,12 +32,12 @@
<Icon name="loading" class="animate-spin" />
</div>
</slot>
</button>
</component>
</template>

<script lang="ts" setup>
import { computed, useAttrs } from 'vue';
import { RouteLocationRaw, useRouter } from 'vue-router';
import { RouteLocationRaw } from 'vue-router';

import Icon, { IconNames } from '~/components/atomic/Icon.vue';

Expand All @@ -62,24 +62,19 @@ const props = withDefaults(
},
);

const router = useRouter();
const httpLink = computed(() => typeof props.to === 'string' && props.to.startsWith('http'));

async function doClick() {
if (props.isLoading) {
return;
const btnAttrs = computed(() => {
if (props.to === null) {
return { type: 'button' };
}

if (!props.to) {
return;
if (httpLink.value) {
return { href: props.to };
}

if (typeof props.to === 'string' && props.to.startsWith('http')) {
window.location.href = props.to;
return;
}

await router.push(props.to);
}
return { to: props.to };
});

const attrs = useAttrs();
const passedClasses = computed(() => {
Expand Down