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: allow hook to work with shadow DOM #2982

Merged
merged 8 commits into from Nov 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,7 +15,7 @@ export function useClickOutside<T extends HTMLElement = any>(
if (Array.isArray(nodes)) {
const shouldIgnore =
target?.hasAttribute('data-ignore-outside-clicks') || !document.body.contains(target);
const shouldTrigger = nodes.every((node) => !!node && !node.contains(target));
const shouldTrigger = nodes.every((node) => !!node && !event.composedPath().includes(node));
shouldTrigger && !shouldIgnore && handler();
} else if (ref.current && !ref.current.contains(target)) {
handler();
Expand Down
2 changes: 1 addition & 1 deletion src/mantine-hooks/src/use-focus-trap/create-aria-hider.ts
Expand Up @@ -9,7 +9,7 @@ export function createAriaHider(
) {
const rootNodes: Value[] = Array.from<HTMLElement>(document.querySelectorAll(selector)).map(
(node) => {
if (node.contains(containerNode)) {
if (node?.shadowRoot?.contains(containerNode) || node.contains(containerNode)) {
return undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mantine-hooks/src/use-focus-trap/scope-tab.ts
Expand Up @@ -7,8 +7,8 @@ export function scopeTab(node: HTMLElement, event: KeyboardEvent) {
return;
}
const finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
const leavingFinalTabbable =
finalTabbable === document.activeElement || node === document.activeElement;
const root = node.getRootNode() as unknown as DocumentOrShadowRoot;
const leavingFinalTabbable = finalTabbable === root.activeElement || node === root.activeElement;

if (!leavingFinalTabbable) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/mantine-hooks/src/use-focus-trap/tabbable.ts
Expand Up @@ -21,7 +21,7 @@ function visible(element: HTMLElement) {

let parentElement: HTMLElement = element;
while (parentElement) {
if (parentElement === document.body) {
if (parentElement === document.body || parentElement.nodeType === 11) {
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/mantine-hooks/src/use-focus-trap/use-focus-trap.ts
Expand Up @@ -45,7 +45,7 @@ export function useFocusTrap(active = true): (instance: HTMLElement | null) => v

// Delay processing the HTML node by a frame. This ensures focus is assigned correctly.
setTimeout(() => {
if (node.ownerDocument) {
if (node.getRootNode()) {
processNode();
} else if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
Expand Down