-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
191 additions
and
85 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,53 +1,86 @@ | ||
import React from 'react'; | ||
import { useBottomSheet } from '@/hooks/useBottomSheet'; | ||
import React, { useState, useRef } from 'react'; | ||
|
||
interface IBottomSheetProps { | ||
minHeight: number; | ||
maxHeight: number; | ||
backgroundColor: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
/** | ||
* `BottomSheet` ์ปดํฌ๋ํธ๋ ํ๋จ์์ ์ฌ๋ผ์ค๋ ์ํธ ํํ์ UI๋ฅผ ์ ๊ณตํฉ๋๋ค. | ||
* | ||
* @param {IBottomSheetProps} props - `children`์ ํฌํจํ ์ปดํฌ๋ํธ ์์ฑ | ||
* @param {number} props.minHeight - Bottom Sheet์ ์ต์ ๋์ด๋ฅผ ํ๋ฉด ๋น์จ๋ก ๋ํ๋ ๋๋ค (0.0 - 1.0). | ||
* @param {number} props.maxHeight - Bottom Sheet์ ์ต๋ ๋์ด๋ฅผ ํ๋ฉด ๋น์จ๋ก ๋ํ๋ ๋๋ค (0.0 - 1.0). | ||
* @param {ReactNode} props.children - Bottom Sheet ๋ด๋ถ์ ๋ ๋๋งํ ์ฝํ ์ธ ์ ๋๋ค. | ||
* @returns {ReactNode} - ํ๋จ ์ํธ๋ฅผ ๋ ๋๋งํฉ๋๋ค. | ||
* | ||
* @remarks | ||
* - ๋๋๊ทธ ๋์์ ํตํด ์ํธ๋ฅผ ์ด๊ณ ๋ซ์ ์ ์์ต๋๋ค. | ||
* - `useBottomSheet` ํ ์ ์ฌ์ฉํ์ฌ ์์น ๋ฐ ๋๋๊ทธ ๋์์ ๊ด๋ฆฌํฉ๋๋ค. | ||
* - `minHeight`๋ Bottom Sheet๊ฐ ๋ซํ ์ํ์ ๋์ด ๋น์จ์ ๋ํ๋ ๋๋ค. | ||
* - `maxHeight`๋ Bottom Sheet๊ฐ ์ด๋ฆฐ ์ํ์ ์ต๋ ๋์ด ๋น์จ์ ๋ํ๋ ๋๋ค. | ||
* | ||
* @example | ||
* ```tsx | ||
* <BottomSheet minHeight={0.5} maxHeight={0.85}> | ||
* <div className="p-4"> | ||
* <h2>์์ ์ฝํ ์ธ </h2> | ||
* <p>BottomSheet์ children</p> | ||
* </div> | ||
* </BottomSheet> | ||
* ``` | ||
*/ | ||
|
||
export const BottomSheet = (props: IBottomSheetProps) => { | ||
const { sheet } = useBottomSheet({ minHeight: props.minHeight, maxHeight: props.maxHeight }); | ||
export const BottomSheet = ({ | ||
minHeight, | ||
maxHeight, | ||
backgroundColor, | ||
children, | ||
}: IBottomSheetProps) => { | ||
const [sheetHeight, setSheetHeight] = useState(minHeight); | ||
const startY = useRef(0); | ||
const startHeight = useRef(minHeight); | ||
|
||
const handleStart = (y: number) => { | ||
startY.current = y; | ||
startHeight.current = sheetHeight; | ||
}; | ||
|
||
const handleMove = (y: number) => { | ||
const deltaY = startY.current - y; | ||
const newHeight = startHeight.current + deltaY / window.innerHeight; | ||
setSheetHeight(Math.max(minHeight, Math.min(maxHeight, newHeight))); | ||
}; | ||
|
||
const handleTouchStart = (e: React.TouchEvent) => { | ||
handleStart(e.touches[0].clientY); | ||
}; | ||
|
||
const handleTouchMove = (e: React.TouchEvent) => { | ||
handleMove(e.touches[0].clientY); | ||
}; | ||
|
||
const handleMouseMove = (e: MouseEvent) => { | ||
handleMove(e.clientY); | ||
}; | ||
|
||
const handleMouseUp = () => { | ||
window.removeEventListener('mousemove', handleMouseMove); | ||
window.removeEventListener('mouseup', handleMouseUp); | ||
}; | ||
|
||
const handleMouseDown = (e: React.MouseEvent) => { | ||
handleStart(e.clientY); | ||
window.addEventListener('mousemove', handleMouseMove); | ||
window.addEventListener('mouseup', handleMouseUp); | ||
}; | ||
|
||
const handleClose = () => { | ||
setSheetHeight(minHeight); | ||
}; | ||
|
||
return ( | ||
<div | ||
ref={sheet} | ||
className="bg-grayscale-25 shadow-dark fixed left-0 right-0 z-[1000] flex h-full flex-col rounded-t-lg transition-transform duration-700" | ||
className="fixed bottom-0 left-0 right-0 z-50 rounded-t-2xl bg-white shadow-lg transition-transform duration-700 ease-out" | ||
style={{ | ||
top: `calc(100% - ${props.minHeight * 100}%)`, | ||
backgroundColor: `${backgroundColor}`, | ||
height: `${sheetHeight * 100}vh`, | ||
transform: `translateY(${(1 - sheetHeight) * 100}%)`, | ||
}} | ||
onTouchStart={handleTouchStart} | ||
onTouchMove={handleTouchMove} | ||
onMouseDown={handleMouseDown} | ||
> | ||
<div className="flex items-center justify-center pb-1 pt-2"> | ||
<div className="h-1.5 w-12 rounded-full bg-gray-300" /> | ||
</div> | ||
{props.children} | ||
<div className="flex items-center justify-end pb-1 pr-5 pt-2"> | ||
<button | ||
type="button" | ||
className="bg-grayscale-180 h-[30px] w-[30px] rounded-full text-lg font-semibold text-gray-500" | ||
onClick={handleClose} | ||
> | ||
<p className="text-grayscale-850">โ</p> | ||
</button> | ||
</div> | ||
|
||
<div className="h-full overflow-auto">{children}</div> | ||
</div> | ||
); | ||
}; |
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
Oops, something went wrong.