Modify the Primevue Class p-accordion-header only under certain conditions

I'm currently facing a challenge with customizing the styling of the primevue component class p-accordion-header based on the status of the rendered component determined by the variantNumber.

Here's a snippet of my code:

<Accordion :multiple="true">
    <AccordionTab
      v-for="variantNumber in variants?.keys()"
      :key="'V' + variantNumber"
    >
    </AccordionTab>
  </Accordion>

The variable variants holds a simple list.

Let's say I have a function called

getCurrentVariantClass(variantNumber)
that returns the current class name.

  1. I'm unable to directly add a class to the AccordionTab because it would not be a prop attribute.
  2. I've attempted wrapping a div or a basic HTML tag around the AccordionTab and adding a class there, but the component doesn't render properly.

My question is: How can I dynamically adjust the styles of p-accordion-header based on certain conditions?

Answer №1

If we want to personalize the AccordionTab component by adding a new variant prop, we can achieve this by creating a separate component that extends the original one and includes the prop:

<!-- MyAccordionTab.vue -->
<script>
import { defineComponent } from 'vue'
import AccordionTab from 'primevue/accordiontab'

export default defineComponent({
  name: 'CustomizedAccordionTab',
  extends: AccordionTab,
  props: {
    variant: Number, 👈
  },
  render: AccordionTab.render,
})
</script>

Following a similar approach as above, we can also extend the Accordion component to modify its getTabHeaderClass() method so it can apply a specific class based on the variant prop of each tab:

<!-- MyAccordion.vue -->
<script>
import { defineComponent } from 'vue'
import Accordion from 'primevue/accordion'

export default defineComponent({
  extends: Accordion,
  methods: {
    getTabHeaderClass(tab, i) {
      const headerClasses = Accordion.methods.getTabHeaderClass.call(this, tab, i)
      const variantClass = this.getCurrentVariantClass(tab.props.variant)
      return headerClasses.concat(variantClass)
    },
    getCurrentVariantClass(variantNumber) {
      switch (variantNumber) {
        case 1:
          return 'p-accordion-header-variant-a'
        case 2:
          return 'p-accordion-header-variant-b'
        case 3:
          return 'p-accordion-header-variant-c'
      }
    },
  },
  render: Accordion.render,
})
</script>

<style scoped>
.p-accordion-header-variant-a .p-accordion-header-link {
  border: solid 1px red;
}
.p-accordion-header-variant-b .p-accordion-header-link {
  border: solid 1px green;
}
.p-accordion-header-variant-c .p-accordion-header-link {
  border: solid 1px blue;
}
</style>

Finally, integrate these customized components into your application:

<!-- App.vue -->
<script setup>
import Accordion from '@/components/MyAccordion.vue'
import AccordionTab from '@/components/MyAccordionTab.vue'

const tabs = [/*...*/]
</script>

<template>
  <Accordion multiple>
    <AccordionTab v-for="tab in tabs" :key="tab.title" :variant="tab.variant" :header="tab.title">
      <p>{{ tab.content }}</p>
    </AccordionTab>
  </Accordion>
</template>

Check out the demo here!

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

Creating a line break in a Vue.js confirmation dialog is a helpful feature that can be achieved with a

Is there a way to add a new line after the confirmation dialog question and insert a note below it? this.$dialog.confirm("Create Group","Would you like to create group "+ this.groupName +"?<br/>(NOTE: Selected project or empl ...

Can a class be declared in C++ without having to implement it as well?

Apologies for the ambiguity of this question, but I couldn't find a better way to phrase it. Is it feasible to achieve the following scenario: #include<iostream> class wsx; class wsx { public: wsx(); } wsx::wsx() { std::cout<<"WSX"; } ...

The Django template fails to implement CSS styling

I am trying to apply text-align: justify and reduce the text width on a Django template file, but it seems that my CSS isn't working for only this specific element. I have no idea how to solve this issue even though my static files and direction are f ...

Design a fluid row that allows for horizontal scrolling with text alignment options on the left, center, and right side

I created a row with horizontal scrolling on smaller screens. Is there a way to have text aligned left, center, and right all on the same line? I want to achieve this with text aligned in multiple positions. Check out the demonstration on Stackblitz C ...

Adjusting the width of a Material UI select/dropdown component

In my material-ui grid setup, each <Grid> contains a <Paper> element to define the grid's boundaries precisely. Inside every <Paper>, there is a custom <DropDown> component (a modified version of Material-UI's Select). I ...

