Creating a versatile icon button component by customizing Vuetify's VBtn element for reusability

I have been experimenting with the example Codepen (https://codepen.io/whoistobias/pen/yLNgjwy) in an attempt to enhance the Vuetify VBtn component and create a reusable Button Icon component.

I am very close to achieving my goal, but as usual, I face difficulties towards the end.

The key code snippet is as follows:

const VBtn = Vue.options.components["VBtn"];
const VIcon = Vue.options.components["VIcon"];

const ButtonIcon = {
  name: 'ButtonIcon',
  
  extends: VBtn,

  props: {
    icon: {
      default: true,
      type: Boolean
    },
    iconColor: {
      default: 'info',
      type: String
    },
    iconName: {
      default: 'help',
      type: String
    },
    outline: {
      default: true,
      type: Boolean
    }
  },

  methods: {
    genContent () {
      return this.$createElement('div', {
        class: 'v-btn__content'
      }, this.$slots.default || [this.$createElement(
        VIcon,
        {
          props: {
            color: this.iconColor
          }
        },
        this.iconName
      )])
    }
  }
};

The issue lies in the insertion of this.iconName into VIcon. The result shows "add" instead of just add when the value of the prop iconName is add.

I have tried to understand the information in the Vue documentation regarding the use of createElement - https://v2.vuejs.org/v2/guide/render-function.html?redirect=true#createElement-Arguments

Here is the link to my modified version on Codepen: https://codepen.io/scp-nm/pen/PoQPVEP

Any assistance on this matter would be highly appreciated.

Answer №1

Your issue lies in the incorrect naming of your icons. The default icon names begin with mdi-. You can find a comprehensive list of available icons at .

In this case, it seems like using mdi-plus for "add" and mdi-help-circle-outline for "help" would be appropriate:

                                               👇
<ButtonIcon icon-color="success" icon-name="mdi-plus" title="Icon Button (custom prop values)" />
const ButtonIcon = {
  ⋮
  props: {
    iconName: {   👇
      default: 'mdi-help-circle-outline',
      type: String
    },
  },
}

Check out the demo for more information.

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

Ensure that this regular expression is able to accurately match all numbers, even those without decimal points

Seeking help to create a script that can extract the negative percentage value between two numbers. One of the numbers is dynamic and includes different currencies, decimals, etc., so I believe a regex is needed for this task. The current script works, but ...

Transforming a JavaScript component based on classes into a TypeScript component by employing dynamic prop destructuring

My current setup involves a class based component that looks like this class ArInput extends React.Component { render() { const { shadowless, success, error } = this.props; const inputStyles = [ styles.input, !shadowless && s ...

Create a user interface design for displaying alerts on a web

Currently, I am in the process of creating a form that includes a text box. One important validation I need to implement is that the name entered must be unique. My approach involves accessing the parent div and modifying the entire HTML content within it ...

Issue with clientHeight not functioning properly with line breaks in Angular 2 application after ngAfterViewInit

I have successfully created a Gridify page in my Angular 2 application using the Gridify library. To initialize it, I've utilized a custom ngAfterViewChecked method: ngAfterViewChecked() { var selector = document.querySelector('.read-grid& ...

Using a function as a prop in Vue js to retrieve data from an API

I am facing an issue with a component that I want to decouple from the data fetching implementation. My goal is to be able to pass a data fetching callback as a prop. The reason for this is so that I can easily mock the data fetching process in storybook. ...

Node: effectively managing SQL scripts with dynamic variable replacement

My SQL statements have grown quite large, and I want to store them as separate files with syntax highlighting. The solution I found on StackOverflow is perfect for my needs. In my case, I write SQL queries in JavaScript using Template Literal syntax for v ...

Refresh a selection menu using a checkmark

Can you help me with a script to enable/disable a dropdown based on the checkbox status? <body onload="enableDropdown(false);"> <form name="form1" method="post" onload="enableDropdown(false);"> <input type="checkbox" name="others" oncl ...

Unable to locate the module named '@/components/HelloWorld.vue' or its associated type declarations. Error encountered in Vetur(2307)

After creating a new project using the vue command line tool with "vue create .", I encountered an issue when trying to run "npm serve". The component import is showing up as red, even though I haven't made any changes to the project yet. The error m ...

Exploring the Overlapping of Objects in Three.js

I am dealing with multiple meshes in my code and I am trying to figure out how to make the renderer display the object that should be in front, somewhat similar to what is shown in this example: --> . I have read about render depth, but I am unsure of h ...

What is the best way to convert a string value such as '20180131T120000Z' into a Date object?

Can anyone help me figure out how to parse the following String date format (yyyyMMddThhmmssZ) to Date? const date = Date.parse("20171201T120000Z"); console.log(date); The result I'm getting is NaN. What would be the most efficient method to achieve ...

Using Typescript to map a string to an enum

Having trouble mapping a string to an enum and receiving the error message TypeError: Cannot convert undefined value to object export enum CallErrorType { UNAUTHENTICATED, } const handler: { [key in CallErrorType]: (message: string) => void; } = { ...

How should I manage objects that are passed by reference in the context of functional programming?

Lately, I have been experimenting with some code in an attempt to delve deeper into functional programming. However, I seem to have hit a snag. I am struggling with handling an object. My goal is to add key-value pairs to an object without reassigning it, ...

Prevent form clearing by implementing input validation

While working on setting up a sign-up form, I encountered an issue where I needed to validate whether the information entered already existed in the database. The code itself is functioning properly and can successfully detect if any existing data is foun ...

What steps can I take to ensure my code runs smoothly from a USB across all devices?

My code for a concept website is stored on a USB stick, but I'm facing a problem with my buttons being anchored to a G: drive on Windows. However, when I use a Chromebook, the USB stick is recognized as removable media and not a G: drive, causing the ...

Enhance Your VuePress Experience with Unique Sidebar Contents for Every Single Page

I am in search of a way to personalize links and sidebar on each individual page, avoiding the rendering of headings as table of contents. My aim is to have custom content displayed like this: Node JS -Lecture 1 Introduction --Sub Lecture -Lecture 2 Basic ...

Enhance pagination and column filtering in JQGrid without relying on local data (loadonce = false)

I'm currently facing an issue with my web application development. I am utilizing Laravel 5.8 for the backend and JQGrid version 4.6.0 to create grids. One of the grids I have is constructed with a dynamic URL that fetches JSON data from the server b ...

Creating a flexible route path with additional query parameters

I am facing a challenge in creating a unique URL, similar to this format: http://localhost:3000/boarding-school/delhi-ncr However, when using router.push(), the dynamic URL is being duplicated like so: http://localhost:3000/boarding-school/boarding-school ...

Confirm the dimensions of an image prior to uploading using Node, Express, and Multer

Currently, I am developing a project using Nodejs with the express framework and ejs as the view engine. I have encountered an issue while working on image uploads. I am utilizing Multer for this task, but I need to implement a requirement where images wil ...

Generating a unique image switch with disabled hover functions

My latest project involves coding a block that displays an image on an HTML page, with a new image appearing each time the user hovers over it. However, there's a glitch in my code that causes the images to flicker erratically if the mouse is moved ev ...

Tallying the number of words delimited by a comma

Here is how my counter function is structured: function count() { var value = ids.val(); return (value == '') ? 0 : value.replace(/\s,?|,$/g, '').split(',').length; } After checking the returned value, data is ...