Search code examples
vuejs2flexboxtransitioncss-grid

vue transition-group item with flex/grid wrapper moves to top-left


I have this:

<transition-group
    name="product-list"
    tag="section"
    class="product-list"
    @before-leave="beforeLeave"
>
    <product
        v-for="watch in watches"
        :key="watch.id"
        :item="watch"
    >
    </product>
</transition-group>

and as it says in the docs:

.product-list-enter-active,
.product-list-leave-active {
    transition: all 0.3s;
}
.product-list-enter,
.product-list-leave-to {
    opacity: 0;
    transform: scale(0.5);
}
.product-list-leave-active {
    position: absolute;
}

Issue: when any element is leaving (it is removed from the list) it goes top-left because of the position absolute... which kinda breaks the aesthetics of it


Solution

  • Found this solutions:

    <transition-group
        @before-leave="beforeLeave">
        ...
    </transition>
    
    methods: {
        beforeLeave(el) {
            const {marginLeft, marginTop, width, height} = window.getComputedStyle(el)
    
            el.style.left = `${el.offsetLeft - parseFloat(marginLeft, 10)}px`
            el.style.top = `${el.offsetTop - parseFloat(marginTop, 10)}px`
            el.style.width = width
            el.style.height = height
        }
    }
    

    thanks to docmars in forum.vuejs.org

    If there is a better solution out there please let me know