Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- node설치
- react npm install
- googleicon
- package.json
- npm start
- 이미지반응형
- minwidth
- vscode git clone
- 웹아이콘
- 아이콘사용법
- slickslider
- npm install
- fontawesome
- legacy-peer
- 글자들여쓰기
- npm install 문제
- MediaQuery
- 의존성문제
- XEIcon
- 그누보드반응형
- owlcarousel
- 플러그인
- window 정책변경
- Git clone
- git lab clone
- 정적객체
- maxwidth
- 단어단위로떨어지기
- 동적객체
- node 오류
Archives
- Today
- Total
어쩌다 알게 된 ƪ(•̃͡•̃͡ ƪ
팝업 컴포넌트 3차 (버튼 props로 받기) 본문
728x90
팝업 컴포넌트 2차 (외부 클릭시, esc 닫힘_useRef 사용)
;}const DefaultPopup = ({ text, isOpen, setIsOpen}: DefaultPopupProps) => { return ( setIsOpen(true)} /> {isOpen && ( setI" data-og-host="dazzle-bini.tistory.com" data-og-source-url="https://dazzle-bini.tistory.com/287" data-og-url="https://dazzle-bini.tis
dazzle-bini.tistory.com
+ 하단 버튼 1개일 수도 있고 2개일 수도 있고 없을수도 있는 경우 -> props로 받기
수정된 부분 💛 표시
"use client";
import { useEffect, useRef } from "react";
import { Button } from "./Buttons";
interface ButtonProps{ 💛
text: string;
className?: string;
onClick?: () => void;
}
interface DefaultPopupProps{
text: string;
isOpen: boolean;
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
buttons?: ButtonProps[]; 💛
}
const DefaultPopup = ({
text,
isOpen,
setIsOpen,
buttons 💛
}: DefaultPopupProps) => {
const outsideRef = useRef<HTMLDivElement>(null);
useEffect(() => {
// 외부 클릭시
const handleClickOutside = (e: MouseEvent) => {
if(outsideRef.current && !outsideRef.current.contains(e.target as Node)){
setIsOpen(false);
}
}
// esc 눌렀을 시
const handleEsc = (e: KeyboardEvent) => {
console.log(e.key);
console.log(e.code);
if(e.key === "Escape"){
setIsOpen(false);
}
}
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleEsc);
return() => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('keydown', handleEsc);
}
}, [setIsOpen]);
return (
<div className="btn_pop_wrap">
<Button
text={text}
onClick={() => setIsOpen(true)}
/>
{isOpen && (
<div className="pop_wrap" ref={outsideRef}>
<div className="pop_header">
<Button
text="X"
onClick={() => setIsOpen(false)}
/>
</div>
<div className="pop_content">
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
팝업 내용 <br />
</div>
{buttons && buttons.length > 0 && ( 💛
<div className="pop_footer">
{buttons.map((v, index) => (
<Button
key={index}
className={v.className}
text={v.text}
onClick={() => v.onClick}
/>
))}
</div>
)}
</div>
)}
</div>
)
}
export { DefaultPopup };
💻 없을 경우는 안나타 나게 조건 걸기
✔ map 사용
728x90
'개발 > 🔵 React-TypeScript' 카테고리의 다른 글
| 팝업 컴포넌트 5차 (position 개선_유니온 / enum 방법) (0) | 2025.09.19 |
|---|---|
| 팝업 컴포넌트 4차 (외부 감지 + 버튼에 토글 동작 겹치는 이슈 해결하기) (0) | 2025.09.19 |
| 팝업 컴포넌트 2차 (외부 클릭시, esc 닫힘_useRef 사용) (0) | 2025.09.18 |
| 팝업 컴포넌트 1차 (0) | 2025.09.18 |
| 버튼 컴포넌트 1차 (0) | 2025.09.18 |
Comments
