index.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. {title && <Dialog.Title
  55. as="h3"
  56. className="text-lg font-medium leading-6 text-text-primary"
  57. >
  58. {title}
  59. </Dialog.Title>}
  60. {showClose && <Dialog.Title className="flex items-center mb-4" as="div">
  61. <XMarkIcon className='w-4 h-4 text-text-tertiary' onClick={onClose} />
  62. </Dialog.Title>}
  63. {description && <Dialog.Description className='text-text-tertiary text-xs font-normal mt-2'>{description}</Dialog.Description>}
  64. {children}
  65. </>
  66. {footer || (footer === null
  67. ? null
  68. : <div className="mt-10 flex flex-row justify-end">
  69. <Button
  70. className='mr-2'
  71. onClick={() => {
  72. onCancel && onCancel()
  73. }}>{t('common.operation.cancel')}</Button>
  74. <Button
  75. onClick={() => {
  76. onOk && onOk()
  77. }}>{t('common.operation.save')}</Button>
  78. </div>)}
  79. </div>
  80. </div>
  81. </Dialog>
  82. )
  83. }