Previews

No matching results.

x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<nav aria-label="My favorite foods" data-view-component="true">
<nav-list>
<nav-list-group>
<div data-view-component="true">
<div data-view-component="true" class="ActionList-sectionDivider">
<h3 id="heading-title-f73a15b1-cbaa-46a0-934f-730990380812" data-view-component="true" class="ActionList-sectionDivider-title">
My favorite foods
</h3>
</div>
<ul id="foods" role="list" aria-labelledby="heading-title-f73a15b1-cbaa-46a0-934f-730990380812" data-view-component="true" class="ActionListWrap">
<li data-targets="nav-list.items nav-list.items" data-item-id="" data-view-component="true" class="ActionListItem">
<a id="item-26ffd382-21e5-4d8a-9ce4-106bfac95889" href="/foods/popplers" data-view-component="true" class="ActionListContent">
<span data-view-component="true" class="ActionListItem-label">
Popplers
</span></a>
</li>
<li data-targets="nav-list.items nav-list.items" data-item-id="" data-view-component="true" class="ActionListItem">
<a id="item-28d2c5ae-d4b9-4a59-9c13-ac14795723c4" href="/foods/slurm" data-view-component="true" class="ActionListContent">
<span data-view-component="true" class="ActionListItem-label">
Slurm
</span></a>
</li>
</ul>
<div src="/view-components/rails-app/nav_list_items" hidden="hidden" data-target="nav-list-group.showMoreItem" data-action="click:nav-list-group#showMore" data-current-page="1" data-total-pages="2" data-targets="nav-list.items nav-list.items" data-item-id="" data-view-component="true" class="ActionListItem">
<button data-list-id="foods" id="item-4dcb3d53-b0a7-48d9-829f-2cdb764b94f6" data-view-component="true" class="ActionListContent">
<span data-view-component="true" class="ActionListItem-label color-fg-accent">
Show more foods
</span> <span class="ActionListItem-visual ActionListItem-visual--trailing">
<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-plus">
<path d="M7.75 2a.75.75 0 0 1 .75.75V7h4.25a.75.75 0 0 1 0 1.5H8.5v4.25a.75.75 0 0 1-1.5 0V8.5H2.75a.75.75 0 0 1 0-1.5H7V2.75A.75.75 0 0 1 7.75 2Z"></path>
</svg>
</span>
</button>
</div>
</div>
</nav-list-group>
<li role="presentation" aria-hidden="true" data-view-component="true" class="ActionList-sectionDivider"></li>
<nav-list-group>
<div data-view-component="true">
<div data-view-component="true" class="ActionList-sectionDivider">
<h3 id="heading-title-e17dc822-6297-4a71-9a23-afe5da1b14c0" data-view-component="true" class="ActionList-sectionDivider-title">
My favorite snacks
</h3>
</div>
<ul id="snacks" role="list" aria-labelledby="heading-title-e17dc822-6297-4a71-9a23-afe5da1b14c0" data-view-component="true" class="ActionListWrap">
<li data-targets="nav-list.items nav-list.items" data-item-id="" data-view-component="true" class="ActionListItem">
<a id="item-7ab31fca-de69-45af-8804-0532aa021782" href="/foods/popplers" data-view-component="true" class="ActionListContent">
<span data-view-component="true" class="ActionListItem-label">
Popplers
</span></a>
</li>
<li data-targets="nav-list.items nav-list.items" data-item-id="" data-view-component="true" class="ActionListItem">
<a id="item-30a8c888-2d27-4d64-834a-ede2daaf8da4" href="/foods/slurm" data-view-component="true" class="ActionListContent">
<span data-view-component="true" class="ActionListItem-label">
Slurm
</span></a>
</li>
</ul>
<div src="/view-components/rails-app/nav_list_items" hidden="hidden" data-target="nav-list-group.showMoreItem" data-action="click:nav-list-group#showMore" data-current-page="1" data-total-pages="4" data-targets="nav-list.items nav-list.items" data-item-id="" data-view-component="true" class="ActionListItem">
<button data-list-id="snacks" id="item-8e2d4c52-6158-4a55-b5cd-53bb4779299b" data-view-component="true" class="ActionListContent">
<span data-view-component="true" class="ActionListItem-label color-fg-accent">
Show more snacks
</span> <span class="ActionListItem-visual ActionListItem-visual--trailing">
<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-plus">
<path d="M7.75 2a.75.75 0 0 1 .75.75V7h4.25a.75.75 0 0 1 0 1.5H8.5v4.25a.75.75 0 0 1-1.5 0V8.5H2.75a.75.75 0 0 1 0-1.5H7V2.75A.75.75 0 0 1 7.75 2Z"></path>
</svg>
</span>
</button>
</div>
</div>
</nav-list-group>
</nav-list>
</nav>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
render(Primer::Beta::NavList.new(aria: { label: "My favorite foods" })) do |list|
list.with_group(id: "foods") do |group|
group.with_heading(title: "My favorite foods")
group.with_item(label: "Popplers", href: "/foods/popplers")
group.with_item(label: "Slurm", href: "/foods/slurm")
group.with_show_more_item(label: "Show more foods", src: UrlHelpers.nav_list_items_path, pages: 2) do |item|
item.with_trailing_visual_icon(icon: :plus)
end
end
list.with_group(id: "snacks") do |group|
group.with_heading(title: "My favorite snacks")
group.with_item(label: "Popplers", href: "/foods/popplers")
group.with_item(label: "Slurm", href: "/foods/slurm")
group.with_show_more_item(label: "Show more snacks", src: UrlHelpers.nav_list_items_path, pages: 4) do |item|
item.with_trailing_visual_icon(icon: :plus)
end
end
end

