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:
@@ -0,0 +1,74 @@
|
||||
import {
|
||||
describe, it, expect,
|
||||
} from 'vitest';
|
||||
import { defineComponent } from 'vue';
|
||||
import { mount } from '@vue/test-utils';
|
||||
import { createEventSpies } from '@tests/shared/Spies/EventTargetSpy';
|
||||
import { useEscapeKeyListener } from '@/presentation/components/Shared/Modal/Hooks/UseEscapeKeyListener';
|
||||
import { formatAssertionMessage } from '@tests/shared/FormatAssertionMessage';
|
||||
|
||||
const EventSource: EventTarget = document;
|
||||
type EventName = keyof DocumentEventMap;
|
||||
|
||||
describe('UseEscapeKeyListener', () => {
|
||||
it('executes the callback when the Escape key is pressed', () => {
|
||||
// arrange
|
||||
const expectedCallbackCall = true;
|
||||
let callbackCalled = false;
|
||||
const callback = () => {
|
||||
callbackCalled = true;
|
||||
};
|
||||
|
||||
// act
|
||||
mountWrapperComponent(callback);
|
||||
const event = new KeyboardEvent('keyup', { key: 'Escape' });
|
||||
EventSource.dispatchEvent(event);
|
||||
|
||||
// assert
|
||||
expect(callbackCalled).to.equal(expectedCallbackCall);
|
||||
});
|
||||
|
||||
it('adds the event listener on component mount', () => {
|
||||
// arrange
|
||||
const expectedEventType: EventName = 'keyup';
|
||||
const { isAddEventCalled, formatListeners } = createEventSpies(EventSource, afterEach);
|
||||
|
||||
// act
|
||||
mountWrapperComponent();
|
||||
|
||||
// assert
|
||||
const isAdded = isAddEventCalled(expectedEventType);
|
||||
expect(isAdded).to.equal(true, formatAssertionMessage([
|
||||
`Expected event type to be added: "${expectedEventType}".`,
|
||||
`Current listeners: ${formatListeners()}`,
|
||||
]));
|
||||
});
|
||||
|
||||
it('removes the event listener once unmounted', () => {
|
||||
// arrange
|
||||
const expectedEventType: EventName = 'keyup';
|
||||
const { isRemoveEventCalled, formatListeners } = createEventSpies(EventSource, afterEach);
|
||||
|
||||
// act
|
||||
const wrapper = mountWrapperComponent();
|
||||
wrapper.unmount();
|
||||
|
||||
// assert
|
||||
const isRemoved = isRemoveEventCalled(expectedEventType);
|
||||
expect(isRemoved).to.equal(true, formatAssertionMessage([
|
||||
`Expected event type to be removed: "${expectedEventType}".`,
|
||||
`Remaining listeners: ${formatListeners()}`,
|
||||
]));
|
||||
});
|
||||
});
|
||||
|
||||
function mountWrapperComponent(
|
||||
callback = () => {},
|
||||
) {
|
||||
return mount(defineComponent({
|
||||
setup() {
|
||||
useEscapeKeyListener(callback);
|
||||
},
|
||||
template: '<div></div>',
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user