@@ -6,19 +6,14 @@ import { ReactComponent as CloseIcon } from './icon-close.svg'
66
77import './styles.scss'
88import ScrollLock from 'react-scrolllock'
9+ import { Draggable } from '../Draggable'
910
10- export function Dialog ( { isOpen, title, className, persistent, width, height, showCloseButton, onDismiss, onCloseBtnClick, onClickOutside, onPressEscape, style, ...props } ) {
11+ export function Dialog ( { isOpen, title, className, persistent, width, height, draggable , contained , showCloseButton, onDismiss, onCloseBtnClick, onClickOutside, onPressEscape, style, ...props } ) {
1112 // Set an unique ID for the dialog
1213 const [ id ] = useState ( `${ nanoid ( ) } ` )
1314
1415 const [ clickStartedInsideDialog , setClickStartedInsideDialog ] = useState ( undefined )
15-
16- const parsedStyles = useMemo ( ( ) => {
17- const _style = { ...style }
18- if ( width ) _style . width = width
19- if ( height ) _style . height = height
20- return _style
21- } , [ style , width , height ] )
16+ const [ isDragging , setIsDragging ] = useState ( false )
2217
2318 // onCreated lifecycle-hook
2419 useEffect ( ( ) => {
@@ -40,19 +35,27 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
4035 }
4136 }
4237
43- function handleMouseUp ( e ) {
44- setClickStartedInsideDialog ( false ) ;
38+ function handleMouseUp ( e ) {
39+ setClickStartedInsideDialog ( false )
40+ setIsDragging ( false )
4541 }
4642
4743 // Register eventhandlers
48- document . addEventListener ( 'keydown' , handleKeyDown ) ;
49- document . addEventListener ( 'mouseup' , handleMouseUp ) ;
44+ document . addEventListener ( 'keydown' , handleKeyDown )
45+ document . addEventListener ( 'mouseup' , handleMouseUp )
5046 return ( ) => {
51- document . removeEventListener ( 'keydown' , handleKeyDown ) ;
47+ document . removeEventListener ( 'keydown' , handleKeyDown )
5248 document . removeEventListener ( 'mouseup' , handleMouseUp )
5349 }
5450 } )
5551
52+ const dialogClasses = useMemo ( ( ) => {
53+ let classes = 'dialog'
54+ if ( isDragging ) classes += ' dialog-no-select'
55+
56+ return classes
57+ } , [ isDragging ] )
58+
5659 // Plays the shaking animation-effect when the dialog is persistent
5760 function shakeDialogBox ( ) {
5861 const dialog = document . getElementById ( `dialog-${ id } ` )
@@ -66,7 +69,7 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
6669 }
6770
6871 function handleBackdropClick ( e ) {
69- if ( clickStartedInsideDialog ) return ;
72+ if ( clickStartedInsideDialog ) return
7073 const clickedBackdrop = e . target . id === `dialog-backdrop-${ id } `
7174
7275 if ( isOpen && clickedBackdrop ) {
@@ -86,22 +89,26 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
8689 isOpen === true &&
8790 < ScrollLock isActive = { isOpen } >
8891 < div id = { `dialog-backdrop-${ id } ` } className = { `dialog-backdrop ${ className } ` } onMouseUp = { ( e ) => handleBackdropClick ( e ) } >
89- < div
90- id = { `dialog-${ id } ` }
91- className = 'dialog'
92- aria-label = 'dialog'
93- aria-modal = 'true'
94- role = 'dialog'
95- style = { parsedStyles }
96- onMouseDown = { ( e ) => { setClickStartedInsideDialog ( true ) ; e . preventDefault ( ) ; e . stopPropagation ( ) ; } }
97- >
98- { ! persistent && showCloseButton &&
99- < button className = 'dialog-close-btn' onClick = { ( e ) => { handleCloseBtnClick ( ) ; e . preventDefault ( ) } } aria-label = 'Lukk' >
100- < CloseIcon alt = '' />
101- </ button > }
102- { props . children }
103- </ div >
92+ < Draggable active = { draggable && isDragging } contained = { contained } width = { width } height = { height } >
93+ < div
94+ id = { `dialog-${ id } ` }
95+ className = { dialogClasses }
96+ aria-label = 'dialog'
97+ aria-modal = 'true'
98+ role = 'dialog'
99+ style = { style }
100+ onMouseDown = { ( ) => { setClickStartedInsideDialog ( true ) } }
101+ >
102+ { draggable && < div className = 'dialog-drag-area' onMouseDown = { ( ) => setIsDragging ( true ) } /> }
103+ { ! persistent && showCloseButton &&
104+ < button className = 'dialog-close-btn' onClick = { ( e ) => { handleCloseBtnClick ( ) ; e . preventDefault ( ) } } aria-label = 'Lukk' >
105+ < CloseIcon alt = '' />
106+ </ button > }
107+ { props . children }
108+ </ div >
109+ </ Draggable >
104110 </ div >
111+
105112 </ ScrollLock >
106113 )
107114}
@@ -133,6 +140,8 @@ export function DialogActions ({ children, style }) {
133140Dialog . propTypes = {
134141 children : PropTypes . any ,
135142 className : PropTypes . string ,
143+ contained : PropTypes . bool ,
144+ draggable : PropTypes . bool ,
136145 height : PropTypes . string ,
137146 isOpen : PropTypes . bool . isRequired ,
138147 onClickOutside : PropTypes . func ,
0 commit comments