Skip to content

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

UX: Compact nested replies layout on mobile (#44967)

Rafael dos Santos Silva3a7581a94f76
diff --git a/app/assets/stylesheets/common/nested-view.scss b/app/assets/stylesheets/common/nested-view.scss
index 40888aacef2..c0878498215 100644
--- a/app/assets/stylesheets/common/nested-view.scss
+++ b/app/assets/stylesheets/common/nested-view.scss
@@ -1354,77 +1354,82 @@
}
// ── Individual nested post overrides ──
- // Gutter stays wide enough for the avatar so sticky avatars remain attached
- // to the mobile rail, while children remain indented in the content column.
+ // Avatars render inline in the header (see the post template), so the
+ // gutter only holds a thin rail and each level costs ~12px of indent.
.nested-post {
- --nested-line-gutter: var(--nested-avatar-size);
+ --nested-line-gutter: 0.5rem;
+ --nested-line-width: 2px;
--nested-column-gap: var(--space-1);
- --nested-mobile-line-offset: calc(var(--nested-line-gutter) / 2);
- --nested-line-center-adjust: var(--nested-line-width);
- --nested-mobile-line-nudge: calc(-1 * var(--space-1));
- --nested-mobile-line-start: calc(var(--nested-avatar-size) + 6px);
- grid-template-columns: var(--nested-line-gutter) 1fr;
+ --nested-vertical-gap: var(--space-1);
+ --nested-rail-color: var(--primary-low);
+ grid-template-columns: var(--nested-line-gutter) minmax(0, 1fr);
column-gap: var(--nested-column-gap);
- // Tighter vertical spacing between siblings
- --nested-vertical-gap: 0.25em;
-
- // Keep the gutter bounded to the post row on mobile too. The depth-line
- // remains available as the collapse target for this post, but the sticky
- // avatar stops with the post instead of following the entire child subtree.
> .nested-post__gutter {
grid-row: 1;
position: relative;
- z-index: 1;
- pointer-events: none;
+ margin-bottom: 0;
+
+ // Tint the rail with the poster's avatar colour, clamping lightness so
+ // near-white or near-black avatars stay visible in both colour schemes.
+ &.--avatar-color {
+ --nested-rail-color: color-mix(
+ in srgb,
+ var(--nested-avatar-color) 75%,
+ var(--primary-medium)
+ );
- .topic-avatar,
- .nested-post__placeholder-avatar {
- pointer-events: auto;
+ @supports (color: oklch(from red l c h)) {
+ --nested-rail-color: oklch(
+ from var(--nested-avatar-color) clamp(0.55, l, 0.72) c h
+ );
+ }
}
}
- // Depth-line: vertical bar under the avatar, no decorative hooks.
+ // Posts without a collapse target (e.g. capped at max depth) still get
+ // a rail so every level reads the same.
+ > .nested-post__gutter:not(:has(.nested-post__depth-line))::after {
+ content: "";
+ position: absolute;
+ left: calc(50% - var(--nested-line-width) / 2);
+ top: var(--space-1);
+ bottom: var(--space-1);
+ width: var(--nested-line-width);
+ border-radius: var(--nested-line-width);
+ background: var(--nested-rail-color);
+ }
+
+ // The rail spans this post only; children draw their own, one level in.
> .nested-post__gutter .nested-post__depth-line {
position: absolute;
- top: var(--nested-mobile-line-start);
- bottom: 0;
- left: var(--nested-mobile-line-nudge);
+ inset: 0;
min-height: 0;
- pointer-events: auto;
- &::after {
- left: calc(
- var(--nested-mobile-line-offset) - var(--nested-line-center-adjust)
- );
- top: 0;
- bottom: 0;
+ // Widen the tap target beyond the visible rail.
+ &::before {
+ content: "";
+ position: absolute;
+ inset: 0 calc(-1 * var(--nested-column-gap)) 0 -0.25rem;
}
- // Override the desktop L-bend on collapsed lines — straight bar
+ &::after,
&--collapsed::after {
- border: none;
- background: var(--primary-low);
+ left: calc(50% - var(--nested-line-width) / 2);
+ top: var(--space-1);
+ bottom: var(--space-1);
width: var(--nested-line-width);
- border-radius: 0;
- left: calc(
- var(--nested-mobile-line-offset) - var(--nested-line-center-adjust)
- );
- bottom: 0;
+ border: none;
+ border-radius: var(--nested-line-width);
+ background: var(--nested-rail-color);
+ opacity: 1;
}
}
- // Hide the collapse icon — tapping the line itself collapses
.nested-post__depth-line-icon {
display: none;
}
- .nested-post__collapsed-bar {
- padding-inline: 0;
- }
-
- // Mobile strips desktop connector hooks, so the desktop padding used to
- // make room for those hooks must not shift the post's internal grid math.
&:not(.--depth-0) {
padding-top: 0;
@@ -1434,38 +1439,157 @@
}
}
- &:not(.--depth-0) + &:not(.--depth-0) {
+ &:not(.--depth-0) + &:not(.--depth-0),
+ .nested-post-children > &:first-child {
margin-top: var(--nested-vertical-gap);
}
- // No hover interaction on touch — depth-line handles collapse
> .nested-post__parent-line-btn {
display: none;
}
- // Root threads rendered as full-width white cards.
- // padding-left: 0 so the gutter starts at the card edge (= viewport
- // edge after negative margins), giving consistent line spacing.
+ // Root threads are full-width cards, so the card edge already separates
+ // them; dropping their rail gives every level below one step of indent.
+ // The depth-line stays in the DOM (0-width) for assistive tech, and the
+ // header tap covers touch.
&.--depth-0 {
+ grid-template-columns: 0 minmax(0, 1fr);
+ column-gap: 0;
background: var(--secondary);
border-radius: 0;
- padding: 0.5em 0.5em 0.5em 0;
+ padding: var(--space-2);
margin-top: 0;
+
+ > .nested-post__gutter::after,
+ > .nested-post__gutter .nested-post__depth-line::before,
+ > .nested-post__gutter .nested-post__depth-line::after {
+ content: none;
+ }
}
- // Compact post headers
+ // Single-line header: avatar · name · title ········ date
.nested-post__header {
- flex-wrap: wrap;
+ align-items: center;
gap: var(--space-2);
+ min-height: var(--nested-avatar-size);
+
+ > .topic-avatar {
+ flex: none;
+ width: auto;
+ margin: 0;
+ padding: 0;
+ border-top: none;
+ float: none;
+ }
.topic-meta-data {
+ display: contents;
margin-left: 0;
+
+ .names {
+ flex: 0 1 auto;
+ flex-direction: row;
+ flex-wrap: nowrap;
+ align-items: baseline;
+ gap: var(--space-1);
+ min-width: 0;
+ overflow: hidden;
+ white-space: nowrap;
+
+ span {
+ margin: 0;
+ }
+
+ .first {
+ flex: none;
+ font-size: var(--font-down-1);
+ }
+
+ // One identity per post; .first already follows the
+ // username/full-name priority setting.
+ .second {
+ display: none;
+ }
+
+ .user-title {
+ flex: 0 1 auto;
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ font-size: var(--font-down-2);
+ font-weight: normal;
+ color: var(--primary-medium);
+
+ a {
+ color: inherit;
+ }
+ }
+ }
+ }
+
+ .post-infos {
+ flex: none;
+ order: 2;
+ transform: none;
}
}
+ // Fills the gap between name and date; bleeds vertically for a
+ // comfortable tap target without growing the header.
+ .nested-post__header-collapse {
+ order: 1;
+ flex: 1 1 0;
+ align-self: stretch;
+ min-width: 0;
+ margin-block: calc(-1 * var(--space-2));
+ padding: 0;
+ border: none;
+ background: none;
+ cursor: pointer;
+ }
+
.nested-post__content {
overflow-x: auto;
+ margin-top: var(--space-1);
}
+
+ .nested-post__menu {
+ margin: 0;
+
+ nav.post-controls .actions .btn {
+ min-height: 0;
+ padding-block: var(--space-1);
+ }
+ }
+
+ .nested-post__collapsed-bar {
+ padding-inline: 0;
+ }
+
+ .nested-post__collapsed-avatar {
+ display: flex;
+ }
+
+ .nested-post__placeholder--ignored {
+ min-height: var(--nested-avatar-size);
+ padding-bottom: var(--space-2);
+ }
+
+ .nested-post__placeholder--deleted {
+ margin-top: 0;
+ padding-bottom: var(--space-2);
+ }
+
+ .nested-post__placeholder-avatar {
+ position: static;
+ flex: none;
+ }
+ }
+
+ // Set on the element itself so it wins over the depth colour it inherits.
+ .nested-post__depth-line:focus-visible,
+ .nested-post__depth-line--highlighted {
+ --nested-rail-color: var(--tertiary);
}
}
diff --git a/frontend/discourse/app/components/nested/post.gjs b/frontend/discourse/app/components/nested/post.gjs
index 9ed213072f6..29fa5f13afa 100644
--- a/frontend/discourse/app/components/nested/post.gjs
+++ b/frontend/discourse/app/components/nested/post.gjs
@@ -6,6 +6,7 @@ import { action } from "@ember/object";
import { getOwner } from "@ember/owner";
import { cancel, scheduleOnce } from "@ember/runloop";
import { service } from "@ember/service";
+import { trustHTML } from "@ember/template";
import { modifier } from "ember-modifier";
import ShareTopicModal from "discourse/components/modal/share-topic";
import NestedRepliesExpandButton from "discourse/components/nested-replies-expand-button";
@@ -26,6 +27,7 @@ import { nativeShare } from "discourse/lib/pwa-utils";
import { clipboardCopy } from "discourse/lib/utilities";
import { and, not, or } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
+import dBoundAvatarTemplate from "discourse/ui-kit/helpers/d-bound-avatar-template";
import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
@@ -33,6 +35,31 @@ import nestedPostUrl from "../../lib/nested-post-url.js";
import processNode from "../../lib/process-node.js";
import NestedPostChildren from "./post-children.gjs";
+const NestedPostAvatar = <template>
+ {{#if @isDeletedPlaceholder}}
+ <div class="nested-post__placeholder-avatar">
+ {{dIcon "trash-can"}}
+ </div>
+ {{else if @isIgnoredPlaceholder}}
+ <button
+ aria-label={{i18n "nested_replies.toggle_ignored_content"}}
+ class="nested-post__placeholder-avatar nested-post__placeholder-avatar--reveal"
+ data-post-number={{@post.post_number}}
+ disabled={{@loading}}
+ type="button"
+ {{on "click" @reveal}}
+ >
+ {{#if @loading}}
+ {{dIcon "spinner" class="fa-spin"}}
+ {{else}}
+ {{dIcon "far-eye-slash"}}
+ {{/if}}
+ </button>
+ {{else}}
+ <PostAvatar @post={{@post}} @size="small" />
+ {{/if}}
+</template>;
+
export default class NestedPost extends Component {
@service appEvents;
@service capabilities;
@@ -154,6 +181,13 @@ export default class NestedPost extends Component {
});
}
+ get avatarColorStyle() {
+ const color = this.args.post.avatar_color;
+ if (this.isMobile && /^[0-9a-f]{6}$/i.test(color ?? "")) {
+ return trustHTML(`--nested-avatar-color: #${color}`);
+ }
+ }
+
get depthClass() {
return `--depth-${this.args.depth}`;
}
@@ -628,29 +662,24 @@ export default class NestedPost extends Component {
{{on "mouseleave" @unhighlightParentLine}}
></button>
{{/if}}
- <div class="nested-post__gutter">
- {{#if this.isDeletedPlaceholder}}
- <div class="nested-post__placeholder-avatar">
- {{dIcon "trash-can"}}
- </div>
- {{else if this.renderIgnoredPlaceholder}}
- <button
- aria-label={{i18n "nested_replies.toggle_ignored_content"}}
- class="nested-post__placeholder-avatar nested-post__placeholder-avatar--reveal"
- data-post-number={{@post.post_number}}
- disabled={{this.loadingIgnoredContent}}
- type="button"
- {{on "click" this.revealIgnoredContent}}
- >
- {{#if this.loadingIgnoredContent}}
- {{dIcon "spinner" class="fa-spin"}}
- {{else}}
- {{dIcon "far-eye-slash"}}
- {{/if}}
- </button>
- {{else}}
- <PostAvatar @post={{@post}} @size="small" />
- {{/if}}
+ <div
+ class={{dConcatClass
+ "nested-post__gutter"
+ (if this.avatarColorStyle "--avatar-color")
+ }}
+ style={{this.avatarColorStyle}}
+ >
+ {{! Mobile renders the avatar inline in the header so the gutter
+ only needs to be as wide as the thread rail. }}
+ {{#unless this.isMobile}}
+ <NestedPostAvatar
+ @isDeletedPlaceholder={{this.isDeletedPlaceholder}}
+ @isIgnoredPlaceholder={{this.renderIgnoredPlaceholder}}
+ @loading={{this.loadingIgnoredContent}}
+ @post={{@post}}
+ @reveal={{this.revealIgnoredContent}}
+ />
+ {{/unless}}
{{#if (and this.showDepthLine (not this.effectiveCollapsed))}}
<button
aria-label={{this.depthLineLabel}}
@@ -693,6 +722,13 @@ export default class NestedPost extends Component {
"nested_replies.ignored_post_placeholder"
}}</span>
{{else}}
+ {{! Mobile has no gutter avatar, so keep the poster
+ recognisable once the header is collapsed away. }}
+ {{#if this.isMobile}}
+ <span class="nested-post__collapsed-avatar">
+ {{dBoundAvatarTemplate @post.avatar_template "small"}}
+ </span>
+ {{/if}}
<span
class="nested-post__collapsed-username"
>{{@post.username}}</span>
@@ -711,6 +747,15 @@ export default class NestedPost extends Component {
data-post-number={{@post.post_number}}
>
<div class="nested-post__placeholder-actions">
+ {{#if this.isMobile}}
+ <NestedPostAvatar
+ @isDeletedPlaceholder={{this.isDeletedPlaceholder}}
+ @isIgnoredPlaceholder={{this.renderIgnoredPlaceholder}}
+ @loading={{this.loadingIgnoredContent}}
+ @post={{@post}}
+ @reveal={{this.revealIgnoredContent}}
+ />
+ {{/if}}
<span class="nested-post__placeholder-label">{{i18n
"nested_replies.deleted_post_placeholder"
}}</span>
@@ -751,6 +796,15 @@ export default class NestedPost extends Component {
data-post-number={{@post.post_number}}
>
<div class="nested-post__placeholder-actions">
+ {{#if this.isMobile}}
+ <NestedPostAvatar
+ @isDeletedPlaceholder={{this.isDeletedPlaceholder}}
+ @isIgnoredPlaceholder={{this.renderIgnoredPlaceholder}}
+ @loading={{this.loadingIgnoredContent}}
+ @post={{@post}}
+ @reveal={{this.revealIgnoredContent}}
+ />
+ {{/if}}
<span class="nested-post__placeholder-label">{{i18n
"nested_replies.ignored_post_placeholder"
}}</span>
@@ -777,6 +831,9 @@ export default class NestedPost extends Component {
<PostNotice @post={{@post}} />
{{/if}}
<div class="nested-post__header">
+ {{#if this.isMobile}}
+ <PostAvatar @post={{@post}} @size="small" />
+ {{/if}}
<PluginOutlet
@name="post-metadata"
@outletArgs={{postOutletArgs}}
@@ -802,6 +859,17 @@ export default class NestedPost extends Component {
"nested_replies.pinned_reply"
}}</span>
{{/if}}
+ {{! Pointer-only shortcut for the depth line, which
+ remains the accessible collapse control. }}
+ {{#if (and this.isMobile this.showDepthLine)}}
+ <button
+ aria-hidden="true"
+ class="nested-post__header-collapse"
+ tabindex="-1"
+ type="button"
+ {{on "click" this.handleDepthLine}}
+ ></button>
+ {{/if}}
</div>
<div class="nested-post__content regular">
<PluginOutlet
diff --git a/frontend/discourse/tests/integration/components/nested/post-test.gjs b/frontend/discourse/tests/integration/components/nested/post-test.gjs
index a9f864139cd..7ff86abdcfd 100644
--- a/frontend/discourse/tests/integration/components/nested/post-test.gjs
+++ b/frontend/discourse/tests/integration/components/nested/post-test.gjs
@@ -161,7 +161,7 @@ module("Integration | Component | Nested | Post", function (hooks) {
);
});
- test("mobile collapsed posts keep an avatar in the gutter", async function (assert) {
+ test("mobile collapsed posts keep an avatar in the collapsed bar", async function (assert) {
const site = getOwner(this).lookup("service:site");
sinon.stub(site, "mobileView").value(true);
@@ -170,11 +170,11 @@ module("Integration | Component | Nested | Post", function (hooks) {
assert.dom(".nested-post__article").doesNotExist("collapses the post body");
assert
- .dom(".nested-post__gutter .topic-avatar")
- .exists("renders the post avatar area in the mobile gutter");
+ .dom(".nested-post__collapsed-bar .nested-post__collapsed-avatar img")
+ .exists("renders the poster avatar in the collapsed bar");
assert
- .dom(".nested-post__collapsed-avatar")
- .doesNotExist("does not duplicate the avatar in the collapsed bar");
+ .dom(".nested-post__gutter .topic-avatar")
+ .doesNotExist("keeps the mobile gutter avatar-free");
});
test("post registration can update post topic", async function (assert) {
diff --git a/lib/nested_replies/post_tree_serializer.rb b/lib/nested_replies/post_tree_serializer.rb
index 24a4909e5fd..172ab779230 100644
--- a/lib/nested_replies/post_tree_serializer.rb
+++ b/lib/nested_replies/post_tree_serializer.rb
@@ -43,6 +43,7 @@ module NestedReplies
json[:direct_reply_count] = reply_counts[post.post_number] || 0
json[:total_descendant_count] = descendant_counts[post.id] || 0
+ json[:avatar_color] = avatar_color(post.user)
if post.deleted_at.present?
json[:deleted_post_placeholder] = true
@@ -84,6 +85,18 @@ module NestedReplies
private
+ # Hex colour that represents the poster's avatar, used to tint thread rails.
+ # Uploaded avatars rely on the backfilled dominant colour, so this can be nil.
+ def avatar_color(user)
+ return if user.nil?
+
+ if user.uploaded_avatar_id
+ user.uploaded_avatar&.dominant_color.presence
+ elsif SiteSetting.default_avatars.blank?
+ User.letter_avatar_color(User.normalize_username(user.username))
+ end
+ end
+
def topic_view_json
@topic_view_json ||=
TopicViewSerializer.new(@topic_view, scope: @guardian, root: false).as_json
diff --git a/lib/nested_replies/tree_loader.rb b/lib/nested_replies/tree_loader.rb
index 1615b4b9ef8..02fa55114d8 100644
--- a/lib/nested_replies/tree_loader.rb
+++ b/lib/nested_replies/tree_loader.rb
@@ -11,7 +11,7 @@ module NestedReplies
SIBLINGS_PER_ANCESTOR = 5
POST_INCLUDES = [
- { user: %i[primary_group flair_group] },
+ { user: %i[primary_group flair_group uploaded_avatar] },
:reply_to_user,
:deleted_by,
:incoming_email,
diff --git a/spec/lib/nested_replies/post_tree_serializer_spec.rb b/spec/lib/nested_replies/post_tree_serializer_spec.rb
index 91a5676a6d7..7eb389d869a 100644
--- a/spec/lib/nested_replies/post_tree_serializer_spec.rb
+++ b/spec/lib/nested_replies/post_tree_serializer_spec.rb
@@ -79,5 +79,42 @@ RSpec.describe NestedReplies::PostTreeSerializer do
expect(json).not_to have_key(:ignored_post_placeholder)
end
end
+
+ describe "avatar_color" do
+ it "uses the letter avatar colour when the author has no uploaded avatar" do
+ json = serializer_for(user).serialize_post(reply, {})
+
+ expect(json[:avatar_color]).to eq(
+ User.letter_avatar_color(User.normalize_username(ignored_user.username)),
+ )
+ end
+
+ it "uses the dominant colour of an uploaded avatar" do
+ upload = Fabricate(:image_upload, dominant_color: "a1b2c3")
+ ignored_user.update!(uploaded_avatar: upload)
+
+ json = serializer_for(user).serialize_post(reply.reload, {})
+
+ expect(json[:avatar_color]).to eq("a1b2c3")
+ end
+
+ it "is nil when the uploaded avatar has no dominant colour yet" do
+ upload = Fabricate(:image_upload)
+ upload.update_column(:dominant_color, nil)
+ ignored_user.update!(uploaded_avatar: upload)
+
+ json = serializer_for(user).serialize_post(reply.reload, {})
+
+ expect(json[:avatar_color]).to be_nil
+ end
+
+ it "is nil when custom default avatars are configured" do
+ SiteSetting.default_avatars = "https://example.com/avatar.png"
+
+ json = serializer_for(user).serialize_post(reply, {})
+
+ expect(json[:avatar_color]).to be_nil
+ end
+ end
end
end
diff --git a/spec/system/nested_view_spec.rb b/spec/system/nested_view_spec.rb
index f4e4f3e7f4a..7c023b45d59 100644
--- a/spec/system/nested_view_spec.rb
+++ b/spec/system/nested_view_spec.rb
@@ -492,10 +492,10 @@ RSpec.describe "Nested view" do
end
end
- it "collapses a root branch from the depth line", mobile: true do
+ it "collapses a root branch from its header", mobile: true do
nested_view.visit_nested(topic)
- nested_view.click_depth_line(root_reply)
+ nested_view.click_header_collapse(root_reply)
expect(nested_view).to have_no_mobile_focus
expect(nested_view).to have_collapsed_bar_for(root_reply)
@@ -514,6 +514,16 @@ RSpec.describe "Nested view" do
expect(nested_view).to have_root_post(sibling_root_reply)
end
+ it "collapses a child branch from its header", mobile: true do
+ nested_view.visit_nested(topic)
+
+ nested_view.click_header_collapse(child_reply)
+
+ expect(nested_view).to have_collapsed_bar_for(child_reply)
+ expect(nested_view).to have_no_children_visible_for(child_reply)
+ expect(nested_view).to have_root_post(sibling_root_reply)
+ end
+
it "collapses a branch with hidden replies from the depth line", mobile: true do
nested_view.visit_nested(topic)
diff --git a/spec/system/page_objects/pages/nested_view.rb b/spec/system/page_objects/pages/nested_view.rb
index 1d5d92ff9a1..f388e7cba79 100644
--- a/spec/system/page_objects/pages/nested_view.rb
+++ b/spec/system/page_objects/pages/nested_view.rb
@@ -542,6 +542,11 @@ module PageObjects
self
end
+ def click_header_collapse(post)
+ within(post_container(post)) { find(".nested-post__header-collapse").click }
+ self
+ end
+
def click_collapsed_bar(post)
find(wrapper_selector(post, "> .nested-post__main > .nested-post__collapsed-bar")).click
self