Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useTransition: Turbo drive - cannot properly disconnect #418

Open
BriceFab opened this issue Sep 21, 2023 · 1 comment
Open

useTransition: Turbo drive - cannot properly disconnect #418

BriceFab opened this issue Sep 21, 2023 · 1 comment

Comments

@BriceFab
Copy link

BriceFab commented Sep 21, 2023

Hello,

I tested to implement useTransition with Turbo drive (SPA naviguation).
And it doesn't disconnect properly:

import { Controller } from '@hotwired/stimulus';
import { useTransition } from 'stimulus-use';

export default class extends Controller {
    static targets = ['menu']

    initialize() {
        useTransition(this, {
            element: this.menuTarget,
            enterActive: 'fade-enter-active',
            enterFrom: 'fade-enter-from',
            enterTo: 'fade-enter-to',
            leaveActive: 'fade-leave-active',
            leaveFrom: 'fade-leave-from',
            leaveTo: 'fade-leave-to',
            hiddenClass: 'hidden',
            // set this, because the item *starts* in an open state
            transitioned: false,
        });
    }
    disconnect() {
        this.leave();
    }

    closeMenu() {
        this.leave();
    }

    openMenu() {
        this.enter();
    }

    toggleMenu() {
        this.toggleTransition();
    }
}
{% extends 'layout/base.html.twig' %}

{% block body %}
    <a href="{{ path('test') }}">
        page 1
    </a>

    <div {{ stimulus_controller('front--menu-toggler') }}>
        <button {{ stimulus_action('front--menu-toggler', 'toggleMenu') }}>
            Toggler
        </button>

        <div class="hidden" {{ stimulus_target('front--menu-toggler', 'menu') }}>
            mon menu
        </div>
    </div>
{% endblock %}

image
Each time I naviguate between page, it will register multiple instance of this controller and events are trigger multiple times (which will cause issue and memory leaks).

Thank in advance

@marcoroth marcoroth changed the title Turbo drive - cannot properly disconnect useTransition: Turbo drive - cannot properly disconnect Nov 26, 2023
@marcoroth
Copy link
Member

Hey @BriceFab, thank you for reporting this!

Is there any way you can provide a small reproducible example for this? This would help immensely for addressing your issues. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants