Skip to content

Commit ec793ab

Browse files
authored
Merge pull request #12 from 1Byte-Software/develop
Develop
2 parents d4c71f4 + 38ea32f commit ec793ab

File tree

13 files changed

+131
-19
lines changed

13 files changed

+131
-19
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { forwardRef } from 'react';
22
import { TypographyLinkStyles } from './styles';
33
import { RdTypographyLinkProps } from './types';
4+
import { Skeleton } from '../../molecules';
45

56
export const TypographyLink = forwardRef(
6-
({ ...antdProps }: RdTypographyLinkProps, ref: RdTypographyLinkProps['ref']) => {
7+
(props: RdTypographyLinkProps, ref: RdTypographyLinkProps['ref']) => {
8+
const { loading, ...antdProps } = props;
9+
10+
if (loading) return <Skeleton active />;
11+
712
return <TypographyLinkStyles ref={ref} {...antdProps} />;
813
}
914
);
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { forwardRef } from 'react';
22
import { TypographyParagraphStyles } from './styles';
33
import { RdTypographyParagraphProps } from './types';
4+
import { Skeleton } from '../../molecules';
45

56
export const TypographyParagraph = forwardRef(
6-
({ ...antdProps }: RdTypographyParagraphProps, ref: RdTypographyParagraphProps['ref']) => {
7+
(props: RdTypographyParagraphProps, ref: RdTypographyParagraphProps['ref']) => {
8+
const { loading, ...antdProps } = props;
9+
10+
if (loading) return <Skeleton active />;
11+
712
return <TypographyParagraphStyles ref={ref} {...antdProps} />;
813
}
914
);

src/atomics/Typography/TypographyText.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { forwardRef, useMemo } from 'react';
22
import { TypographyTextStyles } from './styles';
33
import { RdTypographyTextProps } from './types';
4+
import { Skeleton } from '../../molecules';
45

56
export const TypographyText = forwardRef(
67
(props: RdTypographyTextProps, ref: RdTypographyTextProps['ref']) => {
7-
const { size = 'normal', editable, autoFocus = false, onChange, ...antdProps } = props;
8+
const {
9+
size = 'normal',
10+
editable,
11+
autoFocus = false,
12+
loading = false,
13+
onChange,
14+
...antdProps
15+
} = props;
16+
17+
if (loading) return <Skeleton.Input active />;
818

919
const editableCustom = useMemo(() => {
1020
if (editable && typeof editable === 'object') {
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { forwardRef } from 'react';
2+
import { Skeleton } from '../../molecules';
23
import { TypographyTitleStyles } from './styles';
34
import { RdTypographyTitleProps } from './types';
45

56
export const TypographyTitle = forwardRef(
6-
({ disableMargin, ...antdProps }: RdTypographyTitleProps, ref: RdTypographyTitleProps['ref']) => {
7+
(props: RdTypographyTitleProps, ref: RdTypographyTitleProps['ref']) => {
8+
const { disableMargin, loading, ...antdProps } = props;
9+
10+
if (loading) return <Skeleton.Input active />;
11+
712
return <TypographyTitleStyles disableMargin={disableMargin} ref={ref} {...antdProps} />;
813
}
914
);

src/atomics/Typography/styles.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import styled from '@emotion/styled';
22
import { Typography } from 'antd';
33
import { getComponentOrGlobalToken } from '../../utils/token';
4-
import { RdTypographyTextProps, RdTypographyTitleProps } from './types';
4+
import { RdTypographyParagraphProps, RdTypographyTextProps, RdTypographyTitleProps } from './types';
55
import { css } from '@emotion/react';
66
import { getExcludeForwardProps } from '../../utils/styles';
77

@@ -36,4 +36,19 @@ export const TypographyTextStyles = styled(Typography.Text)<Pick<RdTypographyTex
3636
}}
3737
`;
3838

39-
export const TypographyParagraphStyles = styled(Typography.Paragraph)``;
39+
export const TypographyParagraphStyles = styled(Typography.Paragraph, {
40+
shouldForwardProp: prop =>
41+
getExcludeForwardProps<RdTypographyParagraphProps>(
42+
['minRows'] as (keyof RdTypographyParagraphProps)[],
43+
prop
44+
),
45+
})<RdTypographyParagraphProps>`
46+
${({ minRows }) => {
47+
return (
48+
minRows &&
49+
css`
50+
min-height: ${Number(getComponentOrGlobalToken('Typography', 'lineHeight')) * Number(getComponentOrGlobalToken('Typography', 'fontSize')) * minRows}px;
51+
`
52+
);
53+
}}
54+
`;

src/atomics/Typography/types.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ type TypographyComponentTokenExtend = {};
1414
//#endregion
1515

1616
//#region Define extended types
17-
type TypographyPropsExtend = {};
18-
type TypographyLinkPropsExtend = {};
19-
type TypographyParagraphPropsExtend = {};
20-
type TypographyTextPropsExtend = {
17+
type TypographyBaseProps = {
18+
loading?: boolean;
19+
};
20+
type TypographyPropsExtend = TypographyBaseProps & {};
21+
type TypographyLinkPropsExtend = TypographyBaseProps & {};
22+
type TypographyParagraphPropsExtend = TypographyBaseProps & {
23+
/**
24+
* Min rows of paragraph
25+
*/
26+
minRows?: number;
27+
};
28+
type TypographyTextPropsExtend = TypographyBaseProps & {
2129
/**
2230
* @description The size of the text.
2331
* @default "normal"
@@ -34,7 +42,7 @@ type TypographyTextPropsExtend = {
3442
/**
3543
* Extended properties for customizing the Typography.Title component.
3644
*/
37-
type TypographyTitlePropsExtend = {
45+
type TypographyTitlePropsExtend = TypographyBaseProps & {
3846
/**
3947
* If set to `true`, disables the default margin applied to the Typography.Title component.
4048
* @default false

src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ export * from './atomics';
1515
export * from './molecules';
1616
export * from './organisms';
1717
export * from './templates';
18-
export * from './utils';
18+
export * from './utils';
19+
20+
export * from './types'

src/molecules/Image/Image.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1+
import { Skeleton } from '../Skeleton';
12
import { ImageStyles } from './styles';
23
import { RdImageComponent } from './types';
34

45
export const Image: RdImageComponent = props => {
5-
return <ImageStyles {...props} />;
6+
const { loading = false, ...antdProps } = props;
7+
8+
return (
9+
<>
10+
{/**
11+
* Note:
12+
* Previously, when `loading = true`, the component only rendered the `Skeleton.Node`
13+
* and skipped rendering the `ImageStyles` completely.
14+
*
15+
* However, in that case, the `img` element was never created, so the browser wouldn't start downloading the image,
16+
* and we couldn't catch the `onLoad` event.
17+
*
18+
* Now, even when `loading = true`, the image is still rendered (but hidden) so that:
19+
* - The browser starts loading it
20+
* - The `onLoad` event can be triggered when it's ready
21+
* - Once the image is fully loaded, `loading` can be set to `false`, revealing the image
22+
*
23+
* This helps with smoother image transitions (e.g. fade-in) and avoids a jarring pop-in effect.
24+
*/}
25+
{loading && (
26+
<Skeleton.Node
27+
active
28+
style={{
29+
width: antdProps?.width,
30+
height: antdProps?.height,
31+
}}
32+
/>
33+
)}
34+
<ImageStyles hidden={loading} {...antdProps} />
35+
</>
36+
);
637
};

src/molecules/Image/styles.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
import styled from '@emotion/styled';
22
import { Image } from 'antd';
3+
import { getExcludeForwardProps } from '../../utils';
4+
import { RdImageProps } from './types';
35

4-
export const ImageStyles = styled(Image)``;
6+
export const ImageStyles = styled(Image, {
7+
shouldForwardProp: prop =>
8+
getExcludeForwardProps<RdImageProps>([] as (keyof RdImageProps)[], prop),
9+
})<RdImageProps>`
10+
${({ hidden }) => {
11+
return (
12+
hidden &&
13+
`
14+
display: none
15+
`
16+
);
17+
}}
18+
`;

src/molecules/Image/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ type ImageComponentTokenExtend = {};
1010
//#endregion
1111

1212
//#region Define extended types
13-
type ImagePropsExtend = {};
13+
type ImagePropsExtend = {
14+
// Override loading prop in antd image, use loadingType instead
15+
loading?: boolean;
16+
loadingType?: ImagePropsAntd['loading'];
17+
};
1418
//#endregion
1519

1620
//#region Export types
17-
export type RdImageProps = ImagePropsAntd & ImagePropsExtend;
21+
export type RdImageProps = Omit<ImagePropsAntd, 'loading'> & ImagePropsExtend;
1822
export type RdImageComponentToken = ImageComponentTokenAntd & ImageComponentTokenExtend;
1923
//#endregion
2024

0 commit comments

Comments
 (0)