Skip to content

Commit

Permalink
fix(react-dom): make whileElementsMounted reactive (#2735)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <github-actions@github.com>
  • Loading branch information
atomiks and GitHub Actions committed Jan 15, 2024
1 parent 77fe748 commit d3a773b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sharp-turtles-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@floating-ui/react-dom": patch
---

fix: make `whileElementsMounted` reactive with respect from changing from a function to `undefined`
21 changes: 21 additions & 0 deletions packages/dom/test/functional/autoUpdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,24 @@ import {click} from './utils/click';
);
});
});

test.only(`reactive whileElementsMounted`, async ({page}) => {
await page.goto('http://localhost:1234/autoUpdate');

// option is `false` on mount by default, so test that changing it to `true`
// works
await click(page, `[data-testid="whileElementsMounted-true"]`);
await page.evaluate(() => window.scrollTo(0, 25));

expect(await page.locator('.container').screenshot()).toMatchSnapshot(
`whileElementsMounted-true.png`,
);

// test that changing it back to `undefined` works
await click(page, `[data-testid="whileElementsMounted-false"]`);
await page.evaluate(() => window.scrollTo(0, 50));

expect(await page.locator('.container').screenshot()).toMatchSnapshot(
`whileElementsMounted-false.png`,
);
});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions packages/dom/test/visual/spec/AutoUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export function AutoUpdate() {

const [referenceSize, setReferenceSize] = useState(200);
const [floatingSize, setFloatingSize] = useState(100);
const [whileElementsMounted, setWhileElementsMounted] = useState(false);

const {x, y, strategy, refs, update} = useFloating({
strategy: 'fixed',
whileElementsMounted: whileElementsMounted ? autoUpdate : undefined,
});

useLayoutEffect(() => {
Expand Down Expand Up @@ -181,6 +183,24 @@ export function AutoUpdate() {
</button>
))}
</Controls>

<h2>Reactive whileElementsMounted</h2>
<Controls>
{[true, false].map((bool) => (
<button
key={String(bool)}
onClick={() => {
setWhileElementsMounted(bool);
}}
data-testid={`whileElementsMounted-${bool}`}
style={{
backgroundColor: whileElementsMounted === bool ? 'black' : '',
}}
>
{String(bool)}
</button>
))}
</Controls>
</>
);
}
9 changes: 8 additions & 1 deletion packages/react-dom/src/useFloating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function useFloating<RT extends ReferenceType = ReferenceType>(
const floatingRef = React.useRef<HTMLElement | null>(null);
const dataRef = React.useRef(data);

const hasWhileElementsMounted = whileElementsMounted != null;
const whileElementsMountedRef = useLatestRef(whileElementsMounted);
const platformRef = useLatestRef(platform);

Expand Down Expand Up @@ -129,7 +130,13 @@ export function useFloating<RT extends ReferenceType = ReferenceType>(

update();
}
}, [referenceEl, floatingEl, update, whileElementsMountedRef]);
}, [
referenceEl,
floatingEl,
update,
whileElementsMountedRef,
hasWhileElementsMounted,
]);

const refs = React.useMemo(
() => ({
Expand Down

0 comments on commit d3a773b

Please sign in to comment.