Merge pull request #90 from webzard-io/feat/dialog

render modal in editor view instead of full screen
This commit is contained in:
yz-yu 2021-10-18 18:40:12 +08:00 committed by GitHub
commit 54fbe286ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 5 deletions

View File

@ -1,8 +1,7 @@
import { useEffect, useMemo, useState } from 'react'; import { useEffect, useMemo, useState } from 'react';
import { GridCallbacks } from '@meta-ui/runtime'; import { GridCallbacks, DIALOG_CONTAINER_ID } from '@meta-ui/runtime';
import produce from 'immer'; import produce from 'immer';
import { Box, Tabs, TabList, Tab, TabPanels, TabPanel } from '@chakra-ui/react'; import { Box, Tabs, TabList, Tab, TabPanels, TabPanel } from '@chakra-ui/react';
import { last } from 'lodash';
import { App, stateStore } from '../metaUI'; import { App, stateStore } from '../metaUI';
import { StructureTree } from './StructureTree'; import { StructureTree } from './StructureTree';
import { import {
@ -116,6 +115,12 @@ export const Editor = () => {
background="white" background="white"
transform={`scale(${scale / 100})`} transform={`scale(${scale / 100})`}
> >
<Box
id={DIALOG_CONTAINER_ID}
width="full"
height="full"
position="absolute"
/>
{appComponent} {appComponent}
</Box> </Box>
</Box> </Box>

View File

@ -9,10 +9,13 @@ import {
AlertDialogHeader, AlertDialogHeader,
AlertDialogOverlay, AlertDialogOverlay,
Button, Button,
ModalContentProps,
ModalOverlayProps,
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import { Static, Type } from '@sinclair/typebox'; import { Static, Type } from '@sinclair/typebox';
import Slot from '../_internal/Slot'; import Slot from '../_internal/Slot';
import { ColorSchemePropertySchema } from './Types/ColorScheme'; import { ColorSchemePropertySchema } from './Types/ColorScheme';
import { DIALOG_CONTAINER_ID } from '../../constants';
const HandleButtonPropertySchema = Type.Object({ const HandleButtonPropertySchema = Type.Object({
text: Type.Optional(Type.String()), text: Type.Optional(Type.String()),
@ -37,6 +40,11 @@ const Dialog: ComponentImplementation<Static<typeof PropsSchema>> = ({
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const [title, setTitle] = useState(customerTitle || ''); const [title, setTitle] = useState(customerTitle || '');
const cancelRef = useRef(null); const cancelRef = useRef(null);
const containerRef = useRef<HTMLElement | null>(null);
useEffect(() => {
containerRef.current = document.getElementById(DIALOG_CONTAINER_ID);
}, [containerRef]);
useEffect(() => { useEffect(() => {
subscribeMethods({ subscribeMethods({
@ -53,17 +61,34 @@ const Dialog: ComponentImplementation<Static<typeof PropsSchema>> = ({
}); });
}, []); }, []);
const dialogContentProps: ModalContentProps = {
position: 'absolute',
width: 'full',
containerProps: { position: 'absolute', width: 'full', height: 'full' },
};
const dialogOverlayProps: ModalOverlayProps = {
position: 'absolute',
width: 'full',
height: 'full',
};
const portalProps = {
appendToParentPortal: true,
containerRef,
};
return ( return (
<React.Fragment> <React.Fragment>
<AlertDialog <AlertDialog
isOpen={isOpen} isOpen={isOpen}
leastDestructiveRef={cancelRef} leastDestructiveRef={cancelRef}
onClose={() => setIsOpen(false)} onClose={() => setIsOpen(false)}
portalProps={containerRef.current ? portalProps : undefined}
> >
<AlertDialogOverlay> <AlertDialogOverlay {...(containerRef.current ? dialogOverlayProps : {})}>
<AlertDialogContent> <AlertDialogContent {...(containerRef.current ? dialogContentProps : {})}>
<AlertDialogHeader>{title}</AlertDialogHeader> <AlertDialogHeader>{title}</AlertDialogHeader>
<AlertDialogBody> <AlertDialogBody>
<Slot slotsMap={slotsMap} slot="content" /> <Slot slotsMap={slotsMap} slot="content" />
</AlertDialogBody> </AlertDialogBody>

View File

@ -1,3 +1,4 @@
export const LIST_ITEM_EXP = '$listItem'; export const LIST_ITEM_EXP = '$listItem';
export const LIST_ITEM_INDEX_EXP = '$i'; export const LIST_ITEM_INDEX_EXP = '$i';
export const GRID_HEIGHT = 40; export const GRID_HEIGHT = 40;
export const DIALOG_CONTAINER_ID = 'meta-ui-dialog-container';

View File

@ -24,3 +24,4 @@ export function initMetaUI() {
export * from './utils/parseType'; export * from './utils/parseType';
export * from './utils/parseTypeBox'; export * from './utils/parseTypeBox';
export * from './types/RuntimeSchema'; export * from './types/RuntimeSchema';
export * from './constants';