@@ -2,17 +2,24 @@ import React, { useCallback, useState } from 'react';
2
2
import { createPortal } from 'react-dom' ;
3
3
import disableScroll from 'disable-scroll' ;
4
4
5
- export interface Props {
5
+ export interface ModalProps {
6
6
children : React . ReactNode ;
7
7
isOpen : boolean ;
8
8
close : ( ) => void ;
9
9
elementId : 'root' | string ;
10
10
} ;
11
11
12
- export interface Options {
12
+ export interface ModalOptions {
13
13
preventScroll ?: boolean ;
14
14
} ;
15
15
16
+ export type UseModal = ( elementId : string , options : ModalOptions ) => [
17
+ ModalWrapper : React . FC < { children : React . ReactNode } > ,
18
+ open : ( ) => void ,
19
+ close : ( ) => void ,
20
+ isOpen : boolean
21
+ ] ;
22
+
16
23
const wrapperStyle : React . CSSProperties = {
17
24
position : 'fixed' ,
18
25
top : 0 ,
@@ -40,7 +47,7 @@ const containerStyle: React.CSSProperties = {
40
47
zIndex : 100001
41
48
} ;
42
49
43
- const Modal : React . FC < Props > = ( { children, isOpen = false , close, elementId = 'root' } ) => {
50
+ const Modal : React . FC < ModalProps > = ( { children, isOpen = false , close, elementId = 'root' } ) => {
44
51
if ( isOpen === false ) {
45
52
return null ;
46
53
}
@@ -53,7 +60,7 @@ const Modal: React.FC<Props> = ({ children, isOpen = false, close, elementId = '
53
60
) ;
54
61
} ;
55
62
56
- export const useModal = ( elementId = 'root' , options : Options = { } ) : [ ModalWrapper : ( children : any ) => React . ReactElement , open : ( ) => void , close : ( ) => void , isOpen : boolean ] => {
63
+ export const useModal : UseModal = ( elementId = 'root' , options = { } ) => {
57
64
const { preventScroll = false } = options ;
58
65
const [ isOpen , setOpen ] = useState < boolean > ( false ) ;
59
66
const open = useCallback ( ( ) => {
@@ -69,13 +76,13 @@ export const useModal = (elementId = 'root', options: Options = {}): [ModalWrapp
69
76
}
70
77
} , [ setOpen , preventScroll ] ) ;
71
78
72
- const ModalWrapper = useCallback ( ( { children } ) => {
79
+ const ModalWrapper = React . memo ( ( { children } ) => {
73
80
return (
74
81
< Modal isOpen = { isOpen } close = { close } elementId = { elementId } >
75
82
{ children }
76
83
</ Modal >
77
84
)
78
- } , [ isOpen , close , elementId ] ) ;
85
+ } ) ;
79
86
80
87
return [ ModalWrapper , open , close , isOpen ] ;
81
88
} ;
0 commit comments