Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move modal components to separate files
- Loading branch information
1 parent
509ca80
commit 7589537
Showing
6 changed files
with
170 additions
and
70 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { FC, ElementType, HTMLAttributes } from 'react' | ||
import { Dialog } from '@headlessui/react' | ||
import styled from '@emotion/styled' | ||
|
||
const { Description } = Dialog | ||
|
||
// Wraps around Modal Content | ||
const ModalContent: FC<ModalContentProps> = ({ | ||
as, | ||
className, | ||
children, | ||
...props | ||
}) => { | ||
const _className = className | ||
? `avocado-modal__content ${className}` | ||
: `avocado-modal__content` | ||
return ( | ||
<StyledModalContent {...props} as={as || 'div'} className={_className}> | ||
{children} | ||
</StyledModalContent> | ||
) | ||
} | ||
|
||
interface ModalContentProps extends HTMLAttributes<HTMLElement> { | ||
/** | ||
* as - specifies the html element the component should be rendered as | ||
*/ | ||
as?: ElementType<any> | ||
} | ||
|
||
const StyledModalContent = styled<any>(Description)` | ||
background: white; | ||
margin: 2em auto 0; | ||
max-width: 500px; | ||
padding: 1em; | ||
border-radius: 4px; | ||
` | ||
|
||
export { ModalContent } |
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 +1,4 @@ | ||
export * from './modal' | ||
export * from './content' | ||
export * from './title' | ||
export * from './overlay' |
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,74 +1,71 @@ | ||
import React, { ElementType, FC, HTMLAttributes } from 'react' | ||
import styled from '@emotion/styled' | ||
import { Dialog } from '@headlessui/react' | ||
|
||
/* | ||
* Modal Component | ||
*/ | ||
|
||
const Modal: FC<ModalProps> = ({ | ||
children, | ||
open, | ||
onClose, | ||
// destroy, | ||
initialFocus, | ||
unmount, | ||
className, | ||
as, | ||
...props | ||
}) => { | ||
const _className = className ? `avocado-modal ${className}` : `avocado-modal` | ||
|
||
return ( | ||
<Dialog | ||
<StyledModal | ||
onClose={onClose} | ||
// static={destroy} | ||
unmount={unmount} | ||
as={as} | ||
as={as || 'div'} | ||
initialFocus={initialFocus} | ||
open={open} | ||
{...props} | ||
className={_className} | ||
> | ||
{children} | ||
</Dialog> | ||
</StyledModal> | ||
) | ||
} | ||
|
||
type ModalProps = PrimitiveModalProps & HTMLAttributes<HTMLElement> | ||
|
||
const StyledModal = styled<any>(Dialog)` | ||
position: 'fixed', | ||
zIndex: 10, | ||
width: '100%' | ||
` | ||
|
||
interface PrimitiveModalProps { | ||
/** | ||
* open - sets the state of the Dialog. Whether the Dialog is open or not. | ||
*/ | ||
open: boolean | undefined | ||
onClose: (item: false) => void | ||
destroy?: boolean | ||
unmount?: boolean | ||
as?: React.ElementType<any> | ||
initialFocus?: React.MutableRefObject<any> | ||
} | ||
|
||
const ModalTitle: FC<ModalTitleProps> = ({ children, as, ...props }) => { | ||
const { Title } = Dialog | ||
return ( | ||
<Title as={as} {...props}> | ||
{children} | ||
</Title> | ||
) | ||
} | ||
|
||
const ModalOverlay: FC<HTMLAttributes<HTMLElement>> = ({ | ||
children, | ||
...props | ||
}) => { | ||
const { Overlay } = Dialog | ||
return <Overlay {...props}>{children}</Overlay> | ||
} | ||
/** | ||
* onClose - function to execute while closing the Dialog | ||
*/ | ||
onClose: (item: false) => void | ||
|
||
interface ModalTitleProps { | ||
as: ElementType<any> | ||
} | ||
/** | ||
* unmount - controls if the Dialog should be unmounted or hidden when closed | ||
*/ | ||
unmount?: boolean | ||
|
||
const ModalContent: FC<ModalContentProps> = ({ as, children, ...props }) => { | ||
const { Description } = Dialog | ||
return ( | ||
<Description {...props} as={as}> | ||
{children} | ||
</Description> | ||
) | ||
} | ||
/** | ||
* as - specifies the html element the component should be rendered as | ||
*/ | ||
as?: ElementType<any> | ||
|
||
interface ModalContentProps { | ||
as: ElementType<any> | ||
/** | ||
* initialFocus - control the element that receives focus once the modal is open. Only elements in the tab order can be focused. | ||
*/ | ||
initialFocus?: React.MutableRefObject<any> | ||
} | ||
|
||
export { Modal, ModalTitle, ModalOverlay, ModalContent } | ||
export { 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,38 @@ | ||
import React, { FC, HTMLAttributes } from 'react' | ||
import { Dialog } from '@headlessui/react' | ||
import styled from '@emotion/styled' | ||
|
||
const { Overlay } = Dialog | ||
|
||
/** | ||
* Dims out entire background when modal is open | ||
*/ | ||
const ModalOverlay: FC<HTMLAttributes<HTMLElement>> = ({ | ||
children, | ||
className, | ||
...props | ||
}) => { | ||
const _className = className | ||
? `avocado-modal__overlay ${className}` | ||
: `avocado-modal__overlay` | ||
// just return Overlay from headlessui | ||
return ( | ||
<StyledOverlay {...props} className={_className}> | ||
{children} | ||
</StyledOverlay> | ||
) | ||
} | ||
|
||
const StyledOverlay = styled<any>(Overlay)` | ||
background: rgba(0, 0, 0, 0.65); | ||
position: fixed; | ||
top: 0; | ||
bottom: 0; | ||
display: flex; | ||
align-items: flex-start; | ||
left: 0; | ||
right: 0; | ||
zindex: -1; | ||
` | ||
|
||
export { ModalOverlay } |
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,40 @@ | ||
import React, { ElementType, FC, HTMLAttributes } from 'react' | ||
import { Dialog } from '@headlessui/react' | ||
import styled from '@emotion/styled' | ||
|
||
import colors from '../theme/colors' | ||
|
||
// Modal Title | ||
const { Title } = Dialog | ||
|
||
const ModalTitle: FC<ModalTitleProps> = ({ | ||
children, | ||
className, | ||
as, | ||
...props | ||
}) => { | ||
const _className = className | ||
? `avocado-modal__title ${className}` | ||
: `avocado-modal__title` | ||
|
||
return ( | ||
<StyledTitle as={as} {...props} className={_className}> | ||
{children} | ||
</StyledTitle> | ||
) | ||
} | ||
|
||
interface ModalTitleProps extends HTMLAttributes<HTMLElement> { | ||
/** | ||
* as - specifies the html element the component should be rendered as | ||
*/ | ||
as: ElementType<any> | ||
} | ||
|
||
const StyledTitle = styled<any>(Title)` | ||
font-weight: 700; | ||
padding-bottom: 0.5em; | ||
border-bottom: 1px solid ${colors.gray[4]}; | ||
` | ||
|
||
export { ModalTitle } |