use-toggle-expend.ts 1.1 KB

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