Skip to content

Commit 1961fec

Browse files
committed
Add navigation select component for topic mobile view
1 parent d8aab0e commit 1961fec

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Turbo } from '@hotwired/turbo-rails';
2+
import { Controller } from '@hotwired/stimulus';
3+
4+
import { debug } from '../../utils';
5+
6+
const console = debug('app:javascript:controllers:forms:select-nav');
7+
8+
export default class extends Controller {
9+
static values = {
10+
turboFrame: String,
11+
};
12+
13+
connect() {
14+
console.log('Connected');
15+
}
16+
17+
visit(event) {
18+
const [selectedOption] = this.element.selectedOptions;
19+
const url = selectedOption.value;
20+
21+
// Value is empty for blank option
22+
if (url === '') {
23+
return;
24+
}
25+
26+
console.log('Visiting', url, event);
27+
28+
const opts = {
29+
action: 'advance',
30+
};
31+
32+
if (this.turboFrameValue) {
33+
opts.frame = this.turboFrameValue;
34+
}
35+
36+
Turbo.visit(url, opts);
37+
}
38+
}

app/javascript/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import TableOfContents from './table-of-contents';
2020

2121
import FrameForm from './forms/frame';
2222
import AutosubmitForm from './forms/autosubmit';
23+
import SelectNav from './forms/select-nav';
2324
import SyntaxHighlightPreview from './syntax-highlight/preview';
2425

2526
import SnippetPreview from './snippets/preview';
@@ -44,6 +45,7 @@ application.register('table-of-contents', TableOfContents);
4445

4546
application.register('frame-form', FrameForm);
4647
application.register('autosubmit-form', AutosubmitForm);
48+
application.register('select-nav', SelectNav);
4749
application.register('syntax-highlight-preview', SyntaxHighlightPreview);
4850

4951
application.register('snippet-preview', SnippetPreview);

app/views/topics/index.html.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
<div class="column-content container py-gap mb-3xl">
66
<nav class="sidebar" data-turbo-permanent>
77
<%= turbo_frame_tag :topics_sidebar do %>
8+
<%= render Topics::Select.new(topics, class: "lg:hidden") %>
89
<% topics.each do |topic| %>
9-
<%= tag.div id: dom_id(topic), class: "text-small mb-2" do %>
10+
<%= tag.div id: dom_id(topic), class: "text-small mb-2 hidden lg:block" do %>
1011
<%= link_to topic.name, topic_path(topic), data: {turbo_frame: :topics_mainbar} %>
1112
<span class="text-faint">(<%= topic.pages_count %>)</span>
1213
<% end %>
@@ -16,7 +17,7 @@
1617
<div class="mainbar w-full">
1718
<%= turbo_frame_tag :topics_mainbar, data: {turbo_action: "advance"} do %>
1819
<div>
19-
<h2>Welcome!</h2>
20+
<h3 class="mb-8 lg:hidden">Howdy!</h3>
2021
<div>
2122
<p>Here you‘ll find topics currently covered at Joy of Rails.</p>
2223
<p>Choose a topic from the sidebar to view articles.</p>

app/views/topics/select.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Topics
2+
class Select < ApplicationComponent
3+
include Phlex::Rails::Helpers::CurrentPage
4+
include PhlexConcerns::FlexBlock
5+
6+
attr_reader :topics, :attributes
7+
8+
def initialize(topics = [], **attributes)
9+
@topics = topics
10+
@attributes = attributes
11+
end
12+
13+
def view_template
14+
flex_block(attributes) do
15+
label(for: "topic", class: "block text-sm font-medium text-gray-700") do
16+
"Select a topic"
17+
end
18+
select(
19+
name: "topic",
20+
data: {
21+
:controller => "select-nav",
22+
:action => "select-nav#visit",
23+
"select-nav-turbo-frame-value" => "topics_mainbar"
24+
}
25+
) do
26+
option(value: "") { "Start here" }
27+
topics.each do |topic|
28+
option(value: topic_path(topic), select: current_page?(topic_path(topic))) { topic.name }
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end

app/views/topics/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</div>
1818
</div>
1919
<% else %>
20-
<h3 class="mb-8">Topic: <%= topic.name %></h3>
20+
<h3 class="mb-8 hidden lg:block">Topic: <%= topic.name %></h3>
2121
<ul>
2222
<% topic.pages.as_published_articles.each do |page| %>
2323
<%= tag.li id: dom_id(page) do %>

0 commit comments

Comments
 (0)