Can you provide some guidance on implementing styling props on individual elements within VueJs?

I am currently working on designing a button with various states like hover, focused, pressed, and disabled. Additionally, I need this button to have two different sizes - default (large) and smaller (small). Despite attempting to pass these attributes using vue props, I'm facing issues where they aren't being applied to the button as expected.

Specifically, my focus right now is on setting basic size attributes for the button such as height and width, but it seems that the changes are not reflecting and no errors are being thrown either.

Displayed below is the button in the template:

<button
      class="primaryButton"
      :class="[
        size + 'large',
        type + 'primary',

        {
          'default-state': !isActive,
          hover: isActive && isHovered,
          focused: isActive && isFocused,
          pressed: isActive && isPressed,
          disabled: isDisabled
        }
      ]"
      @mouseenter="isHovered = true"
      @mouseleave="isHovered = false"
      @focus="isFocused = true"
      @mousedown="isPressed = true"
      @mouseup="isPressed = false"
      @click="handleClick"
      :disabled="isDisabled"
    >
      <slot />
</button>

Shown below is the script section:

export default {
  props: {
    size: {
      default: 'large',
      small: 'small'
      //default size
    },
    type: {
      default: 'default',
      focused: 'focused',
      pressed: 'pressed',
      disAbled: 'disAbled'
      //default type
    }
  },
  data() {
    return {
      isActive: true,
      isHovered: false,
      isFocused: false,
      isPressed: false,
      isDisabled: false
    }
  },
  methods: {
    handleClick() {
      if (!this.isDisabled) {
        this.buttonClick
      }
    }
  }
}

Lastly, presented below is the style section:

.primaryButton {
  background-color: #a5042a;
  border: none;
  color: white;
  border-radius: 5px;

}
.primaryButton:hover {
  background-color: #ed063d;
  cursor: pointer;
}

.large-size {
  min-height: 45px;
  min-width: 130px;
}

Answer №1

If you're working in Vue, utilizing slots can be highly beneficial. Create a component titled BaseButton.vue and pass props or fallthrough attributes to customize the button's appearance. Take a look at this brief demo on Vue playground. See below for a snippet of BaseButton.vue

<template>
  <button class="default">
    <slot></slot>
  </button>
</template>

<style scoped>
.default {
    width: 100px;
    height: 30px;
    background-color: blue;
    color: white;
    border: none;
    transition: all .2s;
}

.default:hover {
    background-color: aliceblue;
}

.large {
    width: 200px;
    height: 50px;
    background-color: red;
}

.large:hover {
    /* specific hover styles */
}

.small {
    width: 75px;
    height: 10px;
}
</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I retrieve dt/dd elements and store them in an array using Simple HTML DOM?

Similar Question: Retrieving data from HTML table using preg_match_all in PHP This is the HTML code snippet: <div class="table"> <dl> <dt>ID:</dt> <dd>632991</dd> <d ...

Listening to a sequence of mp3 tracks consecutively

I'm currently working on a project that involves playing a series of mp3 files consecutively. However, I've encountered an issue with my code snippet below: Here's the code snippet: function playAudio(){ var au = document.getElementById( ...

Is your background image not filling the entire page?

Just diving into the world of HTML and CSS (with a touch of Bootstrap), I've set out to create a basic scrolling webpage. However, I've hit a roadblock with the background image not covering the full height of the page. It seems to stop at the h2 ...

Exploring AngularJS to search for and present data in a tabular layout

Can anyone guide me on searching for data within a JSON file and presenting it in a table format? I want the table to be visible only when I click the search button. I would really appreciate it if someone could provide me with instructions or a helpful ...

Issues with webpage responsiveness, width not adjusting correctly

Just completed coding my website and now I'm facing a new challenge. I am focusing on making it responsive for tablet screens (768px) starting with the header section, but for some reason, the modifications I make in the responsive.css file are not ta ...

Is there a way to implement a "report" button for sending messages that doesn't require using email?

