Skip to content

🐛 fix(Menu): outside-click functionality issues when activator is 'parent' #2397

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 1 commit into from
May 8, 2025
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
🐛 fix(Menu): outside-click functionality issues when activator is 'pa…
…rent'
  • Loading branch information
capdiem committed May 8, 2025
commit 6e310f6b073acb2221b5d760eb935ca790c1c852
7 changes: 7 additions & 0 deletions src/Masa.Blazor.JS/src/mixins/outside-click.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { get$ParentElement } from "utils/helper";

class OutsideClick {
dotNetHelper: DotNet.DotNetObject;
listener: (e: MouseEvent & KeyboardEvent & FocusEvent) => void;
Expand Down Expand Up @@ -46,6 +48,11 @@ class OutsideClick {

checkEvent(e: MouseEvent) {
return this.excludedSelectors.some((selector) => {
const parentElement = get$ParentElement(selector);
Copy link
Preview

Copilot AI May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider caching the result of get$ParentElement(selector) when iterating over multiple excluded selectors to reduce redundant DOM queries if the excludedSelectors array grows in size.

Copilot uses AI. Check for mistakes.

if (parentElement) {
return parentElement.contains(e.target as HTMLElement);
}

const elements = Array.from(document.querySelectorAll(selector));
return elements.some(el => el.contains(e.target as HTMLElement));
});
Expand Down
17 changes: 13 additions & 4 deletions src/Masa.Blazor.JS/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,12 @@ export function convertToUnit (str: string | number | null | undefined, unit = '
}
}

export function getActivator(selector: string): HTMLElement | null {
if (selector.startsWith(activator_parent_prefix)) {
function is$ParentSelector(selector: string) {
return selector.startsWith(activator_parent_prefix);
}

Copy link
Preview

Copilot AI May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Adding documentation for get$ParentElement to clarify its behavior—especially that it returns null if no parent element is found—would improve maintainability.

Suggested change
/**
* Retrieves the parent element of the element matching the given selector.
*
* If the selector starts with the `$parent.` prefix, it will look for the parent
* element of the element matching the remaining selector. If no parent element
* is found, `null` is returned.
*
* Special case: If the parent element has the class `m-btn__content`, the function
* will return the grandparent element instead.
*
* @param {string} selector - The selector string, potentially prefixed with `$parent.`.
* @returns {HTMLElement | null} The parent element, or `null` if not found.
*/

Copilot uses AI. Check for mistakes.

export function get$ParentElement(selector: string): HTMLElement | null {
if (is$ParentSelector(selector)) {
const parentSelector = selector.replace(activator_parent_prefix, "");
const parentElement = document.querySelector(parentSelector)?.parentElement;
if (!parentElement) {
Expand All @@ -229,9 +233,14 @@ export function getActivator(selector: string): HTMLElement | null {
}

return parentElement;
} else {
return document.querySelector(selector);
}

return null
}

export function getActivator(selector: string): HTMLElement | null {
const parentElement = get$ParentElement(selector);
return parentElement || document.querySelector(selector);
}

export function isWindow(element: any | Window): element is Window {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading