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

69 lines
2.0 KiB
TypeScript
Raw Normal View History

import { createEffect, createMemo, createSignal, 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'
2022-09-09 11:53:35 +00:00
interface Props {
2022-09-09 11:53:35 +00:00
name: string
2022-11-27 05:49:48 +00:00
variant: 'narrow' | 'wide'
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() && allowClose()) {
hideModal()
props.onClose && props.onClose()
}
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}>
2022-11-27 05:49:48 +00:00
<div
class={clsx(styles.modal, {
2023-06-14 20:16:44 +00:00
[styles.narrow]: props.variant === 'narrow',
2023-09-06 22:50:54 +00:00
[styles.noPadding]: props.noPadding,
[styles.maxHeight]: props.maxHeight
2022-11-27 05:49:48 +00:00
})}
onClick={(event) => event.stopPropagation()}
>
2022-09-09 11:53:35 +00:00
{props.children}
<Show when={allowClose()}>
<div class={styles.close} onClick={handleHide}>
<svg
class={styles.icon}
width="16"
height="18"
viewBox="0 0 16 18"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M7.99987 7.52552L14.1871 0.92334L15.9548 2.80968L9.76764 9.41185L15.9548 16.014L14.1871 17.9004L7.99987 11.2982L1.81269 17.9004L0.0449219 16.014L6.23211 9.41185L0.0449225 2.80968L1.81269 0.92334L7.99987 7.52552Z"
fill="currentColor"
/>
</svg>
</div>
</Show>
2022-09-09 11:53:35 +00:00
</div>
</div>
</Show>
)
}