Implementing atomic design principles in Vue 3 with TypeScript

I'm currently implementing atomic design principles in my Vue application.

Here is the code for my button atom:

<template>
  <ElButton
    :type="button?.type"
    :plain="button?.plain"
    :rounded="button?.rounded"
    :icon="button?.icon"
    :disabled="button?.disabled"
    :loading="button?.loading"
    :size="button?.size"
  >
    {{ button?.label }}
  </ElButton>
</template>

<script lang="ts">
  import { ElButton } from "element-plus"
  import { PropType, defineComponent } from "vue"
  interface IButton {
    label: String
    type: String
    plain?: boolean
    rounded?: boolean
    icon?: String
    disabled?: boolean
    loading?: boolean
    size?: String
    rest?: any
  }
  export default defineComponent({
    name: "Button",
    props: {
      button: Object as PropType<IButton>,
    },
    components: {
      ElButton,
    },
  })
</script>

I have integrated this button into my HelloWorld.vue file.

<script lang="ts">
  import {defineComponent } from "vue"
  import Button from "./atom/input/index.vue"

  export default defineComponent({
    components: {
      Button,
    },
  })
</script>

<template>
  <Button type="success" size="large" label="Primary Button" />
</template>

Everything seems to work fine with my button component. However, the text inside the button is not being displayed.

Even though I passed the label prop to the component, it appears as an attribute of the button when inspecting the button element.

For example:

<button class="el-button el-button--success el-button--large" type="button" label="Primary Button"></button>

Can someone help me identify what I might be missing here?

Answer №1

Here's the issue:

props: { button: {label: string} }

you should be using them like this instead:
props: { label: string }

You have two options:

  <Button :button="{type:'success', size:'large', label:'Primary Button'}" />

or fix the props accordingly

Answer №2

Upon delving into the depths of the Vue TypeScript documentation, I uncovered a fitting solution for implementing interfaces on props using the Vue 3 TypeScript Composition API.

atom/button/index.vue

<template>
  <ElButton
    :type="type"
    :plain="plain"
    :round="round"
    :icon="icon"
    :disabled="disabled"
    :loading="loading"
    :size="size"
  >
    {{ label }}
  </ElButton>
</template>

<script setup lang="ts">
  import { ElButton } from "element-plus"
  interface IButton {
    type: string
    label: string
    plain?: boolean
    round?: boolean
    icon?: string
    disabled?: boolean
    loading?: boolean
    size?: string
  }
  const { type, label, plain, round, icon, disabled, loading, size } =
    defineProps<IButton>()

</script>

/component/HelloWorld.vue

<script setup lang="ts">
  import Button from "./atom/button/index.vue"
  import Input from "./atom/input/index.vue"

  const buttonClick = (type: string): void => {
    console.log(`string text ${type} string text`)
  }
</script>

<template>
  <Button
    :type="'success'"
    :label="'Success'"
    @click="buttonClick('Success')"
  />
</template>

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

Screening new elements to be included in the array

When adding new items to an array in my App, I encountered a problem with filtering the newly added items. If I use .filter by state, it modifies the original array and I am unable to filter from the beginning (original array). I attempted to filter using ...

Resolving TS2304 error using Webpack 2 and Angular 2

I have been closely following the angular documentation regarding webpack 2 integration with angular 2. My code can be found on GitHub here, and it is configured using the webpack.dev.js setup. When attempting to run the development build using npm start ...

How can we stop the Bootstrap carousel from pausing when the mouse hovers over it and keep it cycling automatically?

Is there a way to stop the Bootstrap carousel from pausing when hovering with the mouse and instead have it continue cycling through items automatically? I've tried changing the pause argument from "hover" as mentioned in the documentation, but when ...

An alternative to Socket.io tailored for up-to-date web browsers

Are there any specialized versions of Socket.io or similar libraries that cater to modern browsers exclusively, utilizing Websockets without the need for legacy browser support and outdated code? ...

Using Express.js and Angular for user authentication and session management

