Use v-bind for the dynamic class name part:
<button
class="text-white pt-2 pb-2 pr-7 pl-7 border rounded-lg border-white"
:class="`bg-${bgColor}`"
>
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, how can I make my button component reusable? I want to set it's background color and text color in components where I use this button.
Button :
<template>
<div>
<button
class="text-white pt-2 pb-2 pr-7 pl-7 bg-bgColor border rounded-lg border-white"
>
<slot></slot>
</button>
</div>
</template>
<script>
export default {
props: ["bgColor"],
};
</script>
Here I want to use it:
<template>
<div>
<section>
<h3 class="text-white text-lg">Example</h3>
</section>
<section>
<the-button>Example</the-button>
<the-button>Example</the-button>
</section>
</div>
</template>
<script setup>
import TheButton from "@/components/ui/TheButton.vue";
const props = defineProps(["text"]);
console.log(props.text);
</script>
@p0k3rface here is an example using the composition API
<script setup>
defineProps({
buttonTitle: {
type: String,
required: true,
},
classes: {
type: String,
required: false,
},
});
</script>
<template>
<button
type="button"
class="px-9 py-2 flex-none h-12 uppercase text-md font-medium font-sans"
:class="classes"
>
{{ buttonTitle }}
</button>
</template>
Please or to participate in this conversation.