webapp/src/components/Nav/Modal/Modal.tsx

68 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-10-15 12:09:31 +00:00
import { createEffect, createMemo, createSignal, on, Show } from 'solid-js'
2022-10-25 16:25:42 +00:00
import type { JSX } from 'solid-js'
import { clsx } from 'clsx'
import { hideModal, useModalStore } from '../../../stores/ui'
import { useEscKeyDownHandler } from '../../../utils/useEscKeyDownHandler'
2022-11-27 05:49:48 +00:00
import styles from './Modal.module.scss'
import { redirectPage } from '@nanostores/router'
import { router } from '../../../stores/router'
import { Icon } from '../../_shared/Icon'
2023-10-15 12:09:31 +00:00
import { resetSortedArticles } from '../../../stores/zine/articles'
2022-09-09 11:53:35 +00:00
interface Props {
2022-09-09 11:53:35 +00:00
name: string
2023-10-03 21:52:38 +00:00
variant: 'narrow' | 'wide' | 'medium'
2022-10-25 16:25:42 +00:00
children: JSX.Element
onClose?: () => void
2023-06-14 20:16:44 +00:00
noPadding?: boolean
2023-09-06 22:50:54 +00:00
maxHeight?: boolean
allowClose?: boolean
2022-10-25 16:25:42 +00:00
}
export const Modal = (props: Props) => {
2022-10-25 16:25:42 +00:00
const { modal } = useModalStore()
const [visible, setVisible] = createSignal(false)
const allowClose = createMemo(() => props.allowClose !== false)
const handleHide = () => {
if (modal()) {
if (allowClose()) {
props.onClose && props.onClose()
} else {
redirectPage(router, 'home')
}
}
hideModal()
2022-09-09 11:53:35 +00:00
}
useEscKeyDownHandler(handleHide)
2022-09-09 11:53:35 +00:00
createEffect(() => {
2022-10-25 16:25:42 +00:00
setVisible(modal() === props.name)
2022-09-09 11:53:35 +00:00
})
return (
<Show when={visible()}>
<div class={styles.backdrop} onClick={handleHide}>
2023-10-03 23:06:41 +00:00
<div class="wide-container">
2023-10-03 21:52:38 +00:00
<div
class={clsx(styles.modal, {
[styles.narrow]: props.variant === 'narrow',
['col-auto col-md-20 offset-md-2 col-lg-14 offset-lg-5']: props.variant === 'medium',
[styles.noPadding]: props.noPadding,
[styles.maxHeight]: props.maxHeight
})}
onClick={(event) => event.stopPropagation()}
>
<div class={styles.modalInner}>{props.children}</div>
<div class={styles.close} onClick={handleHide}>
<Icon name="close" class={styles.icon} />
2023-10-03 21:52:38 +00:00
</div>
</div>
2022-09-09 11:53:35 +00:00
</div>
</div>
</Show>
)
}