Skip to content

A platform for community discussion. Free, open, simple.

DEV: adds matchTriggerWidth/matchTriggerMinWidth d-menu (#34128)

Joffrey JAFFEUX16be391fe3c8
diff --git a/app/assets/javascripts/discourse/app/components/discovery/filter-navigation-menu.gjs b/app/assets/javascripts/discourse/app/components/discovery/filter-navigation-menu.gjs
index f4bd7142043..8ee69aecd62 100644
--- a/app/assets/javascripts/discourse/app/components/discovery/filter-navigation-menu.gjs
+++ b/app/assets/javascripts/discourse/app/components/discovery/filter-navigation-menu.gjs
@@ -4,7 +4,7 @@ import { fn } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
-import { cancel, schedule } from "@ember/runloop";
+import { cancel } from "@ember/runloop";
import { service } from "@ember/service";
import { TrackedObject } from "@ember-compat/tracked-built-ins";
import { eq } from "truth-helpers";
@@ -15,7 +15,6 @@ import concatClass from "discourse/helpers/concat-class";
import icon from "discourse/helpers/d-icon";
import withEventValue from "discourse/helpers/with-event-value";
import discourseDebounce from "discourse/lib/debounce";
-import { bind } from "discourse/lib/decorators";
import FilterSuggestions from "discourse/lib/filter-suggestions";
import { resettableTracked } from "discourse/lib/tracked-tools";
import { i18n } from "discourse-i18n";
@@ -86,16 +85,6 @@ export default class FilterNavigationMenu extends Component {
@tracked _selectedIndex = -1;
- constructor() {
- super(...arguments);
- window.addEventListener("resize", this.resizeDMenu);
- }
-
- willDestroy() {
- super.willDestroy(...arguments);
- window.removeEventListener("resize", this.resizeDMenu);
- }
-
get selectedIndex() {
return this._selectedIndex;
}
@@ -387,12 +376,6 @@ export default class FilterNavigationMenu extends Component {
async openFilterMenu() {
if (this.dMenuInstance) {
this.dMenuInstance.show();
-
- // HACK: We don't have a nice way for DMenu to be the same width as
- // the input element, so we set it manually.
- schedule("afterRender", () => {
- this.resizeDMenu();
- });
return;
}
@@ -401,26 +384,9 @@ export default class FilterNavigationMenu extends Component {
component: FilterNavigationMenuList,
data: this.trackedMenuListData,
maxWidth: 2000,
+ matchTriggerWidth: true,
visibilityOptimizer: VISIBILITY_OPTIMIZERS.AUTO_PLACEMENT,
});
-
- // HACK: We don't have a nice way for DMenu to be the same width as
- // the input element, so we set it manually.
- schedule("afterRender", () => {
- this.resizeDMenu();
- });
- }
-
- @bind
- resizeDMenu() {
- if (!this.inputElement || !this.dMenuInstance) {
- return;
- }
-
- if (this.dMenuInstance.content) {
- this.dMenuInstance.content.style.width =
- this.inputElement.offsetWidth + "px";
- }
}
@action
diff --git a/app/assets/javascripts/discourse/tests/integration/components/float-kit/d-menu-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/float-kit/d-menu-test.gjs
index 8525ab3bcf9..963e358fe51 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/float-kit/d-menu-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/float-kit/d-menu-test.gjs
@@ -581,4 +581,42 @@ module("Integration | Component | FloatKit | d-menu", function (hooks) {
assert.dom("span.fk-d-menu__trigger").exists();
});
+
+ test("@matchTriggerWidth", async function (assert) {
+ await render(
+ <template>
+ <DMenu
+ @label="a long label"
+ @inline={{true}}
+ @matchTriggerWidth={{true}}
+ style="width: 200px;"
+ >1</DMenu>
+ </template>
+ );
+
+ await open();
+
+ assert.dom(".fk-d-menu.-content").hasStyle({
+ width: "200px",
+ });
+ });
+
+ test("@matchTriggerMinWidth", async function (assert) {
+ await render(
+ <template>
+ <DMenu
+ @label="a long label"
+ @inline={{true}}
+ @matchTriggerMinWidth={{true}}
+ style="width: 200px;"
+ >1</DMenu>
+ </template>
+ );
+
+ await open();
+
+ assert.dom(".fk-d-menu.-content").hasStyle({
+ minWidth: "200px",
+ });
+ });
});
diff --git a/app/assets/javascripts/float-kit/addon/lib/constants.js b/app/assets/javascripts/float-kit/addon/lib/constants.js
index 9967aae2434..7b763b19c88 100644
--- a/app/assets/javascripts/float-kit/addon/lib/constants.js
+++ b/app/assets/javascripts/float-kit/addon/lib/constants.js
@@ -84,6 +84,8 @@ export const MENU = {
contentClass: null,
class: null,
updateOnScroll: true,
+ matchTriggerMinWidth: false,
+ matchTriggerWidth: false,
},
portalOutletId: "d-menu-portal-outlet",
};
diff --git a/app/assets/javascripts/float-kit/addon/lib/update-position.js b/app/assets/javascripts/float-kit/addon/lib/update-position.js
index f1515c9a415..6cc5f399cf5 100644
--- a/app/assets/javascripts/float-kit/addon/lib/update-position.js
+++ b/app/assets/javascripts/float-kit/addon/lib/update-position.js
@@ -8,6 +8,7 @@ import {
limitShift,
offset,
shift,
+ size,
} from "@floating-ui/dom";
import domFromString from "discourse/lib/dom-from-string";
import { isTesting } from "discourse/lib/environment";
@@ -95,6 +96,28 @@ export async function updatePosition(trigger, content, options) {
middleware.push(hide({ padding: detectOverflowOptions.padding }));
}
+ if (options.matchTriggerWidth) {
+ middleware.push(
+ size({
+ apply({ rects, elements }) {
+ Object.assign(elements.floating.style, {
+ width: `${rects.reference.width}px`,
+ });
+ },
+ })
+ );
+ } else if (options.matchTriggerMinWidth) {
+ middleware.push(
+ size({
+ apply({ rects, elements }) {
+ Object.assign(elements.floating.style, {
+ minWidth: `${rects.reference.width}px`,
+ });
+ },
+ })
+ );
+ }
+
content.dataset.strategy = options.strategy || "absolute";
const { x, y, placement, middlewareData } = await computePosition(