Merge pull request #644 from smartxworks/perf/portal

perf(editor-sdk): add containerRef for portal
This commit is contained in:
yz-yu 2022-11-23 15:32:17 +08:00 committed by GitHub
commit 089cc77fea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import React, {
useMemo,
useCallback,
useImperativeHandle,
useRef,
} from 'react';
import {
Popover,
@ -20,7 +21,10 @@ import { SpecWidget } from './SpecWidget';
import { implementWidget } from '../../utils/widget';
import { WidgetProps } from '../../types/widget';
import { CORE_VERSION, CoreWidgetName } from '@sunmao-ui/shared';
import { PREVENT_POPOVER_WIDGET_CLOSE_CLASS } from '../../constants/widget';
import {
PREVENT_POPOVER_WIDGET_CLOSE_CLASS,
ComponentFormElementId,
} from '../../constants';
type EvenType = {
'sub-popover-close': string[];
@ -52,6 +56,7 @@ export const PopoverWidget = React.forwardRef<
React.ComponentPropsWithoutRef<React.ComponentType> & WidgetProps<PopoverWidgetType>
>((props, ref) => {
const { spec, path, children } = props;
const containerRef = useRef(document.getElementById(ComponentFormElementId) || null);
const isObjectChildren = children && typeof children === 'object';
const [isInit, setIsInit] = useState(false);
const [isOpen, setIsOpen] = useState(false);
@ -171,7 +176,7 @@ export const PopoverWidget = React.forwardRef<
/>
)}
</PopoverTrigger>
<Portal>
<Portal containerRef={containerRef}>
<PopoverContent
width="sm"
className={PREVENT_POPOVER_WIDGET_CLOSE_CLASS}

View File

@ -1,4 +1,4 @@
import React, { Suspense } from 'react';
import React, { Suspense, useRef } from 'react';
import { CORE_VERSION, StyleWidgetName } from '@sunmao-ui/shared';
import { WidgetProps } from '../../../types/widget';
import { implementWidget, mergeWidgetOptionsIntoSpec } from '../../../utils/widget';
@ -14,6 +14,7 @@ import {
PopoverBody,
Portal,
} from '@chakra-ui/react';
import { ComponentFormElementId } from '../../../constants';
type ColorWidgetType = `${typeof CORE_VERSION}/${StyleWidgetName.Color}`;
@ -32,6 +33,7 @@ const SketchPicker = React.lazy(async () => {
export const ColorWidget: React.FC<WidgetProps<ColorWidgetType, string>> = props => {
const { value, onChange } = props;
const containerRef = useRef(document.getElementById(ComponentFormElementId) || null);
const onColorChange = ({ rgb }: any) => {
onChange(`rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${rgb.a})`);
};
@ -62,7 +64,7 @@ export const ColorWidget: React.FC<WidgetProps<ColorWidgetType, string>> = props
boxShadow="rgba(149, 157, 165, 0.2) 0px 8px 24px"
/>
</PopoverTrigger>
<Portal>
<Portal containerRef={containerRef}>
<PopoverContent w="auto">
<PopoverArrow />
<PopoverBody padding={0}>

View File

@ -3,4 +3,7 @@ export {
CORE_VERSION,
CoreWidgetName,
} from '@sunmao-ui/shared';
export const ComponentFormElementId = 'sunmao-component-form';
export { PREVENT_POPOVER_WIDGET_CLOSE_CLASS } from './widget';

View File

@ -1,7 +1,7 @@
import React from 'react';
import { observer } from 'mobx-react-lite';
import { Accordion, Input, Text, VStack } from '@chakra-ui/react';
import { SpecWidget } from '@sunmao-ui/editor-sdk';
import { ComponentFormElementId, SpecWidget } from '@sunmao-ui/editor-sdk';
import { parseType } from '@sunmao-ui/core';
import { generateDefaultValueFromSpec } from '@sunmao-ui/shared';
import { css } from '@emotion/css';
@ -133,6 +133,7 @@ export const ComponentForm: React.FC<Props> = observer(props => {
return (
<ErrorBoundary>
<Accordion
id={ComponentFormElementId}
reduceMotion
className={ComponentFormStyle}
defaultIndex={sections.map((_, i) => i)}

View File

@ -9,9 +9,10 @@ import {
Portal,
} from '@chakra-ui/react';
import { RegistryInterface } from '@sunmao-ui/runtime';
import React, { useMemo } from 'react';
import React, { useMemo, useRef } from 'react';
import { hideCreateTraitsList } from '../../../constants';
import { ComponentSchema } from '@sunmao-ui/core';
import { ComponentFormElementId } from '@sunmao-ui/editor-sdk';
type Props = {
registry: RegistryInterface;
@ -21,6 +22,7 @@ type Props = {
export const AddTraitButton: React.FC<Props> = props => {
const { onAddTrait, registry, component } = props;
const containerRef = useRef(document.getElementById(ComponentFormElementId) || null);
const componentTraitsMap = useMemo(
() =>
component.traits.reduce((result, trait) => {
@ -48,7 +50,7 @@ export const AddTraitButton: React.FC<Props> = props => {
});
return (
<Box>
<Menu>
<Menu isLazy>
<MenuButton
as={Button}
aria-label="add event"
@ -59,7 +61,7 @@ export const AddTraitButton: React.FC<Props> = props => {
>
Add Trait
</MenuButton>
<Portal>
<Portal containerRef={containerRef}>
<MenuList>{menuItems}</MenuList>
</Portal>
</Menu>

View File

@ -73,7 +73,7 @@ export const DataSource: React.FC<Props> = props => {
DataSource
</Text>
<Spacer />
<Menu>
<Menu isLazy>
<MenuButton
as={IconButton}
aria-label="add event"