From d2617c4904294cd1d6ff358c67df899ea6ee4a7d Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 24 Mar 2023 08:18:23 +0000 Subject: [PATCH] UX: Allow opening user-menu tabs in new tab/window (#20792) This commit turns the new user menu tabs into ` {{d-icon @tab.icon}} {{#if @tab.count}} {{/if}} {{yield}} - \ No newline at end of file + \ No newline at end of file diff --git a/app/assets/javascripts/discourse/app/components/user-menu/menu.js b/app/assets/javascripts/discourse/app/components/user-menu/menu.js index cd79a6c7b03..743d9f7feec 100644 --- a/app/assets/javascripts/discourse/app/components/user-menu/menu.js +++ b/app/assets/javascripts/discourse/app/components/user-menu/menu.js @@ -5,7 +5,7 @@ import { NO_REMINDER_ICON } from "discourse/models/bookmark"; import UserMenuTab, { CUSTOM_TABS_CLASSES } from "discourse/lib/user-menu/tab"; import { inject as service } from "@ember/service"; import getUrl from "discourse-common/lib/get-url"; -import DiscourseURL from "discourse/lib/url"; +import { wantsNewWindow } from "discourse/lib/intercept-click"; const DEFAULT_TAB_ID = "all-notifications"; const DEFAULT_PANEL_COMPONENT = "user-menu/notifications-list"; @@ -314,14 +314,17 @@ export default class UserMenu extends Component { } @action - handleTabClick(tab) { - if (this.currentTabId !== tab.id) { - this.currentTabId = tab.id; - this.currentPanelComponent = tab.panelComponent; - this.currentNotificationTypes = tab.notificationTypes; - } else if (tab.linkWhenActive) { - DiscourseURL.routeTo(tab.linkWhenActive); + handleTabClick(tab, event) { + if (wantsNewWindow(event) || this.currentTabId === tab.id) { + // Allow normal navigation to href + return; } + + event.preventDefault(); + + this.currentTabId = tab.id; + this.currentPanelComponent = tab.panelComponent; + this.currentNotificationTypes = tab.notificationTypes; } @action diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js index 6b7bd1df9ca..020ba419e08 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js @@ -748,6 +748,34 @@ acceptance("User menu", function (needs) { await click("#site-logo"); } }); + + test("tabs have hrefs and can be opened in new window/tab", async function (assert) { + await visit("/"); + await click(".d-header-icons .current-user"); + + assert + .dom("#user-menu-button-replies") + .hasAttribute("href", "/u/eviltrout/notifications/responses"); + + // Add a top-level click listener to stub attempts to open a new window/tab + const newWindowOpenedAssertion = assert.async(); + const interceptor = (event) => { + event.preventDefault(); + + newWindowOpenedAssertion(); + const target = event.target; + assert.strictEqual(target.tagName, "A"); + assert.true(target.href.endsWith("/u/eviltrout/notifications/responses")); + }; + + window.addEventListener("click", interceptor); + + try { + await click("#user-menu-button-replies", { shiftKey: true }); + } finally { + window.removeEventListener("click", interceptor); + } + }); }); acceptance("User menu - Dismiss button", function (needs) {