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

NavList: Add aria-current to active nav item #2075

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/flat-bulldogs-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

NavList: Add aria-current to active nav item
2 changes: 2 additions & 0 deletions app/components/primer/alpha/nav_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export class NavListElement extends HTMLElement {
if (currentlySelectedItem) this.#deselect(currentlySelectedItem)

navItem.classList.add('ActionListItem--navActive')
navItem.children[0].setAttribute('aria-current', 'page')

const parentMenu = this.#findParentMenu(navItem)

Expand All @@ -230,6 +231,7 @@ export class NavListElement extends HTMLElement {

#deselect(navItem: HTMLElement) {
navItem.classList.remove('ActionListItem--navActive')
navItem.children[0].removeAttribute('aria-current')
camertron marked this conversation as resolved.
Show resolved Hide resolved

const parentMenu = this.#findParentMenu(navItem)

Expand Down
2 changes: 2 additions & 0 deletions app/components/primer/alpha/nav_list/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def before_render
)
end

@content_arguments[:"aria-current"] = "page" if active?

super

raise "Cannot render a trailing action for an item with subitems" if items.present? && trailing_action.present?
Expand Down
2 changes: 1 addition & 1 deletion previews/primer/alpha/nav_list_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Alpha
class NavListPreview < ViewComponent::Preview
# @label Playground
def playground
render(Primer::Alpha::NavList.new(aria: { label: "Repository settings" }, selected_item_id: :code_review_limits)) do |list|
render(Primer::Alpha::NavList.new(aria: { label: "Repository settings" }, selected_item_id: :collaborators)) do |list|
list.with_heading(title: "Repository settings")

list.with_item(label: "General", href: "/general") do |item|
Expand Down
6 changes: 3 additions & 3 deletions test/components/alpha/nav_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_selected_item
end

refute_selector ".ActionListItem--navActive", text: "Item 1"
assert_selector ".ActionListItem--navActive", text: "Item 2"
assert_selector ".ActionListItem--navActive [aria-current=page]", text: "Item 2"
end

def test_item_can_be_selected_by_any_of_its_ids
Expand All @@ -108,7 +108,7 @@ def test_item_can_be_selected_by_any_of_its_ids
end
end

assert_selector ".ActionListItem--navActive", text: "Item 1"
assert_selector ".ActionListItem--navActive [aria-current=page]", text: "Item 1"
refute_selector ".ActionListItem--navActive", text: "Item 2"
end

Expand Down Expand Up @@ -138,7 +138,7 @@ def test_item_can_be_selected_by_current_page
end

refute_selector ".ActionListItem--navActive", text: "Item 1"
assert_selector ".ActionListItem--navActive", text: "Item 2"
assert_selector ".ActionListItem--navActive [aria-current=page]", text: "Item 2"
end

def test_max_nesting_depth
Expand Down
4 changes: 2 additions & 2 deletions test/system/alpha/nav_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ def select_item_by(id: nil, href: nil)
end

def assert_item_id_selected(item_id)
assert_selector("li[data-item-id='#{item_id}'].ActionListItem--navActive")
assert_selector("li[data-item-id='#{item_id}'].ActionListItem--navActive [aria-current=page]")
end

def assert_item_href_selected(item_href)
assert_selector("li.ActionListItem--navActive a[href='#{item_href}']")
assert_selector("li.ActionListItem--navActive a[href='#{item_href}'][aria-current=page]")
end
end
end