Skip to content

Commit

Permalink
cleanup default values for useInertOthers
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Apr 24, 2024
1 parent 4263180 commit e1aac1c
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions packages/@headlessui-react/src/hooks/use-inert-others.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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
}
}

Expand Down

0 comments on commit e1aac1c

Please sign in to comment.