Skip to content

Commit 6eeae0c

Browse files
authored
Merge pull request #430 from cal-smith/overflow
fix(overflow-menu): stop updating items outside of the angular change detection
2 parents 59b5e54 + cd18cd1 commit 6eeae0c

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

src/dialog/dialog.directive.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ 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;
91+
/**
92+
* Emits an event when the dialog is closed
93+
*/
94+
@Output() onClose: EventEmitter<any> = new EventEmitter();
95+
/**
96+
* Emits an event when the dialog is opened
97+
*/
98+
@Output() onOpen: EventEmitter<any> = new EventEmitter();
9299

93100
@HostBinding("attr.role") role = "button";
94101
@HostBinding("attr.aria-expanded") expanded = false;
@@ -197,6 +204,7 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
197204
open() {
198205
this.dialogService.open(this.viewContainerRef, this.dialogConfig);
199206
this.expanded = true;
207+
this.onOpen.emit();
200208
}
201209

202210
/**
@@ -206,6 +214,9 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
206214
toggle() {
207215
this.dialogService.toggle(this.viewContainerRef, this.dialogConfig);
208216
this.expanded = this.dialogService.isOpen;
217+
if (this.expanded) {
218+
this.onOpen.emit();
219+
}
209220
}
210221

211222
/**

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

Lines changed: 10 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

@@ -60,30 +61,20 @@ export class OverflowMenuOption {
6061
@Output() selected: EventEmitter<any> = new EventEmitter();
6162

6263
public tabIndex = -1;
64+
// note: title must be a real attribute (i.e. not a getter) as of Angular@6 due to
65+
// change after checked errors
66+
public title = null;
6367

6468
constructor(protected elementRef: ElementRef) {}
6569

6670
onClick(event) {
6771
this.selected.emit();
6872
}
6973

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() {
74+
ngAfterViewInit() {
7675
const button = this.elementRef.nativeElement.querySelector("button");
7776
if (button.scrollWidth > button.offsetWidth) {
78-
return true;
77+
this.title = this.elementRef.nativeElement.querySelector("button").textContent;
7978
}
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;
8879
}
8980
}

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

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

911
/**
1012
* The OverFlow menu component encapsulates the OverFlowMenu directive, and the menu iconography into one convienent component
@@ -22,9 +24,11 @@ import { I18n } from "./../../i18n/i18n.module";
2224
template: `
2325
<div
2426
[ibmOverflowMenu]="options"
25-
[ngClass]="{'bx--overflow-menu--open': open === true}"
27+
[ngClass]="{'bx--overflow-menu--open': open}"
2628
[attr.aria-label]="buttonLabel"
2729
[flip]="flip"
30+
(onOpen)="open = true"
31+
(onClose)="open = false"
2832
role="button"
2933
aria-haspopup="true"
3034
class="bx--overflow-menu"
@@ -67,12 +71,9 @@ export class OverflowMenu {
6771

6872
@Input() flip = false;
6973

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

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

0 commit comments

Comments
 (0)