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
<nav aria-labelledby="heading-106a131d-a190-4386-8371-074500b3c899" data-view-component="true"> <h2 id="heading-106a131d-a190-4386-8371-074500b3c899" data-view-component="true" class="ActionListHeader">Repository settings</h2> <nav-list> <ul data-target="nav-list.topLevelList" data-view-component="true" class="ActionListWrap"> <li data-item-id="" data-targets="nav-list.items" data-view-component="true" class="ActionListItem ActionListItem--hasSubItem"> <tool-tip id="tooltip-ab30b74d-ae14-4906-b656-9b743cf43853" for="item-e5213f2b-863d-4d45-ad85-233576e15352" popover="manual" data-direction="ne" data-type="description" data-view-component="true" class="ActionListItem-truncationTooltip sr-only position-absolute">Really really long label that may wrap, truncate, or appear as a tooltip</tool-tip> <button id="item-e5213f2b-863d-4d45-ad85-233576e15352" type="button" aria-expanded="false" data-action=" click:nav-list#handleItemWithSubItemClick keydown:nav-list#handleItemWithSubItemKeydown " data-view-component="true" class="ActionListContent ActionListContent--visual16"> <span class="ActionListItem-visual ActionListItem-visual--leading"> <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-comment-discussion"> <path d="M1.75 1h8.5c.966 0 1.75.784 1.75 1.75v5.5A1.75 1.75 0 0 1 10.25 10H7.061l-2.574 2.573A1.458 1.458 0 0 1 2 11.543V10h-.25A1.75 1.75 0 0 1 0 8.25v-5.5C0 1.784.784 1 1.75 1ZM1.5 2.75v5.5c0 .138.112.25.25.25h1a.75.75 0 0 1 .75.75v2.19l2.72-2.72a.749.749 0 0 1 .53-.22h3.5a.25.25 0 0 0 .25-.25v-5.5a.25.25 0 0 0-.25-.25h-8.5a.25.25 0 0 0-.25.25Zm13 2a.25.25 0 0 0-.25-.25h-.5a.75.75 0 0 1 0-1.5h.5c.966 0 1.75.784 1.75 1.75v5.5A1.75 1.75 0 0 1 14.25 12H14v1.543a1.458 1.458 0 0 1-2.487 1.03L9.22 12.28a.749.749 0 0 1 .326-1.275.749.749 0 0 1 .734.215l2.22 2.22v-2.19a.75.75 0 0 1 .75-.75h1a.25.25 0 0 0 .25-.25Z"></path> </svg> </span> <span data-view-component="true" class="ActionListItem-label ActionListItem-label--truncate"> Really really long label that may wrap, truncate, or appear as a tooltip </span> <span class="ActionListItem-visual ActionListItem-action--trailing"> <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-chevron-down ActionListItem-collapseIcon"> <path d="M12.78 5.22a.749.749 0 0 1 0 1.06l-4.25 4.25a.749.749 0 0 1-1.06 0L3.22 6.28a.749.749 0 1 1 1.06-1.06L8 8.939l3.72-3.719a.749.749 0 0 1 1.06 0Z"></path> </svg> </span> </button> <ul role="list" data-action="keydown:nav-list#handleItemWithSubItemKeydown" aria-labelledby="item-e5213f2b-863d-4d45-ad85-233576e15352" data-view-component="true" class="ActionList ActionList--subGroup"> <li data-item-id="interaction_limits" data-targets="nav-list.items" data-view-component="true" class="ActionListItem--subItem ActionListItem"> <a id="item-f31a7c59-e952-4909-8301-398da54e6834" href="/interaction-limits" data-view-component="true" class="ActionListContent"> <span data-view-component="true" class="ActionListItem-label"> Interaction limits </span> </a> </li> <li data-item-id="code_review_limits" data-targets="nav-list.items" data-view-component="true" class="ActionListItem--subItem ActionListItem"> <a id="item-e21a6441-dbf9-4309-8c02-da1177da4f4b" href="/review-limits" data-view-component="true" class="ActionListContent"> <span data-view-component="true" class="ActionListItem-label"> Code review limits </span> </a> </li> <li data-item-id="reported_content" data-targets="nav-list.items" data-view-component="true" class="ActionListItem--subItem ActionListItem"> <a id="item-c4fd2af4-afea-488c-951e-e9ffaf593512" href="/reported-content" data-view-component="true" class="ActionListContent"> <span data-view-component="true" class="ActionListItem-label"> Reported content </span> </a> </li> </ul> </li> </ul> </nav-list></nav>
1
2
3
4
5
6
7
8
9
10
11
render(Primer::Beta::NavList.new) do |list| list.with_heading(title: "Repository settings") list.with_item(label: "Really really long label that may wrap, truncate, or appear as a tooltip", truncate_label: :show_tooltip) do |item| item.with_leading_visual_icon(icon: :"comment-discussion") item.with_item(label: "Interaction limits", href: "/interaction-limits", selected_by_ids: :interaction_limits) item.with_item(label: "Code review limits", href: "/review-limits", selected_by_ids: :code_review_limits) item.with_item(label: "Reported content", href: "/reported-content", selected_by_ids: :reported_content) endend
No notes provided.
No params configured.
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* eslint-disable custom-elements/expose-class-on-global */import {controller, target, targets} from '@github/catalyst'import {ActionListTruncationObserver} from '../alpha/action_list'@controllerexport class NavListElement extends HTMLElement { @targets items: HTMLElement[] @target topLevelList: HTMLElement #truncationObserver: ActionListTruncationObserver connectedCallback() { // groups are wrapped in <action-list>, which handles resizing on its own if (this.topLevelList) { this.#truncationObserver = new ActionListTruncationObserver(this.topLevelList) } } disconnectedCallback() { if (this.topLevelList) { this.#truncationObserver.unobserve(this.topLevelList) } } 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 }}