app/components/primer/beta/nav_list.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* eslint-disable custom-elements/expose-class-on-global */
import {controller, targets} from '@github/catalyst'
@controller
export class NavListElement extends HTMLElement {
@targets items: HTMLElement[]
selectItemById(itemId: string | null): boolean {
if (!itemId) return false
const selectedItem = this.#findSelectedNavItemById(itemId)
if (selectedItem) {
this.#select(selectedItem)
return true
}
return false
}
selectItemByHref(href: string | null): boolean {
if (!href) return false
const selectedItem = this.#findSelectedNavItemByHref(href)
if (selectedItem) {
this.#select(selectedItem)
return true
}
return false
}
selectItemByCurrentLocation(): boolean {
const selectedItem = this.#findSelectedNavItemByCurrentLocation()
if (selectedItem) {
this.#select(selectedItem)
return true
}
return false
}
// expand collapsible item onClick
expandItem(item: HTMLElement) {
item.nextElementSibling?.removeAttribute('data-hidden')
item.setAttribute('aria-expanded', 'true')
}
collapseItem(item: HTMLElement) {
item.nextElementSibling?.setAttribute('data-hidden', '')
item.setAttribute('aria-expanded', 'false')
item.focus()
}
itemIsExpanded(item: HTMLElement | null) {
if (item?.tagName === 'A') {
return true
}
return item?.getAttribute('aria-expanded') === 'true'
}
// expand/collapse item
handleItemWithSubItemClick(e: Event) {
const el = e.target
if (!(el instanceof HTMLElement)) return
const button = el.closest<HTMLButtonElement>('button')
if (!button) return
if (this.itemIsExpanded(button)) {
this.collapseItem(button)
} else {
this.expandItem(button)
}
/* eslint-disable-next-line no-restricted-syntax */
e.stopPropagation()
}
// collapse item
handleItemWithSubItemKeydown(e: KeyboardEvent) {
const el = e.currentTarget
if (!(el instanceof HTMLElement)) return
let button = el.closest<HTMLButtonElement>('button')
if (!button) {
const button_id = el.getAttribute('aria-labelledby')
if (button_id) {
button = document.getElementById(button_id) as HTMLButtonElement
} else {
return
}
}
if (this.itemIsExpanded(button) && e.key === 'Escape') {
this.collapseItem(button)
}
/* eslint-disable-next-line no-restricted-syntax */
e.stopPropagation()
}
#findSelectedNavItemById(itemId: string): HTMLElement | null {
// First we compare the selected link to data-item-id for each nav item
for (const navItem of this.items) {
if (navItem.classList.contains('ActionListItem--hasSubItem')) {
continue
}
const keys = navItem.getAttribute('data-item-id')?.split(' ') || []
if (keys.includes(itemId)) {
return navItem
}
}
return null
}
#findSelectedNavItemByHref(href: string): HTMLElement | null {
// If we didn't find a match, we compare the selected link to the href of each nav item
const selectedNavItem = this.querySelector<HTMLAnchorElement>(`.ActionListContent[href="${href}"]`)
if (selectedNavItem) {
return selectedNavItem.closest('.ActionListItem')
}
return null
}
#findSelectedNavItemByCurrentLocation(): HTMLElement | null {
return this.#findSelectedNavItemByHref(window.location.pathname)
}
#select(navItem: HTMLElement) {
const currentlySelectedItem = this.querySelector('.ActionListItem--navActive') as HTMLElement
if (currentlySelectedItem) this.#deselect(currentlySelectedItem)
navItem.classList.add('ActionListItem--navActive')
if (navItem.children.length > 0) {
navItem.children[0].setAttribute('aria-current', 'page')
}
const parentMenu = this.#findParentMenu(navItem)
if (parentMenu) {
this.expandItem(parentMenu)
parentMenu.classList.add('ActionListContent--hasActiveSubItem')
}
}
#deselect(navItem: HTMLElement) {
navItem.classList.remove('ActionListItem--navActive')
if (navItem.children.length > 0) {
navItem.children[0].removeAttribute('aria-current')
}
const parentMenu = this.#findParentMenu(navItem)
if (parentMenu) {
this.collapseItem(parentMenu)
parentMenu.classList.remove('ActionListContent--hasActiveSubItem')
}
}
#findParentMenu(navItem: HTMLElement): HTMLElement | null {
if (!navItem.classList.contains('ActionListItem--subItem')) return null
const parent = navItem.closest('li.ActionListItem--hasSubItem')?.querySelector('button.ActionListContent')
if (parent) {
return parent as HTMLElement
} else {
return null
}
}
}
declare global {
interface Window {
NavListElement: typeof NavListElement
}
}