Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Simple collapse /accordion component

I'm trying to troubleshoot this simple collapse/accordion component I made, the prop value seems to be ignored.

This is the full component

<template>
    <div :class="{ active: is_active }">
        <div @click="toggleContent">
            <slot name="title"></slot>
             //here is_active is false and  isActive is true
            {{ is_active }} {{ isActive }}
        </div>

        <transition
            enter-active-class="duration-300 ease-in-out"
            enter-from-class="transform opacity-0"
            enter-to-class="opacity-100"
            leave-active-class="duration-300 ease-in-out"
            leave-from-class="opacity-100"
            leave-to-class="transform opacity-0">
            <div v-if="is_active" >
                <slot name="content"></slot>
            </div>
        </transition>
    </div>
</template>
<script>
export default {
    props: {
        isActive: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            is_active: false
        }
    },
    methods: {
        toggleContent() {
            this.is_active = !this.is_active
        }
    },
    mounted() {
        this.is_active = this.isActive;
        //both show as false always
        console.log('prop', this.isActive, this.is_active);
    }
}
</script>

I thought that the prop values were accessible in the mounted() part so I don't understand why in the template I can see a true but in the mounted it always shows as false.

How can I fix it so that the prop is being considered, I already tried with a watch but that made a weird loop when trying to open a closed accordion.

This is how I use it

<div v-for="section in permission_sections">
//this section.is_open is true but the collapse isn't open
  <collapse :is-active="section.is_open">
    <template v-slot:title>//</template>
    <template v-slot:content>//</template>
  </collapse>
</div>
0 likes
4 replies
vincent15000's avatar

A props is a data that is passed from the parent component to the child component, isn't it ?

Is it how you are using it ?

Furthermore it's normal that you can't access a props inside the mount() method.

eggplantSword's avatar

@vincent15000 I redid it a bit, but now I'm still having issues but when I toggle the component

props: {
        isActive: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            is_active: false
        }
    },
    watch: {
        isActive(val) {
            console.log('isActive', val);
           this.is_active = val
        },
        is_active(val) {
            console.log('is_active', val);
            this.$emit('update:isActive', val)
        }
    },
    methods: {
        toggleContent() {
            this.is_active = !this.is_active
        }
    }

The console.log output is like this, if it's open and I try to close it instead of staying on false/closed it like opens again, same with the reverse it stays closed, if I click it again then it opens but then the v-model is correct but my styles are backwards since I base the styles on the v-model and its set to true but the collapse isn't open

isActive false
is_active false
is_active true
1 like
Udev's avatar

noticed this :class="{ active: is_active }" . should it be :class="{ 'active': is_active }".

1 like

Please or to participate in this conversation.