From 70f88f4e2663841f28d873e27acf4c40930c96f5 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 3 Jul 2024 22:37:44 +0200 Subject: [PATCH] Fix hanging tests when using `anchor` prop (#3357) * add test that verifies unit test hang * bail when parsing the `maxHeight` results in `NaN` * playground cleanup Testing using this playground example, so cleaned it up to be more modern using newer components, transition prop and so on. * use CSS instead of JS Let's make it a CSS problem instead of a JS problem. The `round(up, , )` will behave similar to a `Math.ceil()` that we had in the JS implementation. See: https://developer.mozilla.org/en-US/docs/Web/CSS/round * Remove CSS solution for now I want to re-enable this in the future, but unfortunately for now we can't use it because Chrome only introduced support for this in the last 2 months. This reverts commit daac60d45ec3f02b324d0d8b18078a995e885733. * update changelog --- packages/@headlessui-react/CHANGELOG.md | 1 + .../src/components/popover/popover.test.tsx | 20 +++++ .../src/internal/floating.tsx | 13 +++- .../listbox/listbox-with-pure-tailwind.tsx | 75 ++++++++----------- 4 files changed, 61 insertions(+), 48 deletions(-) diff --git a/packages/@headlessui-react/CHANGELOG.md b/packages/@headlessui-react/CHANGELOG.md index b4a58bf..07b1f16 100644 --- a/packages/@headlessui-react/CHANGELOG.md +++ b/packages/@headlessui-react/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix prematurely added anchoring styles on `ListboxOptions` ([#3337](https://github.com/tailwindlabs/headlessui/pull/3337)) - Ensure `unmount` on `Dialog` works in combination with the `transition` prop on `DialogBackdrop` and `DialogPanel` components ([#3352](https://github.com/tailwindlabs/headlessui/pull/3352)) - Fix crash in `Combobox` component when in `virtual` mode when options are empty ([#3356](https://github.com/tailwindlabs/headlessui/pull/3356)) +- Fix hanging tests when using `anchor` prop ([#3357](https://github.com/tailwindlabs/headlessui/pull/3357)) ## [2.1.1] - 2024-06-26 diff --git a/packages/@headlessui-react/src/components/popover/popover.test.tsx b/packages/@headlessui-react/src/components/popover/popover.test.tsx index 1ca6e7d..85d00d0 100644 --- a/packages/@headlessui-react/src/components/popover/popover.test.tsx +++ b/packages/@headlessui-react/src/components/popover/popover.test.tsx @@ -804,6 +804,26 @@ describe('Rendering', () => { assertActiveElement(getByText('restorable')) }) ) + + it( + 'should be possible to use the `anchor` prop on the `PopoverPanel`', + suppressConsoleLogs(async () => { + render( + + Trigger + Panel open + + ) + + assertPopoverButton({ state: PopoverState.InvisibleUnmounted }) + assertPopoverPanel({ state: PopoverState.InvisibleUnmounted }) + + await click(getPopoverButton()) + + assertPopoverButton({ state: PopoverState.Visible }) + assertPopoverPanel({ state: PopoverState.Visible }) + }) + ) }) describe('Multiple `Popover.Button` warnings', () => { diff --git a/packages/@headlessui-react/src/internal/floating.tsx b/packages/@headlessui-react/src/internal/floating.tsx index 6baaa43..216e1f9 100644 --- a/packages/@headlessui-react/src/internal/floating.tsx +++ b/packages/@headlessui-react/src/internal/floating.tsx @@ -375,9 +375,16 @@ function useFixScrollingPixel(element: HTMLElement | null) { if (!element) return let observer = new MutationObserver(() => { - let maxHeight = element.style.maxHeight - if (parseFloat(maxHeight) !== parseInt(maxHeight)) { - element.style.maxHeight = `${Math.ceil(parseFloat(maxHeight))}px` + let maxHeight = window.getComputedStyle(element).maxHeight + + let maxHeightFloat = parseFloat(maxHeight) + if (isNaN(maxHeightFloat)) return + + let maxHeightInt = parseInt(maxHeight) + if (isNaN(maxHeightInt)) return + + if (maxHeightFloat !== maxHeightInt) { + element.style.maxHeight = `${Math.ceil(maxHeightFloat)}px` } }) diff --git a/playgrounds/react/pages/listbox/listbox-with-pure-tailwind.tsx b/playgrounds/react/pages/listbox/listbox-with-pure-tailwind.tsx index 0d845b0..de62f69 100644 --- a/playgrounds/react/pages/listbox/listbox-with-pure-tailwind.tsx +++ b/playgrounds/react/pages/listbox/listbox-with-pure-tailwind.tsx @@ -1,4 +1,4 @@ -import { Listbox, Transition } from '@headlessui/react' +import { Label, Listbox, ListboxButton, ListboxOption, ListboxOptions } from '@headlessui/react' import { useEffect, useState } from 'react' let people = [ @@ -26,20 +26,12 @@ export default function Home() {
- { - console.log('value:', value) - setActivePerson(value) - }} - > - - Assigned to - + +
- + {active} - + - -
- - {people.map((name) => ( - - - {name} - - - - - - - - ))} - -
-
+ {people.map((name) => ( + + + {name} + + + + + + + + ))} +