Skip to content

Commit 92455ea

Browse files
committed
feature/Use stimulus to display tabs
1 parent 4d222cd commit 92455ea

File tree

4 files changed

+83
-46
lines changed

4 files changed

+83
-46
lines changed

app/assets/stylesheets/tabs.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.tabs {
2+
&--headers{
3+
&-container {
4+
display: flex;
5+
flex-direction: row;
6+
justify-content: space-between;
7+
}
8+
9+
&-wrapper {
10+
display: flex;
11+
flex-direction: row;
12+
}
13+
}
14+
15+
&--header {
16+
padding: 16px;
17+
border-radius: 2px;
18+
border-bottom: 1px solid lightgray;
19+
20+
&:hover {
21+
cursor: pointer;
22+
background-color: #DEF2F1;
23+
}
24+
25+
&-active {
26+
@extend .tabs--header;
27+
28+
// text-decoration: underline;
29+
// text-decoration-color: #3AAFA9;
30+
border-bottom: 2px solid #3AAFA9;
31+
}
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Controller } from 'stimulus'
2+
3+
export default class extends Controller {
4+
static targets = ['tab', 'panel'];
5+
6+
change(event) {
7+
this.markTabAsActive(event)
8+
this.displayPanel(event)
9+
}
10+
11+
markTabAsActive() {
12+
this.tabTargets.forEach(tab => tab.classList.remove('tabs--header-active'))
13+
event.currentTarget.classList.add('tabs--header-active')
14+
}
15+
16+
displayPanel() {
17+
const index = this.tabTargets.indexOf(event.currentTarget)
18+
const activePanel = this.panelTargets[index]
19+
20+
this.panelTargets.forEach(tab => tab.classList.add('hidden'))
21+
activePanel.classList.remove('hidden')
22+
}
23+
}

app/javascript/tabs.vue

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,3 @@ export default {
4040
}
4141
}
4242
</script>
43-
44-
<style lang="scss" scoped>
45-
.tabs {
46-
&--headers{
47-
&-container {
48-
display: flex;
49-
flex-direction: row;
50-
justify-content: space-between;
51-
}
52-
53-
&-wrapper {
54-
display: flex;
55-
flex-direction: row;
56-
}
57-
}
58-
59-
&--header {
60-
padding: 16px;
61-
border-radius: 2px;
62-
border-bottom: 1px solid lightgray;
63-
64-
&:hover {
65-
cursor: pointer;
66-
background-color: #DEF2F1;
67-
}
68-
69-
&-active {
70-
@extend .tabs--header;
71-
72-
// text-decoration: underline;
73-
// text-decoration-color: #3AAFA9;
74-
border-bottom: 2px solid #3AAFA9;
75-
}
76-
}
77-
}
78-
</style>

app/views/users/show.html.erb

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,33 @@
55
/>
66
</card>
77

8-
<%# <card> %>
9-
<tabs>
10-
<tab name="<%= @user.snippets_count %> Snippets" selected="true">
8+
<div data-controller="tabs">
9+
<div class="tabs--headers-container">
10+
<div></div>
11+
<div class="tabs--headers-wrapper">
12+
<div data-target="tabs.tab" data-action="click->tabs#change" class="tabs--header tabs--header-active">
13+
<span><%= @user.snippets.size %> Snippets</span>
14+
</div>
15+
<div data-target="tabs.tab" data-action="click->tabs#change" class="tabs--header">
16+
<span><%= @followers.size %> Followers</span>
17+
</div>
18+
<div data-target="tabs.tab" data-action="click->tabs#change" class="tabs--header">
19+
<span><%= @following.size %> Following</span>
20+
</div>
21+
</div>
22+
</div>
23+
24+
<div class="tabs-details">
25+
<div data-target="tabs.panel">
1126
<% @user.snippets.order(created_at: :desc).each do |snippet| %>
1227
<snippet-preview
1328
:key="<%= snippet.id %>"
1429
:snippet="<%= snippet.serialize(current_user).to_json %>">
1530
</snippet-preview>
1631
<% end %>
17-
</tab>
18-
<tab name="<%= @followers.size %> Followers">
32+
</div>
33+
34+
<div data-target="tabs.panel" class="hidden">
1935
<% @followers.each_with_index do |follower, index| %>
2036
<card>
2137
<a href="<%= user_path(follower)%>" class="follow--container">
@@ -24,8 +40,9 @@
2440
</a>
2541
</card>
2642
<% end %>
27-
</tab>
28-
<tab name="<%= @following.size %> Following">
43+
</div>
44+
45+
<div data-target="tabs.panel" class="hidden">
2946
<% @following.each_with_index do |following_user, index| %>
3047
<card>
3148
<a href="<%= user_path(following_user)%>" class="follow--container">
@@ -34,6 +51,6 @@
3451
</a>
3552
</card>
3653
<% end %>
37-
</tab>
38-
</tabs>
39-
<%# </card> %>
54+
</div>
55+
</div>
56+
</div>

0 commit comments

Comments
 (0)