Files
headlessui/packages/@headlessui-react/src/hooks/use-inert-others.ts
T
Robin Malfait fdd2629795 Improve overal codebase, use modern tech like esbuild and TypeScript 4! (#1055)
* use esbuild for React instead of tsdx

* remove tsdx from Vue

* use consistent names

* add jest and prettier

* update scripts

* ignore some folders for prettier

* run lint script instead of tsdx lint

* run prettier en-masse

This has a few changes because of the new prettier version.

* bump typescript to latest version

* make typescript happy

* cleanup playground package.json

* make esbuild a dev dependency

* make scripts consistent

* fix husky hooks

* add dedicated watch script

* add `yarn playground-react` and `yarn react-playground` (alias)

This will make sure to run a watcher for the actual @headlessui/react
package, and start a development server in the playground-react package.

* ignore formatting in the .next folder

* run prettier on playground-react package

* setup playground-vue

Still not 100% working, but getting there!

* add playground aliases in @headlessui/vue and @headlessui/react

This allows you to run `yarn react playground` or `yarn vue playground`
from the root.

* add `clean` script

* move examples folder in playground-vue to root

* ensure new lines for consistency in scripts

* fix typescript issue

* fix typescript issues in playgrounds

* make sure to run prettier on everything it can

* run prettier on all files

* improve error output

If you minify the code, then it could happen that the errors are a bit
obscure. This will hardcode the component name to improve errors.

* add the `prettier-plugin-tailwindcss` plugin, party!

* update changelog
2022-01-27 17:07:38 +01:00

106 lines
3.2 KiB
TypeScript

import { MutableRefObject } from 'react'
import { useIsoMorphicEffect } from './use-iso-morphic-effect'
let interactables = new Set<HTMLElement>()
let originals = new Map<HTMLElement, { 'aria-hidden': string | null; inert: boolean }>()
function inert(element: HTMLElement) {
element.setAttribute('aria-hidden', 'true')
// @ts-expect-error `inert` does not exist on HTMLElement (yet!)
element.inert = true
}
function restore(element: HTMLElement) {
let original = originals.get(element)
if (!original) return
if (original['aria-hidden'] === null) element.removeAttribute('aria-hidden')
else element.setAttribute('aria-hidden', original['aria-hidden'])
// @ts-expect-error `inert` does not exist on HTMLElement (yet!)
element.inert = original.inert
}
export function useInertOthers<TElement extends HTMLElement>(
container: MutableRefObject<TElement | null>,
enabled: boolean = true
) {
useIsoMorphicEffect(() => {
if (!enabled) return
if (!container.current) return
let element = container.current
// Mark myself as an interactable element
interactables.add(element)
// Restore elements that now contain an interactable child
for (let original of originals.keys()) {
if (original.contains(element)) {
restore(original)
originals.delete(original)
}
}
// Collect direct children of the body
document.querySelectorAll('body > *').forEach((child) => {
if (!(child instanceof HTMLElement)) return // Skip non-HTMLElements
// Skip the interactables, and the parents of the interactables
for (let interactable of interactables) {
if (child.contains(interactable)) return
}
// Keep track of the elements
if (interactables.size === 1) {
originals.set(child, {
'aria-hidden': child.getAttribute('aria-hidden'),
// @ts-expect-error `inert` does not exist on HTMLElement (yet!)
inert: child.inert,
})
// Mutate the element
inert(child)
}
})
return () => {
// Inert is disabled on the current element
interactables.delete(element)
// We still have interactable elements, therefore this one and its parent
// will become inert as well.
if (interactables.size > 0) {
// Collect direct children of the body
document.querySelectorAll('body > *').forEach((child) => {
if (!(child instanceof HTMLElement)) return // Skip non-HTMLElements
// Skip already inert parents
if (originals.has(child)) return
// Skip the interactables, and the parents of the interactables
for (let interactable of interactables) {
if (child.contains(interactable)) return
}
originals.set(child, {
'aria-hidden': child.getAttribute('aria-hidden'),
// @ts-expect-error `inert` does not exist on HTMLElement (yet!)
inert: child.inert,
})
// Mutate the element
inert(child)
})
} else {
for (let element of originals.keys()) {
// Restore
restore(element)
// Cleanup
originals.delete(element)
}
}
}
}, [enabled])
}