Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core/menu-category): expand category if nested item becomes active #1617

Merged
merged 12 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
---

Automcatically 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 @@ -12626,7 +12626,7 @@
"type": "string"
}
],
"optional": false,
"optional": true,
"required": false
},
{
Expand All @@ -12647,7 +12647,7 @@
"type": "string"
}
],
"optional": false,
"optional": true,
"required": false
},
{
Expand All @@ -12668,7 +12668,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
52 changes: 36 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,32 @@ 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 (let i = 0; i < (mutations.length ?? 0); i++) {
const mutation = mutations[i];
if (
mutation.attributeName === 'class' &&
mutation.target instanceof HTMLElement
) {
if (mutation.target.classList.contains('active')) {
this.showItems = true;
this.onExpandCategory(true);
return;
}
}
}
nuke-ellington marked this conversation as resolved.
Show resolved Hide resolved
}

private isCategoryItemListVisible() {
Expand All @@ -171,7 +188,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 +202,7 @@ export class MenuCategory {
this.onNestedItemsChanged();
});

this.ixMenu.addEventListener(
this.ixMenu?.addEventListener(
'expandChange',
({ detail: menuExpand }: CustomEvent<boolean>) => {
this.menuExpand = menuExpand;
Expand All @@ -195,8 +215,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.
Loading