Skip to content

Commit b6edc5f

Browse files
Elm1992sqchen
andauthored
fix: centered 为 true 时设置 draggable 无法拖拽 (#6948)
Co-authored-by: sqchen <chenshiqi@sshlx.com>
1 parent 8b0f138 commit b6edc5f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

packages/@core/ui-kit/popup-ui/src/modal/modal.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ const shouldDraggable = computed(
104104
() => draggable.value && !shouldFullscreen.value && header.value,
105105
);
106106
107+
const shouldCentered = computed(
108+
() => centered.value && !shouldFullscreen.value,
109+
);
110+
107111
const getAppendTo = computed(() => {
108112
return appendToMain.value
109113
? `#${ELEMENT_ID_MAIN_CONTENT}>div:not(.absolute)>div`
@@ -115,6 +119,7 @@ const { dragging, transform } = useModalDraggable(
115119
headerRef,
116120
shouldDraggable,
117121
getAppendTo,
122+
shouldCentered,
118123
);
119124
120125
const firstOpened = ref(false);
@@ -132,7 +137,9 @@ watch(
132137
dialogRef.value = innerContentRef.$el;
133138
// reopen modal reassign value
134139
const { offsetX, offsetY } = transform;
135-
dialogRef.value.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
140+
dialogRef.value.style.transform = shouldCentered.value
141+
? `translate(${offsetX}px, calc(-50% + ${offsetY}px))`
142+
: `translate(${offsetX}px, ${offsetY}px)`;
136143
}
137144
},
138145
{ immediate: true },
@@ -239,7 +246,7 @@ function handleClosed() {
239246
'shadow-3xl': !bordered,
240247
'left-0 top-0 size-full max-h-full !translate-x-0 !translate-y-0':
241248
shouldFullscreen,
242-
'top-1/2 !-translate-y-1/2': centered && !shouldFullscreen,
249+
'top-1/2': centered && !shouldFullscreen,
243250
'duration-300': !dragging,
244251
hidden: isClosed,
245252
},

packages/@core/ui-kit/popup-ui/src/modal/use-modal-draggable.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function useModalDraggable(
1414
dragRef: Ref<HTMLElement | undefined>,
1515
draggable: ComputedRef<boolean>,
1616
containerSelector?: ComputedRef<string | undefined>,
17+
centered?: ComputedRef<boolean>,
1718
) {
1819
const transform = reactive({
1920
offsetX: 0,
@@ -73,7 +74,10 @@ export function useModalDraggable(
7374
transform.offsetY = moveY;
7475

7576
if (targetRef.value) {
76-
targetRef.value.style.transform = `translate(${moveX}px, ${moveY}px)`;
77+
const isCentered = centered?.value;
78+
targetRef.value.style.transform = isCentered
79+
? `translate(${moveX}px, calc(-50% + ${moveY}px))`
80+
: `translate(${moveX}px, ${moveY}px)`;
7781
dragging.value = true;
7882
}
7983
};
@@ -108,7 +112,7 @@ export function useModalDraggable(
108112

109113
const target = unrefElement(targetRef);
110114
if (target) {
111-
target.style.transform = 'none';
115+
target.style.transform = '';
112116
}
113117
};
114118

0 commit comments

Comments
 (0)