Files
Suman/apps/suman-bosna/src/modules/common/components/modal/index.tsx
Nermin Karic ada6fd857c design upgrade
2026-11-23 00:59:47 +01:00

124 lines
3.9 KiB
TypeScript

import { Dialog, Transition } from "@headlessui/react"
import { clx } from "@modules/common/components/ui"
import React, { Fragment } from "react"
import { ModalProvider, useModal } from "@lib/context/modal-context"
import X from "@modules/common/icons/x"
type ModalProps = {
isOpen: boolean
close: () => void
size?: "small" | "medium" | "large"
search?: boolean
children: React.ReactNode
'data-testid'?: string
}
const Modal = ({
isOpen,
close,
size = "medium",
search = false,
children,
'data-testid': dataTestId
}: ModalProps) => {
return (
<Transition appear show={isOpen} as={Fragment}>
<Dialog as="div" className="relative z-[75]" onClose={close}>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 h-screen bg-black/40 backdrop-blur-[2px]" />
</Transition.Child>
<div className="fixed inset-0 overflow-y-hidden">
<div
className={clx(
"flex min-h-full h-full justify-center p-4 text-center",
{
"items-center": !search,
"items-start": search,
}
)}
>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
<Dialog.Panel
data-testid={dataTestId}
className={clx(
"flex flex-col justify-start w-full transform p-6 sm:p-8 text-left align-middle transition-all max-h-[85vh] h-fit overflow-y-auto",
{
"max-w-md": size === "small",
"max-w-xl": size === "medium",
"max-w-3xl": size === "large",
"bg-transparent shadow-none": search,
"bg-background shadow-lg border border-border rounded-2xl": !search,
}
)}
>
<ModalProvider close={close}>{children}</ModalProvider>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog>
</Transition>
)
}
const Title: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const { close } = useModal()
return (
<Dialog.Title className="mb-6 flex items-start justify-between gap-4">
<div className="font-display text-2xl text-foreground [&_h1]:font-display [&_h1]:text-2xl [&_h1]:font-normal [&_h2]:font-normal">
{children}
</div>
<button
onClick={close}
data-testid="close-modal-button"
aria-label="Zatvori"
className="-mr-2 -mt-1 flex h-9 w-9 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
>
<X size={18} />
</button>
</Dialog.Title>
)
}
const Description: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return (
<Dialog.Description className="flex text-sm text-foreground items-center justify-center pt-2 pb-4 h-full">
{children}
</Dialog.Description>
)
}
const Body: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return <div className="w-full">{children}</div>
}
const Footer: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return <div className="mt-6 flex items-center justify-end gap-3 border-t border-border/70 pt-6">{children}</div>
}
Modal.Title = Title
Modal.Description = Description
Modal.Body = Body
Modal.Footer = Footer
export default Modal