Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Modal): refactored modal to pass in subcomponents for better cu…
…stomization BREAKING CHANGE: removed all props
- Loading branch information
1 parent
75d203a
commit 9bc121c
Showing
6 changed files
with
198 additions
and
73 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
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,103 +1,82 @@ | ||
import React, { forwardRef, ReactNode, useImperativeHandle } from 'react' | ||
import React, { forwardRef } from 'react' | ||
import clsx from 'clsx' | ||
import { twMerge } from 'tailwind-merge' | ||
|
||
import { IComponentBaseProps } from '../types' | ||
|
||
import Button from '../Button' | ||
|
||
export type ModalRef = { | ||
accept: () => void | ||
cancel: () => void | ||
} | ||
import ModalActions from './ModalActions' | ||
import ModalBody from './ModalBody' | ||
import ModalHeader from './ModalHeader' | ||
import Portal from './Portal' | ||
|
||
export type ModalProps = React.HTMLAttributes<HTMLDivElement> & | ||
IComponentBaseProps & { | ||
open?: boolean | ||
title?: string | ||
footer?: boolean | ||
acceptText?: string | ||
cancelText?: string | ||
closeOnBlur?: boolean | ||
onAccept?: () => void | ||
onCancel?: () => void | ||
responsive?: boolean | ||
onClickBackdrop?: () => void | ||
} | ||
|
||
const Modal = forwardRef<ModalRef, ModalProps>( | ||
const Modal = forwardRef<HTMLDivElement, ModalProps>( | ||
( | ||
{ | ||
children, | ||
open, | ||
title, | ||
footer = true, | ||
acceptText = 'Accept', | ||
cancelText = 'Close', | ||
onAccept, | ||
onCancel, | ||
responsive, | ||
onClickBackdrop, | ||
dataTheme, | ||
className, | ||
closeOnBlur = true, | ||
...props | ||
}, | ||
ref | ||
): JSX.Element => { | ||
const classes = twMerge( | ||
const containerClasses = twMerge( | ||
'modal', | ||
className, | ||
clsx({ | ||
'modal-open': open, | ||
'modal-bottom sm:modal-middle': responsive, | ||
}) | ||
) | ||
|
||
useImperativeHandle(ref, (): ModalRef => { | ||
return { | ||
accept: () => { | ||
onAccept && onAccept() | ||
}, | ||
cancel: () => { | ||
onCancel && onCancel() | ||
}, | ||
} | ||
}) | ||
|
||
const handleBackdropClick: React.MouseEventHandler = (e) => { | ||
if (e.target === e.currentTarget) { | ||
e.stopPropagation() | ||
if (closeOnBlur && onCancel) { | ||
onCancel() | ||
} | ||
} | ||
} | ||
const bodyClasses = twMerge( | ||
'modal-box', | ||
className | ||
) | ||
|
||
return ( | ||
<div | ||
aria-label="Modal" | ||
aria-hidden={!open} | ||
aria-modal={open} | ||
{...props} | ||
data-theme={dataTheme} | ||
className={classes} | ||
onClick={handleBackdropClick} | ||
> | ||
<div className="modal-box"> | ||
{title ? <div className="w-full mb-8 text-xl">{title}</div> : null} | ||
|
||
<div>{children}</div> | ||
|
||
{footer ? ( | ||
<div className="modal-action"> | ||
<Button onClick={onAccept} color="primary"> | ||
{acceptText} | ||
</Button> | ||
<Button onClick={onCancel}>{cancelText}</Button> | ||
</div> | ||
) : null} | ||
<Portal> | ||
<div | ||
aria-label="Modal" | ||
aria-hidden={!open} | ||
aria-modal={open} | ||
data-theme={dataTheme} | ||
className={containerClasses} | ||
onClick={(e) => { | ||
e.stopPropagation() | ||
if (e.target === e.currentTarget) { | ||
e.stopPropagation() | ||
if (onClickBackdrop) { | ||
onClickBackdrop() | ||
} | ||
} | ||
}} | ||
> | ||
<div | ||
{...props} | ||
className={bodyClasses} | ||
ref={ref} | ||
> | ||
{children} | ||
</div> | ||
</div> | ||
</div> | ||
</Portal> | ||
) | ||
} | ||
) | ||
|
||
Modal.displayName = 'Modal' | ||
|
||
export default Modal | ||
export default Object.assign(Modal, { | ||
Header: ModalHeader, | ||
Body: ModalBody, | ||
Actions: ModalActions, | ||
}) |
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,28 @@ | ||
import React from 'react' | ||
import { twMerge } from 'tailwind-merge' | ||
|
||
import { IComponentBaseProps } from '../types' | ||
|
||
type ModalActionsProps = React.HTMLAttributes<HTMLDivElement> & | ||
IComponentBaseProps | ||
|
||
const ModalActions = React.forwardRef<HTMLDivElement, ModalActionsProps>(({ | ||
children, | ||
className, | ||
...props | ||
}, ref) => { | ||
const classes = twMerge('modal-action', className) | ||
return ( | ||
<div | ||
{...props} | ||
className={classes} | ||
ref={ref} | ||
> | ||
{children} | ||
</div> | ||
) | ||
}) | ||
|
||
ModalActions.displayName = "ModalActions" | ||
|
||
export default ModalActions |
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,23 @@ | ||
import React from 'react' | ||
|
||
type ModalBodyProps = React.HTMLAttributes<HTMLDivElement> | ||
|
||
const ModalBody = React.forwardRef<HTMLDivElement, ModalBodyProps>(({ | ||
children, | ||
className, | ||
...props | ||
}, ref) => { | ||
return ( | ||
<div | ||
{...props} | ||
className={className} | ||
ref={ref} | ||
> | ||
{children} | ||
</div> | ||
) | ||
}) | ||
|
||
ModalBody.displayName = "ModalBody" | ||
|
||
export default ModalBody |
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,25 @@ | ||
import React from 'react' | ||
import { twMerge } from 'tailwind-merge' | ||
|
||
type ModalHeaderProps = React.HTMLAttributes<HTMLDivElement> | ||
|
||
const ModalHeader = React.forwardRef<HTMLDivElement, ModalHeaderProps>(({ | ||
children, | ||
className, | ||
...props | ||
}, ref) => { | ||
const classes = twMerge('w-full mb-8 text-xl', className) | ||
return ( | ||
<div | ||
{...props} | ||
className={classes} | ||
ref={ref} | ||
> | ||
{children} | ||
</div> | ||
) | ||
}) | ||
|
||
ModalHeader.displayName = "ModalHeader" | ||
|
||
export default ModalHeader |
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,53 @@ | ||
import React, { | ||
ReactNode, | ||
useState, | ||
useEffect, | ||
forwardRef, | ||
HTMLProps, | ||
} from 'react' | ||
import ReactDOM from 'react-dom' | ||
|
||
const hasDOM = !!( | ||
typeof window !== 'undefined' && | ||
window.document && | ||
window.document.createElement | ||
) | ||
|
||
interface PortalProps extends HTMLProps<HTMLDivElement> { | ||
children: ReactNode | ||
} | ||
|
||
const Portal = forwardRef<HTMLDivElement, PortalProps>( | ||
({ children, ...props }, ref) => { | ||
if (!hasDOM) return null | ||
|
||
const [rootNode, setRootNode] = useState<HTMLElement>() | ||
|
||
useEffect(() => () => { | ||
// unmount portal | ||
if (rootNode) { | ||
document.body.removeChild(rootNode) | ||
} | ||
setRootNode(undefined) | ||
}, []) | ||
|
||
if (!rootNode) { | ||
const portal = document.createElement('div') | ||
portal.style.display = 'flex' | ||
setRootNode(portal) | ||
document.body.appendChild(portal) | ||
} | ||
|
||
return rootNode | ||
? ReactDOM.createPortal( | ||
<div ref={ref} {...props}> | ||
{children} | ||
</div>, | ||
rootNode | ||
) | ||
: null | ||
}) | ||
|
||
Portal.displayName = 'Portal' | ||
|
||
export default Portal |