forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow NavList to paginate multiple groups (primer#2406)
Co-authored-by: strackoverflow <strackoverflow@users.noreply.github.com>
- Loading branch information
1 parent
57586da
commit fb9bf25
Showing
12 changed files
with
156 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/view-components': patch | ||
--- | ||
|
||
Fix an issue where multiple groups could not be paginated within the same NavList |
Binary file modified
BIN
+53.8 KB
(180%)
...ots/snapshots.test.ts-snapshots/primer/beta/nav_list/show_more_item/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+53.8 KB
(180%)
...ots/snapshots.test.ts-snapshots/primer/beta/nav_list/show_more_item/focused.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
<% with_post_list_content do %> | ||
<% if show_more_item? %> | ||
<%= show_more_item %> | ||
<nav-list-group> | ||
<% with_post_list_content do %> | ||
<% if show_more_item? %> | ||
<%= show_more_item %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<% render_parent %> | ||
<% render_parent %> | ||
</nav-list-group> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import {controller, target, targets} from '@github/catalyst' | ||
|
||
@controller | ||
export class NavListGroupElement extends HTMLElement { | ||
@target showMoreItem: HTMLElement | ||
@targets focusMarkers: HTMLElement[] | ||
|
||
connectedCallback(): void { | ||
this.setShowMoreItemState() | ||
} | ||
|
||
get showMoreDisabled(): boolean { | ||
return this.showMoreItem.hasAttribute('aria-disabled') | ||
} | ||
|
||
set showMoreDisabled(value: boolean) { | ||
if (value) { | ||
this.showMoreItem.setAttribute('aria-disabled', 'true') | ||
} else { | ||
this.showMoreItem.removeAttribute('aria-disabled') | ||
} | ||
this.showMoreItem.classList.toggle('disabled', value) | ||
} | ||
|
||
set currentPage(value: number) { | ||
this.showMoreItem.setAttribute('data-current-page', value.toString()) | ||
} | ||
|
||
get currentPage(): number { | ||
return parseInt(this.showMoreItem.getAttribute('data-current-page') as string) || 1 | ||
} | ||
|
||
get totalPages(): number { | ||
return parseInt(this.showMoreItem.getAttribute('data-total-pages') as string) || 1 | ||
} | ||
|
||
get paginationSrc(): string { | ||
return this.showMoreItem.getAttribute('src') || '' | ||
} | ||
|
||
private async showMore(e: Event) { | ||
e.preventDefault() | ||
if (this.showMoreDisabled) return | ||
this.showMoreDisabled = true | ||
let html | ||
try { | ||
const paginationURL = new URL(this.paginationSrc, window.location.origin) | ||
this.currentPage++ | ||
paginationURL.searchParams.append('page', this.currentPage.toString()) | ||
const response = await fetch(paginationURL) | ||
if (!response.ok) return | ||
html = await response.text() | ||
if (this.currentPage === this.totalPages) { | ||
this.showMoreItem.hidden = true | ||
} | ||
} catch (err) { | ||
// Ignore network errors | ||
this.showMoreDisabled = false | ||
this.currentPage-- | ||
return | ||
} | ||
const fragment = this.#parseHTML(document, html) | ||
fragment?.querySelector('li > a')?.setAttribute('data-targets', 'nav-list-group.focusMarkers') | ||
const listId = (e.target as HTMLElement).closest('button')!.getAttribute('data-list-id')! | ||
const list = document.getElementById(listId)! | ||
list.append(fragment) | ||
this.focusMarkers.pop()?.focus() | ||
this.showMoreDisabled = false | ||
} | ||
|
||
private setShowMoreItemState() { | ||
if (!this.showMoreItem) { | ||
return | ||
} | ||
|
||
if (this.currentPage < this.totalPages) { | ||
this.showMoreItem.hidden = false | ||
} else { | ||
this.showMoreItem.hidden = true | ||
} | ||
} | ||
|
||
#parseHTML(document: Document, html: string): DocumentFragment { | ||
const template = document.createElement('template') | ||
// eslint-disable-next-line github/no-inner-html | ||
template.innerHTML = html | ||
return document.importNode(template.content, true) | ||
} | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
NavListGroupElement: typeof NavListGroupElement | ||
} | ||
} | ||
|
||
window.NavListGroupElement = NavListGroupElement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters