@@ -4,96 +4,91 @@ order: 13
4
4
title:
5
5
zh-CN: 自定义渲染对话框
6
6
en-US: Custom modal content render
7
- debugger: true
8
7
---
9
8
10
9
## zh-CN
11
10
12
- 自定义渲染对话框, 可通过 `react-draggable ` 来实现拖拽。
11
+ 自定义渲染对话框, 可通过 `vueuse ` 来实现拖拽。
13
12
14
13
## en-US
15
14
16
- Custom modal content render. use `react-draggable ` implements draggable.
15
+ Custom modal content render. use `vueuse ` implements draggable.
17
16
18
17
</docs >
19
18
20
19
<template >
21
20
<div >
22
- <a-button type =" primary" @click =" showModal" >Open Draggable Modal</a-button >
23
- <a-modal v-model:visible =" visible" @ok =" handleOk" >
21
+ <a-button type =" primary" @click =" showModal" >Open Modal</a-button >
22
+ <a-modal ref =" modalRef" v-model:visible =" visible" @ok =" handleOk" >
23
+ <p >Some contents...</p >
24
+ <p >Some contents...</p >
25
+ <p >Some contents...</p >
24
26
<template #title >
25
- <div
26
- class =" drag"
27
- style =" width : 100% ; cursor : move "
28
- @mouseover =" handleMouseover"
29
- @mouseout =" handleMouseout"
30
- @focus =" () => {}"
31
- @blur =" () => {}"
32
- >
33
- Draggable Modal
34
- </div >
27
+ <div ref =" modalTitleRef" style =" width : 100% ; cursor : move " >Draggable Modal</div >
35
28
</template >
36
- <p >
37
- Just don&apos ; t learn physics at school and your life will be full of magic and miracles.
38
- </p >
39
- <br />
40
- <p >Day before yesterday I saw a rabbit, and yesterday a deer, and today, you.</p >
41
29
<template #modalRender =" { originVNode } " >
42
- <VueDragResize is-active drag-handle = " .drag " :is-resizable = " false " >
43
- <component :is =" originVNode" ></ component >
44
- </VueDragResize >
30
+ <div :style = " transformStyle " >
31
+ <component :is =" originVNode" / >
32
+ </div >
45
33
</template >
46
34
</a-modal >
47
35
</div >
48
36
</template >
49
37
<script lang="ts">
50
- import { defineComponent , ref } from ' vue' ;
51
- import type { CSSProperties } from ' vue' ;
52
- import VueDragResize from ' vue-drag-resize' ;
38
+ import { defineComponent , ref , computed , CSSProperties , watch , watchEffect } from ' vue' ;
39
+ import { useDraggable } from ' @vueuse/core' ;
53
40
export default defineComponent ({
54
- components: {
55
- VueDragResize ,
56
- },
57
41
setup() {
58
42
const visible = ref <boolean >(false );
59
- const draggleRef = ref ();
60
- const disabled = ref (true );
61
- const bounds = ref <CSSProperties >({ left: 0 , top: 0 , width: 520 , height: 0 });
43
+ const modalTitleRef = ref <HTMLElement >(null );
62
44
const showModal = () => {
63
45
visible .value = true ;
64
46
};
65
-
47
+ const { x, y, isDragging } = useDraggable ( modalTitleRef );
66
48
const handleOk = (e : MouseEvent ) => {
67
49
console .log (e );
68
50
visible .value = false ;
69
51
};
52
+ const startX = ref <number >(0 );
53
+ const startY = ref <number >(0 );
54
+ const startUpdatePos = ref (false );
55
+ const transformX = ref (0 );
56
+ const transformY = ref (0 );
57
+ const preTransformX = ref (0 );
58
+ const preTransformY = ref (0 );
59
+ let timeoutId;
60
+ watch (isDragging , () => {
61
+ clearTimeout (timeoutId );
62
+ timeoutId = setTimeout (() => {
63
+ if (isDragging .value ) {
64
+ startX .value = x .value ;
65
+ startY .value = y .value ;
66
+ preTransformX .value = transformX .value ;
67
+ preTransformY .value = transformY .value ;
68
+ startUpdatePos .value = true ;
69
+ } else {
70
+ startUpdatePos .value = false ;
71
+ }
72
+ });
73
+ });
70
74
71
- const onStart = (_event , uiData ) => {
72
- const { clientWidth, clientHeight } = window .document .documentElement ;
73
- const targetRect = draggleRef .value ?.getBoundingClientRect ();
74
- if (! targetRect ) {
75
- return ;
75
+ watchEffect (() => {
76
+ if (startUpdatePos .value ) {
77
+ transformX .value = preTransformX .value + x .value - startX .value ;
78
+ transformY .value = preTransformY .value + y .value - startY .value ;
76
79
}
77
- bounds .value = {
78
- left: ` ${- targetRect .left + uiData .x }px ` ,
79
- right: ` ${clientWidth - (targetRect .right - uiData .x )} ` ,
80
- top: ` ${- targetRect .top + uiData .y } ` ,
81
- bottom: ` ${clientHeight - (targetRect .bottom - uiData .y )} ` ,
80
+ });
81
+ const transformStyle = computed <CSSProperties >(() => {
82
+ return {
83
+ transform: ` translate(${transformX .value }px, ${transformY .value }px) ` ,
82
84
};
83
- };
85
+ }) ;
84
86
return {
85
87
visible ,
86
88
showModal ,
87
89
handleOk ,
88
- onStart ,
89
- handleMouseover() {
90
- if (disabled .value ) {
91
- disabled .value = false ;
92
- }
93
- },
94
- handleMouseout() {
95
- disabled .value = true ;
96
- },
90
+ modalTitleRef ,
91
+ transformStyle ,
97
92
};
98
93
},
99
94
});
0 commit comments