A platform for community discussion. Free, open, simple.
UX: Add exact URL matching for sidebar section links (#34677)
Krzysztof Kotlarek568a9328b1f9
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.gjs b/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.gjsindex 52666f77213..d5a9fd0229d 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.gjs+++ b/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.gjs@@ -16,6 +16,7 @@ import SectionLinkButton from "../section-link-button";export default class SidebarCustomSection extends Component {@service currentUser;@service navigationMenu;+ @service router;@tracked section = this.initialSection;@@ -39,6 +40,12 @@ export default class SidebarCustomSection extends Component {}}+ get exactUrlMatch() {+ return this.section.links.find((link) => {+ return this.router.currentURL === link.value;+ });+ }+<template><SectionComponent@sectionName={{this.section.slug}}@@ -69,6 +76,7 @@ export default class SidebarCustomSection extends Component {@suffixType={{link.suffixType}}@suffixValue={{link.suffixValue}}@title={{link.title}}+ @exactUrlMatch={{this.exactUrlMatch}}/>{{/each}}diff --git a/app/assets/javascripts/discourse/app/components/sidebar/section-link.gjs b/app/assets/javascripts/discourse/app/components/sidebar/section-link.gjsindex 6d7be1e16de..bb28ee4286e 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/section-link.gjs+++ b/app/assets/javascripts/discourse/app/components/sidebar/section-link.gjs@@ -6,7 +6,7 @@ import didUpdate from "@ember/render-modifiers/modifiers/did-update";import { LinkTo } from "@ember/routing";import { schedule } from "@ember/runloop";import { service } from "@ember/service";-import { eq, or } from "truth-helpers";+import { and, eq, not, or } from "truth-helpers";import concatClass from "discourse/helpers/concat-class";import icon from "discourse/helpers/d-icon";import { bind } from "discourse/lib/decorators";@@ -31,6 +31,7 @@ export function isHex(input) {export default class SectionLink extends Component {@service currentUser;+ @service router;constructor() {super(...arguments);@@ -66,6 +67,10 @@ export default class SectionLink extends Component {classNames.push(this.args.class);}+ if (this.args.href && this.args.href === this.args.exactUrlMatch?.value) {+ classNames.push("exact-url-match");+ }+if (this.args.href &&typeof this.args.currentWhen === "boolean" &&@@ -168,7 +173,7 @@ export default class SectionLink extends Component {@route={{@route}}@query={{or @query (hash)}}@models={{this.models}}- @current-when={{@currentWhen}}+ @current-when={{and (not @exactUrlMatch) @currentWhen}}title={{@title}}data-link-name={{@linkName}}class={{this.linkClass}}diff --git a/app/assets/stylesheets/common/base/sidebar-section-link.scss b/app/assets/stylesheets/common/base/sidebar-section-link.scssindex a39b467bd10..2590e0dda52 100644--- a/app/assets/stylesheets/common/base/sidebar-section-link.scss+++ b/app/assets/stylesheets/common/base/sidebar-section-link.scss@@ -42,7 +42,8 @@}&--active,- &.active {+ &.active,+ &.exact-url-match {background: var(--d-sidebar-active-background);color: var(--d-sidebar-active-color);font-weight: var(--d-sidebar-active-font-weight);diff --git a/spec/system/custom_sidebar_sections_spec.rb b/spec/system/custom_sidebar_sections_spec.rbindex d3a2374e74f..289c589270c 100644--- a/spec/system/custom_sidebar_sections_spec.rb+++ b/spec/system/custom_sidebar_sections_spec.rb@@ -50,6 +50,26 @@ describe "Custom sidebar sections", type: :system doexpect(sidebar).to have_section_link("My preferences", target: "_self")end+ it "prioritizes exact url matches over ember routes" do+ sidebar_section = SidebarSection.find_by(section_type: "community")+ sidebar_url = Fabricate(:sidebar_url, name: "Topics", value: "/latest")+ sidebar_url_2 =+ Fabricate(:sidebar_url, name: "Sorted latest", value: "/latest?ascending=true&order=posts")+ Fabricate(:sidebar_section_link, sidebar_section: sidebar_section, linkable: sidebar_url)+ Fabricate(:sidebar_section_link, sidebar_section: sidebar_section, linkable: sidebar_url_2)++ sign_in user++ visit("/latest?ascending=true&order=posts")+ expect(sidebar).to have_exact_url_match_link("Sorted latest")+ expect(sidebar).to have_no_active_links+ expect(sidebar).to have_no_exact_url_match_link("everything")++ visit("/latest")+ expect(sidebar).to have_active_link("everything")+ expect(sidebar).to have_no_exact_url_match_link("Sorted latest")+ end+it "allows the user to create custom section with `/` path" doSiteSetting.top_menu = "read|posted|latest"diff --git a/spec/system/page_objects/components/navigation_menu/sidebar.rb b/spec/system/page_objects/components/navigation_menu/sidebar.rbindex df6fea28275..11d9f52baf8 100644--- a/spec/system/page_objects/components/navigation_menu/sidebar.rb+++ b/spec/system/page_objects/components/navigation_menu/sidebar.rb@@ -105,6 +105,22 @@ module PageObjectsdef has_my_messages_link_with_unread_count?page.has_css?("#{my_messages_link_css} .sidebar-section-link-content-badge")end++ def has_active_link?(name)+ page.has_css?(".sidebar-section-link[data-link-name='#{name}'].active")+ end++ def has_no_active_links?+ page.has_no_css?(".sidebar-section-link.active")+ end++ def has_exact_url_match_link?(name)+ page.has_css?(".sidebar-section-link[data-link-name='#{name}'].exact-url-match")+ end++ def has_no_exact_url_match_link?(name)+ page.has_no_css?(".sidebar-section-link[data-link-name='#{name}'].exact-url-match")+ endendendend