Skip to content

Commit

Permalink
Improve closeDropdown handling (#532)
Browse files Browse the repository at this point in the history
* Move hide-dropdown handling to LinkTo

* Fix action, improve docs

* Update item.hbs
  • Loading branch information
jeffdaley authored Jan 12, 2024
1 parent d3bbbf2 commit 4fe455f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
25 changes: 7 additions & 18 deletions web/app/components/x/dropdown-list/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,18 @@ export default class XDropdownListItemComponent extends Component<XDropdownListI
this._domElement = element;
}

/**
* The action called on clicked. Runs the parent component's
* `onItemClick` action, if it exists, and hides the dropdown.
* The "link-to" sub-component runs this on the next run loop
* to avoid interfering with Ember's <LinkTo> handling.
*/
@action onClick() {
if (this.args.onItemClick) {
this.args.onItemClick(this.args.value, this.args.attributes);
}

/**
* In production, close the dropdown on the next run loop
* so that we don't interfere with Ember's <LinkTo> handling.
*
* This approach causes issues when testing, so we
* use `schedule` as an approximation.
*
* TODO: Improve this.
*/
if (Ember.testing) {
schedule("afterRender", () => {
this.args.hideContent();
});
} else {
next(() => {
this.args.hideContent();
});
}
this.args.hideContent();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion web/app/components/x/dropdown-list/link-to.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
data-test-x-dropdown-list-item-link-to
{{did-insert @registerElement}}
{{on "mouseenter" @focusMouseTarget}}
{{on "click" @onClick}}
{{on "click" this.onClick}}
role={{@role}}
aria-selected={{@isAriaSelected}}
tabindex="-1"
Expand Down
21 changes: 20 additions & 1 deletion web/app/components/x/dropdown-list/link-to.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Component from "@glimmer/component";
import { XDropdownListInteractiveComponentArgs } from "./_shared";
import { action } from "@ember/object";
import Ember from "ember";
import { next, schedule } from "@ember/runloop";

interface XDropdownListLinkToComponentSignature {
Element: HTMLAnchorElement;
Expand All @@ -14,7 +17,23 @@ interface XDropdownListLinkToComponentSignature {
};
}

export default class XDropdownListLinkToComponent extends Component<XDropdownListLinkToComponentSignature> {}
export default class XDropdownListLinkToComponent extends Component<XDropdownListLinkToComponentSignature> {
/**
* The action to run when the item is clicked.
* We wait until the next run loop so that we don't interfere with
* Ember's <LinkTo> handling. Because this approach causes issues
* when testing, we use `schedule` as an approximation.
*/
@action protected onClick(): void {
if (Ember.testing) {
schedule("afterRender", this.args.onClick);
} else {
next(() => {
this.args.onClick();
});
}
}
}

declare module "@glint/environment-ember-loose/registry" {
export default interface Registry {
Expand Down

0 comments on commit 4fe455f

Please sign in to comment.