|
| 1 | +import { RefObject, useEffect } from 'react' |
| 2 | + |
| 3 | +export function useConstrainedFloatingButton( |
| 4 | + mapContainerRef: RefObject<HTMLDivElement | null>, |
| 5 | + buttonRef: RefObject<HTMLButtonElement | null>, |
| 6 | + isExpanded: boolean, |
| 7 | +) { |
| 8 | + useEffect(() => { |
| 9 | + const updateButtonPosition = () => { |
| 10 | + if (!mapContainerRef.current || !buttonRef.current) return |
| 11 | + |
| 12 | + const mapRect = mapContainerRef.current.getBoundingClientRect() |
| 13 | + const buttonRect = buttonRef.current.getBoundingClientRect() |
| 14 | + const buttonHeight = buttonRect.height || 48 |
| 15 | + const offset = 5 |
| 16 | + |
| 17 | + const buttonElement = buttonRef.current |
| 18 | + |
| 19 | + if (isExpanded) { |
| 20 | + if (buttonElement) { |
| 21 | + buttonElement.style.left = `${offset}px` |
| 22 | + buttonElement.style.bottom = `${3 * offset + offset}px` |
| 23 | + buttonElement.style.top = '' |
| 24 | + buttonElement.style.display = '' |
| 25 | + } |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + const mapTop = mapRect.top |
| 30 | + const mapBottom = mapRect.bottom |
| 31 | + const mapVisible = mapBottom > 0 && mapTop < window.innerHeight |
| 32 | + |
| 33 | + if (!mapVisible) { |
| 34 | + if (buttonElement) { |
| 35 | + buttonElement.style.display = 'none' |
| 36 | + } |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + if (buttonElement) { |
| 41 | + buttonElement.style.display = '' |
| 42 | + } |
| 43 | + |
| 44 | + const desiredBottomFromViewportBottom = offset |
| 45 | + const desiredTopFromViewportTop = window.innerHeight - buttonHeight - offset |
| 46 | + const desiredBottomFromViewportTop = window.innerHeight - desiredBottomFromViewportBottom |
| 47 | + |
| 48 | + let finalTop: number | undefined |
| 49 | + let finalBottom: number | undefined |
| 50 | + |
| 51 | + const buttonTopFromViewportTop = desiredTopFromViewportTop |
| 52 | + const buttonBottomFromViewportTop = desiredBottomFromViewportTop |
| 53 | + |
| 54 | + if (buttonTopFromViewportTop < mapTop) { |
| 55 | + finalTop = mapTop + offset |
| 56 | + finalBottom = undefined |
| 57 | + } else if (buttonBottomFromViewportTop > mapBottom) { |
| 58 | + const constrainedBottomFromTop = mapBottom - offset |
| 59 | + if (constrainedBottomFromTop - buttonHeight < mapTop) { |
| 60 | + finalTop = mapTop + offset |
| 61 | + finalBottom = undefined |
| 62 | + } else { |
| 63 | + finalBottom = window.innerHeight - constrainedBottomFromTop |
| 64 | + finalTop = undefined |
| 65 | + } |
| 66 | + } else { |
| 67 | + finalBottom = desiredBottomFromViewportBottom |
| 68 | + finalTop = undefined |
| 69 | + } |
| 70 | + |
| 71 | + if (buttonElement) { |
| 72 | + buttonElement.style.left = `${mapRect.left + offset}px` |
| 73 | + if (finalTop !== undefined) { |
| 74 | + buttonElement.style.top = `${finalTop}px` |
| 75 | + buttonElement.style.bottom = '' |
| 76 | + } else if (finalBottom !== undefined) { |
| 77 | + buttonElement.style.bottom = `${finalBottom}px` |
| 78 | + buttonElement.style.top = '' |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + updateButtonPosition() |
| 84 | + |
| 85 | + let intersectionObserver: IntersectionObserver | null = null |
| 86 | + if (mapContainerRef.current) { |
| 87 | + intersectionObserver = new IntersectionObserver( |
| 88 | + () => { |
| 89 | + updateButtonPosition() |
| 90 | + }, |
| 91 | + { |
| 92 | + threshold: 0, |
| 93 | + rootMargin: '0px', |
| 94 | + }, |
| 95 | + ) |
| 96 | + intersectionObserver.observe(mapContainerRef.current) |
| 97 | + } |
| 98 | + |
| 99 | + window.document |
| 100 | + .getElementsByClassName('ant-layout-content') |
| 101 | + .item(0) |
| 102 | + ?.addEventListener('scroll', updateButtonPosition) |
| 103 | + window.addEventListener('resize', updateButtonPosition) |
| 104 | + |
| 105 | + return () => { |
| 106 | + if (intersectionObserver) { |
| 107 | + intersectionObserver.disconnect() |
| 108 | + } |
| 109 | + window.document |
| 110 | + .getElementsByClassName('ant-layout-content') |
| 111 | + .item(0) |
| 112 | + ?.removeEventListener('scroll', updateButtonPosition) |
| 113 | + window.removeEventListener('resize', updateButtonPosition) |
| 114 | + } |
| 115 | + }, [mapContainerRef, buttonRef, isExpanded]) |
| 116 | +} |
0 commit comments