In my current project, I am utilizing expressjs and angularjs to create an app. The setup involves expressjs serving a single .html file that houses an angular single-page-application. All routing is handled by angularjs, while expressjs provides web servi ...

Why are my basic style properties not appearing correctly when I use json_encode on an array?

My calendar is written in Javascript within a table with the ID "calendario," allowing me to manipulate it using calendario.rows[i].cells[i]. This calendar lets users make reservations and provides an option to close a day if there are too many reservatio ...

Vue.js watcher fails to observe changes in v-model

I'm encountering an issue with vue.js. I have set it up so that when a new item is added, it is saved to local storage. However, I also want the item to be saved to local storage when editing it in the input field. I thought this should work because o ...

What is the process for accessing the mesh in Aframe when a 3D object is loaded dynamically?

Is there a way to access mesh information of a 3D object loaded at runtime in Aframe? My method for loading the 3D model is as follows: targetObj = document.createElement('a-obj-model'); targetObj.setAttribute('gltf-model', '#wha ...

Next.js 14 useEffect firing twice upon page load

Having an issue with a client component in next js that is calling an API twice at page load using useEffect. Here's the code for the client component: 'use client'; import { useState, useEffect } from 'react'; import { useInView ...

Tips for resolving a 403 error and SSH connection issue on an Azure Web Service website

Recently, my web app created on Azure using Express and Node 18 worked perfectly during development locally. However, when I attempted to host it on an Azure web app, I encountered issues. The site failed to display anything and a 403 error was returned in ...

Images cascading like a downpour on a canvas (Javascript)

I have been experimenting with canvas, attempting to create a simulation of random falling objects. I've successfully drawn the background image, but I'm having trouble with the second image that is supposed to simulate a rain drop. I've ma ...

Unable to access the following element using jQuery's next() method

Can someone help me understand how the "next" function works in jQuery? I'm trying to reveal a hidden textarea when a span element is clicked. The hidden textarea is supposed to be the immediate next sibling of the span, but it's not working as e ...

Is there no body sent in the $.ajax post request?

My server is returning an error when I try to make a simple post request. It's saying that the post request has no body and all the keys have an "undefined" value. Here is the code for my post request: let alert_title = 'Alert'; let alert ...

Javascript - Unable to update button text

I am encountering a problem with updating the text of a Bootstrap button when a collapsed element is opened or closed. The icon part is updating successfully, but I am struggling to get the button text to update and I cannot figure out why. My knowledge o ...

Resizing an image with six corners using the canvas technique

Currently, I am facing two issues: The topcenter, bottomcenter, left and right anchors are not clickable. I'm struggling with the logic to adjust the image size proportionally as described below: The corner anchors should resize both height and wi ...

Tips for resolving the CROSS Origin problem within ionic-vue

Recently, I started exploring the world of Ionic framework with Vuejs and decided to create a basic Crud app. Unfortunately, I'm facing some CORS issues that are preventing me from being able to login. My browser console is displaying an error messag ...

How can you leverage the power of useState in a custom React hook?

Here is a code snippet for a custom hook I created: const useShowBg = () => { const [showBg, useShowBg] = useState(false); return [showBg, useShowBg]; }; export default useShowBg; In my component, I import and use the custom hook like this: im ...

What steps should I take to link form inputs from various child components to an array that is set in a parent component?

I'm in the process of linking form input from various child components (item-input-component) to an array itemList[] that is defined in a parent component (add-invoice-component). The goal is to gather three inputs (itemName, quantity, price), create ...

How can I eliminate text using regular expressions?

Greetings, I am seeking assistance with content removal using regular expressions. Below is the regular expression code I have written: reg = reg.replace(/\|.*?(\|)/g, ''); Input: One-1|two-2|Three-3|Four-4|Five-5 Six-6|Seven-7|Eig ...

What is the behavior of a variable when it is assigned an object?

When using the post method, the application retrieves an object from the HTML form as shown below: code : app.post("/foo", (req, res)=> { const data = req.body; const itemId = req.body.id; console.log(data); console.log(itemId); }) ...