11"use client" ;
22
33import clsx from "clsx" ;
4- import { ReactNode , useEffect , useState } from "react" ;
4+ import { ReactNode , useEffect , useRef , useState } from "react" ;
55
66interface Props {
77 className ?: string ;
@@ -14,6 +14,7 @@ interface Props {
1414export function Modal ( props : Props ) {
1515 const [ daisyModalEnabled , setDaisyModalEnabled ] = useState ( false ) ;
1616 const [ daisyModalOpen , setDaisyModalOpen ] = useState ( false ) ;
17+ const modalDivRef = useRef < HTMLDivElement > ( null ) ;
1718 useEffect ( ( ) => {
1819 if ( props . open ) {
1920 // daisyuiのmodalモードにする → modalを開くアニメーションをする
@@ -27,6 +28,23 @@ export function Modal(props: Props) {
2728 return ( ) => clearTimeout ( timeout ) ;
2829 }
2930 } , [ props . open ] ) ;
31+ useEffect ( ( ) => {
32+ if ( daisyModalEnabled ) {
33+ const updateHeight = ( ) => {
34+ if ( modalDivRef . current && window . visualViewport ) {
35+ modalDivRef . current . style . height = `${ window . visualViewport . height } px` ;
36+ }
37+ } ;
38+ updateHeight ( ) ;
39+ window . visualViewport ?. addEventListener ( "resize" , updateHeight ) ;
40+ return ( ) =>
41+ window . visualViewport ?. removeEventListener ( "resize" , updateHeight ) ;
42+ } else {
43+ if ( modalDivRef . current ) {
44+ modalDivRef . current . style . height = "" ;
45+ }
46+ }
47+ } , [ daisyModalEnabled ] ) ;
3048
3149 return (
3250 < >
@@ -39,6 +57,7 @@ export function Modal(props: Props) {
3957 < div
4058 className = { clsx ( daisyModalEnabled && "modal h-dvh" ) }
4159 role = { daisyModalEnabled ? "dialog" : undefined }
60+ ref = { modalDivRef }
4261 >
4362 < div
4463 className = { clsx (
0 commit comments