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: Persisting the state on the DOM #176

Open
gabriel-curtino opened this issue Nov 13, 2021 · 1 comment
Open

useTransition: Persisting the state on the DOM #176

gabriel-curtino opened this issue Nov 13, 2021 · 1 comment

Comments

@gabriel-curtino
Copy link
Contributor

Hi!

I'm just starting to use stimulus-use and it's great! Thanks for your work.

I've found myself with an issue with useTransition and the transitioned state. This state is being track on the js class instead on the DOM. Let me explain my case:

I have a sidebar that can can be opened/closed by two buttons. For that reason I'm instanciating the controller twice having each one of them tracking their own transitioned state. I think if we can persist this state in the target element (not in the controller element) in an attribute like data-transitioned-state, for example, then any controller instance will just read the current state from the element attribute.

This is also preferabe according the stimulus approach: "A Stimulus application’s state lives as attributes in the DOM; controllers themselves are largely stateless.

Thanks!

@AlexSabur
Copy link

You can do it like this

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

export default class extends Controller {
    static values = {
        'transitioned': Boolean,
    }

    connected = false

    connect() {
        useTransition(this, {
            enterActive: "transition ease-out duration-300",
            enterFrom: "transform opacity-0 translate-y-5",
            enterTo: "transform opacity-100 translate-y-0",
            leaveActive: "transition ease-in duration-300",
            leaveFrom: "transform opacity-100 translate-y-0",
            leaveTo: "transform opacity-0 -translate-y-5",
            transitioned: this.transitionedValue
        })

        this.connected = true
    }

    transitionedValueChanged() {
        if (this.connected) {
            this.transitionedValue
                ? this.enter()
                : this.leave()
        }
    }
}

@marcoroth marcoroth changed the title Persisting the state on the DOM useTransition: Persisting the state on the DOM Aug 6, 2022
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