Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Convert use-focus-on-hide to TS (#1777)

Olga Bulatc801914bcd05
diff --git a/src/composables/use-dialog-content.js b/src/composables/use-dialog-content.js
index 9817422f5..c3108d78c 100644
--- a/src/composables/use-dialog-content.js
+++ b/src/composables/use-dialog-content.js
@@ -24,7 +24,12 @@ import { useFocusOnBlur } from '~/composables/use-focus-on-blur'
export function useDialogContent({ emit, ...props }) {
const focusOnBlur = useFocusOnBlur(props)
useFocusOnShow(props)
- useFocusOnHide(props)
+ useFocusOnHide({
+ dialogRef: props.dialogRef,
+ triggerElementRef: props.triggerElementRef,
+ visibleRef: props.visibleRef,
+ autoFocusOnHideRef: props.autoFocusOnHideRef,
+ })
useHideOnClickOutside(props)
/**
diff --git a/src/composables/use-focus-on-hide.js b/src/composables/use-focus-on-hide.ts
similarity index 55%
rename from src/composables/use-focus-on-hide.js
rename to src/composables/use-focus-on-hide.ts
index 7c8d6687c..3655e0016 100644
--- a/src/composables/use-focus-on-hide.js
+++ b/src/composables/use-focus-on-hide.ts
@@ -1,20 +1,16 @@
-import { watch } from '@nuxtjs/composition-api'
+import { Ref, watch } from '@nuxtjs/composition-api'
import { contains, getActiveElement } from '~/utils/reakit-utils/dom'
import { ensureFocus, isTabbable } from '~/utils/reakit-utils/focus'
-/**
- * @typedef Props
- * @property {import('./types').Ref<HTMLElement>} dialogRef
- * @property {import('./types').Ref<HTMLElement>} triggerElementRef
- * @property {import('./types').Ref<boolean>} visibleRef
- * @property {import('./types').Ref<boolean>} autoFocusOnHideRef
- */
-
-/**
- * @param {HTMLElement} popover
- */
-function hidByFocusingAnotherElement(popover) {
+type Props = {
+ dialogRef: Ref<HTMLElement | null>
+ triggerElementRef: Ref<HTMLElement | null>
+ visibleRef: Ref<boolean>
+ autoFocusOnHideRef: Ref<boolean>
+}
+
+function hidByFocusingAnotherElement(popover: HTMLElement) {
if (!popover) return false
const activeElement = getActiveElement(popover)
@@ -26,21 +22,15 @@ function hidByFocusingAnotherElement(popover) {
return activeElement.getAttribute('data-popover') === 'true'
}
-/**
- * @param {Props} Props
- */
export const useFocusOnHide = ({
dialogRef,
triggerElementRef,
visibleRef,
autoFocusOnHideRef,
-}) => {
+}: Props) => {
watch(
- [dialogRef, triggerElementRef, visibleRef, autoFocusOnHideRef],
- /**
- * @param {[HTMLElement, HTMLElement, boolean, boolean]} deps
- * @param {[unknown, unknown, boolean]} previousDeps
- */
+ [dialogRef, triggerElementRef, visibleRef, autoFocusOnHideRef] as const,
+
(
[dialog, triggerElement, visible, autoFocusOnHide],
[, , previousVisible]
@@ -50,9 +40,9 @@ export const useFocusOnHide = ({
if (!shouldFocus) return
- if (hidByFocusingAnotherElement(dialog)) return
+ if (!dialog || hidByFocusingAnotherElement(dialog)) return
- ensureFocus(triggerElement)
+ if (triggerElement) ensureFocus(triggerElement)
}
)
}
diff --git a/src/utils/reakit-utils/focus.ts b/src/utils/reakit-utils/focus.ts
index 6552c505e..8f002e063 100644
--- a/src/utils/reakit-utils/focus.ts
+++ b/src/utils/reakit-utils/focus.ts
@@ -31,7 +31,11 @@ export function isFocusable(element: Element): element is HTMLElement {
* isTabbable(document.querySelector("input[hidden]")); // false
* isTabbable(document.querySelector("input:disabled")); // false
*/
-export function isTabbable(element: Element): element is HTMLElement {
+export function isTabbableElement(element: Element): element is HTMLElement {
+ return isFocusable(element) && !hasNegativeTabIndex(element)
+}
+
+export function isTabbable(element: Element): boolean {
return isFocusable(element) && !hasNegativeTabIndex(element)
}
@@ -70,9 +74,9 @@ export function getAllTabbableIn(
) {
const elements = Array.from(container.querySelectorAll<HTMLElement>(selector))
- const tabbableElements = elements.filter(isTabbable)
+ const tabbableElements = elements.filter(isTabbableElement)
- if (includeContainer && isTabbable(container)) {
+ if (includeContainer && isTabbableElement(container)) {
tabbableElements.unshift(container)
}
@@ -163,9 +167,10 @@ export function hasFocusWithin(element: Node | Element) {
* @returns \{number\} `requestAnimationFrame` call ID so it can be passed to `cancelAnimationFrame` if needed.
*/
export function ensureFocus(
- element: HTMLElement,
+ el: Element,
{ preventScroll, isActive = hasFocus }: EnsureFocusOptions = {}
) {
+ const element = el as HTMLElement
// TODO: Try to use queueMicrotask before requestAnimationFrame and dispatch
// focus events if the element is not focusable?
if (isActive(element)) return -1