Skip to content

Commit 7ad4483

Browse files
committed
feat: add draft contextMenu component
1 parent 0171801 commit 7ad4483

File tree

3 files changed

+181
-24
lines changed

3 files changed

+181
-24
lines changed

src/components/contextMenu/index.tsx

+36-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
// import * as React from 'react';
2-
// import { prefixClaName } from 'mo/common/className';
3-
// import { ContextView } from 'monaco-editor/esm/vs/base/browser/ui/contextview/contextview';
4-
// import { StandardMouseEvent } from 'monaco-editor/esm/vs/base/browser/mouseEvent';
5-
6-
// export interface IContextMenuProps {
7-
8-
// }
9-
10-
// const ContextMenu: React.FunctionComponent = function(props) {
11-
// const view = new ContextView();
12-
13-
// return (
14-
// <div className={`${prefixClaName('contextmenu')}`} >
15-
// {
16-
// props.children
17-
// }
18-
// </div>
19-
// );
20-
// }
21-
22-
// export {
23-
// ContextMenu
24-
// }
1+
import * as React from 'react';
2+
import { HTMLElementType } from 'mo/common/dom';
3+
import { useContextView } from 'mo/components/contextview';
4+
import './style.scss';
5+
6+
export interface IContextMenu {
7+
anchor: HTMLElementType;
8+
render: () => React.ReactNode
9+
}
10+
11+
export function useContextMenu(props: IContextMenu) {
12+
const { anchor, render } = props;
13+
14+
if (!anchor) {
15+
return;
16+
}
17+
18+
const contextView = useContextView();
19+
20+
const onContextMenu = (e: MouseEvent) => {
21+
e.preventDefault();
22+
contextView!.show({
23+
x: e.clientX,
24+
y: e.clientY,
25+
}, render);
26+
};
27+
28+
anchor.addEventListener('contextmenu', onContextMenu);
29+
30+
const dispose = () => {
31+
contextView!.hide();
32+
anchor.removeEventListener('contextmenu', onContextMenu);
33+
};
34+
35+
return { contextView, dispose };
36+
}

src/components/contextMenu/style.scss

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import 'mo/style/const';
2+
$name: 'context-menu';
3+
4+
// #{prefix($name)} {
5+
// }
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
2+
import * as React from 'react';
3+
import { useContextMenu } from 'mo/components/contextMenu';
4+
import { useEffect } from 'react';
5+
import '../demo.scss';
6+
7+
export const ContextMenuDemo = () => {
8+
useEffect(() => {
9+
const contextView1 = useContextMenu({
10+
anchor: document.getElementById('topLeft'),
11+
render() {
12+
return (
13+
<ul>
14+
<li>Item1</li>
15+
<li>Item2</li>
16+
</ul>
17+
);
18+
},
19+
});
20+
21+
const contextView2 = useContextMenu({
22+
anchor: document.getElementById('topRight'),
23+
render() {
24+
return (
25+
<ul>
26+
<li>Item Div</li>
27+
<li>Item Div</li>
28+
<li>Item Div</li>
29+
<li>Item Div</li>
30+
<li>Item Div</li>
31+
</ul>
32+
);
33+
},
34+
});
35+
36+
const contextView3 = useContextMenu({
37+
anchor: document.getElementById('bottomLeft'),
38+
render() {
39+
return (
40+
<ul>
41+
<li>Item Div</li>
42+
<li>Item Div</li>
43+
<li>Item Div</li>
44+
<li>Item Div</li>
45+
<li>Item Div</li>
46+
</ul>
47+
);
48+
},
49+
});
50+
51+
const contextView4 = useContextMenu({
52+
anchor: document.getElementById('bottomRight'),
53+
render() {
54+
return (
55+
<ul>
56+
<li>Item Div</li>
57+
<li>Item Div</li>
58+
<li>Item Div</li>
59+
<li>Item Div</li>
60+
<li>Item Div</li>
61+
</ul>
62+
);
63+
},
64+
});
65+
66+
return function cleanup() {
67+
contextView1?.dispose();
68+
contextView2?.dispose();
69+
contextView3?.dispose();
70+
contextView4?.dispose();
71+
};
72+
});
73+
74+
return (
75+
<div className="story-wrapper">
76+
<div id="topLeft"
77+
style={
78+
{
79+
position: 'absolute',
80+
width: 200,
81+
height: 200,
82+
top: 0,
83+
left: 0,
84+
background: '#dddddd',
85+
}
86+
}>
87+
Right Click me!
88+
</div>
89+
<div id="topRight"
90+
style={
91+
{
92+
position: 'absolute',
93+
width: 200,
94+
height: 200,
95+
top: 0,
96+
right: 0,
97+
background: '#dddddd',
98+
}
99+
}>
100+
Right Click me!
101+
</div>
102+
<div id="bottomLeft"
103+
style={
104+
{
105+
position: 'absolute',
106+
width: 200,
107+
height: 200,
108+
left: 0,
109+
bottom: 10,
110+
background: '#dddddd',
111+
}
112+
}>
113+
Right Click me!
114+
</div>
115+
<div id="bottomRight"
116+
style={
117+
{
118+
position: 'absolute',
119+
width: 200,
120+
height: 200,
121+
right: 0,
122+
bottom: 10,
123+
background: '#dddddd',
124+
}
125+
}>
126+
Right Click me!
127+
</div>
128+
</div>
129+
);
130+
};
131+
132+
133+
ContextMenuDemo.story = {
134+
name: 'Basic Demo',
135+
};
136+
137+
export default {
138+
title: 'ContextMenu',
139+
component: ContextMenuDemo,
140+
};

0 commit comments

Comments
 (0)