@@ -2,7 +2,13 @@ import { Dialog, Transition } from "@headlessui/react";
2
2
import { XIcon } from "@heroicons/react/solid" ;
3
3
import clsx from "clsx" ;
4
4
import { atom , useAtom } from "jotai" ;
5
- import React , { Fragment , createContext , useContext , useMemo , useState } from "react" ;
5
+ import React , {
6
+ Fragment ,
7
+ createContext ,
8
+ useContext ,
9
+ useMemo ,
10
+ useState
11
+ } from "react" ;
6
12
import HelpLink from "../Buttons/HelpLink" ;
7
13
import { BsArrowsFullscreen , BsFullscreenExit } from "react-icons/bs" ;
8
14
import { useWindowSize } from "react-use-size" ;
@@ -11,10 +17,10 @@ import DialogButton from "../Buttons/DialogButton";
11
17
export type ModalContextProps = {
12
18
props : IModalProps ;
13
19
setProps : ( props : IModalProps ) => void ;
14
- }
20
+ } ;
15
21
16
22
export function useModal ( ) {
17
- return useContext ( ModalContext )
23
+ return useContext ( ModalContext ) ;
18
24
}
19
25
20
26
export const ModalContext = createContext < ModalContextProps > ( undefined ! ) ;
@@ -50,34 +56,37 @@ export interface IModalProps {
50
56
51
57
export function useDialogSize ( size ?: string ) {
52
58
const { height, width } = useWindowSize ( ) ;
53
- return useMemo ( ( ) => clsx (
54
- size === 'full' && {
55
- 'min-w-[1240px] w-[1240px]' : width >= 1280 ,
56
- 'w-[1000px]' : width < 1280 && width >= 1024 ,
57
- 'w-[90vw]' : width < 1024 && width >= 768 ,
58
- 'w-[95vw]' : width < 768 ,
59
- 'h-[90vh]' : height < 1000 ,
60
- 'h-[1000px]' : height > 1000 ,
61
- } ,
62
- size === 'small' && 'max-w-[800px] max-h-[50vh]' ,
63
- size === 'medium' && {
64
- 'w-[1000px]' : width >= 1280 ,
65
- 'w-[850px]' : width < 1280 && width >= 1024 ,
66
- 'w-[500vw]' : width < 1024 && width >= 768 ,
67
- 'w-[95vw]' : width < 768 ,
68
- 'h-[70vh]' : height < 1000 ,
69
- 'h-[800px]' : height > 1000 ,
70
- } ,
71
- size === 'large' && {
72
- 'min-w-[1240px] w-[1240px]' : width >= 1280 ,
73
- 'w-[1000px]' : width < 1280 && width >= 1024 ,
74
- 'w-[600px]' : width < 1024 && width >= 768 ,
75
- 'w-[95vw]' : width < 768 ,
76
- 'h-[80vh]' : height < 1000 ,
77
- 'h-[1000px]' : height > 1000 ,
78
-
79
- } ,
80
- ) , [ height , width , size ] ) ;
59
+ return useMemo (
60
+ ( ) =>
61
+ clsx (
62
+ size === "full" && {
63
+ "min-w-[1240px] w-[1240px]" : width >= 1280 ,
64
+ "w-[1000px]" : width < 1280 && width >= 1024 ,
65
+ "w-[90vw]" : width < 1024 && width >= 768 ,
66
+ "w-[95vw]" : width < 768 ,
67
+ "h-[90vh]" : height < 1000 ,
68
+ "h-[1000px]" : height > 1000
69
+ } ,
70
+ size === "small" && "max-w-[800px] max-h-[50vh]" ,
71
+ size === "medium" && {
72
+ "w-[1000px]" : width >= 1280 ,
73
+ "w-[850px]" : width < 1280 && width >= 1024 ,
74
+ "w-[500vw]" : width < 1024 && width >= 768 ,
75
+ "w-[95vw]" : width < 768 ,
76
+ "h-[70vh]" : height < 1000 ,
77
+ "h-[800px]" : height > 1000
78
+ } ,
79
+ size === "large" && {
80
+ "min-w-[1240px] w-[1240px]" : width >= 1280 ,
81
+ "w-[1000px]" : width < 1280 && width >= 1024 ,
82
+ "w-[600px]" : width < 1024 && width >= 768 ,
83
+ "w-[95vw]" : width < 768 ,
84
+ "h-[80vh]" : height < 1000 ,
85
+ "h-[1000px]" : height > 1000
86
+ }
87
+ ) ,
88
+ [ height , width , size ]
89
+ ) ;
81
90
}
82
91
83
92
export function Modal ( {
@@ -88,9 +97,9 @@ export function Modal({
88
97
footerClassName = "flex my-2 px-8 justify-end" ,
89
98
actions,
90
99
open,
91
- onClose = ( ) => { } ,
92
- childClassName,
93
- allowBackgroundClose,
100
+ onClose = ( ) => { } ,
101
+ childClassName = "w-full h-full" ,
102
+ allowBackgroundClose = true ,
94
103
hideCloseButton,
95
104
size,
96
105
children,
@@ -100,19 +109,23 @@ export function Modal({
100
109
} : IModalProps ) {
101
110
const [ helpLink ] = useAtom ( modalHelpLinkAtom ) ;
102
111
const [ _size , setSize ] = useState ( size ) ;
103
- const sizeClass = useDialogSize ( size ) ;
112
+ const sizeClass = useDialogSize ( _size ) ;
104
113
105
114
return (
106
115
< Transition . Root show = { open } as = { Fragment } >
107
116
< Dialog
108
117
as = "div"
109
118
auto-reopen = "true"
110
119
className = { dialogClassName }
111
- onClose = { allowBackgroundClose ? ( ) => onClose ( ) : ( ) => { } }
120
+ onClose = { allowBackgroundClose ? ( ) => onClose ( ) : ( ) => { } }
112
121
{ ...rest }
113
122
>
114
123
< div
115
- className = { clsx ( "flex items-center justify-center mx-auto my-auto" , sizeClass ) } >
124
+ className = { clsx (
125
+ "flex items-center justify-center mx-auto my-auto" ,
126
+ sizeClass
127
+ ) }
128
+ >
116
129
{ /* @ts -ignore */ }
117
130
< Transition . Child
118
131
as = { Fragment as any }
@@ -135,9 +148,11 @@ export function Modal({
135
148
leaveFrom = "opacity-100 translate-y-0 sm:scale-100"
136
149
leaveTo = "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
137
150
>
138
- < div className = { clsx (
139
- _size !== "small" && childClassName ,
140
- "mt-20 mb-10 justify-between overflow-auto bg-white rounded-lg text-left shadow-xl transform transition-all flex flex-col" ) }
151
+ < div
152
+ className = { clsx (
153
+ _size !== "small" && childClassName ,
154
+ "mt-20 mb-10 justify-between overflow-auto bg-white rounded-lg text-left shadow-xl transform transition-all flex flex-col"
155
+ ) }
141
156
>
142
157
< div className = "py-4 px-4 gap-2 flex item-center rounded-t-lg justify-between bg-gray-100" >
143
158
< h1
@@ -153,19 +168,27 @@ export function Modal({
153
168
corner of the modal that links to the documentation for the modal.
154
169
*/ }
155
170
{ helpLink && < HelpLink link = { helpLink } /> }
156
- { showExpand && _size !== 'full' && size !== 'small' &&
157
- < DialogButton icon = { BsArrowsFullscreen } onClick = { ( ) => setSize ( 'full' ) } /> }
158
-
159
-
160
- { showExpand && _size === 'full' && < DialogButton icon = { BsFullscreenExit } onClick = { ( ) => setSize ( 'medium' ) } /> }
161
-
162
-
171
+ { showExpand && _size !== "full" && size !== "small" && (
172
+ < DialogButton
173
+ icon = { BsArrowsFullscreen }
174
+ onClick = { ( ) => setSize ( "full" ) }
175
+ />
176
+ ) }
177
+
178
+ { showExpand && _size === "full" && (
179
+ < DialogButton
180
+ icon = { BsFullscreenExit }
181
+ onClick = { ( ) => setSize ( "medium" ) }
182
+ />
183
+ ) }
163
184
164
185
{ /* top-right close button */ }
165
- { ! hideCloseButton && < DialogButton icon = { XIcon } onClick = { onClose } /> }
186
+ { ! hideCloseButton && (
187
+ < DialogButton icon = { XIcon } onClick = { onClose } />
188
+ ) }
166
189
</ div >
167
190
168
- < div className = { clsx ( "flex flex-col flex-1 mb-auto " , bodyClass ) } >
191
+ < div className = { clsx ( "flex flex-col flex-1 mb-auto " , bodyClass ) } >
169
192
{ children }
170
193
</ div >
171
194
0 commit comments