Skip to content

Commit

Permalink
[@mantine/hooks] Allow use-focus-trap and use-click-outside hooks to …
Browse files Browse the repository at this point in the history
…work with shadow DOM (#2982)

* fix: allow hook to work with shadow DOM

* fix: allow hook to work with shadow DOM

* fix: allow hook to work with shadow DOM

* fix: allow hook to work with shadow DOM

* fix: allow hook to work with shadow DOM

* fix: ts error with `activeElement`

* fix: eslint issues with line length

* fix: running prettier on changed files
  • Loading branch information
Kinbaum committed Nov 19, 2022
1 parent c0e5992 commit 877d469
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
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

0 comments on commit 877d469

Please sign in to comment.