Skip to content

Commit

Permalink
Fix computation error on disclosure utility
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ju committed Jul 19, 2022
1 parent 8d11546 commit f9b38f4
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions packages/components/addon/components/hds/disclosure/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { debounce } from '@ember/runloop';

export default class HdsDisclosureComponent extends Component {
@tracked isOpen; // notice: if in the future we need to add a "@isOpen" prop to control the status from outside (eg to have the Disclosure opened on render) just add "this.args.isOpen" here to initalize the variable
@tracked toggleRef;
@tracked isToggleClicked;

@action
didInsert(element) {
Expand All @@ -18,12 +18,7 @@ export default class HdsDisclosureComponent extends Component {
if (!this.toggleRef) {
this.toggleRef = event.currentTarget;
}
// we're using an explicit conditional statement to toggle the `isOpen` property to avoid a backtracking rerender assertion caused by setting and getting a `@tracked` property in the same computation
if (this.isOpen) {
this.isOpen = false;
} else {
this.isOpen = true;
}
this.isOpen = !this.isOpen;
// we explicitly apply a focus state to the toggle element to overcome a bug in WebKit (see b8abfcf)
this.toggleRef.focus();
}
Expand All @@ -48,12 +43,15 @@ export default class HdsDisclosureComponent extends Component {

@action
close() {
if (this.isOpen) {
this.isOpen = false;
}
// we call the "onClose" callback if it exists (and is a function)
if (this.args.onClose && typeof this.args.onClose === 'function') {
this.args.onClose();
}
// delay setting `isOpen` as this function may be called multiple times in the same computation
debounce(() => {
if (this.isOpen) {
this.isOpen = false;
}
// we call the "onClose" callback if it exists (and is a function)
if (this.args.onClose && typeof this.args.onClose === 'function') {
this.args.onClose();
}
});
}
}

0 comments on commit f9b38f4

Please sign in to comment.