index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use client'
  2. import { Dialog } from '@headlessui/react'
  3. import { useTranslation } from 'react-i18next'
  4. import { XMarkIcon } from '@heroicons/react/24/outline'
  5. import Button from '../button'
  6. import cn from '@/utils/classnames'
  7. export type IDrawerProps = {
  8. title?: string
  9. description?: string
  10. panelClassname?: string
  11. children: React.ReactNode
  12. footer?: React.ReactNode
  13. mask?: boolean
  14. positionCenter?: boolean
  15. isOpen: boolean
  16. showClose?: boolean
  17. clickOutsideNotOpen?: boolean
  18. onClose: () => void
  19. onCancel?: () => void
  20. onOk?: () => void
  21. unmount?: boolean
  22. }
  23. export default function Drawer({
  24. title = '',
  25. description = '',
  26. panelClassname = '',
  27. children,
  28. footer,
  29. mask = true,
  30. positionCenter,
  31. showClose = false,
  32. isOpen,
  33. clickOutsideNotOpen,
  34. onClose,
  35. onCancel,
  36. onOk,
  37. unmount = false,
  38. }: IDrawerProps) {
  39. const { t } = useTranslation()
  40. return (
  41. <Dialog
  42. unmount={unmount}
  43. open={isOpen}
  44. onClose={() => !clickOutsideNotOpen && onClose()}
  45. className="fixed z-30 inset-0 overflow-y-auto"
  46. >
  47. <div className={cn('flex w-screen h-screen justify-end', positionCenter && '!justify-center')}>
  48. {/* mask */}
  49. <Dialog.Overlay
  50. className={cn('z-40 fixed inset-0', mask && 'bg-black bg-opacity-30')}
  51. />
  52. <div className={cn('relative z-50 flex flex-col justify-between bg-components-panel-bg w-full max-w-sm p-6 overflow-hidden text-left align-middle shadow-xl', panelClassname)}>
  53. <>
  54. <div className='flex justify-between'>
  55. {title && <Dialog.Title
  56. as="h3"
  57. className="text-lg font-medium leading-6 text-text-primary"
  58. >
  59. {title}
  60. </Dialog.Title>}
  61. {showClose && <Dialog.Title className="flex items-center mb-4 cursor-pointer" as="div">
  62. <XMarkIcon className='w-4 h-4 text-text-tertiary' onClick={onClose} />
  63. </Dialog.Title>}
  64. </div>
  65. {description && <Dialog.Description className='text-text-tertiary text-xs font-normal mt-2'>{description}</Dialog.Description>}
  66. {children}
  67. </>
  68. {footer || (footer === null
  69. ? null
  70. : <div className="mt-10 flex flex-row justify-end">
  71. <Button
  72. className='mr-2'
  73. onClick={() => {
  74. onCancel && onCancel()
  75. }}>{t('common.operation.cancel')}</Button>
  76. <Button
  77. onClick={() => {
  78. onOk && onOk()
  79. }}>{t('common.operation.save')}</Button>
  80. </div>)}
  81. </div>
  82. </div>
  83. </Dialog>
  84. )
  85. }