@@ -25,6 +25,10 @@ type Props = {
25
25
size ?: "small" | "medium" | "large" ;
26
26
} ;
27
27
28
+ type CustomModalProps = Pick < Props , "children" | "isDestructive" | "size" > & {
29
+ [ "aria-label" ] : string ;
30
+ } ;
31
+
28
32
export function createModal (
29
33
config : ModalConfig ,
30
34
{
@@ -35,7 +39,7 @@ export function createModal(
35
39
IconButton : FunctionComponent < IconButtonProps > ;
36
40
}
37
41
) {
38
- return function Modal ( props : Props ) {
42
+ function CustomModal ( props : CustomModalProps ) {
39
43
const ref = useRef < HTMLDivElement > ( null ) ;
40
44
const { overlayProps, underlayProps } = useOverlay ( { ...props , isOpen : true } , ref ) ;
41
45
@@ -45,20 +49,12 @@ export function createModal(
45
49
46
50
const { dialogProps } = useDialog (
47
51
{
48
- "aria-label" : props . title ,
52
+ "aria-label" : props [ "aria-label" ] ,
49
53
role : props . isDestructive ? "alertdialog" : "dialog" ,
50
54
} ,
51
55
ref
52
56
) ;
53
57
54
- // Trigger the primary action on 'Enter' if there is one
55
- useKeyPressEvent (
56
- ( key ) => key . code === "Enter" ,
57
- ( ) => props . primaryAction ?. onPress ( )
58
- ) ;
59
-
60
- const { defaultMessages } = useDefaultMessages ( ) ;
61
-
62
58
return createPortal (
63
59
< Box className = { underlay } { ...underlayProps } color = { undefined } >
64
60
< ModalContext . Provider value = { true } >
@@ -71,44 +67,62 @@ export function createModal(
71
67
color = { undefined }
72
68
borderRadius = { config . radius }
73
69
>
74
- < Inset space = { config . padding } >
75
- < Columns space = { 16 } alignY = "top" >
76
- < Title size = { config . titleSize } > { props . title } </ Title >
77
- < Column width = "content" >
78
- < IconButton
79
- icon = { config . closeIcon }
80
- label = { props . closeButtonLabel ?? defaultMessages . Modal . closeButtonLabel }
81
- onPress = { props . onClose }
82
- size = { config . closeIconSize }
83
- tabIndex = { - 1 }
84
- kind = "transparent"
85
- hierarchy = "secondary"
86
- />
87
- </ Column >
88
- </ Columns >
89
- </ Inset >
90
- < Box className = { modalBody } paddingX = { config . padding } >
91
- { props . children }
92
- </ Box >
93
- < Inset space = { config . padding } >
94
- < Actions
95
- primaryAction = {
96
- props . primaryAction
97
- ? { ...props . primaryAction , isDestructive : props . isDestructive }
98
- : undefined
99
- }
100
- secondaryAction = { props . secondaryAction }
101
- size = "large"
102
- loadingMessage = { props . loadingMessage }
103
- error = { props . error }
104
- />
105
- </ Inset >
70
+ { props . children }
106
71
</ Box >
107
72
</ FocusScope >
108
73
</ ModalContext . Provider >
109
74
</ Box >
110
75
) ;
111
- } ;
76
+ }
77
+
78
+ function Modal ( props : Props ) {
79
+ // Trigger the primary action on 'Enter' if there is one
80
+ useKeyPressEvent (
81
+ ( key ) => key . code === "Enter" ,
82
+ ( ) => props . primaryAction ?. onPress ( )
83
+ ) ;
84
+
85
+ const { defaultMessages } = useDefaultMessages ( ) ;
86
+
87
+ return (
88
+ < CustomModal { ...props } aria-label = { props . title } >
89
+ < Inset space = { config . padding } >
90
+ < Columns space = { 16 } alignY = "top" >
91
+ < Title size = { config . titleSize } > { props . title } </ Title >
92
+ < Column width = "content" >
93
+ < IconButton
94
+ icon = { config . closeIcon }
95
+ label = { props . closeButtonLabel ?? defaultMessages . Modal . closeButtonLabel }
96
+ onPress = { props . onClose }
97
+ size = { config . closeIconSize }
98
+ tabIndex = { - 1 }
99
+ kind = "transparent"
100
+ hierarchy = "secondary"
101
+ />
102
+ </ Column >
103
+ </ Columns >
104
+ </ Inset >
105
+ < Box className = { modalBody } paddingX = { config . padding } >
106
+ { props . children }
107
+ </ Box >
108
+ < Inset space = { config . padding } >
109
+ < Actions
110
+ primaryAction = {
111
+ props . primaryAction
112
+ ? { ...props . primaryAction , isDestructive : props . isDestructive }
113
+ : undefined
114
+ }
115
+ secondaryAction = { props . secondaryAction }
116
+ size = "large"
117
+ loadingMessage = { props . loadingMessage }
118
+ error = { props . error }
119
+ />
120
+ </ Inset >
121
+ </ CustomModal >
122
+ ) ;
123
+ }
124
+
125
+ return { CustomModal, Modal } ;
112
126
}
113
127
114
128
export type { Props as ModalProps } ;
0 commit comments