1
+ import './style.scss' ;
1
2
import * as React from 'react' ;
2
3
import * as ReactDOM from 'react-dom' ;
3
- import { prefixClaName , classNames } from 'mo/common/className' ;
4
+ import {
5
+ prefixClaName ,
6
+ classNames ,
7
+ getBEMElement ,
8
+ getBEMModifier ,
9
+ } from 'mo/common/className' ;
4
10
import {
5
11
getRelativePosition ,
6
12
select ,
7
13
HTMLElementType ,
8
14
IPosition ,
9
15
} from 'mo/common/dom' ;
10
- import './style.scss' ;
11
16
import { Utils } from 'dt-utils/lib' ;
12
17
import { EventEmitter } from 'mo/common/event' ;
13
18
@@ -32,15 +37,18 @@ enum ContextViewEvent {
32
37
}
33
38
34
39
const contextViewClass = prefixClaName ( 'context-view' ) ;
35
- const contentClass = '.context-view-content' ;
36
- const emitter = new EventEmitter ( ) ;
40
+ const contentClassName = getBEMElement ( contextViewClass , 'content' ) ;
41
+ const blockClassName = getBEMElement ( contextViewClass , 'block' ) ;
42
+ const shadowClassName = getBEMModifier ( contextViewClass , 'shadow' ) ;
43
+
44
+ const Emitter = new EventEmitter ( ) ;
37
45
38
46
export function useContextView ( props ?: IContextViewProps ) : IContextView {
39
47
const claName = classNames ( contextViewClass , 'fade-in' ) ;
40
48
let contextView : HTMLElementType = select ( '.' + contextViewClass ) ; // Singleton contextView dom
41
49
42
50
const show = ( anchorPos : IPosition , render ?: ( ) => React . ReactNode ) => {
43
- const content = select ( contentClass ) ;
51
+ const content = select ( '.' + contentClassName ) ;
44
52
const renderContent = render || props ?. render ;
45
53
if ( ! renderContent )
46
54
throw Error (
@@ -61,13 +69,13 @@ export function useContextView(props?: IContextViewProps): IContextView {
61
69
const hide = ( ) => {
62
70
if ( contextView ) {
63
71
contextView . style . visibility = 'hidden' ;
64
- ReactDOM . unmountComponentAtNode ( select ( contentClass ) ! ) ;
65
- emitter . emit ( ContextViewEvent . onHide ) ;
72
+ ReactDOM . unmountComponentAtNode ( select ( '.' + contentClassName ) ! ) ;
73
+ Emitter . emit ( ContextViewEvent . onHide ) ;
66
74
}
67
75
} ;
68
76
69
77
const onHide = ( callback : Function ) => {
70
- emitter . subscribe ( ContextViewEvent . onHide , callback ) ;
78
+ Emitter . subscribe ( ContextViewEvent . onHide , callback ) ;
71
79
} ;
72
80
73
81
const onMaskClick = ( e : React . MouseEvent ) => {
@@ -77,7 +85,7 @@ export function useContextView(props?: IContextViewProps): IContextView {
77
85
} ;
78
86
79
87
const dispose = ( ) => {
80
- emitter . unsubscribe ( ContextViewEvent . onHide ) ;
88
+ Emitter . unsubscribe ( ContextViewEvent . onHide ) ;
81
89
} ;
82
90
83
91
if ( ! contextView ) {
@@ -93,17 +101,17 @@ export function useContextView(props?: IContextViewProps): IContextView {
93
101
} else {
94
102
root . appendChild ( contextView ) ;
95
103
}
96
- const shadowClass = ! props ?. shadowOutline ? '' : 'context-view--shadow' ;
104
+ const shadowClass = ! props ?. shadowOutline ? '' : shadowClassName ;
97
105
98
106
ReactDOM . render (
99
107
< >
100
108
< div
101
- className = "context-view-block"
109
+ className = { blockClassName }
102
110
onClick = { onMaskClick }
103
111
onContextMenu = { onMaskClick }
104
112
> </ div >
105
113
< div
106
- className = { classNames ( 'context-view-content' , shadowClass ) }
114
+ className = { classNames ( contentClassName , shadowClass ) }
107
115
> </ div >
108
116
</ > ,
109
117
contextView
0 commit comments