Skip to content

Commit

Permalink
Merge pull request #32 from js-tool-pack/perf
Browse files Browse the repository at this point in the history
Perf
  • Loading branch information
mengxinssfd authored Jul 14, 2023
2 parents d210349 + 70ca063 commit 2a9b696
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 187 deletions.
98 changes: 44 additions & 54 deletions packages/components/src/dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,69 +60,59 @@ export const Dialog: React.FC<DialogProps> = memo((props) => {

const Mask = (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
<div
key="dialog-mask"
className={`${rootClass}__mask`}
style={{ zIndex }}
onClick={handleMaskClick}></div>
);
const TransitionMask = (
<Transition name={`${rootClass}__mask`} appear>
{show && Mask}
</Transition>
<div className={`${rootClass}__mask`} onClick={handleMaskClick}></div>
);
const Box = (
<Layout
key="dialog-box"
{...rest}
className={getClassNames(rootClass, {
[className as string]: className,
[`${rootClass}__center`]: center,
})}
style={{
...style,
transformOrigin,
}}
vertical>
{header !== null && (
<Header className={`${rootClass}__header`}>
<span className={`${rootClass}__title`}>{header}</span>
<Button
className={`${rootClass}__btn-close`}
plain="text"
size="small"
onClick={close}>
<Icon>
<CloseIcon />
</Icon>
</Button>
</Header>
)}
<Main className={`${rootClass}__main`}>{children}</Main>
{footer !== null && (
<Footer className={`${rootClass}__footer`}>{footer}</Footer>
)}
</Layout>
<div
className={getClassNames(`${rootClass}__wrapper`, {
[`${rootClass}__centered`]: centered,
})}>
<Layout
key="dialog-box"
{...rest}
className={getClassNames(rootClass, {
[className as string]: className,
[`${rootClass}__center`]: center,
})}
style={{
...style,
transformOrigin,
}}
vertical>
{header !== null && (
<Header className={`${rootClass}__header`}>
<span className={`${rootClass}__title`}>{header}</span>
<Button
className={`${rootClass}__btn-close`}
plain="text"
size="small"
onClick={close}>
<Icon>
<CloseIcon />
</Icon>
</Button>
</Header>
)}
<Main className={`${rootClass}__main`}>{children}</Main>
{footer !== null && (
<Footer className={`${rootClass}__footer`}>{footer}</Footer>
)}
</Layout>
</div>
);
const TransitionBox = (

return createPortal(
<Transition name="t-dialog" on={onTransitionChange}>
{show && (
<div
key="dialog-wrapper"
style={{ zIndex }}
className={getClassNames(`${rootClass}__wrapper`, {
[`${rootClass}__centered`]: centered,
})}>
key={rootClass}
className={`${rootClass}__root`}
style={{ zIndex }}>
{Mask}
{Box}
</div>
)}
</Transition>
);
return createPortal(
<div className={`${rootClass}__root`}>
{TransitionMask}
{TransitionBox}
</div>,
</Transition>,
document.body,
);
});
Expand Down
7 changes: 4 additions & 3 deletions packages/components/src/dialog/dialog.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export function useTransitionOrigin(props: DialogProps, show: boolean) {

const transformOrigin = useMemo(() => {
const left = `calc(50% + ${-clientSize[0] / 2 + point[0]}px)`;
const styleTop = style?.top;
const top = centered
? `calc(50% + ${-clientSize[1] / 2 + point[1]}px)`
: style?.top !== undefined
? `calc(${point[1]}px - ${style.top})`
? `calc(50% + (${-clientSize[1] / 2 + point[1]}px - ${styleTop || 0}))`
: styleTop !== undefined
? `calc(${point[1]}px - ${styleTop})`
: `calc(${point[1]}px - 50%)`;
return `${left} ${top}`;
}, [clientSize, point, centered, style]);
Expand Down
38 changes: 18 additions & 20 deletions packages/components/src/dialog/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ $c: '#{$prefix}dialog';
border-radius: var(--t-radius);
color: var(--t-text-color);
background-color: var(--t-bg-color);
box-shadow: rgb(0 0 0 / 8%) 0 6px 16px 0,
rgb(0 0 0 / 12%) 0 3px 6px -4px, rgb(0 0 0 / 5%) 0 9px 28px 8px;
box-shadow: rgb(0 0 0 / 8%) 0 6px 16px 0, rgb(0 0 0 / 12%) 0 3px 6px -4px,
rgb(0 0 0 / 5%) 0 9px 28px 8px;
pointer-events: auto;
&__mask {
position: fixed;
Expand Down Expand Up @@ -77,28 +77,26 @@ $c: '#{$prefix}dialog';
&__root {
--t-dialog-transition-duration: var(--t-transition-duration);

// --t-dialog-transition-duration: 5s;
.#{$c}__mask-enter-active,
.#{$c}__mask-leave-active {
transition: opacity var(--t-dialog-transition-duration) ease-in-out;
position: relative;
}
&-enter-active,
&-leave-active {
transition: background-color var(--t-dialog-transition-duration) linear;
.#{$c} {
transition: all var(--t-dialog-transition-duration) ease;
}
.#{$c}__mask-enter-from,
.#{$c}__mask-leave-to {
opacity: 0;
.#{$c}__mask {
transition: opacity var(--t-dialog-transition-duration) linear;
}
.#{$c}-enter-active,
.#{$c}-leave-active {
transition: opacity var(--t-dialog-transition-duration) ease;
.#{$c} {
transition: all var(--t-dialog-transition-duration) ease;
}
}
&-enter-from,
&-leave-to {
background-color: white;
.#{$c} {
transform: scale(0) translateZ(0);
}
.t-dialog-enter-from,
.t-dialog-leave-to {
.#{$c}__mask {
opacity: 0;
.#{$c} {
transform: scale(0) translateZ(0);
}
}
}
}
19 changes: 0 additions & 19 deletions packages/components/src/layouts/Aside.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions packages/components/src/layouts/Footer.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions packages/components/src/layouts/Header.tsx

