@@ -9,22 +9,30 @@ import {
9
9
} from 'mo/common/dom' ;
10
10
import './style.scss' ;
11
11
import { Utils } from 'dt-utils/lib' ;
12
+ import { EventEmitter } from 'mo/common/event' ;
12
13
13
14
export interface IContextViewProps {
15
+ /**
16
+ * Default true
17
+ */
18
+ shadowOutline ?: boolean ;
14
19
render ?: ( ) => React . ReactNode ;
15
20
}
16
21
17
22
export interface IContextView {
18
23
view : HTMLElementType ;
19
24
show ( anchorPos : IPosition , render ?: ( ) => React . ReactNode ) : void ;
20
25
hide ( ) : void ;
26
+ onHide ( callback ?: Function ) : void ;
27
+ dispose ( ) : void ;
21
28
}
22
29
23
30
const contextViewClass = prefixClaName ( 'context-view' ) ;
24
31
const contentClass = '.context-view-content' ;
25
32
26
33
export function useContextView ( props ?: IContextViewProps ) : IContextView {
27
34
const claName = classNames ( contextViewClass , 'fade-in' ) ;
35
+ const emitter = new EventEmitter ( ) ;
28
36
let contextView : HTMLElementType = select ( '.' + contextViewClass ) ; // Singleton contextView dom
29
37
30
38
const show = ( anchorPos : IPosition , render ?: ( ) => React . ReactNode ) => {
@@ -50,15 +58,24 @@ export function useContextView(props?: IContextViewProps): IContextView {
50
58
if ( contextView ) {
51
59
contextView . style . visibility = 'hidden' ;
52
60
ReactDOM . unmountComponentAtNode ( select ( contentClass ) ! ) ;
61
+ emitter . emit ( 'onHide' ) ;
53
62
}
54
63
} ;
55
64
65
+ const onHide = ( callback : Function ) => {
66
+ emitter . subscribe ( 'onHide' , callback ) ;
67
+ } ;
68
+
56
69
const onMaskClick = ( e : React . MouseEvent ) => {
57
70
e . preventDefault ( ) ;
58
71
e . stopPropagation ( ) ;
59
72
hide ( ) ;
60
73
} ;
61
74
75
+ const dispose = ( ) => {
76
+ emitter . unsubscribe ( 'onHide' ) ;
77
+ } ;
78
+
62
79
if ( ! contextView ) {
63
80
contextView = document . createElement ( 'div' ) ;
64
81
contextView . className = classNames (
@@ -72,6 +89,7 @@ export function useContextView(props?: IContextViewProps): IContextView {
72
89
} else {
73
90
root . appendChild ( contextView ) ;
74
91
}
92
+ const shadowClass = ! props ?. shadowOutline ? '' : 'context-view--shadow' ;
75
93
76
94
ReactDOM . render (
77
95
< >
@@ -80,11 +98,13 @@ export function useContextView(props?: IContextViewProps): IContextView {
80
98
onClick = { onMaskClick }
81
99
onContextMenu = { onMaskClick }
82
100
> </ div >
83
- < div className = "context-view-content" > </ div >
101
+ < div
102
+ className = { classNames ( 'context-view-content' , shadowClass ) }
103
+ > </ div >
84
104
</ > ,
85
105
contextView
86
106
) ;
87
107
}
88
108
89
- return { view : contextView , show, hide } ;
109
+ return { view : contextView , show, hide, onHide , dispose } ;
90
110
}
0 commit comments