Skip to content

Commit dc18b72

Browse files
authored
Merge pull request #410 from matestack/fix_issue_408
Fix issue 408: Transition Component is missing the active class
2 parents 9803ff5 + a6702c6 commit dc18b72

File tree

7 files changed

+84
-7
lines changed

7 files changed

+84
-7
lines changed

app/concepts/matestack/ui/core/app/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const store = new Vuex.Store({
5353
setTimeout(function () {
5454
resolve(response["data"])
5555
commit('setPageTemplate', response["data"])
56-
commit('setCurrentLocation', { path: url, search: document.location.search, origin: document.location.origin })
56+
commit('setCurrentLocation', { path: url.split("?")[0], search: document.location.search, origin: document.location.origin })
5757
matestackEventHub.$emit("page_loaded", url);
5858
if (typeof matestackUiCoreTransitionSuccess !== 'undefined') {
5959
matestackUiCoreTransitionSuccess(url);

app/concepts/matestack/ui/core/transition/transition.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const componentDef = {
99
},
1010
computed: Vuex.mapState({
1111
isActive (state) {
12-
return this.componentConfig["link_path"] === state.currentPath
12+
return (this.componentConfig["link_path"].split("?")[0]) === state.currentPathName
13+
},
14+
isChildActive (state) {
15+
return ((this.componentConfig["link_path"].split("?")[0]) !== state.currentPathName) && (state.currentPathName.indexOf(this.componentConfig["link_path"].split("?")[0]) !== -1)
1316
}
1417
}),
1518
methods: {

app/concepts/matestack/ui/core/transition/transition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def setup
66
@tag_attributes.merge!({
77
"href": link_path,
88
"@click.prevent": navigate_to(link_path),
9-
"v-bind:class": "{ active: isActive }"
9+
"v-bind:class": "{ active: isActive, 'active-child': isChildActive }"
1010
})
1111
end
1212

docs/components/transition.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ transition path: :page1_path, text: 'Click me for a transition'
3030

3131
If no text is present, the transition component expects a block that it then *yields* the usual way.
3232

33+
### Active class
34+
35+
The `transition` component automatically gets the `active` class on the clientside when the current path equals the target path.
36+
37+
When a sub page of a parent `transition` component is currently active, the parent `transition` component gets the `active-child` class. A sub page is recognized if the current path is included in the target path of the parent `transition` component:
38+
39+
Parent target: `/some_page`
40+
41+
Currently active: `/some_page/child_page` --> Parent gets `child-active`
42+
43+
Query params do not interfere with this behavior.
44+
3345
## Examples
3446

3547
The transition core component renders the HTML `<a>` tag and performs a page transition

spec/usage/components/transition_spec.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def response
1414
transition path: :page1_path do
1515
button text: "Page 1"
1616
end
17-
transition path: :page2_path, params: { some_other_param: "bar" } do
17+
transition path: :page2_path, params: {some_other_param: "bar" } do
1818
button text: "Page 2"
1919
end
2020
end
@@ -56,6 +56,26 @@ def response
5656
button text: "Back to Page 1"
5757
plain "#{context[:params][:some_other_param]}"
5858
end
59+
transition path: :sub_page2_path do
60+
button text: "Sub Page 2"
61+
end
62+
}
63+
end
64+
65+
end
66+
67+
class Pages::ExampleApp::SubSecondExamplePage < Matestack::Ui::Page
68+
69+
def response
70+
components {
71+
div id: "my-div-on-page-2" do
72+
heading size: 2, text: "This is a Subpage of Page 2"
73+
plain "#{DateTime.now.strftime('%Q')}"
74+
end
75+
transition path: :page1_path do
76+
button text: "Back to Page 1"
77+
plain "#{context[:params][:some_other_param]}"
78+
end
5979
}
6080
end
6181

@@ -72,11 +92,16 @@ def page2
7292
responder_for(Pages::ExampleApp::SecondExamplePage)
7393
end
7494

95+
def sub_page2
96+
responder_for(Pages::ExampleApp::SubSecondExamplePage)
97+
end
98+
7599
end
76100

77101
Rails.application.routes.append do
78102
get 'my_example_app/page1', to: 'example_app_pages#page1', as: 'page1'
79103
get 'my_example_app/page2', to: 'example_app_pages#page2', as: 'page2'
104+
get 'my_example_app/page2/sub_page2', to: 'example_app_pages#sub_page2', as: 'sub_page2'
80105
end
81106
Rails.application.reload_routes!
82107

@@ -206,6 +231,40 @@ def page2
206231
expect(page).to have_selector("body.not-reloaded")
207232
end
208233

234+
it "Example 4 - Sets active class on clientside when target path is current path " do
235+
236+
visit "/my_example_app/page1"
237+
sleep 1
238+
239+
link = find('a.active')
240+
expect(link.text).to eq("Page 1")
241+
242+
click_button("Page 2")
243+
sleep 1
244+
245+
link = find('a.active')
246+
expect(link.text).to eq("Page 2")
247+
248+
# query params should not disturb active class
249+
visit "/my_example_app/page1?some_param=foo"
250+
251+
link = find('a.active')
252+
expect(link.text).to eq("Page 1")
253+
254+
# active sub page sets special active class on parent
255+
visit "/my_example_app/page2"
256+
sleep 1
257+
258+
link = find('a.active')
259+
expect(link.text).to eq("Page 2")
260+
261+
click_button("Sub Page 2")
262+
sleep 1
263+
264+
link = find('a.active-child')
265+
expect(link.text).to eq("Page 2")
266+
end
267+
209268
# supposed to work, but doesn't. Suspect Vue is the culprint here
210269
# it "Example 2 - Perform transition from one page to another by providing route as string (not recommended)" do
211270
#

vendor/assets/javascripts/dist/matestack-ui-core.js

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/assets/javascripts/dist/matestack-ui-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)