Fix misaligned tooltip positions in modal dialogs

This commit fixes a bug that causes tooltips to be slightly misaligned.

Tooltip positioning was incorrect during modal transitions due to their
initial movement, causing tooltips to align incorrectly at the start of
the animation rather than the end.

One way to solve this would be using `autoUpdate` from `floating-ui`
with `animationFrame: true`. However, this recalculates positions tens
of times per second, impacting performance. This is a monkey solution.

This commit adopts a more efficient approach by updating tooltip
positions only at the end of the transitions, which reduces calculations
and conserves resources.

Key changes:

- Addd transition end event listener for updating tooltip positions.
- Use throttling to eliminate excessive position recalculations.

Other supporting changes:

- Improve throttle function to support efficient recalculations of
  positions:
  - Add ability to optionally exclude the first execution (leading
    call).
  - Refactor to simplify it make it easier to follow and read.
  - Fix a bug where initial calls were incorrectly throttled if
    `dateNow()` returned `0`.
- Introduce and use a global hook for efficient DOM event management.
  This greatily introduce safety, reuse and testability of event
  listening.
This commit is contained in:
undergroundwires
2024-05-08 15:24:12 +02:00
parent a3343205b1
commit dd71536316
26 changed files with 1165 additions and 335 deletions

View File

@@ -1,17 +1,14 @@
import { onBeforeMount, onBeforeUnmount } from 'vue';
import { useAutoUnsubscribedEventListener, type UseEventListener } from '@/presentation/components/Shared/Hooks/UseAutoUnsubscribedEventListener';
export function useEscapeKeyListener(callback: () => void) {
const onKeyUp = (event: KeyboardEvent) => {
export function useEscapeKeyListener(
callback: () => void,
eventTarget: EventTarget = document,
useEventListener: UseEventListener = useAutoUnsubscribedEventListener,
): void {
const { startListening } = useEventListener();
startListening(eventTarget, 'keyup', (event) => {
if (event.key === 'Escape') {
callback();
}
};
onBeforeMount(() => {
window.addEventListener('keyup', onKeyUp);
});
onBeforeUnmount(() => {
window.removeEventListener('keyup', onKeyUp);
});
}

View File

@@ -19,7 +19,8 @@
<script lang="ts">
import {
defineComponent, ref, watchEffect, nextTick,
defineComponent, ref, watchEffect,
nextTick, inject,
} from 'vue';
import ModalOverlay from './ModalOverlay.vue';
import ModalContent from './ModalContent.vue';
@@ -28,6 +29,8 @@ import { useCurrentFocusToggle } from './Hooks/UseCurrentFocusToggle';
import { useEscapeKeyListener } from './Hooks/UseEscapeKeyListener';
import { useAllTrueWatcher } from './Hooks/UseAllTrueWatcher';
export const INJECTION_KEY_ESCAPE_LISTENER = Symbol('useEscapeKeyListener');
export default defineComponent({
components: {
ModalOverlay,
@@ -49,6 +52,11 @@ export default defineComponent({
/* eslint-enable @typescript-eslint/no-unused-vars */
},
setup(props, { emit }) {
const listenEscapeKey: typeof useEscapeKeyListener = inject(
INJECTION_KEY_ESCAPE_LISTENER,
useEscapeKeyListener,
);
const isRendered = ref(false);
const isOpen = ref(false);
const overlayTransitionedOut = ref(false);
@@ -56,7 +64,7 @@ export default defineComponent({
useLockBodyBackgroundScroll(isOpen);
useCurrentFocusToggle(isOpen);
useEscapeKeyListener(() => handleEscapeKeyUp());
listenEscapeKey(() => handleEscapeKeyUp());
const {
onAllConditionsMet: onModalFullyTransitionedOut,