Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/components/contextView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { render as renderUtils, unmout } from 'mo/react/render';
import { classNames } from 'mo/common/className';
import {
Expand Down Expand Up @@ -40,6 +40,24 @@ enum ContextViewEvent {

const Emitter = new EventEmitter();

/**
* It's a hook used in functional component
*/
export function useContextViewEle(props?: IContextViewProps) {
const [contextView, setContextView] = useState<IContextView>();

useEffect(() => {
// The useContextView can't be called at initial of useState
// Because this function has rendered an element
setContextView(useContextView(props));
}, []);

return contextView;
}

/**
* TODO: It's not a hook, don't begin with use
*/
export function useContextView(props: IContextViewProps = {}): IContextView {
const { shadowOutline = true } = props;
const claName = classNames(contextViewClass, 'fade-in');
Expand Down
5 changes: 3 additions & 2 deletions src/components/dropdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { forwardRef, useImperativeHandle } from 'react';
import { classNames, getBEMModifier, prefixClaName } from 'mo/common/className';
import { useContextView } from '../contextView';
import { useContextViewEle } from '../contextView';
import {
triggerEvent,
TriggerEvent,
Expand Down Expand Up @@ -30,7 +30,7 @@ export const DropDown = forwardRef<DropDownRef, IDropDownProps>(
trigger = 'click',
...restProps
} = props;
const contextView = useContextView({
const contextView = useContextViewEle({
render: () => overlay,
});

Expand All @@ -48,6 +48,7 @@ export const DropDown = forwardRef<DropDownRef, IDropDownProps>(
);
const events = {
[triggerEvent(trigger)]: function (e: React.MouseEvent) {
if (!contextView) return;
const target = e.currentTarget;
const rect = target.getBoundingClientRect();
let position = getPositionByPlacement(placement, rect);
Expand Down
2 changes: 1 addition & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type { ICollapseProps } from './collapse';
export { useContextMenu } from './contextMenu';
export type { IContextMenuProps } from './contextMenu';

export { useContextView } from './contextView';
export { useContextView, useContextViewEle } from './contextView';
export type { IContextViewProps, IContextView } from './contextView';

export { Modal } from './dialog';
Expand Down
11 changes: 11 additions & 0 deletions src/provider/__tests__/__snapshots__/molecule.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,17 @@ dmlldzJfOV8xNjM4ODQ4MDI1NDI2MTE4Ml8zOF9bMF0TNSl1AAAAAElFTkSuQmCC"
</div>
</div>
</div>
<div
class="mo-context-view fade-in"
style="visibility: hidden;"
>
<div
class="mo-context-view__block"
/>
<div
class="mo-context-view__content mo-context-view--shadow"
/>
</div>
</div>
</DocumentFragment>
`;
11 changes: 11 additions & 0 deletions src/workbench/__tests__/__snapshots__/workbench.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ dmlldzJfOV8xNjM4ODQ4MDI1NDI2MTE4Ml8zOF9bMF0TNSl1AAAAAElFTkSuQmCC"
/>
</div>
</div>
<div
class="mo-context-view fade-in"
style="visibility: hidden;"
>
<div
class="mo-context-view__block"
/>
<div
class="mo-context-view__content mo-context-view--shadow"
/>
</div>
</div>
</DocumentFragment>
`;
7 changes: 3 additions & 4 deletions src/workbench/activityBar/activityBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
itemClassName,
normalItemsClassName,
} from './base';
import { useContextView } from 'mo/components';
import { useContextViewEle } from 'mo/components';
import { UniqueId } from 'mo/common/types';

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

const contextView = useContextView({
render: renderContextMenu,
});
const contextView = useContextViewEle({ render: renderContextMenu });

const onClickMenuItem = useCallback(
(e: React.MouseEvent, item: IMenuItemProps | undefined) => {
Expand All @@ -78,6 +76,7 @@ export function ActivityBar(props: IActivityBar & IActivityBarController) {
const handleRightClick = (e) => {
e.preventDefault();
e.stopPropagation();
if (!contextView) return;
const doms = document.elementsFromPoint(e.pageX, e.pageY);
const itemDom = doms.find((dom) =>
dom.classList.contains(itemClassName)
Expand Down
8 changes: 3 additions & 5 deletions src/workbench/activityBar/activityBarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
itemCheckedClassName,
itemDisabledClassName,
} from './base';
import { Icon, useContextView } from 'mo/components';
import { Icon, useContextViewEle } from 'mo/components';
import { KeybindingHelper } from 'mo/services/keybinding';

export function ActivityBarItem(
Expand Down Expand Up @@ -53,9 +53,7 @@ export function ActivityBarItem(
/>
);

const contextView = useContextView({
render: renderContextMenu,
});
const contextView = useContextViewEle({ render: renderContextMenu });

const onClickMenuItem = (
e: React.MouseEvent,
Expand All @@ -70,7 +68,7 @@ export function ActivityBarItem(
onClick(props.id, props);
}
if (contextMenu.length > 0) {
contextView.show({ x: event.pageX, y: event.pageY });
contextView?.show({ x: event.pageX, y: event.pageY });
}
};

Expand Down
7 changes: 3 additions & 4 deletions src/workbench/sidebar/explore/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import Tree from 'mo/components/tree';
import { IMenuItemProps, Menu } from 'mo/components/menu';
import { Button } from 'mo/components/button';
import type { IFolderTreeController } from 'mo/controller/explorer/folderTree';
import { useContextView } from 'mo/components/contextView';
import { useContextMenu } from 'mo/components/contextMenu';
import {
folderTreeClassName,
folderTreeEditClassName,
folderTreeInputClassName,
} from './base';
import { classNames } from 'mo/common/className';
import { Scrollable } from 'mo/components';
import { Scrollable, useContextViewEle } from 'mo/components';
import { ICollapseItem } from 'mo/components/collapse';

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

// panel context view
const contextView = useContextView();
const contextView = useContextViewEle();

// to detect current tree whether is editable
const hasEditable = detectHasEditableStatus(data);
Expand Down Expand Up @@ -124,7 +123,7 @@ const FolderTree: React.FunctionComponent<IFolderTreeProps> = (props) => {
data: IFolderTreeNodeProps
) => {
onClickContextMenu?.(item, data);
contextView.hide();
contextView?.hide();
};

const handleRightClick = (event, data) => {
Expand Down