From 8c9258487a6aa47a50dd552d5c3edbae31a58a23 Mon Sep 17 00:00:00 2001 From: Chesney Julian Date: Tue, 7 Jan 2025 14:05:49 -0500 Subject: [PATCH] feat: Add flip middleware to popover and tooltip --- .../app/components/f/components/popover.gts | 19 ++++++++++++++++++ .../app/components/f/components/tooltip.gts | 11 ++++++++++ .../ember-core/src/components/popover.gts | 20 +++++++++++++++++-- .../ember-core/src/components/tooltip.gts | 2 ++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/apps/docs-app/app/components/f/components/popover.gts b/apps/docs-app/app/components/f/components/popover.gts index f8ed1a78b..a0f40ac67 100644 --- a/apps/docs-app/app/components/f/components/popover.gts +++ b/apps/docs-app/app/components/f/components/popover.gts @@ -36,6 +36,9 @@ export default class extends Component { @tracked arrow: boolean = true; + @tracked + flip: boolean = false; + @tracked isShown?: boolean; @@ -62,6 +65,7 @@ export default class extends Component { + + + { @tracked _isShown = false; + @tracked + adjustedSide: Direction = 'bottom'; + get hasArrow() { return this.args.arrow ?? true; } @@ -120,6 +124,10 @@ export default class Popover extends Component { get middleware() { const middleware = [offset(this.offset)]; + if (this.args.flip) { + middleware.push(flip()); + } + if (this.hasArrow) { middleware.push(arrow({ element: this.arrow! })); } @@ -201,6 +209,14 @@ export default class Popover extends Component { return; } + if (placement !== this.placement) { + this.adjustedSide = Object.keys(SIDE_TRANSLATION).find( + (side) => SIDE_TRANSLATION[side as Direction] === placement, + ) as Direction; + } else { + this.adjustedSide = this.side; + } + const { x: arrowX, y: arrowY } = middlewareData.arrow!; const staticSide: string = ARROW_SIDE[placement.split('-')[0] as Direction]!; @@ -226,7 +242,7 @@ export default class Popover extends Component { id={{this.id}} class={{classes (unless this.isShown "hidden") - (concat "popover bs-popover-" this.side) + (concat "popover bs-popover-" this.adjustedSide) }} {{onInsert this.initPopover}} {{! @glint-expect-error Modifier types are currently not correct }} diff --git a/packages/ember-core/src/components/tooltip.gts b/packages/ember-core/src/components/tooltip.gts index c928eb4fb..9903151ea 100644 --- a/packages/ember-core/src/components/tooltip.gts +++ b/packages/ember-core/src/components/tooltip.gts @@ -13,6 +13,7 @@ export interface TooltipSignature { Args: { alignment?: Alignment; controlElement?: HTMLElement; + flip?: boolean; isShown?: boolean; offset?: string | number; side?: Direction; @@ -80,6 +81,7 @@ const Tooltip: TOC =