Skip to content

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

FIX: topic-navigation crash when viewport crosses mobile/desktop threshold (#36590)

Régis Hanolb109922acdca
diff --git a/frontend/discourse/app/components/topic-navigation.gjs b/frontend/discourse/app/components/topic-navigation.gjs
index e4325094a5e..3acde3be9d8 100644
--- a/frontend/discourse/app/components/topic-navigation.gjs
+++ b/frontend/discourse/app/components/topic-navigation.gjs
@@ -30,6 +30,8 @@ export default class TopicNavigation extends Component {
canRender = true;
_lastTopicId = null;
_swipeEvents = null;
+ _desktopListenersActive = false;
+ _mobileListenersActive = false;
didUpdateAttrs() {
super.didUpdateAttrs(...arguments);
@@ -232,6 +234,7 @@ export default class TopicNavigation extends Component {
.on("topic:keyboard-trigger", this, this.keyboardTrigger);
if (this.site.desktopView) {
+ this._desktopListenersActive = true;
this.mediaQuery = matchMedia(`(min-width: ${MIN_WIDTH_TIMELINE}px)`);
this.mediaQuery.addEventListener("change", this._checkSize);
this.appEvents.on("composer:opened", this, this.composerOpened);
@@ -244,6 +247,7 @@ export default class TopicNavigation extends Component {
this._checkSize();
this._swipeEvents = new SwipeEvents(this.element);
if (this.site.mobileView) {
+ this._mobileListenersActive = true;
this._swipeEvents.addTouchListeners();
this.element.addEventListener("swipestart", this.onSwipeStart);
this.element.addEventListener("swipeend", this.onSwipeEnd);
@@ -262,7 +266,7 @@ export default class TopicNavigation extends Component {
$(window).off("click.hide-fullscreen");
- if (this.site.desktopView) {
+ if (this._desktopListenersActive) {
this.mediaQuery.removeEventListener("change", this._checkSize);
this.appEvents.off("composer:opened", this, this.composerOpened);
this.appEvents.off("composer:resize-ended", this, this.composerOpened);
@@ -270,7 +274,8 @@ export default class TopicNavigation extends Component {
this.appEvents.off("composer:preview-toggled", this._checkSize);
$("#reply-control").off("div-resized", this._checkSize);
}
- if (this.site.mobileView) {
+
+ if (this._mobileListenersActive) {
this.element.removeEventListener("swipestart", this.onSwipeStart);
this.element.removeEventListener("swipeend", this.onSwipeEnd);
this.element.removeEventListener("swipecancel", this.onSwipeCancel);
diff --git a/spec/system/mobile_mode_spec.rb b/spec/system/mobile_mode_spec.rb
index ae36eb10fd3..d6a332bc2f5 100644
--- a/spec/system/mobile_mode_spec.rb
+++ b/spec/system/mobile_mode_spec.rb
@@ -20,4 +20,30 @@ RSpec.describe "Viewport-based mobile mode", type: :system do
expect(page).not_to have_css("html.desktop-view")
end
end
+
+ context "when resizing viewport while on a topic page" do
+ fab!(:topic)
+ fab!(:post) { Fabricate(:post, topic:) }
+
+ it "handles navigation after resize from mobile to desktop" do
+ resize_window(width: 400) do
+ visit "/t/#{topic.slug}/#{topic.id}"
+ expect(page).to have_css("html.mobile-view")
+ end
+
+ click_logo
+ expect(page).to have_css(".topic-list")
+ end
+
+ it "handles navigation after resize from desktop to mobile" do
+ visit "/t/#{topic.slug}/#{topic.id}"
+ expect(page).to have_css("html.desktop-view")
+
+ resize_window(width: 400) do
+ expect(page).to have_css("html.mobile-view")
+ click_logo
+ expect(page).to have_css(".topic-list")
+ end
+ end
+ end
end