Skip to content

Commit

Permalink
fix(core/menu-category): expand category if nested item becomes active (
Browse files Browse the repository at this point in the history
#1617)

Co-authored-by: matthiashader <[email protected]>
Co-authored-by: matthias <[email protected]>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent b016f93 commit e2316d8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-monkeys-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siemens/ix": patch
---

Automatically expand **ix-menu-category** if nested menu item becomes active.
6 changes: 3 additions & 3 deletions packages/core/component-doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12660,7 +12660,7 @@
"type": "string"
}
],
"optional": false,
"optional": true,
"required": false
},
{
Expand All @@ -12681,7 +12681,7 @@
"type": "string"
}
],
"optional": false,
"optional": true,
"required": false
},
{
Expand All @@ -12702,7 +12702,7 @@
"type": "number"
}
],
"optional": false,
"optional": true,
"required": false
}
],
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1915,15 +1915,15 @@ export namespace Components {
/**
* Icon of the category
*/
"icon": string;
"icon"?: string;
/**
* Display name of the category
*/
"label": string;
"label"?: string;
/**
* Show notification count on the category
*/
"notifications": number;
"notifications"?: number;
}
interface IxMenuExpandIcon {
/**
Expand Down
50 changes: 34 additions & 16 deletions packages/core/src/components/menu-category/menu-category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,36 @@ const DefaultAnimationTimeout = 150;
shadow: true,
})
export class MenuCategory {
@Element() hostElement: HTMLIxMenuCategoryElement;
@Element() hostElement!: HTMLIxMenuCategoryElement;

/**
* Display name of the category
*/
@Prop() label: string;
@Prop() label?: string;

/**
* Icon of the category
*/
@Prop() icon: string;
@Prop() icon?: string;

/**
* Show notification count on the category
*/
@Prop() notifications: number;
@Prop() notifications?: number;

/** @internal */
// eslint-disable-next-line @stencil-community/decorators-style
@Event({ bubbles: true, cancelable: true })
closeOtherCategories: EventEmitter;
closeOtherCategories!: EventEmitter;

@State() menuExpand = false;
@State() showItems = false;
@State() showDropdown = false;
@State() nestedItems: HTMLIxMenuItemElement[] = [];

private observer: MutationObserver;
private menuItemsContainer: HTMLDivElement;
private ixMenu: HTMLIxMenuElement;
private observer?: MutationObserver;
private menuItemsContainer?: HTMLDivElement;
private ixMenu?: HTMLIxMenuElement;

private enterLeaveDebounce = createEnterLeaveDebounce(
() => {
Expand Down Expand Up @@ -130,7 +130,7 @@ export class MenuCategory {
}

private onPointerEnter() {
if (this.ixMenu.expand) {
if (this.ixMenu?.expand) {
return;
}
this.closeOtherCategories.emit();
Expand All @@ -144,15 +144,30 @@ export class MenuCategory {

private onCategoryClick(e: MouseEvent) {
e.stopPropagation();
if (this.ixMenu.expand) {
e?.stopPropagation();
if (this.ixMenu?.expand) {
this.onExpandCategory(!this.showItems);
return;
}
}

private onNestedItemsChanged() {
private onNestedItemsChanged(mutations?: MutationRecord[]) {
this.nestedItems = this.getNestedItems();

if (!this.menuExpand || this.showItems || !mutations) {
return;
}

for (const mutation of mutations ?? []) {
if (
mutation.attributeName === 'class' &&
mutation.target instanceof HTMLElement &&
mutation.target.classList.contains('active')
) {
this.showItems = true;
this.onExpandCategory(true);
return;
}
}
}

private isCategoryItemListVisible() {
Expand All @@ -171,7 +186,10 @@ export class MenuCategory {
}

componentDidLoad() {
this.observer = createMutationObserver(() => this.onNestedItemsChanged());
this.observer = createMutationObserver((mutations: MutationRecord[]) =>
this.onNestedItemsChanged(mutations)
);

this.observer.observe(this.hostElement, {
attributes: true,
childList: true,
Expand All @@ -182,7 +200,7 @@ export class MenuCategory {
this.onNestedItemsChanged();
});

this.ixMenu.addEventListener(
this.ixMenu?.addEventListener(
'expandChange',
({ detail: menuExpand }: CustomEvent<boolean>) => {
this.menuExpand = menuExpand;
Expand All @@ -195,8 +213,8 @@ export class MenuCategory {
}

clearMenuItemStyles() {
this.menuItemsContainer.style.removeProperty('max-height');
this.menuItemsContainer.style.removeProperty('opacity');
this.menuItemsContainer?.style.removeProperty('max-height');
this.menuItemsContainer?.style.removeProperty('opacity');
}

disconnectedCallback() {
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/tests/menu/menu.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ test.describe('menu', () => {
}
);

regressionTest(
'expands category if nested item becomes active',
async ({ page }) => {
await page.goto('menu/basic');
await page.locator('ix-menu-expand-icon').click();
await page
.locator('ix-menu-item')
.nth(2)
.evaluate((el: HTMLIxMenuItemElement) => (el.active = true));

await expect(page).toHaveScreenshot({
animations: 'disabled',
});
}
);

regressionTest(
'show scrollbar on overflow on expand on lg',
async ({ page }) => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e2316d8

Please sign in to comment.