Skip to content

Commit 21f5137

Browse files
authored
Merge pull request #598 from cal-smith/dialog
feat(dialog): add configurable placements
2 parents 24a608b + ca0be87 commit 21f5137

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/dialog/dialog.component.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from "rxjs";
1919
import { throttleTime } from "rxjs/operators";
2020
// the AbsolutePosition is required to import the declaration correctly
21-
import { position, AbsolutePosition } from "@carbon/utils-position";
21+
import Position, { position, AbsolutePosition, Positions } from "@carbon/utils-position";
2222
import { cycleTabs, getFocusElementList } from "./../common/tab.service";
2323
import { DialogConfig } from "./dialog-config.interface";
2424

@@ -79,7 +79,6 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
7979
/**
8080
* Handles offsetting the `Dialog` item based on the defined position
8181
* to not obscure the content beneath.
82-
* @protected
8382
*/
8483
protected addGap = {
8584
"left": pos => position.addOffset(pos, 0, -this.dialogConfig.gap),
@@ -90,6 +89,11 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
9089
"right-bottom": pos => position.addOffset(pos, 0, this.dialogConfig.gap)
9190
};
9291

92+
/**
93+
* Extra placements. Child classes can add to this for use in `placeDialog`.
94+
*/
95+
protected placements: Positions = {};
96+
9397
/**
9498
* Creates an instance of `Dialog`.
9599
* @param {ElementRef} elementRef
@@ -198,13 +202,14 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
198202
* Uses the position service to position the `Dialog` in screen space
199203
*/
200204
placeDialog(): void {
205+
const positionService = new Position(this.placements);
201206
// helper to find the position based on the current/given environment
202207
const findPosition = (reference, target, placement) => {
203208
let pos;
204209
if (this.dialogConfig.appendInline) {
205-
pos = this.addGap[placement](position.findRelative(reference, target, placement));
210+
pos = this.addGap[placement](positionService.findRelative(reference, target, placement));
206211
} else {
207-
pos = this.addGap[placement](position.findAbsolute(reference, target, placement));
212+
pos = this.addGap[placement](positionService.findAbsolute(reference, target, placement));
208213
}
209214
return pos;
210215
};
@@ -218,13 +223,13 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
218223
const placements = this.dialogConfig.placement.split(",");
219224

220225
// find the best placement
221-
dialogPlacement = position.findBestPlacement(parentEl, el, placements);
226+
dialogPlacement = positionService.findBestPlacement(parentEl, el, placements);
222227

223228
// calculate the final position
224229
const pos = findPosition(parentEl, el, dialogPlacement);
225230

226231
// update the element
227-
position.setElement(el, pos);
232+
positionService.setElement(el, pos);
228233
setTimeout(() => { this.placement = dialogPlacement; });
229234
}
230235

0 commit comments

Comments
 (0)