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

Lh 393 creating a image #8

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createStyles } from "@travelmakers-design-v2/styles";
import { Props } from "./Image";

export default createStyles((theme, { load }: { load: boolean }) => {
return {
loading: {
display: "none",
},
};
});
56 changes: 56 additions & 0 deletions packages/travelmakers-design-core/src/components/Image/Image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { PolymorphicRef } from "@travelmakers-design-v2/styles";
import React, { forwardRef, useState } from "react";
import useStyles from "./Image.style";
import { ImageComponent, ImageProps } from "./Image.type";

export interface Props extends React.ImgHTMLAttributes<HTMLImageElement> {
/** true일 경우 lazy load가 적용됩니다. */
lazy?: boolean;

/** 이미지 src를 정합니다. */
src: string;

/** 이미지 설명을 추가합니다. */
alt: string;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

P4: 태그에 기본적으로 들어가는 src, width, height, alt는 자동완성이 지원이 되니까 Props에서는 지우는게 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

width, height만 삭제하는걸로 소통완료


export const Image: ImageComponent & {
displayName?: string;
} = forwardRef(
<C extends React.ElementType = "img">(
{ lazy = true, src, alt, className, ...props }: ImageProps<C>,
ref: PolymorphicRef<C>
) => {
const [load, setLoad] = useState(false);
const { classes, cx } = useStyles({ load });

return (
<>
<img
ref={ref}
src={src}
alt={alt}
Copy link
Contributor

Choose a reason for hiding this comment

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

P4: type Props부분과 동일하게 컴포넌트내의 props<img />태그에도 정리가 되면 좋을 것 같습니당

Copy link
Contributor Author

Choose a reason for hiding this comment

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

확인완료 :)

loading={lazy ? "lazy" : "eager"}
className={cx(className, !load && classes.loading)}
onLoad={() => setLoad(true)}
onError={(e) => {
setLoad(true);
e.currentTarget.src = require("./img/error.png");
}}
{...props}
/>
{!load && (
<img
loading={lazy ? "lazy" : "eager"}
src={
"data:image/gif;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mO8UA8AAiUBUcc3qzwAAAAASUVORK5CYII="
}
Copy link
Contributor

Choose a reason for hiding this comment

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

P4: 로드시 노출되는 이미지에도 loading={lazy ? "lazy" : "eager"}가 있는게 좋을까용?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

좋아요~! ✅

{...props}
/>
)}
</>
);
}
);

Image.displayName = "Image";
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
ClassNames,
PolymorphicComponentProps,
TmComponentProps,
} from "@travelmakers-design-v2/styles";
import useStyles from "./Image.style";
import { Props } from "./Image";

export type ImageStylesNames = ClassNames<typeof useStyles>;

export interface SharedImageProps
extends Props,
TmComponentProps<ImageStylesNames> {}

export type ImageProps<C extends React.ElementType> = PolymorphicComponentProps<
C,
SharedImageProps
>;

export type ImageComponent = <C extends React.ElementType = "img">(
props: ImageProps<C>
) => React.ReactElement;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Image } from "./Image";
export type { ImageProps } from "./Image.type";
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Meta } from "@storybook/react";
import { Image } from "../Image";

export default {
title: "@travelmakers-design-v2/core/General/Image",
component: Image,
argTypes: {
src: {
defaultValue: "https://picsum.photos/600/400",
description: "이미지 src를 정합니다.",
table: {
type: {
summary: "string",
},
},
control: { type: "text" },
},
lazy: {
defaultValue: true,
description: "true일 경우 lazy load가 적용됩니다.",
control: { type: "boolean" },
},
width: {
defaultValue: 400,
description: "Image 컴포넌트의 너비를 정합니다.",
control: { type: "number" },
},
height: {
defaultValue: 400,
description: "Image 컴포넌트의 높이를 정합니다.",
control: { type: "number" },
},
alt: {
defaultValue: "",
description: "이미지 설명을 추가합니다.",
table: {
type: {
summary: "string",
},
},
control: { type: "text" },
},
},
} as Meta;

export const Default = (props) => {
return <Image {...props} />;
};