Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor portal #1040

Merged
merged 22 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions packages/zent/RELEASE_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@

导出的组件名字变了,老的写法

#### `Portal`

合并`Portal`和`LayeredPortal`。

去除`Portal.withEscToClose`和`Portal.withNonScrollable`,`Portal`添加两个参数`withEscToClose`和`withNonScrollable`,`PurePortal`添加`withEscToClose`。

去除`onMount`和`onUnmount`,直接使用`componentDidMount`和`componentWillUnmount`即可。

```js
import { Layout } from 'zent';

Expand Down
1 change: 0 additions & 1 deletion packages/zent/__tests__/preview-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('previewImage render', () => {
showRotateBtn: true,
index: 0,
});
expect(document.querySelectorAll('.zent-portal').length).toBe(1);
expect(document.querySelectorAll('.zent-image-p-anchor').length).toBe(1);
expect(document.querySelectorAll('.zent-show-image').length).toBe(1);

Expand Down
27 changes: 10 additions & 17 deletions packages/zent/src/dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import * as React from 'react';
import { Component } from 'react';
import { CSSTransition } from 'react-transition-group';

import Portal, { IPortalProps } from '../portal';
import Portal from '../portal';
import isBrowser from '../utils/isBrowser';
import { DialogElWrapper, DialogInnerEl } from './DialogEl';
import { DialogElWrapper, DialogInnerEl, IMousePosition } from './DialogEl';
import { openDialog, closeDialog } from './open';

const { withNonScrollable, withESCToClose } = Portal;
const DialogPortal = withNonScrollable(Portal as React.ComponentType<
IPortalProps
>);
const DialogPortalESCToClose = withESCToClose(DialogPortal);

const TIMEOUT = 300; // ms

let mousePosition = null;
let mousePosition: IMousePosition | null = null;

// Inspired by antd and rc-dialog
if (isBrowser) {
Expand All @@ -31,7 +25,7 @@ export interface IDialogProps {
title?: React.ReactNode;
children?: React.ReactNode;
footer?: React.ReactNode;
visible?: boolean;
visible: boolean;
closeBtn?: boolean;
onClose?: (
e:
Expand All @@ -43,7 +37,7 @@ export interface IDialogProps {
maskClosable?: boolean;
className?: string;
prefix?: string;
style?: React.CSSProperties;
style: React.CSSProperties;
onOpened?: () => void;
onClosed?: () => void;
}
Expand All @@ -70,7 +64,7 @@ export class Dialog extends Component<IDialogProps, IDialogState> {
static openDialog = openDialog;
static closeDialog = closeDialog;

lastMousePosition = null;
lastMousePosition: IMousePosition | null = null;

constructor(props: IDialogProps) {
super(props);
Expand Down Expand Up @@ -139,14 +133,13 @@ export class Dialog extends Component<IDialogProps, IDialogState> {
this.lastMousePosition = null;
}

// 有关闭按钮的时候同时具有ESC关闭的行为
const PortalComponent = closeBtn ? DialogPortalESCToClose : DialogPortal;

return (
<PortalComponent
<Portal
visible={visible || exiting}
onClose={this.onClose}
className={`${prefix}-dialog-r-anchor`}
withEscToClose={closeBtn}
withNonScrollable
>
<DialogElWrapper
prefix={prefix}
Expand Down Expand Up @@ -176,7 +169,7 @@ export class Dialog extends Component<IDialogProps, IDialogState> {
</DialogInnerEl>
</CSSTransition>
</DialogElWrapper>
</PortalComponent>
</Portal>
);
}
}
Expand Down
12 changes: 7 additions & 5 deletions packages/zent/src/dialog/DialogEl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { Component, createRef } from 'react';
import cx from 'classnames';
import focusWithoutScroll from '../utils/dom/focusWithoutScroll';

export interface IMousePosition {
x: number;
y: number;
}

export interface IDialogInnerElProps {
prefix?: string;
title?: React.ReactNode;
Expand All @@ -11,10 +16,7 @@ export interface IDialogInnerElProps {
closeBtn?: boolean;
style?: React.CSSProperties;
footer?: React.ReactNode;
mousePosition?: {
x: number;
y: number;
} | null;
mousePosition?: IMousePosition | null;
}

export class DialogInnerEl extends Component<IDialogInnerElProps> {
Expand All @@ -41,7 +43,7 @@ export class DialogInnerEl extends Component<IDialogInnerElProps> {
const origin = `${mousePosition.x - x}px ${mousePosition.y - y}px 0`;
const style = this.dialogEl.style;
['Webkit', 'Moz', 'Ms', 'ms'].forEach(prefix => {
style[`${prefix}TransformOrigin`] = origin;
style[`${prefix}TransformOrigin` as any] = origin;
});
style.transformOrigin = origin;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/zent/src/dialog/open.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ICloseDialogOption {
}

interface IStandaloneDialogProps {
options: IOpenDialogOption & { dialogId: string };
options: Partial<IOpenDialogOption> & { dialogId: string };
container: HTMLDivElement;
}

Expand Down Expand Up @@ -112,7 +112,7 @@ export interface IOpenDialogOption extends Omit<IDialogProps, 'onClose'> {
/*
打开一个dialog,返回值是一个用来关闭dialog的函数。
*/
export function openDialog(options: IOpenDialogOption = {}) {
export function openDialog(options: Partial<IOpenDialogOption> = {}) {
if (!isBrowser) return noop;

const { dialogId = uniqueId('__zent-dialog__'), parentComponent } = options;
Expand Down
36 changes: 16 additions & 20 deletions packages/zent/src/loading/FullScreenLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import * as React from 'react';
import cx from 'classnames';
import isUndefined from 'lodash-es/isUndefined';
import isNumber from 'lodash-es/isNumber';

import PurePortal from '../portal/PurePortal';
import withNonScrollable from '../portal/withNonScrollable';
import useDelayed from './hooks/useDelayed';
import { IFullScreenLoadingProps, FullScreenDefaultProps } from './props';
import LoadingMask from './components/LoadingMask';
import { LayeredPortal } from '../portal';

const NO_STYLE = {};

const NonScrollablePurePortal = withNonScrollable(PurePortal);
const NO_STYLE: Partial<CSSStyleDeclaration> = {};

export function FullScreenLoading(props: IFullScreenLoadingProps) {
const {
Expand All @@ -29,22 +26,21 @@ export function FullScreenLoading(props: IFullScreenLoadingProps) {
return null;
}

const style = isUndefined(zIndex) ? NO_STYLE : { zIndex };
const style = isNumber(zIndex) ? NO_STYLE : { zIndex };

return (
<NonScrollablePurePortal selector={document.body} append>
<div
className={cx('zent-loading', 'zent-loading--fullscreen', className)}
style={style}
>
<LoadingMask
icon={icon}
size={iconSize}
text={iconText}
textPosition={textPosition}
/>
</div>
</NonScrollablePurePortal>
<LayeredPortal
className={cx('zent-loading', 'zent-loading--fullscreen', className)}
style={style}
withNonScrollable
>
<LoadingMask
icon={icon}
size={iconSize}
text={iconText}
textPosition={textPosition}
/>
</LayeredPortal>
);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/zent/src/loading/props.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as React from 'react';

export interface ILoadingBaseProps {
loading?: boolean;
delay?: number;
icon?: 'youzan' | 'circle';
loading: boolean;
delay: number;
icon: 'youzan' | 'circle';
iconSize?: number;
iconText?: React.ReactNode;
textPosition?: 'top' | 'bottom' | 'left' | 'right';
textPosition: 'top' | 'bottom' | 'left' | 'right';
className?: string;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/zent/src/pop/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import capitalize from 'lodash-es/capitalize';
import isFunction from 'lodash-es/isFunction';

import Popover from '../popover';
import { CSSProperties } from 'react';

const { Position } = Popover;

Expand All @@ -12,7 +11,7 @@ const ARROW_OFFSET_V = 9;

const createPosition = (x, y, side) => {
return {
getCSSStyle(): CSSProperties {
getCSSStyle(): Partial<CSSStyleDeclaration> {
return {
position: 'absolute',
left: `${Math.round(x)}px`,
Expand Down
6 changes: 1 addition & 5 deletions packages/zent/src/popover/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IPopoverContentState {
*/
export default class PopoverContent extends Component<
IPopoverContentProps,
any
IPopoverContentState
> {
positionReady: boolean;
positionedParent: Element | null;
Expand Down Expand Up @@ -135,7 +135,6 @@ export default class PopoverContent extends Component<
containerBoundingBoxViewport: parentBoundingBox,
}
);

if (!isEqualPlacement(this.state.position, position)) {
this.setState(
{
Expand All @@ -162,7 +161,6 @@ export default class PopoverContent extends Component<

componentDidMount() {
const { visible } = this.props;

if (visible) {
this.adjustPosition();
}
Expand Down Expand Up @@ -196,12 +194,10 @@ export default class PopoverContent extends Component<

return (
<Portal
prefix={prefix}
visible={visible}
selector={containerSelector}
className={cls}
style={position.getCSSStyle()}
onMount={this.adjustPosition}
>
<div className={`${prefix}-popover-content`}>
{children}
Expand Down
6 changes: 3 additions & 3 deletions packages/zent/src/popover/placement/invisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const locate: PositionFunctionImpl = () => {
const y = -100000;

return {
getCSSStyle() {
getCSSStyle(): Partial<CSSStyleDeclaration> {
return {
position: 'fixed',
left: `${x}px`,
top: `${y}px`,
zIndex: -10,
opacity: 0,
zIndex: '-10',
opacity: '0',
};
},

Expand Down
2 changes: 1 addition & 1 deletion packages/zent/src/popover/position-function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface IPopoverPosition {
getCSSStyle: () => React.CSSProperties;
getCSSStyle: () => Partial<CSSStyleDeclaration>;
name: string;
}

Expand Down
20 changes: 0 additions & 20 deletions packages/zent/src/portal/ClosablePortal.tsx

This file was deleted.

Loading