Skip to content

Commit fcd73b2

Browse files
committed
Implement current page controller to update active state of nav links
1 parent c1049f1 commit fcd73b2

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.updateActiveLink();
12+
this.updateSelectedOption();
13+
this.updateActiveLink = this.updateActiveLink.bind(this);
14+
15+
window.addEventListener('turbo:before-render', this.updateActiveLink);
16+
window.addEventListener('turbo:before-frame-render', this.updateActiveLink);
17+
}
18+
19+
disconnect() {
20+
window.removeEventListener('turbo:before-render', this.updateActiveLink);
21+
window.removeEventListener(
22+
'turbo:before-frame-render',
23+
this.updateActiveLink,
24+
);
25+
}
26+
27+
updateActiveLink(event) {
28+
console.log('updateActiveLink');
29+
this.element.querySelectorAll('a').forEach((link) => {
30+
if (link.getAttribute('href') === this.currentUrl) {
31+
link.classList.add('active');
32+
link.closest('li').classList.add('active');
33+
} else {
34+
link.classList.remove('active');
35+
link.closest('li').classList.remove('active');
36+
}
37+
});
38+
}
39+
40+
updateSelectedOption() {
41+
this.element.querySelectorAll('option').forEach((option) => {
42+
if (option.getAttribute('value') === this.currentUrl) {
43+
option.selected = true;
44+
}
45+
});
46+
}
47+
48+
get currentUrl() {
49+
return window.location.pathname;
50+
}
51+
}

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/views/topics/nav.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def topics_select
2424
select(
2525
name: "topic",
2626
data: {
27-
:controller => "select-nav",
27+
:controller => "select-nav current-page",
2828
:action => "select-nav#visit",
2929
"select-nav-turbo-frame-value" => "topics-mainbar"
3030
}
@@ -43,20 +43,27 @@ def topics_select
4343
end
4444

4545
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-
}
46+
ul(data: {controller: "current-page"}) do
47+
topics.each do |topic|
48+
li(
49+
id: dom_id(topic),
50+
class: [
51+
"text-small p-1 hidden lg:block"
52+
]
5353
) do
54-
topic.name
55-
end
54+
a(
55+
href: topic_path(topic),
56+
data: {
57+
turbo_frame: "topics-mainbar"
58+
}
59+
) do
60+
topic.name
61+
end
5662

57-
whitespace
58-
span(class: "text-faint") do
59-
plain "(#{topic.pages_count})"
63+
whitespace
64+
span(class: "text-faint") do
65+
plain "(#{topic.pages_count})"
66+
end
6067
end
6168
end
6269
end

0 commit comments

Comments
 (0)