Skip to content

Commit

Permalink
🐛 fix: LazyLoad add loading dom (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed May 11, 2023
1 parent e6d2c2f commit 7d45403
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/builtins/Previewer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Card } from 'antd';
import { createStyles } from 'antd-style';
import { IPreviewerProps } from 'dumi/dist/client/theme-api/types';
import Previewer from 'dumi/theme-default/builtins/Previewer';
import { rgba } from 'polished';
import { useMemo, useState } from 'react';
import LazyLoad from 'react-lazy-load';
import DemoProvider from '../../components/DemoProvider';

Expand Down Expand Up @@ -122,13 +124,40 @@ const useStyles = createStyles(({ css, token, prefixCls }) => {
export default (props: IPreviewerProps) => {
const { styles, cx, theme } = useStyles();

const [loading, setLoading] = useState(true);

const height = useMemo(() => {
if (typeof props.iframe === 'number') {
return props.iframe;
}
if (props.height) {
return props.height;
}
return 300;
}, [props.iframe, props.height]);

return (
<div className={cx(styles.container, styles[props.codePlacement as 'left' | 'right' | 'top'])}>
<LazyLoad>
<LazyLoad
elementType="section"
onContentVisible={() => {
setLoading(false);
}}
>
<DemoProvider inherit={props.inherit || theme.demoInheritSiteTheme}>
<Previewer {...props} />
</DemoProvider>
</LazyLoad>
{loading && (
<Card
className="loading"
loading
style={{
height,
width: '100%',
}}
/>
)}
</div>
);
};

1 comment on commit 7d45403

@vercel
Copy link

@vercel vercel bot commented on 7d45403 May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.