Commit c9bf352
authored
This PR fixes an issue where the `--button-width` and `--input-width`
CSS variables weren't always up to date.
We compute these values initially when the component mounts based on the
`getBoundingClientRect` of the button and input elements.
To ensure we catch changes in size, we setup a `ResizeObserver` that
watches for changes to the button and input elements.
Unfortunately, `ResizeObserver` doesn't fire when the size changes due
to CSS properties such as `transform` or `scale`. As far as I can tell,
there isn't a single event or Observer we can use to catch all possible
changes.
One solution to this problem would be to delay the computation of the
sizes until after all transitions have completed and then we could even
introduce a small delay to ensure everything is in its final state.
However, you could literally use `hover:scale-110` on the Listbox button
which would mean that the size changes whenever you hover over the
button.
To fix this in a more generic way, we setup a `requestAnimationFrame`
loop that checks the size of the button and input elements on each
frame. If the size has changed, we update the CSS variables.
Note: we will only re-render if the size has actually changed, so this
shouldn't cause unnecessary re-renders.
The internal hook we use (`useElementSize`) also now receives an
`enabled` option such that we only run this `requestAnimationFrame` loop
when the component is enabled.
For components such as the `Combobox`, `Listbox` and `Menu` that means
that we only start measuring when the corresponding dropdown is in an
open state.
Hopefully we can fix this kind of issue with an Observer in the future
(e.g.: `PerformanceObserver` with `LayoutShift`
(https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift)) but this
is still experimental today.
Fixes: #3612
Fixes: #3598
1 parent 0f27e7f commit c9bf352
File tree
6 files changed
+31
-19
lines changed- packages/@headlessui-react
- src
- components
- combobox
- listbox
- popover
- hooks
6 files changed
+31
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1337 | 1337 | | |
1338 | 1338 | | |
1339 | 1339 | | |
1340 | | - | |
1341 | | - | |
| 1340 | + | |
| 1341 | + | |
1342 | 1342 | | |
1343 | 1343 | | |
1344 | 1344 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
794 | 794 | | |
795 | 795 | | |
796 | 796 | | |
797 | | - | |
| 797 | + | |
798 | 798 | | |
799 | 799 | | |
800 | 800 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
581 | 581 | | |
582 | 582 | | |
583 | 583 | | |
584 | | - | |
| 584 | + | |
585 | 585 | | |
586 | 586 | | |
587 | 587 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
851 | 851 | | |
852 | 852 | | |
853 | 853 | | |
854 | | - | |
| 854 | + | |
855 | 855 | | |
856 | 856 | | |
857 | 857 | | |
| |||
Lines changed: 25 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 11 | + | |
| 12 | + | |
18 | 13 | | |
19 | 14 | | |
20 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
21 | 32 | | |
22 | | - | |
23 | | - | |
24 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
25 | 36 | | |
26 | 37 | | |
27 | | - | |
| 38 | + | |
28 | 39 | | |
29 | | - | |
| 40 | + | |
30 | 41 | | |
31 | 42 | | |
32 | 43 | | |
| |||
0 commit comments