The art of combining CSS3 gradients with background images

I've created a function that changes the parent element's background gradient when a checkbox's status is toggled. Check out the Lorem link to see it in action! Click here to view. However, I've encountered an issue with applying this ...

Creating a visually appealing Django filter form with custom styling in HTML

Currently, this is how my django filter form appears: https://i.sstatic.net/JQWFG.png Below is the html code I am using: <form action="" method="get" class="form-inline"> {{myfilter.form|bootstrap}} <but ...

Seamless transition of lightbox fading in and out

Looking to create a smooth fade in and out effect for my lightbox using CSS and a bit of JavaScript. I've managed to achieve the fading in part, but now I'm wondering how to make it fade out as well. Here is the CSS code: @-webkit-keyframes Fad ...

Looking for assistance in using JavaScript to determine the percentage of a DIV's width in pixels

Using PHP, I am generating boxes whose width is set to 100% of a container in CSS. Now, I want to determine the equivalent pixel value of that box... HTML <div class="masonry" > <? while($row = $stmt->fetch()){ ?> <div class="i ...

Managing the width of an HTML table column based on its text content dynamically

Here is an example of an html table : <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="480px" class="societe" ><div style="float:left;font-size:12px;padding-top:2px" ><?php echo $_SESSION ...

Could LXML potentially determine the value of a class in HTML?

For instance, consider the following scenario: <button class="oW_lN oF4XW sqdOP yWX7d _8A5w5 ">MyTextIsHere</button> The "MyTextIsHere" part of the code can contain only two specific values: "Yes" and "No". Is it feasible to determine wh ...

What is the best way to apply a single slot-element to multiple elements?

Considering a slot structure like: <span slot="?" slot-scope="data>...</span> What placeholder should be used in place of the question mark to make it versatile for any implementation? ...

retrieve a property value using a Vue directive

I've been working on a Vue directive that should return true if a permission matches with the user permissions stored in Vuex. However, I'm facing difficulties accessing the state properly. Within Vue components, I have no trouble accessing the ...

Utilizing nested flex containers with overflow-x property

I'm looking to set up a layout with 2 columns side by side. The first column should have a fixed width of 200px, while the second column should take up the remaining space on the screen. Additionally, I want the content in the second column to auto-sc ...

Transferring information from blade to vue component in Vue/Laravel

Hey there, I'm currently diving into the world of Vue and trying to grasp its concepts. One thing that's been boggling my mind is how to tweak a component I've created for a create form so it can also be utilized for an edit form. Specifical ...

What is the best way to perfectly center the navbar-nav class element in a menu using Bootstrap 5?

Currently, I am in the process of developing a backend for a novice website while also revamping the front end. My knowledge of frontend development is limited, so I have opted to utilize Bootstrap 5. One of my tasks involves transferring the menu from the ...

The alignment of elements in the div seems to be slightly skewed

Currently, I am in the process of creating a website with the temporary name yeet.io. I am facing an issue where I am attempting to center both an input and h1 element inside a div vertically and horizontally, but they keep appearing misaligned for some r ...

Guide on using JavaScript to implement the universal CSS selector

One technique I frequently employ is using the CSS universal selector to reset the dimensions in my HTML document: * { border: 0; margin: 0; padding: 0; } I wonder if a similar approach can be achieved with JavaScript as well? When it come ...

Error: The Vue bind object property cannot be read because it is undefined in the class binding

I'm attempting to associate a class with an object property, but I encounter an issue when trying to trigger the @click event on the list object - the console states that it cannot read the isSelected property of the object. My goal is to activate a c ...

Creating PNG images in a scripting language for web applications

Imagine giving your website user the ability to specify a radius size, let's say 5px, and in return receiving a PNG file with a circle of that radius. This question can be divided into two sections: In what language and using which technologies can ...