fix: fix modal position after drag to prevent offset on reopen (#4261)

* fix: fix typo

* fix: fix modal position after drag to prevent offset on reopen
pull/48/MERGE
handsomeFu 2024-08-28 20:43:03 +08:00 committed by GitHub
parent c439d5601f
commit 1bd6c2e5f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 14 deletions

View File

@ -73,7 +73,11 @@ const shouldDraggable = computed(
() => draggable.value && !shouldFullscreen.value, () => draggable.value && !shouldFullscreen.value,
); );
const { dragging } = useModalDraggable(dialogRef, headerRef, shouldDraggable); const { dragging, transform } = useModalDraggable(
dialogRef,
headerRef,
shouldDraggable,
);
// const loadingStyle = computed(() => { // const loadingStyle = computed(() => {
// // py-5 4px*5*2 // // py-5 4px*5*2
@ -96,6 +100,10 @@ watch(
if (contentRef.value) { if (contentRef.value) {
const innerContentRef = contentRef.value.getContentRef(); const innerContentRef = contentRef.value.getContentRef();
dialogRef.value = innerContentRef.$el; dialogRef.value = innerContentRef.$el;
// reopen modal reassign value
const { offsetX, offsetY } = transform;
dialogRef.value.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
} }
} }
}, },

View File

@ -3,7 +3,7 @@
* *
*/ */
import { onBeforeUnmount, onMounted, ref, watchEffect } from 'vue'; import { onBeforeUnmount, onMounted, reactive, ref, watchEffect } from 'vue';
import type { ComputedRef, Ref } from 'vue'; import type { ComputedRef, Ref } from 'vue';
import { unrefElement } from '@vueuse/core'; import { unrefElement } from '@vueuse/core';
@ -13,10 +13,10 @@ export function useModalDraggable(
dragRef: Ref<HTMLElement | undefined>, dragRef: Ref<HTMLElement | undefined>,
draggable: ComputedRef<boolean>, draggable: ComputedRef<boolean>,
) { ) {
let transform = { const transform = reactive({
offsetX: 0, offsetX: 0,
offsetY: 0, offsetY: 0,
}; });
const dragging = ref(false); const dragging = ref(false);
@ -63,10 +63,8 @@ export function useModalDraggable(
moveY = Math.min(Math.max(moveY, minTop), maxTop); moveY = Math.min(Math.max(moveY, minTop), maxTop);
// + y; // + y;
transform = { transform.offsetX = moveX;
offsetX: moveX, transform.offsetY = moveY;
offsetY: moveY,
};
if (targetRef.value) { if (targetRef.value) {
targetRef.value.style.transform = `translate(${moveX}px, ${moveY}px)`; targetRef.value.style.transform = `translate(${moveX}px, ${moveY}px)`;
@ -100,10 +98,9 @@ export function useModalDraggable(
}; };
const resetPosition = () => { const resetPosition = () => {
transform = { transform.offsetX = 0;
offsetX: 0, transform.offsetY = 0;
offsetY: 0,
};
const target = unrefElement(targetRef); const target = unrefElement(targetRef);
if (target) { if (target) {
target.style.transform = 'none'; target.style.transform = 'none';
@ -127,6 +124,7 @@ export function useModalDraggable(
return { return {
dragging, dragging,
resetPosition, resetPosition,
transform,
}; };
} }

View File

@ -38,7 +38,7 @@ function openAutoHeightModal() {
autoHeightModalApi.open(); autoHeightModalApi.open();
} }
function openDargModal() { function openDragModal() {
dragModalApi.open(); dragModalApi.open();
} }
@ -83,7 +83,7 @@ function handleUpdateTitle() {
<Card class="mb-4" title="可拖拽示例"> <Card class="mb-4" title="可拖拽示例">
<p class="mb-3">配置 draggable 可开启拖拽功能</p> <p class="mb-3">配置 draggable 可开启拖拽功能</p>
<Button type="primary" @click="openDargModal"></Button> <Button type="primary" @click="openDragModal"></Button>
</Card> </Card>
<Card class="mb-4" title="动态配置示例"> <Card class="mb-4" title="动态配置示例">