Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions src/components/DropdownField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import useContentHeaderHeight from '@hooks/useContentHeaderHeight';
import useRemeasureOnWindowResize from '@hooks/useRemeasureOnWindowResize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSafeAreaInsets from '@hooks/useSafeAreaInsets';
import useWindowDimensions from '@hooks/useWindowDimensions';
Expand All @@ -11,6 +12,7 @@ import type AnchorAlignment from '@src/types/utils/AnchorAlignment';
import type {ComponentRef, ReactNode, Ref} from 'react';
import type {View} from 'react-native';

import {shallowEqual} from 'fast-equals';
import React, {useImperativeHandle, useRef, useState} from 'react';

import type {DropdownFieldRowProps} from './DropdownFieldRow';
Expand Down Expand Up @@ -42,7 +44,7 @@ const ANCHOR_ALIGNMENT_ABOVE: AnchorAlignment = {
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
};

/** Where and how big the container is, measured off the row each time it opens */
/** Where and how big the container is, measured off the row each time it opens and again whenever the window changes size under it */
type DropdownLayout = {
horizontal: number;
vertical: number;
Expand Down Expand Up @@ -139,36 +141,63 @@ function DropdownField({renderDropdown, shouldOpenInDropdown, onPress, listHeade

const closeDropdown = () => updateVisibility(false);

/** Measures the row and hands back where the container goes, and whether that side has room for a list */
const measureLayout = (onMeasured: (measuredLayout: DropdownLayout, hasUsableHeight: boolean) => void) => {
anchorRef.current?.measureInWindow((x, y, width, height) => {
const spaceBelow = windowHeight - (y + height + CONTAINER_GAP);
const spaceAbove = y - CONTAINER_GAP - (safeAreaTop + contentHeaderHeight);
const shouldOpenAbove = spaceBelow < maxContainerHeight && spaceAbove > spaceBelow;
const availableHeight = (shouldOpenAbove ? spaceAbove : spaceBelow) - CONTAINER_BORDER;

onMeasured(
{
horizontal: x,
vertical: shouldOpenAbove ? y - CONTAINER_GAP : y + height + CONTAINER_GAP,
width,
height: Math.min(maxContainerHeight, availableHeight),
shouldOpenAbove,
},
availableHeight >= minUsableHeight,
);
});
};

const openDropdown = () => {
if (isSmallScreenWidth) {
setHasEverOpened(true);
updateVisibility(true);
return;
}

anchorRef.current?.measureInWindow((x, y, width, height) => {
const spaceBelow = windowHeight - (y + height + CONTAINER_GAP);
const spaceAbove = y - CONTAINER_GAP - (safeAreaTop + contentHeaderHeight);
const shouldOpenAbove = spaceBelow < maxContainerHeight && spaceAbove > spaceBelow;
const availableHeight = (shouldOpenAbove ? spaceAbove : spaceBelow) - CONTAINER_BORDER;

if (availableHeight < minUsableHeight && onPress) {
measureLayout((measuredLayout, hasUsableHeight) => {
if (!hasUsableHeight && onPress) {
onPress();
return;
}

setLayout({
horizontal: x,
vertical: shouldOpenAbove ? y - CONTAINER_GAP : y + height + CONTAINER_GAP,
width,
height: Math.min(maxContainerHeight, availableHeight),
shouldOpenAbove,
});
setLayout(measuredLayout);
setHasEverOpened(true);
updateVisibility(true);
});
};

const remeasureOpenDropdown = (isStale: () => boolean) => {
measureLayout((measuredLayout, hasUsableHeight) => {
if (isStale()) {
return;
}
// Pressing the row here would have opened its page instead, but a resize is no reason to navigate, so the list just closes.
if (!hasUsableHeight && onPress) {
closeDropdown();
return;
}
setLayout((currentLayout) => (shallowEqual(currentLayout, measuredLayout) ? currentLayout : measuredLayout));
});
};

// The row moves and resizes with the window, so an open pop-over is placed off it again. A bottom sheet doesn't hang off the row.
useRemeasureOnWindowResize(isVisible && !isSmallScreenWidth, remeasureOpenDropdown);

const handlePress = () => {
if (!shouldOpenInDropdown) {
onPress?.();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ function PopoverWithMeasuredContentBase({
const hasStaticDimensions = popoverDimensions.width > 0 && popoverDimensions.height > 0;
const modalId = ComposerFocusManager.getId();

// Static dimensions skip `onLayout`, so sync height changes from the prop.
// Static dimensions skip `onLayout`, so sync size changes from the prop.
if (hasStaticDimensions && popoverWidth !== popoverDimensions.width) {
setPopoverWidth(popoverDimensions.width);
}
if (hasStaticDimensions && popoverHeight !== popoverDimensions.height) {
setPopoverHeight(popoverDimensions.height);
}
Expand Down
9 changes: 9 additions & 0 deletions src/hooks/useRemeasureOnWindowResize/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type UseRemeasureOnWindowResize from './types';

/**
* A native window can't be dragged to a new size. It only changes size on rotation, split view or, on Android, when the
* keyboard shows, and re-measuring then would move or close a list while its search is being typed in.
*/
const useRemeasureOnWindowResize: UseRemeasureOnWindowResize = () => {};

export default useRemeasureOnWindowResize;
34 changes: 34 additions & 0 deletions src/hooks/useRemeasureOnWindowResize/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import useWindowDimensions from '@hooks/useWindowDimensions';

import CONST from '@src/CONST';

import {useEffect, useEffectEvent} from 'react';

import type UseRemeasureOnWindowResize from './types';

/**
* Keeps something anchored to an element in place while the browser window is resized, e.g. a pop-over under a row in
* the RHP, which follows the window's right edge. The RHP only takes the new width after its own layout pass, so a
* measurement on the same frame still sees the element a step behind: it keeps the pop-over close while the window is
* dragged, and the element is measured once more after the resize settles. The first run also happens as soon as the
* hook turns active.
*/
const useRemeasureOnWindowResize: UseRemeasureOnWindowResize = (isActive, remeasure) => {
const {windowWidth, windowHeight} = useWindowDimensions();
const onRemeasure = useEffectEvent(remeasure);

useEffect(() => {
if (!isActive) {
return;
}
let isStale = false;
onRemeasure(() => isStale);
const settledRemeasureTimeout = setTimeout(() => onRemeasure(() => isStale), CONST.TIMING.RESIZE_DEBOUNCE_TIME);
return () => {
isStale = true;
clearTimeout(settledRemeasureTimeout);
};
}, [isActive, windowWidth, windowHeight]);
};

export default useRemeasureOnWindowResize;
12 changes: 12 additions & 0 deletions src/hooks/useRemeasureOnWindowResize/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Measures the anchor again and applies the result. Call `isStale` once the measurement comes back: it is true when the
* window changed again or the hook went inactive in the meantime, and the result should then be dropped.
*/
type UseRemeasureOnWindowResizeCallback = (isStale: () => boolean) => void;

/**
* Calls `remeasure` while `isActive`, whenever the window changes size. The callback doesn't need to be stable.
*/
type UseRemeasureOnWindowResize = (isActive: boolean, remeasure: UseRemeasureOnWindowResizeCallback) => void;

export default UseRemeasureOnWindowResize;
30 changes: 29 additions & 1 deletion tests/unit/PopoverWithMeasuredContentPositionTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const POPOVER_HEIGHT = 400;
const POPOVER_WIDTH = 300;
const WINDOW_MARGIN = CONST.MODAL.POPOVER_MENU_PADDING;

const mockWindowWidth = 1000;
let mockAnchorPosition: PopoverAnchorPosition | undefined;

jest.mock('@hooks/useWindowDimensions', () => () => ({windowWidth: 1000, windowHeight: 600}));
jest.mock('@hooks/useWindowDimensions', () => () => ({windowWidth: mockWindowWidth, windowHeight: 600}));

jest.mock('@components/Popover', () => {
const MockPopover = ({anchorPosition, children}: {anchorPosition: PopoverAnchorPosition; children: ReactNode}) => {
Expand Down Expand Up @@ -48,6 +49,33 @@ function renderPopoverAt(vertical: number, verticalAlignment: ValueOf<typeof CON
return mockAnchorPosition;
}

describe('PopoverWithMeasuredContentBase horizontal positioning', () => {
it('positions the popover by its current width when a static width changes while it is open', () => {
// Given an open popover as wide as its anchor row, which ends a gutter short of the window's right edge
const ROW_RIGHT_EDGE = mockWindowWidth - 20;
const buildPopover = (width: number) => (
<PopoverWithMeasuredContentBase
isVisible
onClose={() => {}}
anchorRef={createRef<ComponentRef<typeof View>>()}
anchorPosition={{horizontal: ROW_RIGHT_EDGE - width, vertical: 100}}
anchorAlignment={{horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT, vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP}}
popoverDimensions={{height: POPOVER_HEIGHT, width}}
>
<Text>content</Text>
</PopoverWithMeasuredContentBase>
);
const {rerender} = render(buildPopover(POPOVER_WIDTH));

// When the row gets narrower and the popover is given the new width and left edge
const narrowerWidth = POPOVER_WIDTH - 100;
rerender(buildPopover(narrowerWidth));

// Then it stays on the row's left edge, since the width it opened with would look like an overflow and shift it left
expect(mockAnchorPosition?.left).toBe(ROW_RIGHT_EDGE - narrowerWidth);
});
});

describe('PopoverWithMeasuredContentBase vertical positioning', () => {
describe('BOTTOM alignment (popover opens above the anchor)', () => {
it('pins the popover bottom edge to the anchor when the popover fits above it', () => {
Expand Down
Loading
Loading