-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc27e88
commit ea28953
Showing
33 changed files
with
629 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
MONGODB_URI="mongodb://localhost:27017" | ||
MONGODB_URI="mongodb://localhost:27017/abolfazlNote" | ||
JWT_SECRET=degrjgniuwreghnrwg%^&%#&^%*#%^645756%^& |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from "react"; | ||
|
||
export default function IconButton() { | ||
return <span>x</span>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useCallback, useRef } from 'react'; | ||
// @ts-ignore | ||
import { Modal as ModalWrapper } from 'react-dialog-polyfill'; | ||
// import { useOnClickOutside } from '@avtkit/hooks/useOnClickOutside'; | ||
import { ModalProps } from '../types'; | ||
import DefaultCloseButton from './CloseButton'; | ||
import { useModal } from '../useModal'; | ||
import { useOnClickOutside } from '../../../hooks/useOnClickOutside'; | ||
|
||
const Modal: React.FC<ModalProps> = (props: ModalProps) => { | ||
const { | ||
index, | ||
children, | ||
closeButton: CloseButton = DefaultCloseButton, | ||
renderOnOpen = false, | ||
clickOverlayClose = true, | ||
escClose = true, | ||
showExitButton = true | ||
} = props; | ||
|
||
const { activeModal, closeModal } = useModal(); | ||
const isActive = activeModal.id === index; | ||
|
||
const wrapperRef = useRef<HTMLDivElement | null>(); | ||
|
||
const closeHandler = useCallback(() => { | ||
if (isActive) { | ||
closeModal(activeModal.id); | ||
} | ||
}, [isActive, activeModal.id, closeModal]); | ||
|
||
useOnClickOutside(wrapperRef, () => { | ||
if (clickOverlayClose && showExitButton) { | ||
closeHandler(); | ||
} | ||
}); | ||
|
||
return ( | ||
<ModalWrapper | ||
className="modal-wrapper" | ||
open={isActive} | ||
onCancel={() => { | ||
if (escClose) { | ||
closeHandler(); | ||
} | ||
}} | ||
> | ||
<div | ||
className='modal-buttons-container' | ||
ref={(ref) => { | ||
if (ref) { | ||
wrapperRef.current = ref; | ||
} | ||
}} | ||
> | ||
<div className="ms-modal-buttons-container"> | ||
{!!CloseButton && showExitButton && ( | ||
// @ts-ignore | ||
<CloseButton onClick={() => closeHandler()} /> | ||
)} | ||
</div> | ||
{(renderOnOpen || isActive) && children} | ||
</div> | ||
</ModalWrapper> | ||
); | ||
}; | ||
|
||
Modal.defaultProps = { | ||
clickOverlayClose: true, | ||
}; | ||
|
||
export default Modal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
|
||
export type ModalContextType<T> = { | ||
modalsStack: ModalStack<T>[]; | ||
setModalsStack: React.Dispatch<React.SetStateAction<ModalStack<T>[]>>; | ||
}; | ||
|
||
export type ModalStack<T> = { | ||
id: string; | ||
options?: T; | ||
}; | ||
|
||
export default React.createContext<ModalContextType<{}>>({ | ||
modalsStack: [], | ||
setModalsStack: () => {}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React, { useState } from 'react'; | ||
import { ModalProviderProps } from '../types'; | ||
import ModalContext, { ModalStack } from './ModalContext'; | ||
|
||
const ModalsManager = ({ children }: ModalProviderProps) => { | ||
const [modalsStack, setModalsStack] = useState<ModalStack<{}>[]>([]); | ||
|
||
return <ModalContext.Provider value={{ modalsStack, setModalsStack }}>{children}</ModalContext.Provider>; | ||
}; | ||
|
||
export default ModalsManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as ModalsProvider } from './components/ModalsProvider'; | ||
export { default as Modal } from './components/Modal'; | ||
export { default as ModalContext } from './components/ModalContext'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ModalStack } from "./components/ModalContext"; | ||
|
||
export type ModalProviderProps = { | ||
defaultStack?: ModalStack<any>[]; | ||
children: React.ReactNode | ||
}; | ||
|
||
export type ModalProps = { | ||
children: React.ReactNode | ||
index: string; | ||
renderOnOpen?: boolean; | ||
closeButton?: React.ReactNode | false; | ||
extraButtons?: { id: string; button: React.FC<any> }[]; | ||
clickOverlayClose?: boolean; | ||
escClose?: boolean; | ||
buttonsPosition?: { x: string; y: string }; | ||
backgroundColor?: string; | ||
showExitButton?: boolean | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useCallback, useContext, useEffect, useState } from 'react'; | ||
import ModalContext, { ModalStack, ModalContextType } from './components/ModalContext'; | ||
import { uniqBy, isEqual } from 'lodash'; | ||
|
||
export const useModal = <T>() => { | ||
const { modalsStack, setModalsStack } = useContext<ModalContextType<T>>( | ||
ModalContext as unknown as React.Context<ModalContextType<T>> | ||
); | ||
const [activeModal, setActiveModal] = useState<ModalStack<T>>(modalsStack[0] || { id: '' }); | ||
|
||
useEffect(() => { | ||
const newStack = modalsStack[0] || { id: '' }; | ||
|
||
if (!isEqual(newStack, activeModal)) { | ||
setActiveModal(newStack); | ||
} | ||
}, [modalsStack, activeModal]); | ||
|
||
const closeModal = useCallback( | ||
(id: string) => { | ||
setModalsStack((previousStacks) => { | ||
const clone = [...previousStacks]; | ||
const index = clone.findIndex((m) => m.id === id); | ||
if (index >= 0) { | ||
clone.splice(index, 1); | ||
} | ||
|
||
return clone; | ||
}); | ||
}, | ||
[setModalsStack] | ||
); | ||
|
||
const addToModalsStack = useCallback( | ||
(newStacks: ModalStack<T>[], removeDuplicates: boolean = true) => { | ||
setModalsStack((previousStacks) => | ||
removeDuplicates ? uniqBy([...previousStacks, ...newStacks], 'id') : [...previousStacks, ...newStacks] | ||
); | ||
}, | ||
[setModalsStack] | ||
); | ||
|
||
return { | ||
activeModal, | ||
closeModal, | ||
addToModalsStack, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.