@@ -4,96 +4,91 @@ order: 13
44title:
55 zh-CN: 自定义渲染对话框
66 en-US: Custom modal content render
7- debugger: true
87---
98
109## zh-CN
1110
12- 自定义渲染对话框, 可通过 `react-draggable ` 来实现拖拽。
11+ 自定义渲染对话框, 可通过 `vueuse ` 来实现拖拽。
1312
1413## en-US
1514
16- Custom modal content render. use `react-draggable ` implements draggable.
15+ Custom modal content render. use `vueuse ` implements draggable.
1716
1817</docs >
1918
2019<template >
2120 <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 >
2426 <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 >
3528 </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 >
4129 <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 >
4533 </template >
4634 </a-modal >
4735 </div >
4836</template >
4937<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' ;
5340export default defineComponent ({
54- components: {
55- VueDragResize ,
56- },
5741 setup() {
5842 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 );
6244 const showModal = () => {
6345 visible .value = true ;
6446 };
65-
47+ const { x, y, isDragging } = useDraggable ( modalTitleRef );
6648 const handleOk = (e : MouseEvent ) => {
6749 console .log (e );
6850 visible .value = false ;
6951 };
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+ });
7074
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 ;
7679 }
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) ` ,
8284 };
83- };
85+ }) ;
8486 return {
8587 visible ,
8688 showModal ,
8789 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 ,
9792 };
9893 },
9994});
0 commit comments