The Button component is a versatile and customizable button that can function as both a navigational link and a regular button, with options for full-width display, primary or secondary styling, and custom label text. This component is ideal for developers looking for a reusable button that adapts to various use cases within their applications.
<ButtonComponent
label="Go Home"
link="/"
secondary
/>
<ButtonComponent label="Submit" @click="handleSubmit" fullWidth />
The button's appearance is controlled via Tailwind CSS classes, allowing for easy customization. The component supports both a primary and secondary styling mode, with each mode having tailored styles for light and dark themes.
To integrate the Dynamic Button component into your Nuxt 3 application:
ButtonComponent
component in your application as required, providing the necessary props for customization.<script setup lang="ts">
interface PropsInterface {
label: string;
link?: string | null;
fullWidth?: boolean;
secondary?: boolean;
}
interface EmitsInterface {
(event: 'click'): void;
}
const props = withDefaults(defineProps<PropsInterface>(), {
link: null,
fullWidth: false,
secondary: false,
});
const emits = defineEmits<EmitsInterface>();
const buttonWidth = props.fullWidth ? ' w-full' : '';
const getButtonStyle = computed(() => {
if (props.secondary) {
return 'border-black dark:border-primary bg-primary dark:bg-black text-black dark:text-primary hover:bg-black dark:hover:bg-primary hover:text-primary dark:hover:text-black';
} else {
return 'border-black dark:border-primary bg-black dark:bg-primary text-primary dark:text-black hover:bg-primary dark:hover:bg-black hover:text-black dark:hover:text-primary';
}
});
function clicked() {
emits('click');
}
</script>
<template>
<div
class="max-w-xl"
:class="buttonWidth"
>
<div class="flex">
<div :class="buttonWidth">
<!-- ROUTER BUTTON -->
<NuxtLink
v-if="link"
:to="link"
class="flex justify-center rounded-lg border-2 px-4 py-1 font-bold"
:class="getButtonStyle"
>
{{ label }}
</NuxtLink>
<!-- REGULAR BUTTON -->
<div
v-else
class="flex cursor-pointer justify-center rounded-lg border-2 px-4 py-1 font-bold"
:class="getButtonStyle"
@click="clicked()"
>
{{ label }}
</div>
</div>
</div>
</div>
</template>