@@ -15,16 +15,65 @@ var _reactDom = require('react-dom');
1515
1616var _reactDom2 = _interopRequireDefault ( _reactDom ) ;
1717
18+ var _propTypes = require ( 'prop-types' ) ;
19+
20+ var _propTypes2 = _interopRequireDefault ( _propTypes ) ;
21+
22+ var _classnames = require ( 'classnames' ) ;
23+
24+ var _classnames2 = _interopRequireDefault ( _classnames ) ;
25+
26+ var _shortid = require ( 'shortid' ) ;
27+
28+ var _shortid2 = _interopRequireDefault ( _shortid ) ;
29+
1830require ( './modali.css' ) ;
1931
2032function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
2133
22- var Modali = function Modali ( _ref ) {
23- var isShown = _ref . isShown ,
24- hide = _ref . hide ,
25- options = _ref . options ,
26- children = _ref . children ;
34+ var Button = function Button ( _ref ) {
35+ var onClick = _ref . onClick ,
36+ label = _ref . label ,
37+ isStyleDefault = _ref . isStyleDefault ,
38+ isStyleCancel = _ref . isStyleCancel ,
39+ isStyleDestructive = _ref . isStyleDestructive ;
40+
41+ var buttonClass = ( 0 , _classnames2 . default ) ( {
42+ 'modali-button' : true ,
43+ 'modali-button-cancel' : isStyleCancel ,
44+ 'modali-button-default' : isStyleDefault ,
45+ 'modali-button-destructive' : isStyleDestructive
46+ } ) ;
47+ return _react2 . default . createElement (
48+ 'button' ,
49+ {
50+ type : 'button' ,
51+ className : buttonClass ,
52+ onClick : onClick
53+ } ,
54+ label
55+ ) ;
56+ } ;
2757
58+ Button . defaultProps = {
59+ isStyleDefault : false ,
60+ isStyleCancel : false ,
61+ isStyleDestructive : false
62+ } ;
63+
64+ Button . propTypes = {
65+ onClick : _propTypes2 . default . func . isRequired ,
66+ label : _propTypes2 . default . string . isRequired ,
67+ isStyleDefault : _propTypes2 . default . bool ,
68+ isStyleCancel : _propTypes2 . default . bool ,
69+ isStyleDestructive : _propTypes2 . default . bool
70+ } ;
71+
72+ var Modal = function Modal ( _ref2 ) {
73+ var isModalVisible = _ref2 . isModalVisible ,
74+ hide = _ref2 . hide ,
75+ options = _ref2 . options ,
76+ children = _ref2 . children ;
2877
2978 function handleOverlayClicked ( e ) {
3079 if ( e . target . className !== 'modali-wrapper' ) {
@@ -42,7 +91,45 @@ var Modali = function Modali(_ref) {
4291 }
4392 }
4493
45- return isShown ? _reactDom2 . default . createPortal ( _react2 . default . createElement (
94+ function renderBody ( ) {
95+ if ( children ) {
96+ return children ;
97+ } if ( options && options . message ) {
98+ return _react2 . default . createElement (
99+ 'div' ,
100+ { className : 'modali-body-style' } ,
101+ options . message
102+ ) ;
103+ }
104+ return false ;
105+ }
106+
107+ function renderFooter ( ) {
108+ var buttons = options . buttons ;
109+
110+ return _react2 . default . createElement (
111+ 'div' ,
112+ { className : 'modali-footer' } ,
113+ buttons . map ( function ( button ) {
114+ return _react2 . default . createElement (
115+ _react2 . default . Fragment ,
116+ {
117+ key : _shortid2 . default . generate ( )
118+ } ,
119+ button
120+ ) ;
121+ } )
122+ ) ;
123+ }
124+
125+ var modaliClass = ( 0 , _classnames2 . default ) ( {
126+ modali : true ,
127+ 'modali-size-large' : options && options . large ,
128+ 'modali-size-regular' : ! options || options && ! options . large ,
129+ 'modali-animated modali-animation-fade-in' : options && options . animated
130+ } ) ;
131+
132+ return isModalVisible ? _reactDom2 . default . createPortal ( _react2 . default . createElement (
46133 _react2 . default . Fragment ,
47134 null ,
48135 _react2 . default . createElement ( 'div' , { className : 'modali-overlay' } ) ,
@@ -51,13 +138,18 @@ var Modali = function Modali(_ref) {
51138 { className : 'modali-wrapper' , 'aria-modal' : true , 'aria-hidden' : true , tabIndex : - 1 , role : 'dialog' , onClick : handleOverlayClicked } ,
52139 _react2 . default . createElement (
53140 'div' ,
54- { className : 'modali ' + ( options && options . large ? 'modali-size-large' : 'modali-size-regular' ) + ' ' + ( options && options . animated ? 'modali-animated modali-animation-fade-in' : '' ) } ,
141+ { className : modaliClass } ,
55142 _react2 . default . createElement (
56143 'div' ,
57144 { className : 'modali-content' } ,
58145 options !== undefined && options . closeButton === false ? null : _react2 . default . createElement (
59146 'div' ,
60147 { className : 'modali-header' } ,
148+ options !== undefined && options . title !== undefined && _react2 . default . createElement (
149+ 'div' ,
150+ { className : 'modali-title' } ,
151+ options . title
152+ ) ,
61153 _react2 . default . createElement (
62154 'button' ,
63155 { type : 'button' , className : 'modali-close-button' , 'data-dismiss' : 'modal' , 'aria-label' : 'Close' , onClick : hide } ,
@@ -71,14 +163,18 @@ var Modali = function Modali(_ref) {
71163 _react2 . default . createElement (
72164 'div' ,
73165 { className : 'modali-body' } ,
74- children
75- )
166+ renderBody ( )
167+ ) ,
168+ options && options . buttons && options . buttons . length > 0 && renderFooter ( )
76169 )
77170 )
78171 )
79172 ) , document . body ) : null ;
80173} ;
81174
175+ var Modali = function Modali ( ) { } ;
176+ Modali . Button = Button ;
177+ Modali . Modal = Modal ;
82178exports . default = Modali ;
83179var useModali = exports . useModali = function useModali ( options ) {
84180 var _useState = ( 0 , _react . useState ) ( false ) ,
@@ -88,44 +184,57 @@ var useModali = exports.useModali = function useModali(options) {
88184
89185 var _useState3 = ( 0 , _react . useState ) ( false ) ,
90186 _useState4 = _slicedToArray ( _useState3 , 2 ) ,
91- isShown = _useState4 [ 0 ] ,
92- setIsShown = _useState4 [ 1 ] ;
187+ isModalVisible = _useState4 [ 0 ] ,
188+ setIsModalVisible = _useState4 [ 1 ] ;
189+
190+ var _useState5 = ( 0 , _react . useState ) ( false ) ,
191+ _useState6 = _slicedToArray ( _useState5 , 2 ) ,
192+ isShown = _useState6 [ 0 ] ,
193+ setIsShown = _useState6 [ 1 ] ;
194+
195+ var isModalVisibleRef = ( 0 , _react . useRef ) ( isModalVisible ) ;
196+ isModalVisibleRef . current = isModalVisible ;
197+ var timeoutHack = void 0 ;
198+
199+ function toggle ( ) {
200+ timeoutHack = setTimeout ( function ( ) {
201+ setIsModalVisible ( ! isModalVisibleRef . current ) ;
202+ clearTimeout ( timeoutHack ) ;
203+ } , 10 ) ;
204+ setIsShown ( ! isShown ) ;
205+ setHasToggledBefore ( true ) ;
206+ }
93207
94208 function handleKeyDown ( event ) {
95- if ( options === undefined && event . keyCode === 27 ) {
96- toggle ( ) ;
97- }
98- if ( options !== undefined && options . keyboardClose === true && event . keyCode === 27 ) {
99- toggle ( ) ;
100- }
101- if ( options !== undefined && options . onEscapeKeyDown ) {
209+ if ( event . keyCode !== 27 || options && options . keyboardClose === false ) return ;
210+ toggle ( ) ;
211+ if ( options && options . onEscapeKeyDown ) {
102212 options . onEscapeKeyDown ( ) ;
103- toggle ( ) ;
104213 }
105214 }
106215
107216 ( 0 , _react . useEffect ) ( function ( ) {
108217 if ( isShown ) {
109- options && options . onShow && options . onShow ( ) ;
218+ if ( options && options . onShow ) {
219+ options . onShow ( ) ;
220+ }
110221 document . addEventListener ( 'keydown' , handleKeyDown ) ;
111222 document . body . classList . add ( 'modali-open' ) ;
112223 }
113224 if ( ! isShown && hasToggledBefore ) {
114- options && options . onHide && options . onHide ( ) ;
225+ if ( options && options . onHide ) {
226+ options . onHide ( ) ;
227+ }
115228 document . body . classList . remove ( 'modali-open' ) ;
116229 }
117230 return function ( ) {
118231 return document . removeEventListener ( 'keydown' , handleKeyDown ) ;
119232 } ;
120233 } , [ isShown ] ) ;
121234
122- function toggle ( ) {
123- setIsShown ( ! isShown ) ;
124- setHasToggledBefore ( true ) ;
125- }
126-
127235 return [ {
128236 isShown : isShown ,
237+ isModalVisible : isModalVisible ,
129238 hide : toggle ,
130239 options : options
131240 } , toggle ] ;
0 commit comments