|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities'; |
| 4 | +import { POSITIONS } from './constants'; |
| 5 | +import type { Position, PositioningProps } from './types'; |
| 6 | +import { resolveBoundaryPadding, resolveElementRef } from './utils'; |
| 7 | + |
| 8 | +type UseOverflowShiftOptions = { |
| 9 | + overflowBoundary: PositioningProps['overflowBoundary']; |
| 10 | + overflowBoundaryPadding: PositioningProps['overflowBoundaryPadding']; |
| 11 | + position: Position; |
| 12 | + coverTarget: boolean; |
| 13 | + containerEl: HTMLElement | null; |
| 14 | + targetEl: HTMLElement | null; |
| 15 | + targetDocument: Document | undefined; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Keeps the surface inside the `overflowBoundary` rect by writing |
| 20 | + * `transform: translate3d(dx, dy, 0)` on the surface. Runs whenever |
| 21 | + * `overflowBoundary` is set — `overflowBoundaryPadding` is an optional |
| 22 | + * modifier that adds extra breathing room (defaults to 0). Measures rects |
| 23 | + * on every ResizeObserver / scroll / resize tick so the shift stays |
| 24 | + * accurate as the user scrolls the page or resizes the boundary. |
| 25 | + * |
| 26 | + * Mirrors v9 + Floating UI's `shift()` middleware — which is driven by |
| 27 | + * `overflowBoundary` alone, with `overflowBoundaryPadding` as a modifier. |
| 28 | + * Shift runs only on the **cross axis** of the primary placement; |
| 29 | + * main-axis overflow is native flip's job. |
| 30 | + */ |
| 31 | +export function useOverflowShift({ |
| 32 | + overflowBoundary, |
| 33 | + overflowBoundaryPadding, |
| 34 | + position, |
| 35 | + coverTarget, |
| 36 | + containerEl, |
| 37 | + targetEl, |
| 38 | + targetDocument, |
| 39 | +}: UseOverflowShiftOptions): void { |
| 40 | + useIsomorphicLayoutEffect(() => { |
| 41 | + if (coverTarget || !containerEl || !targetEl) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const boundary = resolveElementRef(overflowBoundary); |
| 46 | + const view = targetDocument?.defaultView; |
| 47 | + const ResizeObserverCtor = view?.ResizeObserver; |
| 48 | + |
| 49 | + if (!boundary || !view || !ResizeObserverCtor) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const surface = containerEl; |
| 54 | + |
| 55 | + const apply = () => { |
| 56 | + const previous = surface.style.transform; |
| 57 | + surface.style.transform = ''; |
| 58 | + const surfaceRect = surface.getBoundingClientRect(); |
| 59 | + surface.style.transform = previous; |
| 60 | + |
| 61 | + const boundaryRect = boundary.getBoundingClientRect(); |
| 62 | + const direction = view.getComputedStyle(surface).direction === 'rtl' ? 'rtl' : 'ltr'; |
| 63 | + const padding = resolveBoundaryPadding(overflowBoundaryPadding, direction); |
| 64 | + |
| 65 | + const isBlockAxisPrimary = position === POSITIONS.above || position === POSITIONS.below; |
| 66 | + |
| 67 | + let dx = 0; |
| 68 | + let dy = 0; |
| 69 | + |
| 70 | + if (isBlockAxisPrimary) { |
| 71 | + const leftOverflow = boundaryRect.left + padding.left - surfaceRect.left; |
| 72 | + const rightOverflow = surfaceRect.right - (boundaryRect.right - padding.right); |
| 73 | + |
| 74 | + if (leftOverflow > 0) { |
| 75 | + dx = leftOverflow; |
| 76 | + } else if (rightOverflow > 0) { |
| 77 | + dx = -rightOverflow; |
| 78 | + } |
| 79 | + } else { |
| 80 | + const topOverflow = boundaryRect.top + padding.top - surfaceRect.top; |
| 81 | + const bottomOverflow = surfaceRect.bottom - (boundaryRect.bottom - padding.bottom); |
| 82 | + |
| 83 | + if (topOverflow > 0) { |
| 84 | + dy = topOverflow; |
| 85 | + } else if (bottomOverflow > 0) { |
| 86 | + dy = -bottomOverflow; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (dx === 0 && dy === 0) { |
| 91 | + surface.style.removeProperty('transform'); |
| 92 | + } else { |
| 93 | + surface.style.setProperty('transform', `translate3d(${dx}px, ${dy}px, 0)`); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + apply(); |
| 98 | + |
| 99 | + const observer = new ResizeObserverCtor(apply); |
| 100 | + observer.observe(surface); |
| 101 | + observer.observe(boundary); |
| 102 | + observer.observe(targetEl); |
| 103 | + |
| 104 | + view.addEventListener('scroll', apply, true); |
| 105 | + view.addEventListener('resize', apply); |
| 106 | + |
| 107 | + return () => { |
| 108 | + observer.disconnect(); |
| 109 | + view.removeEventListener('scroll', apply, true); |
| 110 | + view.removeEventListener('resize', apply); |
| 111 | + surface.style.removeProperty('transform'); |
| 112 | + }; |
| 113 | + }, [overflowBoundary, overflowBoundaryPadding, position, coverTarget, containerEl, targetEl, targetDocument]); |
| 114 | +} |
0 commit comments