-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||
}, | ||
}; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"use client"; | ||
|
||
import { PolymorphicRef } from "@travelmakers-design-v2/styles"; | ||
import { forwardRef, PropsWithChildren, useState } from "react"; | ||
import useStyles from "./Image.style"; | ||
import { ImageComponent, ImageProps } from "./Image.type"; | ||
|
||
export interface Props { | ||
/** true일 경우 lazy load가 적용됩니다. */ | ||
lazy?: boolean; | ||
|
||
/** 이미지 src를 정합니다. */ | ||
src: string; | ||
|
||
/** Image 컴포넌트의 너비를 정합니다. */ | ||
width?: number | string; | ||
|
||
/** Image 컴포넌트의 높이를 정합니다. */ | ||
height?: number | string; | ||
|
||
/** 이미지 설명을 추가합니다. */ | ||
alt: string; | ||
} | ||
|
||
export const Image: ImageComponent & { | ||
displayName?: string; | ||
} = forwardRef( | ||
<C extends React.ElementType = "img">( | ||
{ | ||
lazy = true, | ||
src, | ||
alt, | ||
className, | ||
children, | ||
...props | ||
}: PropsWithChildren<ImageProps<C>>, | ||
ref: PolymorphicRef<C> | ||
) => { | ||
const [load, setLoad] = useState(false); | ||
const [error, setError] = useState(false); | ||
const { classes, cx } = useStyles({ load }); | ||
|
||
return ( | ||
<> | ||
<img | ||
ref={ref} | ||
src={src} | ||
alt={alt} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P4: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
setError(true); | ||
e.currentTarget.src = require("./img/error.png"); | ||
}} | ||
{...props} | ||
/> | ||
{!load && ( | ||
<img | ||
src={ | ||
"data:image/gif;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mO8UA8AAiUBUcc3qzwAAAAASUVORK5CYII=" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P4: 로드시 노출되는 이미지에도 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋아요~! ✅ |
||
alt={alt} | ||
{...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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Image } from "./Image"; | ||
export type { ImageProps } from "./Image"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 이 부분은 혹시 로컬에서 에러가 안나오는 상황일까용? |
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} />; | ||
}; |
There was a problem hiding this comment.
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에서는 지우는게 어떨까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
width, height만 삭제하는걸로 소통완료