Skip to content

Commit

Permalink
chore: add modal component
Browse files Browse the repository at this point in the history
  • Loading branch information
Rue-pro committed Mar 14, 2024
1 parent f52ea7a commit 9958b43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/shared/ui/Modal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ComponentChildren } from 'preact'
import { createPortal } from 'preact/compat'
import { useEffect, useRef } from 'preact/hooks'

import { browser } from '@shared/browser'

import { Button } from '../Button'
import { ClearIcon } from '../icons/ClearIcon'
import styles from './styles.module.scss'

interface Props {
open: boolean
onClose: () => void
children: ComponentChildren
}

export const Modal = ({ open, children, onClose }: Props) => {
const dialogRef = useRef<HTMLDialogElement | null>(null)

useEffect(() => {
if (dialogRef.current) {
open ? dialogRef.current.showModal() : onClose()
}
}, [open])

if (!open) return null

return createPortal(
<dialog ref={dialogRef} className={styles.modal}>
<Button
variant="secondary"
className={styles['modal__close-btn']}
onClick={onClose}
startIcon={<ClearIcon />}
capture={browser.i18n.getMessage('close')}
></Button>
{children}
</dialog>,
document.getElementsByTagName('body')[0],
)
}
13 changes: 13 additions & 0 deletions src/shared/ui/Modal/styles.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.modal {
width: 100%;
max-width: 100%;
height: 100%;
max-height: 100%;
padding: var(--spacing-5);
background-color: var(--color-neutral-50);
border: none;

&__close-btn {
margin-inline: auto 0;
}
}

0 comments on commit 9958b43

Please sign in to comment.