Skip to content

Commit 4b836ba

Browse files
fix(core): shellbar context area should allow ngIf (#13510)
* fix(core): shellbar context area should allow ngIf * fix(core): add event that is fired when context area item is shown/hidden * fix(core): use output signal and inject for new changes
1 parent 09db527 commit 4b836ba

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed

libs/core/shellbar/shellbar-context-area/shellbar-context-area.component.ts

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import { ChangeDetectionStrategy, Component, ElementRef, inject } from '@angular/core';
1+
import {
2+
AfterViewInit,
3+
ChangeDetectionStrategy,
4+
Component,
5+
DestroyRef,
6+
ElementRef,
7+
inject,
8+
output
9+
} from '@angular/core';
10+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
11+
import { ResizeObserverService } from '@fundamental-ngx/cdk/utils';
12+
import { isEqual, sortBy } from 'lodash-es';
213
import { FD_SHELLBAR_COMPONENT } from '../tokens';
314

415
/**
@@ -22,24 +33,56 @@ import { FD_SHELLBAR_COMPONENT } from '../tokens';
2233
`
2334
]
2435
})
25-
export class ShellbarContextAreaComponent {
36+
export class ShellbarContextAreaComponent implements AfterViewInit {
37+
/** Event emitted when items in the context area are hidden or shown. Event parameter is an array of all the items that are hidden. */
38+
contentItemVisibilityChange = output<HTMLElement[]>();
39+
40+
/** @hidden */
41+
el: ElementRef = inject(ElementRef);
42+
2643
/** @hidden */
2744
private readonly _shellbar = inject(FD_SHELLBAR_COMPONENT);
2845

2946
/** @hidden */
30-
constructor(public el: ElementRef) {}
47+
private _resizeObserverService = inject(ResizeObserverService);
48+
49+
/** @hidden */
50+
private readonly _destroyRef = inject(DestroyRef);
51+
52+
/** @hidden */
53+
private _hiddenItems: HTMLElement[];
54+
55+
/** @hidden */
56+
ngAfterViewInit(): void {
57+
this._resizeObserverService
58+
.observe(this.el.nativeElement)
59+
.pipe(takeUntilDestroyed(this._destroyRef))
60+
.subscribe(() => {
61+
this.showElements();
62+
this.hideElementsIfNeeded();
63+
});
64+
}
3165

3266
/**
3367
* Iteratively hides elements if the end of the actions exceed the end of the shellbar.
3468
*/
3569
hideElementsIfNeeded(): void {
36-
const elements: { el: HTMLElement; priority: number }[] = this._getElementsWithPriority();
70+
const newHiddenItems: HTMLElement[] = [];
71+
const contextAreaItems: { el: HTMLElement; priority: number }[] = this._getContextAreaItemsWithPriority();
3772
while (this._shellbar._actionsExceedShellbarWidth()) {
38-
const shownElements = elements.filter((el) => el.el.style.display !== 'none');
73+
const shownElements = contextAreaItems.filter((item) => item?.el?.style?.display !== 'none');
3974
if (shownElements.length === 0) {
4075
break;
4176
}
42-
shownElements[shownElements.length - 1].el.style.display = 'none';
77+
const lastItem = shownElements[shownElements.length - 1];
78+
if (lastItem?.el?.style) {
79+
newHiddenItems.push(lastItem.el);
80+
lastItem.el.style.display = 'none';
81+
}
82+
}
83+
if (!isEqual(sortBy(this._hiddenItems), sortBy(newHiddenItems))) {
84+
this._hiddenItems = newHiddenItems;
85+
this.contentItemVisibilityChange.emit(newHiddenItems);
4386
}
4487
}
4588

@@ -48,8 +91,10 @@ export class ShellbarContextAreaComponent {
4891
* to exceed the end of th eshellbar.
4992
*/
5093
showElements(): void {
51-
this._getElementsWithPriority().forEach((el) => {
52-
el.el.style.display = '';
94+
this._getContextAreaItemsWithPriority().forEach((item) => {
95+
if (item?.el?.style) {
96+
item.el.style.display = '';
97+
}
5398
});
5499
}
55100

@@ -58,8 +103,8 @@ export class ShellbarContextAreaComponent {
58103
* The elements are sorted based on their priority, with elements having
59104
* higher priority shown first.
60105
*/
61-
private _getElementsWithPriority(): { el: HTMLElement; priority: number }[] {
62-
return [...this.el.nativeElement.childNodes]
106+
private _getContextAreaItemsWithPriority(): { el: HTMLElement; priority: number }[] {
107+
return [...this.el.nativeElement.children]
63108
.map((element: HTMLElement, index) => {
64109
const hasPriorityAttribute = element.hasAttribute && element.hasAttribute('fdShellbarHidePriority');
65110
const priority = hasPriorityAttribute

libs/docs/core/shellbar/examples/shellbar-branding-context-area-example/shellbar-branding-context-area-example.component.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<fd-shellbar-title> Corporate Portal </fd-shellbar-title>
1212
</fd-shellbar-branding>
1313

14-
<fd-shellbar-context-area>
14+
<fd-shellbar-context-area (contentItemVisibilityChange)="itemVisibilityChanged($event)">
1515
<fd-shellbar-separator fdShellbarHidePriority="1"></fd-shellbar-separator>
1616
<span
1717
fdShellbarHidePriority="2"
@@ -28,7 +28,9 @@
2828
>
2929
</fd-product-menu>
3030
<span fdShellbarHidePriority="4" fd-button label="Button priority 4"></span>
31-
<span fdShellbarHidePriority="3" fd-button label="Button priority 3"></span>
31+
@if (showButtonWithPriority3) {
32+
<span fdShellbarHidePriority="3" fd-button label="Button priority 3"></span>
33+
}
3234
</fd-shellbar-context-area>
3335

3436
<fdp-search-field
@@ -187,3 +189,7 @@ <h5 fd-panel-title id="panel-expandable-title-1">Other Accounts</h5>
187189
</fd-user-menu>
188190
</fd-shellbar-actions>
189191
</fd-shellbar>
192+
193+
<br />
194+
195+
<button fd-button (click)="toggleButton3()">Toggle display of button with priority 3</button>

libs/docs/core/shellbar/examples/shellbar-branding-context-area-example/shellbar-branding-context-area-example.component.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ViewChild } from '@angular/core';
1+
import { Component, ElementRef, ViewChild } from '@angular/core';
22
import { FormsModule } from '@angular/forms';
33
import { ClickedDirective } from '@fundamental-ngx/cdk';
44
import { AvatarComponent } from '@fundamental-ngx/core/avatar';
@@ -12,7 +12,13 @@ import { PanelModule } from '@fundamental-ngx/core/panel';
1212
import { PopoverModule } from '@fundamental-ngx/core/popover';
1313
import { ProductSwitchItem, ProductSwitchModule } from '@fundamental-ngx/core/product-switch';
1414
import { SegmentedButtonComponent } from '@fundamental-ngx/core/segmented-button';
15-
import { ShellbarMenuItem, ShellbarModule, ShellbarUser, ShellbarUserMenu } from '@fundamental-ngx/core/shellbar';
15+
import {
16+
ShellbarComponent,
17+
ShellbarMenuItem,
18+
ShellbarModule,
19+
ShellbarUser,
20+
ShellbarUserMenu
21+
} from '@fundamental-ngx/core/shellbar';
1622
import {
1723
UserMenuBodyComponent,
1824
UserMenuComponent,
@@ -74,6 +80,9 @@ export class ShellbarBrandingContextAreaExampleComponent {
7480
@ViewChild(UserMenuComponent)
7581
userMenuComponent: UserMenuComponent;
7682

83+
@ViewChild(ShellbarComponent, { read: ElementRef })
84+
shellbar: ElementRef;
85+
7786
expanded = true;
7887

7988
isOpen = false;
@@ -82,6 +91,8 @@ export class ShellbarBrandingContextAreaExampleComponent {
8291

8392
inputText = '';
8493

94+
showButtonWithPriority3 = true;
95+
8596
suggestions: SuggestionItem[] = [
8697
{
8798
value: 'Apple'
@@ -342,4 +353,12 @@ export class ShellbarBrandingContextAreaExampleComponent {
342353
duration: 5000
343354
});
344355
}
356+
357+
toggleButton3(): void {
358+
this.showButtonWithPriority3 = !this.showButtonWithPriority3;
359+
}
360+
361+
itemVisibilityChanged(event: any): void {
362+
console.log(event);
363+
}
345364
}

0 commit comments

Comments
 (0)