You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.8 KiB

  1. import { Fragment, PropsWithChildren } from 'react';
  2. import { Dialog, Transition } from '@headlessui/react';
  3. export default function Modal({
  4. children,
  5. show = false,
  6. maxWidth = '2xl',
  7. closeable = true,
  8. onClose = () => {},
  9. styling = 'def',
  10. className = '',
  11. }: PropsWithChildren<{
  12. show: boolean;
  13. maxWidth?: 'sm' | 'md' | 'lg' | 'xl' | '2xl';
  14. closeable?: boolean;
  15. onClose: CallableFunction;
  16. styling?: 'def' | 'success' | 'error' | 'info' | 'warning';
  17. className?: string;
  18. }>) {
  19. const close = () => {
  20. if (closeable) {
  21. onClose();
  22. }
  23. };
  24. const maxWidthClass = {
  25. sm: 'sm:max-w-sm',
  26. md: 'sm:max-w-md',
  27. lg: 'sm:max-w-lg',
  28. xl: 'sm:max-w-xl',
  29. '2xl': 'sm:max-w-2xl',
  30. }[maxWidth];
  31. const stylingClass = {
  32. 'def': 'bg-neutral-white',
  33. 'success': 'bg-success-background border-success-border',
  34. 'error': 'bg-error-background border-error-border',
  35. 'info': 'bg-info-background border-info-border',
  36. 'warning': 'bg-warning-background border-warning-border',
  37. }[styling];
  38. return (
  39. <Transition show={show} as={Fragment} leave="duration-200">
  40. <Dialog
  41. as="div"
  42. id="modal"
  43. className="fixed inset-0 flex overflow-y-auto px-4 py-6 sm:px-0 items-center z-50 transform transition-all"
  44. onClose={close}
  45. >
  46. <Transition.Child
  47. as={Fragment}
  48. enter="ease-out duration-300"
  49. enterFrom="opacity-0"
  50. enterTo="opacity-100"
  51. leave="ease-in duration-200"
  52. leaveFrom="opacity-100"
  53. leaveTo="opacity-0"
  54. >
  55. <div className="absolute inset-0 bg-neutral-0 opacity-50" />
  56. </Transition.Child>
  57. <Transition.Child
  58. as={Fragment}
  59. enter="ease-out duration-300"
  60. enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  61. enterTo="opacity-100 translate-y-0 sm:scale-100"
  62. leave="ease-in duration-200"
  63. leaveFrom="opacity-100 translate-y-0 sm:scale-100"
  64. leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  65. >
  66. <Dialog.Panel
  67. className={`mb-6 bg-white border-4 rounded-lg overflow-hidden shadow-xl items-center justify-center text-center transform transition-all sm:w-full sm:mx-auto ${maxWidthClass} ${stylingClass} ${className}`}
  68. >
  69. {children}
  70. </Dialog.Panel>
  71. </Transition.Child>
  72. </Dialog>
  73. </Transition>
  74. );
  75. }