Skip to content

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

Convert use-hide-on-click-outside and use-event-listener-outside to TS (#1778)

Olga Bulatfb416118df77
diff --git a/src/composables/use-dialog-content.js b/src/composables/use-dialog-content.js
index 199355b17..7b2e665f1 100644
--- a/src/composables/use-dialog-content.js
+++ b/src/composables/use-dialog-content.js
@@ -33,7 +33,13 @@ export function useDialogContent({ emit, ...props }) {
visibleRef: props.visibleRef,
autoFocusOnHideRef: props.autoFocusOnHideRef,
})
- useHideOnClickOutside(props)
+ useHideOnClickOutside({
+ dialogRef: props.dialogRef,
+ visibleRef: props.visibleRef,
+ hideOnClickOutsideRef: props.hideOnClickOutsideRef,
+ triggerElementRef: props.triggerElementRef,
+ hideRef: props.hideRef,
+ })
/**
* @param {KeyboardEvent} event
diff --git a/src/composables/use-event-listener-outside.js b/src/composables/use-event-listener-outside.ts
similarity index 61%
rename from src/composables/use-event-listener-outside.js
rename to src/composables/use-event-listener-outside.ts
index aa026f5ed..306105c73 100644
--- a/src/composables/use-event-listener-outside.js
+++ b/src/composables/use-event-listener-outside.ts
@@ -1,43 +1,42 @@
-import { watch, ref } from '@nuxtjs/composition-api'
+import { Ref, ref, watch } from '@nuxtjs/composition-api'
-import { contains, getDocument } from '~/utils/reakit-utils/dom'
+import { contains, getDocument, isInDocument } from '~/utils/reakit-utils/dom'
-/**
- * @param {Element} target
- */
-function isInDocument(target) {
- const document = getDocument(target)
- if (target.tagName === 'HTML') return true
- return contains(document.body, target)
+interface Props {
+ /**
+ * A click outside of this element will trigger the `listener` function.
+ */
+ containerRef: Ref<HTMLElement | null>
+ /**
+ * The element that triggers the dialog and will be focused when modal is closed.
+ */
+ triggerRef: Ref<HTMLElement | null>
+ /**
+ * The type of event to listen to.
+ */
+ eventType: string
+ /**
+ * The function to be called on a click outside of the `containerRef` element
+ * @param e - the event object
+ */
+ listener: (e: Event) => void
+ /**
+ * Whether the listener should be active or not.
+ */
+ shouldListenRef?: Ref<boolean>
}
-/**
- * @typedef Props
- * @property {import('./types').Ref<HTMLElement>} containerRef
- * @property {import('./types').Ref<HTMLElement>} triggerRef
- * @property {string} eventType
- * @property {(e: Event) => void} listener
- * @property {import('./types').Ref<boolean>} [shouldListenRef]
- */
-
-/**
- * @param {Props} props
- */
export const useEventListenerOutside = ({
containerRef,
triggerRef,
eventType,
listener,
shouldListenRef,
-}) => {
+}: Props) => {
const boundEventRef = ref()
watch(
- /** @type {const} */ ([
- containerRef,
- triggerRef,
- shouldListenRef || ref(false),
- ]),
+ [containerRef, triggerRef, shouldListenRef || ref(false)] as const,
([container, trigger, shouldListen], _, onInvalidate) => {
if (boundEventRef.value && !shouldListen) {
const document = getDocument(container)
@@ -46,10 +45,7 @@ export const useEventListenerOutside = ({
if (!shouldListen) return
- /**
- * @param {Event} event
- */
- const onEvent = (event) => {
+ boundEventRef.value = (event: Event) => {
if (!listener || !container || !(event.target instanceof Element))
return
const target = event.target
@@ -66,8 +62,6 @@ export const useEventListenerOutside = ({
listener(event)
}
- boundEventRef.value = onEvent
-
const document = getDocument(container)
document.addEventListener(eventType, boundEventRef.value)
onInvalidate(() => {
diff --git a/src/composables/use-hide-on-click-outside.js b/src/composables/use-hide-on-click-outside.ts
similarity index 65%
rename from src/composables/use-hide-on-click-outside.js
rename to src/composables/use-hide-on-click-outside.ts
index 5f6dac6d1..ff1c4056e 100644
--- a/src/composables/use-hide-on-click-outside.js
+++ b/src/composables/use-hide-on-click-outside.ts
@@ -1,41 +1,35 @@
-import { ref, watch, computed } from '@nuxtjs/composition-api'
+import { ref, watch, computed, Ref } from '@nuxtjs/composition-api'
import { getDocument } from '~/utils/reakit-utils/dom'
+import { useEventListenerOutside } from '~/composables/use-event-listener-outside'
-import { useEventListenerOutside } from './use-event-listener-outside'
-
-/**
- * @typedef Props
- * @property {import('./types').Ref<HTMLElement>} dialogRef
- * @property {import('./types').Ref<boolean>} visibleRef
- * @property {import('./types').Ref<boolean>} hideOnClickOutsideRef
- * @property {import('./types').Ref<HTMLElement>} triggerElementRef
- * @property {import('./types').Ref<() => void>} hideRef
- */
+type Props = {
+ dialogRef: Ref<HTMLElement | null>
+ visibleRef: Ref<boolean>
+ hideOnClickOutsideRef: Ref<boolean>
+ triggerElementRef: Ref<HTMLElement | null>
+ hideRef: Ref<() => void>
+}
-/**
- * @param {Props} props
- * @return {import('./types').Ref<EventTarget>}
- */
function useMouseDownTargetRef({
dialogRef,
visibleRef,
hideOnClickOutsideRef,
-}) {
+}: {
+ dialogRef: Props['dialogRef']
+ visibleRef: Props['visibleRef']
+ hideOnClickOutsideRef: Props['hideOnClickOutsideRef']
+}): Ref<EventTarget> {
const mouseDownTargetRef = ref()
watch(
- [visibleRef, hideOnClickOutsideRef, dialogRef],
- /**
- * @param {[boolean, boolean, HTMLElement]} deps
- * @param {unknown} _
- * @param {(cb: () => void) => void} onInvalidate
- */
+ [visibleRef, hideOnClickOutsideRef, dialogRef] as const,
([visible, hideOnClickOutside, popover], _, onInvalidate) => {
if (!(visible && hideOnClickOutside)) return
const document = getDocument(popover)
- const onMouseDown = (event) => (mouseDownTargetRef.value = event.target)
+ const onMouseDown = (event: MouseEvent) =>
+ (mouseDownTargetRef.value = event.target)
document.addEventListener('mousedown', onMouseDown)
onInvalidate(() => {
document.removeEventListener('mousedown', onMouseDown)
@@ -47,16 +41,13 @@ function useMouseDownTargetRef({
return mouseDownTargetRef
}
-/**
- * @param {Props} props
- */
export function useHideOnClickOutside({
dialogRef,
visibleRef,
hideOnClickOutsideRef,
triggerElementRef,
hideRef,
-}) {
+}: Props) {
const mouseDownTargetRef = useMouseDownTargetRef({
dialogRef,
visibleRef,
@@ -71,7 +62,7 @@ export function useHideOnClickOutside({
containerRef: dialogRef,
triggerRef: triggerElementRef,
eventType: 'click',
- listener: (event) => {
+ listener: (event: Event) => {
if (mouseDownTargetRef.value === event.target) {
// Make sure the element that has been clicked is the same that last
// triggered the mousedown event. This prevents the dialog from closing
@@ -87,7 +78,7 @@ export function useHideOnClickOutside({
containerRef: dialogRef,
triggerRef: triggerElementRef,
eventType: 'focusin',
- listener: (event) => {
+ listener: (event: Event) => {
const document = getDocument(dialogRef.value)
if (event.target !== document) {
hideRef.value()
diff --git a/src/utils/reakit-utils/dom.ts b/src/utils/reakit-utils/dom.ts
index 916b2d41c..75ad1492e 100644
--- a/src/utils/reakit-utils/dom.ts
+++ b/src/utils/reakit-utils/dom.ts
@@ -64,3 +64,9 @@ export function isVisible(element: Element) {
element.getClientRects().length > 0
)
}
+
+export function isInDocument(target: Element) {
+ const document = getDocument(target)
+ if (target.tagName === 'HTML') return true
+ return contains(document.body, target)
+}