generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set active class on current forecast tab
We define a JS TagController which is instantated 3 times, once for each tag. There's some magic (or convention) being used here: TARGET `static targets = [ "day" ]` identifies the DOM element to which we wish to bind: `"data-tab-target" = "day"` We then refer to this element with `this.dayTarget` CLASS HELPER `static classes = [ "active" ]` similar to the `dayTarget` we can now reference our active class as `this.activeClass` As we've now got 4 data attributes I've used the Rails [`tag()` method][] so that we can group our data attributes under the `data` key which I think helps readability: ``` <%= tag.div class: "tab py-4 ...", data: { date: forecast.date.to_s, controller: "tab", "tab-active-class": "active", "tab-target": "day" } do %> ``` [`tag()` method][]: https://apidock.com/rails/ActionView/Helpers/TagHelper/tag
- Loading branch information
Showing
8 changed files
with
89 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
// Connects to data-controller="tab" | ||
export default class extends Controller { | ||
static classes = [ "active" ] | ||
static targets = [ "day" ] | ||
|
||
connect() { | ||
} | ||
|
||
switch_tab() { | ||
this.makeAllTabsInactive() | ||
this.dayTarget.classList.toggle(this.activeClass) | ||
} | ||
|
||
makeAllTabsInactive() { | ||
document.querySelectorAll('.tabs .tab').forEach( | ||
(el) => el.classList.remove(this.activeClass) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
<div class="tab py-4 flex-1 <%= day %> mx-2 px-1 text-center daqi-low border-b-0 border-l-2 border-r-2 border-t-2 border-gray-300" data-date="<%= forecast.date %>" > | ||
<%= link_to update_styled_forecast_path(day: day), data: { turbo_frame: "day_predictions" } do %> | ||
<div class="day"> | ||
<%= forecast.date == Date.today ? 'Today' : forecast.date.strftime('%A') %> | ||
</div> | ||
<div class="date"> | ||
<%= forecast.date.strftime("%d %B") %> | ||
</div> | ||
<div class="daqi-marker text-4xl py-2 text-green-600"> | ||
● | ||
</div> | ||
<div class="daqi-label text-base"> | ||
<%= forecast.air_pollution.label.capitalize %> | ||
</div> | ||
<div class="daqi-value font-normal"> | ||
Index <%= forecast.air_pollution.value %>/10 | ||
</div> | ||
<% end %> | ||
</div> | ||
<%= tag.div class: "tab py-4 flex-1 #{day} mx-2 px-1 text-center daqi-low border-b-0 border-l-2 border-r-2 border-t-2 border-gray-300 #{day == :today ? "active" : ''}", | ||
data: { | ||
date: forecast.date.to_s, | ||
controller: "tab", | ||
"tab-active-class": "active", | ||
"tab-target": "day" | ||
} do | ||
%> | ||
<%= | ||
link_to update_styled_forecast_path(day: day), | ||
data: { | ||
turbo_frame: "day_predictions", | ||
action: "click->tab#switch_tab" | ||
} do %> | ||
<div class="day"> | ||
<%= forecast.date == Date.today ? 'Today' : forecast.date.strftime('%A') %> | ||
</div> | ||
<div class="date"> | ||
<%= forecast.date.strftime("%d %B") %> | ||
</div> | ||
<div class="daqi-marker text-4xl py-2 text-green-600"> | ||
● | ||
</div> | ||
<div class="daqi-label text-base"> | ||
<%= forecast.air_pollution.label.capitalize %> | ||
</div> | ||
<div class="daqi-value font-normal"> | ||
Index <%= forecast.air_pollution.value %>/10 | ||
</div> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
<div data-controller="forecasts"> | ||
<%= render partial: "styled_forecasts/location" %> | ||
<%= render partial: "styled_forecasts/forecast_tabs", forecasts: @forecasts %> | ||
<%= render partial: "styled_forecasts/predictions", locals: { forecast: @forecasts.first } %> | ||
<%= render partial: "sharing" %> | ||
<%= render partial: "learning" %> | ||
<%= render partial: "subscribe" %> | ||
</div> | ||
<%= render partial: "styled_forecasts/location" %> | ||
<%= render partial: "styled_forecasts/forecast_tabs", forecasts: @forecasts %> | ||
<%= render partial: "styled_forecasts/predictions", locals: { forecast: @forecasts.first } %> | ||
<%= render partial: "sharing" %> | ||
<%= render partial: "learning" %> | ||
<%= render partial: "subscribe" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters