Skip to content

Commit

Permalink
Mark the current admin link in the main mav for accessibility
Browse files Browse the repository at this point in the history
By using the `aria-current` attribute [1] we can mark the current link
in the main nav as active for screen readers.

[1] https://w3c.github.io/aria/#aria-current
  • Loading branch information
waiting-for-dev authored and elia committed Sep 29, 2023
1 parent 8dc406a commit b0c39ca
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
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
4 changes: 4 additions & 0 deletions admin/app/components/solidus_admin/sidebar/item/component.rb
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

0 comments on commit b0c39ca

Please sign in to comment.