1- import React , { useEffect , useState } from 'react' ;
1+ import React , { useRef , useState } from 'react' ;
22import { Dialog as HDialog } from '@headlessui/react' ;
33import { Button } from '@/components/elements/button/index' ;
44import { XIcon } from '@heroicons/react/solid' ;
5- import DialogIcon , { IconPosition } from '@/components/elements/dialog/DialogIcon' ;
6- import { AnimatePresence , motion , useAnimation } from 'framer-motion' ;
7- import ConfirmationDialog from '@/components/elements/dialog/ConfirmationDialog' ;
8- import DialogContext from './context' ;
9- import DialogFooter from '@/components/elements/dialog/DialogFooter' ;
10- import styles from './style.module.css' ;
5+ import { AnimatePresence , motion } from 'framer-motion' ;
6+ import { DialogContext , IconPosition , RenderDialogProps , styles } from './' ;
117
12- export interface DialogProps {
13- open : boolean ;
14- onClose : ( ) => void ;
15- }
16-
17- export interface FullDialogProps extends DialogProps {
18- hideCloseIcon ?: boolean ;
19- preventExternalClose ?: boolean ;
20- title ?: string ;
21- description ?: string | undefined ;
22- children ?: React . ReactNode ;
23- }
24-
25- const spring = { type : 'spring' , damping : 15 , stiffness : 300 , duration : 0.15 } ;
268const variants = {
27- open : { opacity : 1 , scale : 1 , transition : spring } ,
28- closed : { opacity : 0 , scale : 0.85 , transition : spring } ,
9+ open : {
10+ scale : 1 ,
11+ opacity : 1 ,
12+ transition : {
13+ type : 'spring' ,
14+ damping : 15 ,
15+ stiffness : 300 ,
16+ duration : 0.15 ,
17+ } ,
18+ } ,
19+ closed : {
20+ scale : 0.75 ,
21+ opacity : 0 ,
22+ transition : {
23+ type : 'easeIn' ,
24+ duration : 0.15 ,
25+ } ,
26+ } ,
2927 bounce : {
3028 scale : 0.95 ,
29+ opacity : 1 ,
3130 transition : { type : 'linear' , duration : 0.075 } ,
3231 } ,
3332} ;
3433
35- const Dialog = ( {
34+ export default ( {
3635 open,
3736 title,
3837 description,
3938 onClose,
4039 hideCloseIcon,
4140 preventExternalClose,
4241 children,
43- } : FullDialogProps ) => {
44- const controls = useAnimation ( ) ;
45-
42+ } : RenderDialogProps ) => {
43+ const container = useRef < HTMLDivElement > ( null ) ;
4644 const [ icon , setIcon ] = useState < React . ReactNode > ( ) ;
4745 const [ footer , setFooter ] = useState < React . ReactNode > ( ) ;
4846 const [ iconPosition , setIconPosition ] = useState < IconPosition > ( 'title' ) ;
47+ const [ down , setDown ] = useState ( false ) ;
48+
49+ const onContainerClick = ( down : boolean , e : React . MouseEvent < HTMLDivElement > ) : void => {
50+ if ( e . target instanceof HTMLElement && container . current ?. isSameNode ( e . target ) ) {
51+ setDown ( down ) ;
52+ }
53+ } ;
4954
5055 const onDialogClose = ( ) : void => {
5156 if ( ! preventExternalClose ) {
5257 return onClose ( ) ;
5358 }
54-
55- controls
56- . start ( 'bounce' )
57- . then ( ( ) => controls . start ( 'open' ) )
58- . catch ( console . error ) ;
5959 } ;
6060
61- useEffect ( ( ) => {
62- controls . start ( open ? 'open' : 'closed' ) . catch ( console . error ) ;
63- } , [ open ] ) ;
64-
6561 return (
6662 < AnimatePresence >
6763 { open && (
@@ -78,10 +74,17 @@ const Dialog = ({
7874 >
7975 < div className = { 'fixed inset-0 bg-gray-900/50 z-40' } />
8076 < div className = { 'fixed inset-0 overflow-y-auto z-50' } >
81- < div className = { styles . container } >
77+ < div
78+ ref = { container }
79+ className = { styles . container }
80+ onMouseDown = { onContainerClick . bind ( this , true ) }
81+ onMouseUp = { onContainerClick . bind ( this , false ) }
82+ >
8283 < HDialog . Panel
8384 as = { motion . div }
84- animate = { controls }
85+ initial = { 'closed' }
86+ animate = { down ? 'bounce' : 'open' }
87+ exit = { 'closed' }
8588 variants = { variants }
8689 className = { styles . panel }
8790 >
@@ -125,11 +128,3 @@ const Dialog = ({
125128 </ AnimatePresence >
126129 ) ;
127130} ;
128-
129- const _Dialog = Object . assign ( Dialog , {
130- Confirm : ConfirmationDialog ,
131- Footer : DialogFooter ,
132- Icon : DialogIcon ,
133- } ) ;
134-
135- export default _Dialog ;
0 commit comments