index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use client'
  2. import { Dialog, DialogBackdrop, DialogTitle } 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 inset-0 z-30 overflow-y-auto"
  46. >
  47. <div className={cn('flex h-screen w-screen justify-end', positionCenter && '!justify-center')}>
  48. {/* mask */}
  49. <DialogBackdrop
  50. className={cn('fixed inset-0 z-40', mask && 'bg-black bg-opacity-30')}
  51. onClick={() => {
  52. !clickOutsideNotOpen && onClose()
  53. }}
  54. />
  55. <div className={cn('relative z-50 flex w-full max-w-sm flex-col justify-between overflow-hidden bg-components-panel-bg p-6 text-left align-middle shadow-xl', panelClassname)}>
  56. <>
  57. <div className='flex justify-between'>
  58. {title && <DialogTitle
  59. as="h3"
  60. className="text-lg font-medium leading-6 text-text-primary"
  61. >
  62. {title}
  63. </DialogTitle>}
  64. {showClose && <DialogTitle className="mb-4 flex cursor-pointer items-center" as="div">
  65. <XMarkIcon className='h-4 w-4 text-text-tertiary' onClick={onClose} />
  66. </DialogTitle>}
  67. </div>
  68. {description && <div className='mt-2 text-xs font-normal text-text-tertiary'>{description}</div>}
  69. {children}
  70. </>
  71. {footer || (footer === null
  72. ? null
  73. : <div className="mt-10 flex flex-row justify-end">
  74. <Button
  75. className='mr-2'
  76. onClick={() => {
  77. onCancel && onCancel()
  78. }}>{t('common.operation.cancel')}</Button>
  79. <Button
  80. onClick={() => {
  81. onOk && onOk()
  82. }}>{t('common.operation.save')}</Button>
  83. </div>)}
  84. </div>
  85. </div>
  86. </Dialog>
  87. )
  88. }