|
| 1 | +import React, { useEffect, useRef, ChangeEvent } from 'react'; |
| 2 | +import { BottomSheet } from '@/components/atoms/bottomSheet'; |
| 3 | +import Close from '@/assets/close_20.svg'; |
| 4 | + |
| 5 | +const DEFAULT_BOTTOM_SHEET_HEIGHT = 290; |
| 6 | +const MAX_IMAGE = 9; |
| 7 | + |
| 8 | +interface BottomContentProps { |
| 9 | + images: string[]; |
| 10 | + setImages: (images: string[]) => void; |
| 11 | + showGallery: boolean; |
| 12 | + setShowGallery: (value: boolean) => void; |
| 13 | + setBottomSheetHeight: (height: number) => void; |
| 14 | +} |
| 15 | + |
| 16 | +export const BottomContent = ({ |
| 17 | + images, |
| 18 | + showGallery, |
| 19 | + setImages, |
| 20 | + setShowGallery, |
| 21 | + setBottomSheetHeight, |
| 22 | +}: BottomContentProps) => { |
| 23 | + const observerRef = useRef<MutationObserver | null>(null); |
| 24 | + const isFirstOpenRef = useRef(true); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (!showGallery && observerRef.current) { |
| 28 | + isFirstOpenRef.current = true; |
| 29 | + observerRef.current.disconnect(); |
| 30 | + observerRef.current = null; |
| 31 | + } |
| 32 | + }, [showGallery]); |
| 33 | + |
| 34 | + const observeHeightChanges = () => { |
| 35 | + const bottomSheetElement = document.querySelector( |
| 36 | + '[data-rsbs-root="true"]', |
| 37 | + ) as HTMLElement; |
| 38 | + |
| 39 | + if (!bottomSheetElement) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + observerRef.current?.disconnect(); |
| 44 | + |
| 45 | + observerRef.current = new MutationObserver((mutations) => { |
| 46 | + mutations.forEach((mutation) => { |
| 47 | + if (mutation.attributeName === 'style') { |
| 48 | + if (isFirstOpenRef.current) { |
| 49 | + setBottomSheetHeight(DEFAULT_BOTTOM_SHEET_HEIGHT); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const computedStyle = getComputedStyle(bottomSheetElement); |
| 54 | + const newHeight = parseInt( |
| 55 | + computedStyle.getPropertyValue('--rsbs-overlay-h'), |
| 56 | + 10, |
| 57 | + ); |
| 58 | + const translateY = parseInt( |
| 59 | + computedStyle.getPropertyValue('--rsbs-overlay-translate-y'), |
| 60 | + 10, |
| 61 | + ); |
| 62 | + |
| 63 | + setBottomSheetHeight( |
| 64 | + newHeight === DEFAULT_BOTTOM_SHEET_HEIGHT |
| 65 | + ? newHeight - translateY |
| 66 | + : newHeight, |
| 67 | + ); |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + observerRef.current.observe(bottomSheetElement, { |
| 73 | + attributes: true, |
| 74 | + attributeOldValue: true, |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + const handleSpringStart = () => { |
| 79 | + observeHeightChanges(); |
| 80 | + }; |
| 81 | + |
| 82 | + const handleSpringEnd = () => { |
| 83 | + if (showGallery) { |
| 84 | + isFirstOpenRef.current = false; |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <BottomSheet |
| 90 | + open={showGallery} |
| 91 | + onSpringStart={handleSpringStart} |
| 92 | + onSpringEnd={handleSpringEnd} |
| 93 | + onDismiss={() => setShowGallery(false)} |
| 94 | + snapPoints={({ maxHeight }) => [DEFAULT_BOTTOM_SHEET_HEIGHT, maxHeight]} |
| 95 | + defaultSnap={() => DEFAULT_BOTTOM_SHEET_HEIGHT} |
| 96 | + blocking={false} |
| 97 | + header={ |
| 98 | + <BottomSheetHeader |
| 99 | + images={images} |
| 100 | + setImages={setImages} |
| 101 | + setShowGallery={setShowGallery} |
| 102 | + /> |
| 103 | + } |
| 104 | + > |
| 105 | + <BottomSheetContent images={images} setImages={setImages} /> |
| 106 | + </BottomSheet> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +const BottomSheetHeader = ({ |
| 111 | + images, |
| 112 | + setImages, |
| 113 | + setShowGallery, |
| 114 | +}: { |
| 115 | + images: string[]; |
| 116 | + setImages: (images: string[]) => void; |
| 117 | + setShowGallery: (value: boolean) => void; |
| 118 | +}) => { |
| 119 | + const inputRef = useRef<HTMLInputElement>(null); |
| 120 | + const openNativeGallery = () => { |
| 121 | + inputRef.current?.click(); |
| 122 | + }; |
| 123 | + |
| 124 | + const handleImageUpload = (event: ChangeEvent<HTMLInputElement>) => { |
| 125 | + if (!event.target.files) return; |
| 126 | + |
| 127 | + const fileArray = Array.from(event.target.files).map((file) => |
| 128 | + URL.createObjectURL(file), |
| 129 | + ); |
| 130 | + setImages(images ? [...images, ...fileArray] : [...fileArray]); |
| 131 | + setShowGallery(true); |
| 132 | + }; |
| 133 | + |
| 134 | + return ( |
| 135 | + <div className='flex items-center justify-between px-1 text-lg font-medium'> |
| 136 | + <input |
| 137 | + ref={inputRef} |
| 138 | + type='file' |
| 139 | + accept='image/*' |
| 140 | + multiple |
| 141 | + className='hidden' |
| 142 | + onChange={handleImageUpload} |
| 143 | + /> |
| 144 | + |
| 145 | + <div> |
| 146 | + <span className='text-white'>사진</span> |
| 147 | + <span className='ml-3 tracking-[2px] text-gray-700'> |
| 148 | + <span className='text-primary-500'>{images?.length ?? 0}</span>/ |
| 149 | + {MAX_IMAGE} |
| 150 | + </span> |
| 151 | + </div> |
| 152 | + |
| 153 | + <button |
| 154 | + className='h-[34px] w-[83px] rounded-md bg-gray-300 text-sm text-gray-800' |
| 155 | + onClick={openNativeGallery} |
| 156 | + > |
| 157 | + 사진 찾기 |
| 158 | + </button> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +const BottomSheetContent = ({ |
| 164 | + images, |
| 165 | + setImages, |
| 166 | +}: { |
| 167 | + images: string[]; |
| 168 | + setImages: (images: string[]) => void; |
| 169 | +}) => { |
| 170 | + return images.length ? ( |
| 171 | + <div className='mt-2 grid grid-cols-3 gap-[3px]'> |
| 172 | + {images.map((src, index) => ( |
| 173 | + <div key={index} className='relative'> |
| 174 | + <img |
| 175 | + src={src} |
| 176 | + alt={`User Update Image ${src}`} |
| 177 | + className='h-32 w-full object-cover' |
| 178 | + /> |
| 179 | + <button |
| 180 | + className='absolute right-2 top-2 flex h-[28px] w-[28px] items-center justify-center rounded-full bg-gray-50' |
| 181 | + onClick={() => setImages(images.filter((_, i) => i !== index))} |
| 182 | + > |
| 183 | + <Close /> |
| 184 | + </button> |
| 185 | + </div> |
| 186 | + ))} |
| 187 | + </div> |
| 188 | + ) : ( |
| 189 | + <div className='mt-[74px] text-center'> |
| 190 | + <span className='text-base text-gray-600'> |
| 191 | + 앨범에서 사진을 찾아보세요. |
| 192 | + </span> |
| 193 | + </div> |
| 194 | + ); |
| 195 | +}; |
0 commit comments