This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
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 #20 from claustra01/shooter_modal
Add: スマホを画面に向かって垂直に机の上に置いてねモーダルの実装
- Loading branch information
Showing
5 changed files
with
182 additions
and
7 deletions.
There are no files selected for viewing
Binary file not shown.
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,21 @@ | ||
.modal-background{ | ||
background-color:#08000080; | ||
width:100%; | ||
height:100%; | ||
position:fixed; | ||
left:0; | ||
top:0; | ||
z-index:0; | ||
|
||
} | ||
|
||
.modal-content-wrapper{ | ||
position:absolute; | ||
left:50%; | ||
top:30%; | ||
translate: -50% -50%; | ||
z-index:10; | ||
border: none; | ||
border-radius: 8px; | ||
overflow: hidden; | ||
} |
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,81 @@ | ||
import { useCallback, useEffect, useRef } from "react"; | ||
import type { FC, ReactNode } from "react"; | ||
import { createPortal } from "react-dom"; | ||
import styles from "./index.module.css"; | ||
|
||
type Props = { | ||
open: boolean; | ||
children: ReactNode; | ||
onClose?: () => void; | ||
onOpen?: () => void; | ||
}; | ||
|
||
export const Modal: FC<Props> = ({ | ||
children, | ||
open, | ||
onClose = () => {}, | ||
onOpen = () => {}, | ||
}) => { | ||
const dialogRef = useRef<HTMLDialogElement>(null); | ||
|
||
const handleShowModal = useCallback(() => { | ||
onOpen(); | ||
dialogRef.current?.showModal(); | ||
}, [onOpen]); | ||
|
||
const handleCloseModal = useCallback(() => { | ||
onClose(); | ||
dialogRef.current?.close(); | ||
}, [onClose]); | ||
|
||
useEffect(() => { | ||
if (open) { | ||
handleShowModal(); | ||
} else { | ||
handleCloseModal(); | ||
} | ||
}, [open, handleShowModal, handleCloseModal]); | ||
|
||
const handleKeyUp = useCallback( | ||
(event: KeyboardEvent) => { | ||
if (event.key === "Escape") { | ||
handleCloseModal(); | ||
} | ||
}, | ||
[handleCloseModal], | ||
); | ||
|
||
useEffect(() => { | ||
if (open) { | ||
document.addEventListener("keyup", handleKeyUp); | ||
} else { | ||
document.removeEventListener("keyup", handleKeyUp); | ||
} | ||
return () => { | ||
document.removeEventListener("keyup", handleKeyUp); | ||
}; | ||
}, [open, handleKeyUp]); | ||
|
||
return createPortal( | ||
<> | ||
<dialog | ||
ref={dialogRef} | ||
className={styles["modal-content-wrapper"]} | ||
onKeyDown={(e) => e.key === "Enter" && handleShowModal()} | ||
tabIndex={-1} | ||
> | ||
{children} | ||
</dialog> | ||
{open && ( | ||
<button | ||
type="button" | ||
className={styles["modal-background"]} | ||
onClick={handleCloseModal} | ||
onKeyDown={(e) => e.key === "Enter" && handleCloseModal()} | ||
aria-label="Close modal" | ||
/> | ||
)} | ||
</>, | ||
document.body, | ||
); | ||
}; |
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