use-toggle-expend.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { useEffect, useState } from 'react'
  2. import { useStore } from '@/app/components/workflow/store'
  3. type Params = {
  4. ref: React.RefObject<HTMLDivElement>
  5. hasFooter?: boolean
  6. isInNode?: boolean
  7. }
  8. const useToggleExpend = ({ ref, hasFooter = true, isInNode }: Params) => {
  9. const [isExpand, setIsExpand] = useState(false)
  10. const [wrapHeight, setWrapHeight] = useState(ref.current?.clientHeight)
  11. const panelWidth = useStore(state => state.panelWidth)
  12. const editorExpandHeight = isExpand ? wrapHeight! - (hasFooter ? 56 : 29) : 0
  13. useEffect(() => {
  14. setWrapHeight(ref.current?.clientHeight)
  15. // eslint-disable-next-line react-hooks/exhaustive-deps
  16. }, [isExpand])
  17. const wrapClassName = (() => {
  18. if (!isExpand)
  19. return ''
  20. if (isInNode)
  21. return 'fixed z-10 right-[9px] top-[166px] bottom-[8px] p-4 bg-white rounded-xl'
  22. return 'absolute z-10 left-4 right-6 top-[52px] bottom-0 pb-4 bg-white'
  23. })()
  24. const wrapStyle = isExpand
  25. ? {
  26. boxShadow: '0px 0px 12px -4px rgba(16, 24, 40, 0.05), 0px -3px 6px -2px rgba(16, 24, 40, 0.03)',
  27. width: panelWidth - 1,
  28. }
  29. : {}
  30. return {
  31. wrapClassName,
  32. wrapStyle,
  33. editorExpandHeight,
  34. isExpand,
  35. setIsExpand,
  36. }
  37. }
  38. export default useToggleExpend