Improve active state check for menu and tree item (#19281)
Co-authored-by: Niels Lyngsø <niels.lyngso@gmail.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { html, customElement, property, ifDefined, state } from '@umbraco-cms/backoffice/external/lit';
|
||||
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
|
||||
import { ensureSlash } from '@umbraco-cms/backoffice/router';
|
||||
import { debounce } from '@umbraco-cms/backoffice/utils';
|
||||
|
||||
/**
|
||||
@@ -62,8 +63,12 @@ export class UmbMenuItemLayoutElement extends UmbLitElement {
|
||||
return;
|
||||
}
|
||||
|
||||
const location = window.location.pathname;
|
||||
this._isActive = location.includes(this.href);
|
||||
/* Check if the current location includes the href of this menu item
|
||||
We ensure that the paths ends with a slash to avoid collisions with paths like /path-1 and /path-1-2 where /path is in both.
|
||||
Instead we compare /path-1/ with /path-1-2/ which wont collide.*/
|
||||
const location = ensureSlash(window.location.pathname);
|
||||
const compareHref = ensureSlash(this.href);
|
||||
this._isActive = location.includes(compareHref);
|
||||
}
|
||||
|
||||
override render() {
|
||||
|
||||
@@ -20,6 +20,7 @@ import type { UmbEntityActionEvent } from '@umbraco-cms/backoffice/entity-action
|
||||
import { UmbDeprecation, UmbPaginationManager, debounce } from '@umbraco-cms/backoffice/utils';
|
||||
import { UmbChangeEvent } from '@umbraco-cms/backoffice/event';
|
||||
import { UmbParentEntityContext, type UmbEntityModel, type UmbEntityUnique } from '@umbraco-cms/backoffice/entity';
|
||||
import { ensureSlash } from '@umbraco-cms/backoffice/router';
|
||||
|
||||
export abstract class UmbTreeItemContextBase<
|
||||
TreeItemType extends UmbTreeItemModel,
|
||||
@@ -449,9 +450,12 @@ export abstract class UmbTreeItemContextBase<
|
||||
return;
|
||||
}
|
||||
|
||||
const path = this.#path.getValue();
|
||||
const location = window.location.pathname;
|
||||
const isActive = location.includes(path);
|
||||
/* Check if the current location includes the path of this tree item.
|
||||
We ensure that the paths ends with a slash to avoid collisions with paths like /path-1 and /path-1-2 where /path-1 is in both.
|
||||
Instead we compare /path-1/ with /path-1-2/ which wont collide.*/
|
||||
const location = ensureSlash(window.location.pathname);
|
||||
const comparePath = ensureSlash(this.#path.getValue());
|
||||
const isActive = location.includes(comparePath);
|
||||
this.#isActive.setValue(isActive);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user