(props: IContextViewProps = {})
| 59 | * TODO: It's not a hook, don't begin with use |
| 60 | */ |
| 61 | export function useContextView(props: IContextViewProps = {}): IContextView { |
| 62 | const { shadowOutline = true } = props; |
| 63 | const claName = classNames(contextViewClass, 'fade-in'); |
| 64 | let contextView: HTMLElementType = select(`.${contextViewClass}`); // Singleton contextView dom |
| 65 | |
| 66 | const show = (anchorPos: IPosition, render?: () => React.ReactNode) => { |
| 67 | const content = select<HTMLElement>('.' + contentClassName); |
| 68 | const renderContent = render || props?.render; |
| 69 | if (!renderContent) |
| 70 | throw new Error( |
| 71 | 'ContextView show Error: the render parameter is required!' |
| 72 | ); |
| 73 | renderUtils( |
| 74 | <div |
| 75 | ref={() => { |
| 76 | // Notice: if want to get the computed offsetHeight of contextView, |
| 77 | // must display contextView ahead. |
| 78 | if (contextView) { |
| 79 | const position = getRelativePosition( |
| 80 | contextView, |
| 81 | anchorPos |
| 82 | ); |
| 83 | contextView.style.cssText = ` |
| 84 | visibility: visible; |
| 85 | top: ${position.y}px; |
| 86 | left: ${position.x}px; |
| 87 | `; |
| 88 | } |
| 89 | }} |
| 90 | > |
| 91 | {renderContent()} |
| 92 | </div>, |
| 93 | content! |
| 94 | ); |
| 95 | }; |
| 96 | |
| 97 | const hide = () => { |
| 98 | if (contextView) { |
| 99 | const contentContainer = select<HTMLElement>( |
| 100 | `.${contentClassName}` |
| 101 | ); |
| 102 | if (contentContainer) { |
| 103 | unmout(contentContainer); |
| 104 | } |
| 105 | contextView.style.visibility = 'hidden'; |
| 106 | Emitter.emit(ContextViewEvent.onHide); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | const onHide = (callback: Function) => { |
| 111 | Emitter.subscribe(ContextViewEvent.onHide, callback); |
| 112 | }; |
| 113 | |
| 114 | const onMaskClick = (e: React.MouseEvent) => { |
| 115 | e.preventDefault(); |
| 116 | e.stopPropagation(); |
| 117 | hide(); |
| 118 | }; |
no test coverage detected