Visually indicate the active menu item in the left sidebar (#1738)

This commit is contained in:
Aditya Chandel
2025-12-02 20:22:25 -07:00
committed by GitHub
parent 76e904436b
commit a4b0e22ad8
4 changed files with 108 additions and 22 deletions
@@ -4,7 +4,7 @@
@if (homeMenu$ | async; as homeMenu) { @if (homeMenu$ | async; as homeMenu) {
@for (item of homeMenu; track item; let i = $index) { @for (item of homeMenu; track item; let i = $index) {
@if (!item.separator) { @if (!item.separator) {
<li app-menuitem [item]="item" [index]="i" [root]="true"></li> <li app-menuitem [item]="item" [index]="i" [root]="true" menuKey="home"></li>
} }
} }
} }
@@ -14,7 +14,7 @@
@if (libraryMenu$ | async; as libraryMenu) { @if (libraryMenu$ | async; as libraryMenu) {
@for (item of libraryMenu; track item; let i = $index) { @for (item of libraryMenu; track item; let i = $index) {
@if (!item.separator) { @if (!item.separator) {
<li app-menuitem [item]="item" [index]="i" [root]="true"></li> <li app-menuitem [item]="item" [index]="i" [root]="true" menuKey="library"></li>
} }
} }
} }
@@ -24,7 +24,7 @@
@if (shelfMenu$ | async; as shelfMenu) { @if (shelfMenu$ | async; as shelfMenu) {
@for (item of shelfMenu; track item; let i = $index) { @for (item of shelfMenu; track item; let i = $index) {
@if (!item.separator) { @if (!item.separator) {
<li app-menuitem [item]="item" [index]="i" [root]="true"></li> <li app-menuitem [item]="item" [index]="i" [root]="true" menuKey="shelf"></li>
} }
} }
} }
@@ -34,7 +34,7 @@
@if (magicShelfMenu$ | async; as shelfMenu) { @if (magicShelfMenu$ | async; as shelfMenu) {
@for (item of shelfMenu; track item; let i = $index) { @for (item of shelfMenu; track item; let i = $index) {
@if (!item.separator) { @if (!item.separator) {
<li app-menuitem [item]="item" [index]="i" [root]="true"></li> <li app-menuitem [item]="item" [index]="i" [root]="true" menuKey="magicShelf"></li>
} }
} }
} }
@@ -1,13 +1,12 @@
<ng-container> <ng-container>
@if (root) { @if (root) {
@if (item.hasDropDown) { @if (item.hasDropDown) {
<div class=" flex items-center justify-between layout-menuitem-root-text cursor-pointer pt-4"> <div class="layout-menuitem-root-text root-item-with-dropdown">
<div class="flex items-center justify-between gap-2 cursor-pointer"> <div class="root-item-label-container">
<div>{{ item.label }}</div> <div>{{ item.label }}</div>
@if (item.hasCreate) { @if (item.hasCreate) {
<i <i
class="pi pi-plus plus-icon" class="pi pi-plus plus-icon"
style="font-size: 0.8rem; cursor: pointer;"
(click)="openDialog(item)" (click)="openDialog(item)"
></i> ></i>
} }
@@ -17,14 +16,13 @@
[ngClass]="{ [ngClass]="{
'pi': true, 'pi': true,
'pi-angle-up': isExpanded(key), 'pi-angle-up': isExpanded(key),
'pi-angle-down': !isExpanded(key), 'pi-angle-down': !isExpanded(key)
'text-surface-500': true, }"
'hover:text-surface-200': true, class="expand-icon"
'pr-3': true ></i>
}"></i>
</div> </div>
} @else { } @else {
<div class="flex items-center justify-between layout-menuitem-root-text"> <div class="layout-menuitem-root-text root-item">
<div>{{ item.label }}</div> <div>{{ item.label }}</div>
</div> </div>
} }
@@ -32,14 +30,15 @@
<div> <div>
<div <div
class="flex flex-row items-center justify-between hover:bg-surface-800 rounded-md cursor-pointer" class="menu-item-container"
(click)="triggerLink()" (click)="triggerLink()"
> >
@if ((item.routerLink && !item.items)) { @if ((item.routerLink && !item.items)) {
<a <a
#link #link
(click)="itemClick($event)" (click)="itemClick($event)"
[ngClass]="item.class + ' flex items-center gap-2 w-full'" [ngClass]="item.class"
class="menu-item-link"
[routerLink]="item.routerLink" [routerLink]="item.routerLink"
routerLinkActive="active-route" routerLinkActive="active-route"
[routerLinkActiveOptions]="item.routerLinkActiveOptions || { paths: 'exact', queryParams: 'ignored', matrixParams: 'ignored', fragment: 'ignored' }" [routerLinkActiveOptions]="item.routerLinkActiveOptions || { paths: 'exact', queryParams: 'ignored', matrixParams: 'ignored', fragment: 'ignored' }"
@@ -55,13 +54,13 @@
pRipple pRipple
> >
<i [ngClass]="item.icon" class="layout-menuitem-icon"></i> <i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
<span class="layout-menuitem-text w-full truncate"> <span class="layout-menuitem-text menu-item-text">
{{ item.label }} {{ item.label }}
</span> </span>
</a> </a>
} }
<span> <span class="menu-item-end-content">
@if (item.type === 'Library' || item.type === 'Shelf' || item.type === 'magicShelfItem') { @if (item.type === 'Library' || item.type === 'Shelf' || item.type === 'magicShelfItem') {
@if ((item.type !== 'Library' || (admin || canManipulateLibrary)) && item.label !== 'Unshelved' && item.label !== 'Kobo') { @if ((item.type !== 'Library' || (admin || canManipulateLibrary)) && item.label !== 'Unshelved' && item.label !== 'Kobo') {
<p-button <p-button
@@ -73,22 +72,21 @@
[rounded]="true" [rounded]="true"
[label]="hovered ? '' : (item.bookCount$ | async)?.toString() || ''" [label]="hovered ? '' : (item.bookCount$ | async)?.toString() || ''"
[icon]="hovered ? 'pi pi-ellipsis-v' : ''" [icon]="hovered ? 'pi pi-ellipsis-v' : ''"
[style.height]="'30px'" class="entity-menu-button"
[style.display]="'flex'"
></p-button> ></p-button>
} }
<p-menu #entitymenu [model]="item.menu" [popup]="true" appendTo="body"></p-menu> <p-menu #entitymenu [model]="item.menu" [popup]="true" appendTo="body"></p-menu>
} }
@if (item.type === 'Library' && (!admin && !canManipulateLibrary) || item.type === 'All Books' || item.label === 'Unshelved' || item.label === 'Kobo') { @if (item.type === 'Library' && (!admin && !canManipulateLibrary) || item.type === 'All Books' || item.label === 'Unshelved' || item.label === 'Kobo') {
<p class="text-[var(--primary-color)] pr-3"> <p class="book-count">
{{ (item.bookCount$ | async)?.toString() || '0' }} {{ (item.bookCount$ | async)?.toString() || '0' }}
</p> </p>
} }
</span> </span>
</div> </div>
<div [@children]="isExpanded(key) ? 'expanded' : 'collapsed'" style="overflow: hidden;"> <div [@children]="isExpanded(key) ? 'expanded' : 'collapsed'" class="submenu-container">
@if (item.items) { @if (item.items) {
<ul> <ul>
@for (child of item.items; track child; let i = $index) { @for (child of item.items; track child; let i = $index) {
@@ -1,8 +1,92 @@
.plus-icon { .plus-icon {
color: var(--p-primary-color); color: var(--p-primary-color);
transition: color 0.2s ease; transition: color 0.2s ease;
font-size: 0.8rem;
cursor: pointer;
&:hover { &:hover {
color: var(--p-green-400); color: var(--p-green-400);
} }
} }
.root-item-with-dropdown {
display: flex;
align-items: center;
justify-content: space-between;
cursor: pointer;
padding-top: 1rem;
}
.root-item-label-container {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.5rem;
cursor: pointer;
}
.expand-icon {
color: var(--p-surface-500);
padding-right: 0.75rem;
&:hover {
color: var(--p-surface-200);
}
}
.root-item {
display: flex;
align-items: center;
justify-content: space-between;
}
.menu-item-container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
border-radius: 8px;
cursor: pointer;
&:hover {
background-color: var(--p-surface-700);
border-radius: 8px;
}
}
:host(.active-menuitem) .menu-item-container {
background-color: var(--p-surface-800);
border-radius: 8px;
}
.menu-item-link {
display: flex;
align-items: center;
gap: 0.5rem;
width: 100%;
}
.menu-item-text {
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.menu-item-end-content {
display: flex;
}
.entity-menu-button {
height: 30px;
display: flex;
}
.book-count {
color: var(--primary-color);
padding-right: 0.75rem;
}
.submenu-container {
overflow: hidden;
}
@@ -43,6 +43,7 @@ export class AppMenuitemComponent implements OnInit, OnDestroy {
@Input() index!: number; @Input() index!: number;
@Input() @HostBinding('class.layout-root-menuitem') root!: boolean; @Input() @HostBinding('class.layout-root-menuitem') root!: boolean;
@Input() parentKey!: string; @Input() parentKey!: string;
@Input() menuKey!: string;
@ViewChild('linkRef') linkRef!: ElementRef<HTMLAnchorElement>; @ViewChild('linkRef') linkRef!: ElementRef<HTMLAnchorElement>;
hovered = false; hovered = false;
@@ -93,7 +94,8 @@ export class AppMenuitemComponent implements OnInit, OnDestroy {
} }
ngOnInit() { ngOnInit() {
this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index); const rootKey = this.menuKey ? this.menuKey + '-' : '';
this.key = this.parentKey ? this.parentKey + '-' + this.index : rootKey + String(this.index);
this.expandedItems.add(this.key); this.expandedItems.add(this.key);
if (this.item.routerLink) { if (this.item.routerLink) {
this.updateActiveStateFromRoute(); this.updateActiveStateFromRoute();
@@ -143,6 +145,8 @@ export class AppMenuitemComponent implements OnInit, OnDestroy {
} }
if (this.item.items) { if (this.item.items) {
this.active = !this.active; this.active = !this.active;
} else {
this.active = true;
} }
this.menuService.onMenuStateChange({key: this.key}); this.menuService.onMenuStateChange({key: this.key});
} }