This file was deleted.

73 changes: 55 additions & 18 deletions packages/components/src/layouts/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
import React from 'react';
import { getComponentClass } from '@pkg/shared';
import { getClassNames } from '@tool-pack/basic';
import { BaseLayoutsProps } from './layout.types';
import type { BaseLayoutsProps, LayoutProps } from './layout.types';
import { useElement } from './useElement';
import { RequiredPart } from '@tool-pack/types';

const rootClass = getComponentClass('layout');
export const Layout = React.forwardRef<
HTMLElement,
BaseLayoutsProps & {
vertical?: boolean;
}
>((props, ref) => {
const { vertical, className, children, ...rest } = props;
return (
<section
{...rest}
ref={ref}
className={getClassNames(rootClass, {
export const Layout = React.forwardRef<HTMLElement, LayoutProps>(
(props, ref) => {
const { tag, vertical, className, children, ...rest } =
props as RequiredPart<LayoutProps, 'tag'>;
return React.createElement(tag, {
...rest,
ref,
className: getClassNames(rootClass, {
vertical,
[className as string]: className,
})}>
{children}
</section>
);
});
}),
children,
});
},
);
Layout.defaultProps = {
tag: 'section',
} satisfies Partial<LayoutProps>;

// main
const mainRootClass = getComponentClass('main');
export const Main: React.FC<BaseLayoutsProps> = (props) => {
return useElement(props, mainRootClass);
};
Main.defaultProps = {
tag: 'main',
} satisfies Partial<BaseLayoutsProps>;

// aside
const asideRootClass = getComponentClass('aside');
export const Aside: React.FC<BaseLayoutsProps> = (props) => {
return useElement(props, asideRootClass);
};
Aside.defaultProps = {
tag: 'aside',
} satisfies Partial<BaseLayoutsProps>;

// header
const headerRootClass = getComponentClass('header');
export const Header: React.FC<BaseLayoutsProps> = (props) => {
return useElement(props, headerRootClass);
};
Header.defaultProps = {
tag: 'header',
} satisfies Partial<BaseLayoutsProps>;

// footer
const footerRootClass = getComponentClass('footer');
export const Footer: React.FC<BaseLayoutsProps> = (props) => {
return useElement(props, footerRootClass);
};
Footer.defaultProps = {
tag: 'footer',
} satisfies Partial<BaseLayoutsProps>;
19 changes: 0 additions & 19 deletions packages/components/src/layouts/Main.tsx

This file was deleted.

6 changes: 1 addition & 5 deletions packages/components/src/layouts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
// import './layouts.scss';
export type { LayoutProps, BaseLayoutsProps } from './layout.types';
export * from './Layout';
export * from './Header';
export * from './Footer';
export * from './Main';
export * from './Aside';
7 changes: 6 additions & 1 deletion packages/components/src/layouts/layout.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import React from 'react';

export type BaseLayoutsProps = React.HTMLAttributes<HTMLElement>;
export type BaseLayoutsProps = React.HTMLAttributes<HTMLElement> & {
tag?: keyof HTMLElementTagNameMap;
};
export type LayoutProps = BaseLayoutsProps & {
vertical?: boolean;
};
21 changes: 21 additions & 0 deletions packages/components/src/layouts/useElement.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BaseLayoutsProps } from './layout.types';
import { RequiredPart } from '@tool-pack/types';
import React from 'react';
import { getClassNames } from '@tool-pack/basic';

export function useElement(props: BaseLayoutsProps, rootClass: string) {
const { children, className, tag, ...rest } = props as RequiredPart<
BaseLayoutsProps,
'tag'
>;
return React.createElement(
tag,
{
...rest,
className: getClassNames(rootClass, {
[className as string]: className,
}),
},
children,
);
}
Loading

0 comments on commit 2a9b696

Please sign in to comment.