Skip to content

Improve active state check for menu and tree item #19281

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
Loading