useResizeObserver.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as React from 'react'
  2. import { TLViewport, TLBounds, debounce } from '@tldraw/core'
  3. import { useApp } from './useApp'
  4. const getNearestScrollableContainer = (element: HTMLElement): HTMLElement | Document => {
  5. let parent = element.parentElement
  6. while (parent) {
  7. if (parent === document.body) {
  8. return document
  9. }
  10. const { overflowY } = window.getComputedStyle(parent)
  11. const hasScrollableContent = parent.scrollHeight > parent.clientHeight
  12. if (
  13. hasScrollableContent &&
  14. (overflowY === 'auto' || overflowY === 'scroll' || overflowY === 'overlay')
  15. ) {
  16. return parent
  17. }
  18. parent = parent.parentElement
  19. }
  20. return document
  21. }
  22. export function useResizeObserver<T extends HTMLElement>(
  23. ref: React.RefObject<T>,
  24. viewport: TLViewport,
  25. onBoundsChange?: (bounds: TLBounds) => void
  26. ) {
  27. const app = useApp()
  28. const rIsMounted = React.useRef(false)
  29. // When the element resizes, update the bounds (stored in inputs)
  30. // and broadcast via the onBoundsChange callback prop.
  31. const updateBounds = React.useCallback(() => {
  32. if (rIsMounted.current) {
  33. const rect = ref.current?.getBoundingClientRect()
  34. if (rect) {
  35. const bounds: TLBounds = {
  36. minX: rect.left,
  37. maxX: rect.left + rect.width,
  38. minY: rect.top,
  39. maxY: rect.top + rect.height,
  40. width: rect.width,
  41. height: rect.height,
  42. }
  43. viewport.updateBounds(bounds)
  44. onBoundsChange?.(bounds)
  45. }
  46. } else {
  47. // Skip the first mount
  48. rIsMounted.current = true
  49. }
  50. }, [ref, onBoundsChange])
  51. React.useEffect(() => {
  52. const scrollingAnchor = ref.current ? getNearestScrollableContainer(ref.current) : document
  53. const debouncedupdateBounds = debounce(updateBounds, 100)
  54. scrollingAnchor.addEventListener('scroll', debouncedupdateBounds)
  55. window.addEventListener('resize', debouncedupdateBounds)
  56. return () => {
  57. scrollingAnchor.removeEventListener('scroll', debouncedupdateBounds)
  58. window.removeEventListener('resize', debouncedupdateBounds)
  59. }
  60. }, [])
  61. React.useLayoutEffect(() => {
  62. const resizeObserver = new ResizeObserver(entries => {
  63. if (entries[0].contentRect) {
  64. updateBounds()
  65. }
  66. })
  67. if (ref.current) {
  68. resizeObserver.observe(ref.current)
  69. }
  70. return () => {
  71. resizeObserver.disconnect()
  72. }
  73. }, [ref])
  74. React.useLayoutEffect(() => {
  75. updateBounds()
  76. setTimeout(() => {
  77. app.api.cameraToCenter()
  78. })
  79. }, [ref])
  80. }