Skip to content

Commit 2cf87db

Browse files
authored
fix: remove contextView warning in console (#832)
* fix: remove contextView warning in console * fix: replace useContextView with useContextViewEle * test: update tests
1 parent 8a63067 commit 2cf87db

File tree

8 files changed

+54
-17
lines changed

8 files changed

+54
-17
lines changed

src/components/contextView/index.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { render as renderUtils, unmout } from 'mo/react/render';
33
import { classNames } from 'mo/common/className';
44
import {
@@ -40,6 +40,24 @@ enum ContextViewEvent {
4040

4141
const Emitter = new EventEmitter();
4242

43+
/**
44+
* It's a hook used in functional component
45+
*/
46+
export function useContextViewEle(props?: IContextViewProps) {
47+
const [contextView, setContextView] = useState<IContextView>();
48+
49+
useEffect(() => {
50+
// The useContextView can't be called at initial of useState
51+
// Because this function has rendered an element
52+
setContextView(useContextView(props));
53+
}, []);
54+
55+
return contextView;
56+
}
57+
58+
/**
59+
* TODO: It's not a hook, don't begin with use
60+
*/
4361
export function useContextView(props: IContextViewProps = {}): IContextView {
4462
const { shadowOutline = true } = props;
4563
const claName = classNames(contextViewClass, 'fade-in');

src/components/dropdown/index.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { forwardRef, useImperativeHandle } from 'react';
22
import { classNames, getBEMModifier, prefixClaName } from 'mo/common/className';
3-
import { useContextView } from '../contextView';
3+
import { useContextViewEle } from '../contextView';
44
import {
55
triggerEvent,
66
TriggerEvent,
@@ -30,7 +30,7 @@ export const DropDown = forwardRef<DropDownRef, IDropDownProps>(
3030
trigger = 'click',
3131
...restProps
3232
} = props;
33-
const contextView = useContextView({
33+
const contextView = useContextViewEle({
3434
render: () => overlay,
3535
});
3636

@@ -48,6 +48,7 @@ export const DropDown = forwardRef<DropDownRef, IDropDownProps>(
4848
);
4949
const events = {
5050
[triggerEvent(trigger)]: function (e: React.MouseEvent) {
51+
if (!contextView) return;
5152
const target = e.currentTarget;
5253
const rect = target.getBoundingClientRect();
5354
let position = getPositionByPlacement(placement, rect);

src/components/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type { ICollapseProps } from './collapse';
2222
export { useContextMenu } from './contextMenu';
2323
export type { IContextMenuProps } from './contextMenu';
2424

25-
export { useContextView } from './contextView';
25+
export { useContextView, useContextViewEle } from './contextView';
2626
export type { IContextViewProps, IContextView } from './contextView';
2727

2828
export { Modal } from './dialog';

src/provider/__tests__/__snapshots__/molecule.test.tsx.snap

+11
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,17 @@ dmlldzJfOV8xNjM4ODQ4MDI1NDI2MTE4Ml8zOF9bMF0TNSl1AAAAAElFTkSuQmCC"
12341234
</div>
12351235
</div>
12361236
</div>
1237+
<div
1238+
class="mo-context-view fade-in"
1239+
style="visibility: hidden;"
1240+
>
1241+
<div
1242+
class="mo-context-view__block"
1243+
/>
1244+
<div
1245+
class="mo-context-view__content mo-context-view--shadow"
1246+
/>
1247+
</div>
12371248
</div>
12381249
</DocumentFragment>
12391250
`;

src/workbench/__tests__/__snapshots__/workbench.test.tsx.snap

+11
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,17 @@ dmlldzJfOV8xNjM4ODQ4MDI1NDI2MTE4Ml8zOF9bMF0TNSl1AAAAAElFTkSuQmCC"
317317
/>
318318
</div>
319319
</div>
320+
<div
321+
class="mo-context-view fade-in"
322+
style="visibility: hidden;"
323+
>
324+
<div
325+
class="mo-context-view__block"
326+
/>
327+
<div
328+
class="mo-context-view__content mo-context-view--shadow"
329+
/>
330+
</div>
320331
</div>
321332
</DocumentFragment>
322333
`;

src/workbench/activityBar/activityBar.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
itemClassName,
1717
normalItemsClassName,
1818
} from './base';
19-
import { useContextView } from 'mo/components';
19+
import { useContextViewEle } from 'mo/components';
2020
import { UniqueId } from 'mo/common/types';
2121

2222
export function ActivityBar(props: IActivityBar & IActivityBarController) {
@@ -63,9 +63,7 @@ export function ActivityBar(props: IActivityBar & IActivityBarController) {
6363
<Menu role="menu" onClick={onClickMenuItem} data={contextMenu} />
6464
);
6565

66-
const contextView = useContextView({
67-
render: renderContextMenu,
68-
});
66+
const contextView = useContextViewEle({ render: renderContextMenu });
6967

7068
const onClickMenuItem = useCallback(
7169
(e: React.MouseEvent, item: IMenuItemProps | undefined) => {
@@ -78,6 +76,7 @@ export function ActivityBar(props: IActivityBar & IActivityBarController) {
7876
const handleRightClick = (e) => {
7977
e.preventDefault();
8078
e.stopPropagation();
79+
if (!contextView) return;
8180
const doms = document.elementsFromPoint(e.pageX, e.pageY);
8281
const itemDom = doms.find((dom) =>
8382
dom.classList.contains(itemClassName)

src/workbench/activityBar/activityBarItem.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
itemCheckedClassName,
1212
itemDisabledClassName,
1313
} from './base';
14-
import { Icon, useContextView } from 'mo/components';
14+
import { Icon, useContextViewEle } from 'mo/components';
1515
import { KeybindingHelper } from 'mo/services/keybinding';
1616

1717
export function ActivityBarItem(
@@ -53,9 +53,7 @@ export function ActivityBarItem(
5353
/>
5454
);
5555

56-
const contextView = useContextView({
57-
render: renderContextMenu,
58-
});
56+
const contextView = useContextViewEle({ render: renderContextMenu });
5957

6058
const onClickMenuItem = (
6159
e: React.MouseEvent,
@@ -70,7 +68,7 @@ export function ActivityBarItem(
7068
onClick(props.id, props);
7169
}
7270
if (contextMenu.length > 0) {
73-
contextView.show({ x: event.pageX, y: event.pageY });
71+
contextView?.show({ x: event.pageX, y: event.pageY });
7472
}
7573
};
7674

src/workbench/sidebar/explore/folderTree.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import Tree from 'mo/components/tree';
66
import { IMenuItemProps, Menu } from 'mo/components/menu';
77
import { Button } from 'mo/components/button';
88
import type { IFolderTreeController } from 'mo/controller/explorer/folderTree';
9-
import { useContextView } from 'mo/components/contextView';
109
import { useContextMenu } from 'mo/components/contextMenu';
1110
import {
1211
folderTreeClassName,
1312
folderTreeEditClassName,
1413
folderTreeInputClassName,
1514
} from './base';
1615
import { classNames } from 'mo/common/className';
17-
import { Scrollable } from 'mo/components';
16+
import { Scrollable, useContextViewEle } from 'mo/components';
1817
import { ICollapseItem } from 'mo/components/collapse';
1918

2019
export interface IFolderTreeProps extends IFolderTreeController, IFolderTree {
@@ -95,7 +94,7 @@ const FolderTree: React.FunctionComponent<IFolderTreeProps> = (props) => {
9594
const contextMenu = useRef<ReturnType<typeof useContextMenu>>();
9695

9796
// panel context view
98-
const contextView = useContextView();
97+
const contextView = useContextViewEle();
9998

10099
// to detect current tree whether is editable
101100
const hasEditable = detectHasEditableStatus(data);
@@ -124,7 +123,7 @@ const FolderTree: React.FunctionComponent<IFolderTreeProps> = (props) => {
124123
data: IFolderTreeNodeProps
125124
) => {
126125
onClickContextMenu?.(item, data);
127-
contextView.hide();
126+
contextView?.hide();
128127
};
129128

130129
const handleRightClick = (event, data) => {

0 commit comments

Comments
 (0)