Skip to content

Commit 9e2c706

Browse files
authored
Merge pull request #275 from joyofrails/feat/topics-current-page
Topic nav shows active current page
2 parents c1049f1 + 1af1f0a commit 9e2c706

File tree

10 files changed

+93
-23
lines changed

10 files changed

+93
-23
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
3+
import { debug } from '../utils';
4+
5+
const console = debug('app:javascript:controllers:current-page');
6+
7+
export default class extends Controller {
8+
connect() {
9+
console.log('Connected');
10+
11+
this.updateActive();
12+
this.updateActive = this.updateActive.bind(this);
13+
14+
document.addEventListener('turbo:before-render', this.updateActive);
15+
document.addEventListener('turbo:before-frame-render', this.updateActive);
16+
}
17+
18+
disconnect() {
19+
document.removeEventListener('turbo:before-render', this.updateActive);
20+
document.removeEventListener(
21+
'turbo:before-frame-render',
22+
this.updateActive,
23+
);
24+
}
25+
26+
updateActive(event) {
27+
console.log('updateActiveLink');
28+
this.element.querySelectorAll('a').forEach((link) => {
29+
if (link.getAttribute('href') === this.currentUrl) {
30+
link.classList.add('active');
31+
link.closest('li').classList.add('active');
32+
} else {
33+
link.classList.remove('active');
34+
link.closest('li').classList.remove('active');
35+
}
36+
});
37+
38+
this.element.querySelectorAll('option').forEach((option) => {
39+
if (option.getAttribute('value') === this.currentUrl) {
40+
option.selected = true;
41+
}
42+
});
43+
}
44+
45+
get currentUrl() {
46+
return window.location.pathname;
47+
}
48+
}

app/javascript/controllers/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { application } from './application';
66

77
import ClipboardCopy from './clipboard-copy';
8+
import CurrentPage from './current-page';
89
import Darkmode from './darkmode';
910
import Flash from './flash';
1011
import ModalOpener from './modal-opener';
@@ -31,16 +32,19 @@ import SnippetTweet from './snippets/tweet';
3132
import SearchCombobox from './searches/combobox';
3233
import SearchListbox from './searches/listbox';
3334

34-
application.register('modal-opener', ModalOpener);
35-
application.register('analytics', AnalyticsCustomEvent);
3635
application.register('clipboard-copy', ClipboardCopy);
36+
application.register('current-page', CurrentPage);
3737
application.register('darkmode', Darkmode);
3838
application.register('flash', Flash);
39+
application.register('modal-opener', ModalOpener);
40+
3941
application.register('dialog', Dialog);
4042

4143
application.register('pwa-installation', PwaInstallation);
4244
application.register('pwa-web-push-subscription', PwaWebPushSubscription);
4345
application.register('pwa-web-push-demo', PwaWebPushDemo);
46+
47+
application.register('analytics', AnalyticsCustomEvent);
4448
application.register('table-of-contents', TableOfContents);
4549

4650
application.register('frame-form', FrameForm);

app/javascript/css/application.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
@import './components/site-header.css';
3131
@import './components/main-content.css';
3232
@import './components/article-content.css';
33+
@import './components/column-content.css';
3334
@import './components/footer.scss';
3435
@import './components/footer.css';
3536
@import './components/newsletter-banner.css';

app/javascript/css/components/article-content.css

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,3 @@
131131
padding-inline-start: var(--space-s);
132132
}
133133
}
134-
135-
.column-content {
136-
& li:has(+ li) {
137-
margin-bottom: var(--space-xs);
138-
}
139-
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.column-content {
2+
& li.active {
3+
background-color: var(--joy-background-header);
4+
}
5+
}

app/javascript/css/config/theme.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
--joy-background-banner: var(--joy-color-100);
4646
--joy-background-footer: var(--joy-color-200);
4747
--joy-background-blockquote: var(--joy-color-100);
48+
--joy-background-active: var(--joy-color-200);
4849

4950
--joy-background-mask: var(--color-darken);
5051
--joy-background-reset: white;
@@ -103,6 +104,7 @@
103104
--joy-background-banner: var(--color-slate-900);
104105
--joy-background-footer: var(--color-slate-800);
105106
--joy-background-blockquote: var(--color-slate-800);
107+
--joy-background-active: var(--color-slate-800);
106108

107109
--joy-background-mask: var(--color-lighten);
108110
--joy-background-reset: var(--color-slate-950);

app/javascript/css/utilities/custom.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
margin-bottom: var(--grid-gutter);
33
}
44

5+
.mb-xs {
6+
margin-bottom: var(--space-xs);
7+
}
8+
59
.mb-xl {
610
margin-bottom: var(--space-xl);
711
}

app/javascript/css/utilities/tailwind.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@
590590
fill: var(--joy-text);
591591
}
592592

593+
.p-1 {
594+
padding: 0.25rem;
595+
}
596+
593597
.p-2 {
594598
padding: 0.5rem;
595599
}

app/views/topics/nav.rb

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Topics
22
class Nav < ApplicationComponent
33
include Phlex::Rails::Helpers::CurrentPage
44
include Phlex::Rails::Helpers::DOMID
5+
include Phlex::Rails::Helpers::Request
56
include PhlexConcerns::FlexBlock
67

78
attr_reader :topics, :attributes
@@ -24,7 +25,7 @@ def topics_select
2425
select(
2526
name: "topic",
2627
data: {
27-
:controller => "select-nav",
28+
:controller => "select-nav current-page",
2829
:action => "select-nav#visit",
2930
"select-nav-turbo-frame-value" => "topics-mainbar"
3031
}
@@ -33,7 +34,7 @@ def topics_select
3334
topics.each do |topic|
3435
option(
3536
value: topic_path(topic),
36-
selected: current_page?(topic_path(topic))
37+
selected: request&.path == topic_path(topic)
3738
) do
3839
topic.name
3940
end
@@ -43,20 +44,27 @@ def topics_select
4344
end
4445

4546
def topics_list
46-
topics.each do |topic|
47-
div id: dom_id(topic), class: "text-small mb-2 hidden lg:block" do
48-
a(
49-
href: topic_path(topic),
50-
data: {
51-
turbo_frame: "topics-mainbar"
52-
}
47+
ul(data: {controller: "current-page"}) do
48+
topics.each do |topic|
49+
li(
50+
id: dom_id(topic),
51+
class: [
52+
"text-small p-1 hidden lg:block"
53+
]
5354
) do
54-
topic.name
55-
end
55+
a(
56+
href: topic_path(topic),
57+
data: {
58+
turbo_frame: "topics-mainbar"
59+
}
60+
) do
61+
topic.name
62+
end
5663

57-
whitespace
58-
span(class: "text-faint") do
59-
plain "(#{topic.pages_count})"
64+
whitespace
65+
span(class: "text-faint") do
66+
plain "(#{topic.pages_count})"
67+
end
6068
end
6169
end
6270
end

app/views/topics/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<h3 class="mb-8 hidden lg:block"><%= topic.name %></h3>
2121
<ul>
2222
<% topic.pages.as_published_articles.each do |page| %>
23-
<%= tag.li id: dom_id(page) do %>
23+
<%= tag.li id: dom_id(page), class: "mb-xs" do %>
2424
<%= render Pages::Info.new(
2525
title: page.title,
2626
request_path: page.request_path,

0 commit comments

Comments
 (0)