Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Mark the current link in the main nav for accessibility #5273

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
hover:text-red-500 hover:bg-gray-50
<%= link_active_classes %>
<%= link_level_classes %>
">
"
aria-current="<%= aria_current %>"
>
<%= icon %>
<%= name %>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ def nested_items
def active?
@item.active?(@url_helpers, @fullpath)
end

def aria_current
@item.current?(@url_helpers, @fullpath) ? "page" : "false"
end
end
20 changes: 17 additions & 3 deletions admin/lib/solidus_admin/main_nav_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,31 @@ def path(url_helpers)
end
end

# Returns whether the item should be marked as current
#
# An item is considered the current one if its base path (that is, the path
# without any query parameters) matches the given full path.
#
# @param url_helpers [#solidus_admin, #spree] context object giving access
# to url helpers
# @param fullpath [String] the full path of the current request
# @return [Boolean]
def current?(url_helpers, fullpath)
path(url_helpers) == fullpath.gsub(/\?.*$/, '')
end

# Returns whether the item should be marked as active
#
# An item is considered active if its base path (that is, the path without
# any query parameters) matches the given full path.
# An item is considered active when it is the current item or any of its
# children is active.
#
# @param url_helpers [#solidus_admin, #spree] context object giving access
# to url helpers
# @param fullpath [String] the full path of the current request
# @return [Boolean]
# @see #current?
def active?(url_helpers, fullpath)
(path(url_helpers) == fullpath.gsub(/\?.*$/, '')) ||
current?(url_helpers, fullpath) ||
children.any? { |child| child.active?(url_helpers, fullpath) }
end
end
Expand Down
37 changes: 34 additions & 3 deletions admin/spec/solidus_admin/main_nav_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ def url_helpers(solidus_admin: {}, spree: {})
end
end

describe "#active?" do
describe "#current?" do
it "returns true when the path matches the current request path" do
item = described_class.new(key: "foo", route: :foo_path, position: 1)
url_helpers = url_helpers(solidus_admin: { foo_path: "/foo" })

expect(
item.active?(url_helpers, "/foo")
item.current?(url_helpers, "/foo")
).to be(true)
end

Expand All @@ -106,14 +106,45 @@ def url_helpers(solidus_admin: {}, spree: {})
url_helpers = url_helpers(solidus_admin: { foo_path: "/foo" })

expect(
item.active?(url_helpers, "/foo?bar=baz")
item.current?(url_helpers, "/foo?bar=baz")
).to be(true)
end

it "returns false when the path does not match the current request base path" do
item = described_class.new(key: "foo", route: :foo_path, position: 1)
url_helpers = url_helpers(solidus_admin: { foo_path: "/foo" })

expect(
item.current?(url_helpers, "/bar")
).to be(false)
end
end

describe "#active?" do
it "returns true when it's the current item" do
item = described_class.new(key: "foo", route: :foo_path, position: 1)
url_helpers = url_helpers(solidus_admin: { foo_path: "/foo" })

expect(
item.active?(url_helpers, "/foo")
).to be(true)
end

it "returns true when one of its children is active" do
item = described_class.new(
key: "foo", route: :foo_path, position: 1, children: [described_class.new(key: "bar", route: :bar_path, position: 1)]
)
url_helpers = url_helpers(solidus_admin: { foo_path: "/foo", bar_path: "/bar" })

expect(
item.active?(url_helpers, "/bar")
).to be(true)
end

it "returns false otherwise" do
item = described_class.new(key: "foo", route: :foo_path, position: 1)
url_helpers = url_helpers(solidus_admin: { foo_path: "/foo" })

expect(
item.active?(url_helpers, "/bar")
).to be(false)
Expand Down