Skip to content

Commit f5b711d

Browse files
committed
default dropdown to append to body
1 parent d8e7c21 commit f5b711d

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

src/dialog/dialog.service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ export class DialogService {
105105
if (!this.dialogRef) {
106106
// holder for either the provided view, or the view from DialogPlaceholderService
107107
let view = viewContainer;
108-
if (dialogConfig.appendToBody && this.dialogPlaceholderService.viewContainerRef) {
108+
if (dialogConfig.appendInline) {
109+
// add our component to the view
110+
this.dialogRef = view.createComponent(this.componentFactory, 0, this.injector);
111+
} else if (this.dialogPlaceholderService.viewContainerRef) {
109112
view = this.dialogPlaceholderService.viewContainerRef;
110113
// add our component to the view
111114
this.dialogRef = view.createComponent(this.componentFactory, 0, this.injector);
112-
} else if (dialogConfig.appendToBody && !this.dialogPlaceholderService.viewContainerRef) {
115+
} else {
113116
// fallback to the old insertion method if the viewref doesn't exist
114117
this.dialogRef = view.createComponent(this.componentFactory, 0, this.injector);
115118
setTimeout(() => {
116119
window.document.querySelector("body").appendChild(this.dialogRef.location.nativeElement);
117120
});
118-
} else {
119-
// add our component to the view
120-
this.dialogRef = view.createComponent(this.componentFactory, 0, this.injector);
121121
}
122122

123123
// initialize some extra options
@@ -148,11 +148,11 @@ export class DialogService {
148148

149149
if (this.dialogRef) {
150150
let elementToFocus = this.dialogRef.instance.dialogConfig["previouslyFocusedElement"];
151-
if (this.dialogRef.instance.dialogConfig.appendToBody && this.dialogPlaceholderService.viewContainerRef) {
151+
if (this.dialogRef.instance.dialogConfig.appendInline) {
152+
viewContainer.remove(viewContainer.indexOf(this.dialogRef.hostView));
153+
} else if (this.dialogPlaceholderService.viewContainerRef) {
152154
const vcRef = this.dialogPlaceholderService.viewContainerRef;
153155
vcRef.remove(vcRef.indexOf(this.dialogRef.hostView));
154-
} else {
155-
viewContainer.remove(viewContainer.indexOf(this.dialogRef.hostView));
156156
}
157157
this.dialogRef = null;
158158
this.isOpen = false;

src/dropdown/dropdown.component.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ import { I18n } from "./../i18n/i18n.module";
5555
</button>
5656
<div
5757
#dropdownMenu
58-
*ngIf="!menuIsClosed"
5958
[ngClass]="{
6059
'drop-up': dropUp
6160
}">
62-
<ng-content></ng-content>
61+
<ng-content *ngIf="!menuIsClosed"></ng-content>
6362
</div>
6463
</div>
6564
`,
@@ -98,9 +97,23 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
9897
*/
9998
@Input() disabled = false;
10099
/**
100+
* Deprecated. Dropdown now defaults to appending inline
101101
* Set to `true` if the `Dropdown` is to be appended to the DOM body.
102102
*/
103-
@Input() appendToBody = false;
103+
@Input() set appendToBody (v) {
104+
console.log("appendToBody has been deprecated. dropdowns now append to the body by default.");
105+
console.log("ensure you have an ibm-placeholder in your app.");
106+
console.log("use appendInline if you need to position your dropdowns within the normal page flow");
107+
this.appendInline = !v;
108+
}
109+
110+
get appendToBody() {
111+
return !this.appendInline;
112+
}
113+
/**
114+
* set to `true` to place the dropdown view inline with the component
115+
*/
116+
@Input() appendInline = false;
104117
/**
105118
* Query string for the element that contains the `Dropdown`.
106119
* Used to trigger closing the dropdown if it scrolls outside of the viewport of the `scrollableContainer`.
@@ -397,14 +410,11 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
397410
*/
398411
_appendToBody() {
399412
const positionDropdown = () => {
400-
position.setElement(
401-
this.dropdownWrapper,
402-
position.addOffset(
403-
position.findAbsolute(this.elementRef.nativeElement, this.dropdownWrapper, "bottom"),
404-
window.scrollY,
405-
window.scrollX
406-
)
407-
);
413+
let pos = position.findAbsolute(this.elementRef.nativeElement, this.dropdownWrapper, "bottom");
414+
// add -40 to the top position to account for carbon styles
415+
pos = position.addOffset(pos, -40, 0);
416+
pos = position.addOffset(pos, window.scrollY, window.scrollX);
417+
position.setElement(this.dropdownWrapper, pos);
408418
};
409419
this.dropdownMenu.nativeElement.style.display = "block";
410420
this.dropdownWrapper = document.createElement("div");
@@ -426,9 +436,9 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
426436
openMenu() {
427437
this.menuIsClosed = false;
428438

429-
// move the dropdown list to the body if appendToBody is true
439+
// move the dropdown list to the body if we're not appending inline
430440
// and position it relative to the dropdown wrapper
431-
if (this.appendToBody) {
441+
if (!this.appendInline) {
432442
this.addScrollEventListener();
433443
this._appendToBody();
434444
}
@@ -477,7 +487,7 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
477487
}
478488

479489
// move the list back in the component on close
480-
if (this.appendToBody) {
490+
if (!this.appendInline) {
481491
this.removeScrollEventListener();
482492
this._appendToDropdown();
483493
}
@@ -492,7 +502,7 @@ export class Dropdown implements OnInit, AfterContentInit, OnDestroy {
492502
*/
493503
addScrollEventListener() {
494504
if (this.scrollableContainer) {
495-
const container = document.querySelector(this.scrollableContainer);
505+
const container: HTMLElement = document.querySelector(this.scrollableContainer);
496506

497507
if (container) {
498508
this.scroll = fromEvent(container, "scroll")

0 commit comments

Comments
 (0)