Skip to content

Commit 9f527f5

Browse files
committed
feat(contextview): add onHide, dispose function, and shadowOutline option
1 parent 629e343 commit 9f527f5

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/components/contextview/index.tsx

+22-2
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,30 @@ import {
99
} from 'mo/common/dom';
1010
import './style.scss';
1111
import { Utils } from 'dt-utils/lib';
12+
import { EventEmitter } from 'mo/common/event';
1213

1314
export interface IContextViewProps {
15+
/**
16+
* Default true
17+
*/
18+
shadowOutline?: boolean;
1419
render?: () => React.ReactNode;
1520
}
1621

1722
export interface IContextView {
1823
view: HTMLElementType;
1924
show(anchorPos: IPosition, render?: () => React.ReactNode): void;
2025
hide(): void;
26+
onHide(callback?: Function): void;
27+
dispose(): void;
2128
}
2229

2330
const contextViewClass = prefixClaName('context-view');
2431
const contentClass = '.context-view-content';
2532

2633
export function useContextView(props?: IContextViewProps): IContextView {
2734
const claName = classNames(contextViewClass, 'fade-in');
35+
const emitter = new EventEmitter();
2836
let contextView: HTMLElementType = select('.' + contextViewClass); // Singleton contextView dom
2937

3038
const show = (anchorPos: IPosition, render?: () => React.ReactNode) => {
@@ -50,15 +58,24 @@ export function useContextView(props?: IContextViewProps): IContextView {
5058
if (contextView) {
5159
contextView.style.visibility = 'hidden';
5260
ReactDOM.unmountComponentAtNode(select(contentClass)!);
61+
emitter.emit('onHide');
5362
}
5463
};
5564

65+
const onHide = (callback: Function) => {
66+
emitter.subscribe('onHide', callback);
67+
};
68+
5669
const onMaskClick = (e: React.MouseEvent) => {
5770
e.preventDefault();
5871
e.stopPropagation();
5972
hide();
6073
};
6174

75+
const dispose = () => {
76+
emitter.unsubscribe('onHide');
77+
};
78+
6279
if (!contextView) {
6380
contextView = document.createElement('div');
6481
contextView.className = classNames(
@@ -72,6 +89,7 @@ export function useContextView(props?: IContextViewProps): IContextView {
7289
} else {
7390
root.appendChild(contextView);
7491
}
92+
const shadowClass = !props?.shadowOutline ? '' : 'context-view--shadow';
7593

7694
ReactDOM.render(
7795
<>
@@ -80,11 +98,13 @@ export function useContextView(props?: IContextViewProps): IContextView {
8098
onClick={onMaskClick}
8199
onContextMenu={onMaskClick}
82100
></div>
83-
<div className="context-view-content"></div>
101+
<div
102+
className={classNames('context-view-content', shadowClass)}
103+
></div>
84104
</>,
85105
contextView
86106
);
87107
}
88108

89-
return { view: contextView, show, hide };
109+
return { view: contextView, show, hide, onHide, dispose };
90110
}

src/components/contextview/style.scss

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ $name: 'context-view';
88
width: initial;
99
z-index: 2500;
1010

11-
.context-view-content {
12-
box-shadow: rgb(0, 0, 0) 0 2px 4px;
13-
overflow: hidden;
14-
}
15-
1611
.#{$name}-block {
1712
bottom: 0;
1813
height: 100%;
@@ -25,3 +20,11 @@ $name: 'context-view';
2520
z-index: -1;
2621
}
2722
}
23+
24+
.context-view-content {
25+
overflow: hidden;
26+
}
27+
28+
.context-view--shadow {
29+
box-shadow: rgb(0, 0, 0) 0 2px 4px;
30+
}

0 commit comments

Comments
 (0)