Skip to content

Commit de4d8de

Browse files
committed
Fold threads initially, and toggle them on click
1 parent 59bc3a7 commit de4d8de

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

app/javascript/controllers/thread_controller.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import { Controller } from "@hotwired/stimulus"
22

33
// Connects to data-controller="thread"
44
export default class extends Controller {
5+
static targets = ["children", "icon"]
6+
57
connect() {
8+
// Initially hide children
9+
if (this.hasChildrenTarget) {
10+
this.childrenTarget.classList.add("hidden")
11+
}
12+
}
13+
14+
toggle() {
15+
if (this.hasChildrenTarget) {
16+
this.childrenTarget.classList.toggle("hidden")
17+
18+
// Rotate the icon
19+
if (this.hasIconTarget) {
20+
this.iconTarget.classList.toggle("rotate-90")
21+
}
22+
}
623
}
724
}

app/views/messages/_thread.html.erb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
<div class="thread-message" style="margin-left: <%= depth * 6 %>px;">
1+
<div class="thread-message" style="margin-left: <%= depth * 6 %>px;" <% if (depth == 0) && message.children&.any? %>data-controller="thread"<% end %>>
22
<% if depth == 0 %>
33
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-md border border-gray-200 dark:border-gray-700 overflow-hidden hover:shadow-lg transition-shadow">
4-
<div class="p-5">
4+
<div class="p-5 <% if message.children&.any? %>cursor-pointer<% end %>" <% if message.children&.any? %>data-action="click->thread#toggle"<% end %>>
55
<div class="flex items-start justify-between gap-4">
66
<div class="flex-1 min-w-0">
77
<h2 class="text-lg font-semibold text-gray-900 dark:text-gray-100 hover:text-red-600 dark:hover:text-red-400 transition-colors mb-2">
8+
<% if message.children&.any? %>
9+
<svg class="w-4 h-4 inline-block transition-transform" data-thread-target="icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
10+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
11+
</svg>
12+
<% end %>
813
<span class="px-0.5">[#<%= message.list_seq %>]</span>
9-
<%= link_to without_list_prefix(message.subject), "/#{list.name}/#{message.list_seq}" %>
14+
<%= link_to without_list_prefix(message.subject), "/#{list.name}/#{message.list_seq}", data: {action: 'click->thread#toggle:stop'} %>
1015
<span class="text-sm text-gray-500 dark:text-gray-400 font-normal ml-2"><%= message.from %></span>
1116
</h2>
1217
<div class="flex items-center gap-3 text-sm text-gray-600 dark:text-gray-400">
@@ -38,7 +43,7 @@
3843
<% end %>
3944

4045
<% if message.children&.any? %>
41-
<div class="mt-2">
46+
<div class="mt-2" <% if depth == 0 %>data-thread-target="children"<% end %>>
4247
<%= render partial: 'thread', collection: message.children, as: :message, locals: {list: list, depth: depth + 1} %>
4348
</div>
4449
<% end %>

0 commit comments

Comments
 (0)