From bf7497efea3008cb08b802626f0ceb8c4ee7b961 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 24 Apr 2024 16:36:39 +0200 Subject: [PATCH] cleanup default values for `useInertOthers` --- .../src/hooks/use-inert-others.tsx | 53 ++++++++----------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/packages/@headlessui-react/src/hooks/use-inert-others.tsx b/packages/@headlessui-react/src/hooks/use-inert-others.tsx index 5d727d55cf..7f96149dad 100644 --- a/packages/@headlessui-react/src/hooks/use-inert-others.tsx +++ b/packages/@headlessui-react/src/hooks/use-inert-others.tsx @@ -74,12 +74,9 @@ function markNotInert(element: HTMLElement) { */ export function useInertOthers( { - allowed, - disallowed, - }: { allowed?: () => (HTMLElement | null)[]; disallowed?: () => (HTMLElement | null)[] } = { - allowed: () => [], - disallowed: () => [], - }, + allowed = () => [], + disallowed = () => [], + }: { allowed?: () => (HTMLElement | null)[]; disallowed?: () => (HTMLElement | null)[] } = {}, enabled = true ) { useIsoMorphicEffect(() => { @@ -88,39 +85,35 @@ export function useInertOthers( let d = disposables() // Mark all disallowed elements as inert - if (disallowed) { - for (let element of disallowed()) { - if (!element) continue + for (let element of disallowed()) { + if (!element) continue - d.add(markInert(element)) - } + d.add(markInert(element)) } // Mark all siblings of allowed elements (and parents) as inert - if (allowed) { - let allowedElements = allowed() - - for (let element of allowedElements) { - if (!element) continue + let allowedElements = allowed() - let ownerDocument = getOwnerDocument(element) - if (!ownerDocument) continue + for (let element of allowedElements) { + if (!element) continue - let parent = element.parentElement - while (parent && parent !== ownerDocument.body) { - // Mark all siblings as inert - for (let node of parent.childNodes) { - // If the node contains any of the elements we should not mark it as inert - // because it would make the elements unreachable. - if (allowedElements.some((el) => node.contains(el))) continue + let ownerDocument = getOwnerDocument(element) + if (!ownerDocument) continue - // Mark the node as inert - d.add(markInert(node as HTMLElement)) - } + let parent = element.parentElement + while (parent && parent !== ownerDocument.body) { + // Mark all siblings as inert + for (let node of parent.childNodes) { + // If the node contains any of the elements we should not mark it as inert + // because it would make the elements unreachable. + if (allowedElements.some((el) => node.contains(el))) continue - // Move up the tree - parent = parent.parentElement + // Mark the node as inert + d.add(markInert(node as HTMLElement)) } + + // Move up the tree + parent = parent.parentElement } }