Skip to content

Commit 838897a

Browse files
committed
fix: resolve memory leaks by detached elements and event listeners
1 parent 6a9754c commit 838897a

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/VueDatePicker/components/DatePicker/DpCalendar.vue

+14-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
</template>
115115

116116
<script lang="ts" setup>
117-
import { computed, nextTick, onMounted, ref } from 'vue';
117+
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue';
118118
import { getISOWeek, getWeek } from 'date-fns';
119119
120120
import {
@@ -216,6 +216,19 @@
216216
}
217217
});
218218
219+
onUnmounted(() => {
220+
if (!defaultedConfig.value.noSwipe) {
221+
if (calendarWrapRef.value) {
222+
calendarWrapRef.value.removeEventListener('touchstart', onTouchStart);
223+
calendarWrapRef.value.removeEventListener('touchend', onTouchEnd);
224+
calendarWrapRef.value.removeEventListener('touchmove', onTouchMove);
225+
}
226+
}
227+
if (props.monthChangeOnScroll && calendarWrapRef.value) {
228+
calendarWrapRef.value.removeEventListener('wheel', onScroll);
229+
}
230+
});
231+
219232
const getTransitionName = (isNext: boolean) => {
220233
if (isNext) return props.vertical ? 'vNext' : 'next';
221234
return props.vertical ? 'vPrevious' : 'previous';

src/VueDatePicker/components/DatepickerMenu.vue

+16-8
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,6 @@
217217
focusMenu();
218218
}
219219
if (menu) {
220-
const stopDefault = (event: Event) => {
221-
isMenuActive.value = true;
222-
if (defaultedConfig.value.allowPreventDefault) {
223-
event.preventDefault();
224-
}
225-
checkStopPropagation(event, defaultedConfig.value, true);
226-
};
227220
menu.addEventListener('pointerdown', stopDefault);
228221
menu.addEventListener('mousedown', stopDefault);
229222
}
@@ -233,7 +226,14 @@
233226
234227
onUnmounted(() => {
235228
window.removeEventListener('resize', getCalendarWidth);
236-
document.addEventListener('mousedown', handleClickOutside);
229+
document.removeEventListener('mousedown', handleClickOutside);
230+
231+
const menu = unrefElement(dpMenuRef);
232+
233+
if (menu) {
234+
menu.removeEventListener('pointerdown', stopDefault);
235+
menu.removeEventListener('mousedown', stopDefault);
236+
}
237237
});
238238
239239
const getCalendarWidth = (): void => {
@@ -306,6 +306,14 @@
306306
}),
307307
);
308308
309+
const stopDefault = (event: Event) => {
310+
isMenuActive.value = true;
311+
if (defaultedConfig.value.allowPreventDefault) {
312+
event.preventDefault();
313+
}
314+
checkStopPropagation(event, defaultedConfig.value, true);
315+
};
316+
309317
const handleDpMenuClick = (ev: Event) => {
310318
checkStopPropagation(ev, defaultedConfig.value, true);
311319
};

0 commit comments

Comments
 (0)