-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from oduck-team/feat/339
feat: 프로필 수정 시, 이미지 crop 기능 구현
- Loading branch information
Showing
19 changed files
with
685 additions
and
159 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/features/users/components/ProfileImageSection/AvatarEditButton.style.ts
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/features/users/components/ProfileImageSection/AvatarEditButton.tsx
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
src/features/users/components/ProfileImageSection/ImageEditButton.style.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { css } from "@emotion/react"; | ||
import styled from "@emotion/styled"; | ||
import { PlusCircle } from "@phosphor-icons/react"; | ||
|
||
export const ImageEditButtonContainer = styled.div<{ | ||
borderRadius: string; | ||
height: string | number; | ||
}>` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
opacity: 0.7; | ||
width: 100%; | ||
height: ${({ height }) => (height === "100%" ? "100%" : `${height}px`)}; | ||
background-color: ${({ theme }) => theme.colors.neutral[100]}; | ||
cursor: pointer; | ||
${({ borderRadius }) => | ||
borderRadius === "50%" && | ||
css` | ||
border-radius: 50%; | ||
`} | ||
`; | ||
|
||
export const PlusCircleIcon = styled(PlusCircle)` | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
color: ${({ theme }) => theme.colors.neutral["05"]}; | ||
transform: translate(-50%, -50%); | ||
transition: | ||
transform linear 0.2s, | ||
color linear 0.2s; | ||
&.resetIcon { | ||
color: ${({ theme }) => theme.colors.warn["50"]}; | ||
transform: translate(-50%, -50%) rotate(45deg); | ||
} | ||
`; |
42 changes: 42 additions & 0 deletions
42
src/features/users/components/ProfileImageSection/ImageEditButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { StrictPropsWithChildren } from "@/types"; | ||
|
||
import { | ||
ImageEditButtonContainer, | ||
PlusCircleIcon, | ||
} from "./ImageEditButton.style"; | ||
|
||
interface ImageEditButtonProps { | ||
croppedImage: string | null; | ||
borderRadius?: "50%" | "none"; | ||
/** default height: 100%, number는 px 단위 */ | ||
height?: "100%" | number; | ||
onClick: () => void; | ||
onReset: () => void; | ||
} | ||
|
||
export default function ImageEditButton({ | ||
croppedImage, | ||
borderRadius = "none", | ||
height = "100%", | ||
onClick, | ||
onReset, | ||
children, | ||
}: StrictPropsWithChildren<ImageEditButtonProps>) { | ||
/* crop된 이미지가 있으면 이미지 제거, 없으면 이미지 등록 */ | ||
const handleClick = croppedImage ? onReset : onClick; | ||
|
||
return ( | ||
<ImageEditButtonContainer | ||
onClick={handleClick} | ||
borderRadius={borderRadius} | ||
height={height} | ||
> | ||
<PlusCircleIcon | ||
className={croppedImage ? "resetIcon" : undefined} | ||
size={24} | ||
/> | ||
|
||
{children} | ||
</ImageEditButtonContainer> | ||
); | ||
} |
20 changes: 0 additions & 20 deletions
20
src/features/users/components/ProfileImageSection/ProfileArtEditButton.style.ts
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/features/users/components/ProfileImageSection/ProfileArtEditButton.tsx
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
src/features/users/components/ProfileImageSection/ProfileAvatar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
import Avatar from "@/components/Avatar"; | ||
import { StrictPropsWithChildren } from "@/types"; | ||
|
||
import AvatarEditButton from "./AvatarEditButton"; | ||
import { ProfileAvatarContainer } from "./ProfileAvatar.style"; | ||
|
||
export default function ProfileAvatar({ children }: StrictPropsWithChildren) { | ||
return <ProfileAvatarContainer>{children}</ProfileAvatarContainer>; | ||
} | ||
|
||
ProfileAvatar.Avatar = Avatar; | ||
ProfileAvatar.AvatarEditButton = AvatarEditButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useState } from "react"; | ||
|
||
import useToast from "@/components/Toast/useToast"; | ||
import { readFile } from "@/libs/imageCrop"; | ||
|
||
const allowedExtensions = ["jpg", "jpeg", "png", "webp"]; | ||
|
||
export default function useCropModal( | ||
inputRef: React.RefObject<HTMLInputElement>, | ||
) { | ||
const [isImageCropModal, setIsImageCropModal] = useState(false); | ||
const [imageSrc, setImageSrc] = useState<string | null>(null); // 사용자의 원본 이미지 | ||
const toast = useToast(); | ||
|
||
const handleImageEditClick = () => { | ||
setIsImageCropModal(true); | ||
if (!imageSrc) inputRef.current?.click(); // 이미지 input open | ||
}; | ||
|
||
const closeImageCropModal = () => { | ||
setIsImageCropModal(false); | ||
resetUploadedImage(); | ||
}; | ||
|
||
/** input file change */ | ||
const handleImageChange = async (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (e.target.files && e.target.files.length > 0) { | ||
const file = e.target.files[0]; | ||
const fileExtension = file.name.split(".").at(-1)?.toLowerCase(); | ||
|
||
if (!allowedExtensions.includes(fileExtension ?? "")) { | ||
toast.error({ | ||
message: `'jpg' 'jpeg' 'png' 'webp' 확장자 이미지를 등록해주세요.`, | ||
duration: 4, | ||
}); | ||
e.target.value = ""; | ||
return; | ||
} | ||
|
||
const imageDataUrl = await readFile(file); | ||
setImageSrc(imageDataUrl); | ||
} | ||
}; | ||
|
||
/** | ||
* 이미지 crop을 취소하거나 완료하면 | ||
* input과 원본 이미지 상태 초기화 | ||
*/ | ||
const resetUploadedImage = () => { | ||
if (inputRef.current) { | ||
inputRef.current.value = ""; | ||
setImageSrc(null); | ||
} | ||
}; | ||
|
||
return { | ||
imageSrc, | ||
isImageCropModal, | ||
handleImageEditClick, | ||
handleImageChange, | ||
closeImageCropModal, | ||
resetUploadedImage, | ||
}; | ||
} |
Oops, something went wrong.