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

Enabling Transitions: always dispatch the toggle event #61

Merged
merged 4 commits into from Sep 6, 2021

Conversation

weaverryan
Copy link
Contributor

@weaverryan weaverryan commented Mar 26, 2021

Hi!

This closes #59.

I've created 3 clean commits to show the incremental progress - I have tried to change as little as possible:

  1. The controller already has open() and close() methods, but they were not consistently called. The first commit always calls these methods when opening or closing the element. This means that the toggle event is now dispatched in open/close situations when it wasn't before, but I think this is a bug fix :). The docs state that the toggle event "fires when the results element is shown or hidden". I also added a new action key to the event set to open or close so that a listener can determine what's happening.

  2. Added a this.isHidden property to the controller to track the "is hidden" state, instead of relying on this.resultsTarget.hidden.

  3. Added a new skipHiddenProperty value that can be set if you want to avoid this.resultsTarget.hidden from being set (because you want to handle hiding/showing yourself).

The end result of this is that a user can now leverage use-transition to add, for example, a fade-in/out for this auto-complete. I'll include that example code below in case anyone is interested :).

Thanks!


Transitions with stimulus-use

A) The HTML:

<div
    data-controller="autocomplete autocomplete-transition"
    data-autocomplete-url-value="/some/url"
    data-autocomplete-skip-hidden-property-value="1"
    data-action="toggle->autocomplete-transition#toggle"
>
    <input data-autocomplete-target="input">

    <div
        data-autocomplete-target="results"
        data-autocomplete-transition-target="results">
    </div>
</div>

B) Custom autocomplete-transition controller:

import { Controller } from 'stimulus';
import { useTransition } from 'stimulus-use';

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

    connect() {
        useTransition(this, {
            element: this.resultsTarget,
            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: 'd-none',
        });
    }

    toggle(event) {
        if (event.detail.action === 'open') {
            this.enter();
        } else {
            this.leave();
        }
    }
}

C) Some fade out / fade in CSS

.fade-enter-active, .fade-leave-active {
    transition: opacity 2000ms;
}
.fade-enter-from, .fade-leave-to {
    opacity: 0;
}
.fade-enter-to, .fade-leave-from {
    opacity: 1;
}

this.element.setAttribute("aria-expanded", "true")
this.element.dispatchEvent(
new CustomEvent("toggle", {
detail: { input: this.input, results: this.results }
detail: { action: 'open', input: this.input, results: this.results }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, this.input and this.results are undefined 100% of the time... and it looks like it was always this way.

If the intention was for these to be this.inputTarget and this.resultsTarget, we could change those. But I wanted to keep my PR diff as small as possible :).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. You have my blessing in broadly fix external events since you're looking at them/care about them. This PR or another is good by me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful then :). I've updated these to be the two targets, which I think could be useful :).

@drnic
Copy link
Collaborator

drnic commented Mar 28, 2021

I don't have an ETA of when I will review this. Will try to do it this week.

@weaverryan
Copy link
Contributor Author

Thanks @drnic!

To make it (hopefully) MUCH easier for you (I know getting a simple test project setup can be annoying... especially to merge someone else's feature), I've created a SUPER simple project you can use to test: https://github.com/weaverryan/stimulus-autocomplete-events

In 5 commands (all in the README) you can test my PR in a real project and also see how an example usage performs (where a custom controller listens to the toggle event to add CSS transitions).

Please let me know if there is anything else I can do to make your job easier :). I'm hoping to feature this (including the transitions) in a screencast over on SymfonyCasts.com.

Thanks!

@weaverryan
Copy link
Contributor Author

Hi!

A friendly ping... and sorry to be annoying :/. The reason is that I'm hoping to show this library in the last video of a tutorial that's being released this week and next. I created a repository that will hopefully make testing this as painless as possible :) https://github.com/weaverryan/stimulus-autocomplete-events

Thank you!

@drnic
Copy link
Collaborator

drnic commented Apr 19, 2021 via email

@seb-jean
Copy link
Contributor

seb-jean commented Sep 3, 2021

Love this PR

@jeanmartin
Copy link

Would love to see this merged.

@drnic drnic merged commit 2331c9e into afcapel:master Sep 6, 2021
@drnic
Copy link
Collaborator

drnic commented Sep 6, 2021

Thanks for reminding me. Merged.

djtango pushed a commit to djtango/stimulus-autocomplete that referenced this pull request Oct 4, 2021
Enabling Transitions: always dispatch the toggle event
@weaverryan weaverryan deleted the toggle-event-always branch October 17, 2021 14:45
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

Successfully merging this pull request may close these issues.

[RFC] Support for useTransition with consistent "toggle" event (or new open/close events)
4 participants