Skip to content

Commit e946895

Browse files
committed
fix(overflow-menu): stop updating items outside of the angular change detection
1 parent 3231be7 commit e946895

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

src/dialog/dialog.directive.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
8787
/**
8888
* Config object passed to the rendered component
8989
*/
90-
@Output() onClose: EventEmitter<any> = new EventEmitter<any>();
9190
dialogConfig: DialogConfig;
9291

92+
@Output() onClose: EventEmitter<any> = new EventEmitter();
93+
@Output() onOpen: EventEmitter<any> = new EventEmitter();
94+
95+
9396
@HostBinding("attr.role") role = "button";
9497
@HostBinding("attr.aria-expanded") expanded = false;
9598
@HostBinding("attr.aria-haspopup") hasPopup = true;
@@ -197,6 +200,7 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
197200
open() {
198201
this.dialogService.open(this.viewContainerRef, this.dialogConfig);
199202
this.expanded = true;
203+
this.onOpen.emit();
200204
}
201205

202206
/**
@@ -206,6 +210,9 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
206210
toggle() {
207211
this.dialogService.toggle(this.viewContainerRef, this.dialogConfig);
208212
this.expanded = this.dialogService.isOpen;
213+
if (this.expanded) {
214+
this.onOpen.emit();
215+
}
209216
}
210217

211218
/**

src/dialog/overflow-menu/overflow-menu-option.component.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
Input,
55
ElementRef,
66
Output,
7-
EventEmitter
7+
EventEmitter,
8+
AfterViewInit
89
} from "@angular/core";
910

1011
/**
@@ -17,7 +18,7 @@ import {
1718
* <ibm-overflow-menu-option type="danger">Danger option</ibm-overflow-menu-option>
1819
* ```
1920
*
20-
* For content that expands beyod the overflow menu `OverflowMenuOption` automatically adds a title attribute.
21+
* For content that expands beyond the overflow menu `OverflowMenuOption` automatically adds a title attribute.
2122
*/
2223
@Component({
2324
selector: "ibm-overflow-menu-option",
@@ -30,12 +31,12 @@ import {
3031
(blur)="tabIndex = -1"
3132
(click)="onClick($event)"
3233
[disabled]="disabled"
33-
[title]="(titleEnabled ? content : '')">
34+
[title]="title">
3435
<ng-content></ng-content>
3536
</button>
3637
`
3738
})
38-
export class OverflowMenuOption {
39+
export class OverflowMenuOption implements AfterViewInit {
3940
@HostBinding("class") optionClass = "bx--overflow-menu-options__option";
4041
@HostBinding("attr.role") role = "presentation";
4142

@@ -61,29 +62,18 @@ export class OverflowMenuOption {
6162

6263
public tabIndex = -1;
6364

65+
public title = null;
66+
6467
constructor(protected elementRef: ElementRef) {}
6568

6669
onClick(event) {
6770
this.selected.emit();
6871
}
6972

70-
/**
71-
* Returns true if the content string is longer than the width of the containing button
72-
*
73-
* note: getter ties into the view check cycle so we always get an accurate value
74-
*/
75-
get titleEnabled() {
73+
ngAfterViewInit() {
7674
const button = this.elementRef.nativeElement.querySelector("button");
7775
if (button.scrollWidth > button.offsetWidth) {
78-
return true;
76+
this.title = this.elementRef.nativeElement.querySelector("button").textContent;
7977
}
80-
return false;
81-
}
82-
83-
/**
84-
* Returns the text content projected into the component
85-
*/
86-
get content(): string {
87-
return this.elementRef.nativeElement.querySelector("button").textContent;
8878
}
8979
}

src/dialog/overflow-menu/overflow-menu.component.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import {
22
Component,
33
ElementRef,
44
Input,
5-
ViewEncapsulation
5+
ViewEncapsulation,
6+
AfterViewInit,
7+
ContentChild
68
} from "@angular/core";
79
import { I18n } from "./../../i18n/i18n.module";
10+
import { OverflowMenuDirective } from "./overflow-menu.directive";
811

912
/**
1013
* The OverFlow menu component encapsulates the OverFlowMenu directive, and the menu iconography into one convienent component
@@ -22,9 +25,11 @@ import { I18n } from "./../../i18n/i18n.module";
2225
template: `
2326
<div
2427
[ibmOverflowMenu]="options"
25-
[ngClass]="{'bx--overflow-menu--open': open === true}"
28+
[ngClass]="{'bx--overflow-menu--open': open}"
2629
[attr.aria-label]="buttonLabel"
2730
[flip]="flip"
31+
(onOpen)="open = true"
32+
(onClose)="open = false"
2833
role="button"
2934
aria-haspopup="true"
3035
class="bx--overflow-menu"
@@ -67,12 +72,9 @@ export class OverflowMenu {
6772

6873
@Input() flip = false;
6974

70-
constructor(protected elementRef: ElementRef, protected i18n: I18n) {}
75+
@ContentChild(OverflowMenuDirective) overflowMenuDirective: OverflowMenuDirective;
7176

72-
get open() {
73-
if (this.elementRef.nativeElement.children[0].getAttribute("aria-expanded") === "true") {
74-
return true;
75-
}
76-
return false;
77-
}
77+
open = false;
78+
79+
constructor(protected elementRef: ElementRef, protected i18n: I18n) {}
7880
}

0 commit comments

Comments
 (0)