I am in need of a button that reads "Report a typo" and alerts me when it is clicked on a specific page. The project I am working on consists of multiple short pages with just a few paragraphs in each HTML template. It would be helpful for me to have a way ...

Choosing an element that does not have a particular string in its colon attribute name

In my code, I have multiple elements structured like this: <g transform="matrix"> <image xlink:href="data:image/png" width="48"> </g> <g transform="matrix"> <image xlink:href="specific" width="48"> </g> <g tran ...

Error: Incorrect data input in Django calendar form

I have a DateForm class defined in my form.py file: class DateForm(forms.Form): date = forms.DateTimeField( input_formats=['%m/%d/%Y %H:%M'], widget=forms.DateTimeInput(attrs={ 'class': 'form-control dateti ...

Rails Polymer is experiencing a glitch where the CSS and Polymer elements are not displaying correctly until a page refresh is

Whenever I first load a page, my polymer elements (like paper-input) fail to load their CSS properly. Here is an example of how the page looks before and after refreshing: https://i.sstatic.net/vsIpE.pnghttps://i.sstatic.net/YAFjP.png Below is the code sn ...

Calculating the median in JavaScript utilizing a for loop

Currently, I am in the process of learning JavaScript. My goal is to calculate the median of a set of numbers that I input through a prompt when I click the button labeled "find median". function CalculateMedia() { var n = prompt("Enter the number of e ...

Display all pages in the DataTables plugin while utilizing responsive tables and additional features

I am struggling to combine the responsive and fixed header attributes of my current function with the "Show All" list feature, similar to what is demonstrated in this example: https://datatables.net/examples/advanced_init/length_menu.html I need assistanc ...

Looking to activate a bootstrap grid breakpoint when a div reaches a specified min-width

I have a webpage layout structured within a Bootstrap grid. In desktop view, one div (divDesktopDiv) is shown, and in mobile view, it is replaced by another div (divMobileDiv). However, my current setup causes the divDesktopDiv to overflow into the neighb ...

Form in HTML - Transmit rows consecutively as an array

Hey there, I have a bit of a perplexing issue that I'm hoping to get some help with. I have a form that pulls data from my database. It displays names in one column and two columns of checkboxes. My goal is to update the database based on the checkb ...

Internet Explorer 11 is not interested in giving attention to a disabled element

I'm trying to limit text input to only one of two fields. My approach involves checking the value of a field when it loses focus, and then disabling the other field if the initial one is not empty. Below is an example: HTML: <div class="contain ...

Discover the power of Vue.js 3 by seamlessly integrating anonymous functions within event handlers

Is it possible to invoke an anonymous function within an event handler in Vue.js 3? I came across several options on various websites: <button @click="return function() { console.log('test')}()">Click me</button> <butto ...

Creating fake data on a worldwide scale with Vue testing utilities

I'm currently working on a Vue component that includes an img tag with the src value coming from props. <template> <div> <img src="some url"> </div> </template> However, I have this img tag used in mult ...

Could you confirm if this is a TypeScript function?

Recently, while delving into the vue-next source code, I stumbled upon a particular line that left me puzzled. Due to my limited experience with TypeScript, I found myself struggling to grasp its purpose. Could someone clarify if this snippet constitutes ...

Comparison of getComputedStyle() and cssText functionality between Internet Explorer and Firefox

Take a look at this example to see the issue in action. I'm attempting to access the cssText property of a <div> using window.getComputedStyle(element) (which provides a CSSStyleDeclaration object). While this works smoothly in Chrome, it' ...

Ways to send a POST variable to PHP without refreshing the webpage

Is there a way to pass a simple variable from a text-box on a different page to PHP without reloading it? I've been informed that Ajax is required for this task, but I'm unsure of how to go about implementing it. Can someone provide me with a sam ...

Unusual problem encountered with Chart.js bar chart, image dispersion

On my HTML page, I have integrated a stacked bar chart using the Chart.js library to visually represent data for users. The data changes based on user selection, and the JavaScript function that enables this onclick feature is: function aggLav(){ [... ...