Commit da2fa94
Freeze values as soon as possible (#3802)
This PR fixes an issue with the `Listbox` component where we didn't
freeze the value soon enough.
This happens when state lives in the parent, and is updated via an
`onChange`.
What is currently happening:
1. User clicks on a listbox option, this should do 3 things:
1. Call the `onChange` with the new value
2. Close the listbox
3. "Freeze" the value, so the old value is still showing while the
listbox options are closing.
The problem is that calling the `onChange` updates the value in the
parent, and the component re-renders with the new value. At the time we
freeze the value, we already received the new value so we are freezing
the incorrect value. This causes a visual glitch. See reproduction:
tailwindlabs/tailwind-plus-issues#1761
This PR fixes that by changing the order a little bit so we freeze the
value as early as possible.
So now, when the user clicks on an option, we trigger a `SelectOption`
action. This will track whether we should freeze the value or not in
state immediately. After that, we call the `onChange`, and then close
the listbox.
Since we know we want to freeze the value _before_ calling `onChange`,
we can be sure we are freezing the correct (old) value.
## Test plan
Made a little video but with a duration of 1000 instead of 100 so you
can clearly see the old value and no visual jumps while the listbox is
closing.
https://github.com/user-attachments/assets/971b8ff4-2b03-4f6e-99af-f21f14d37930
Fixes: tailwindlabs/tailwind-plus-issues#1761
---------
Co-authored-by: Jordan Pittman <jordan@cryptica.me>1 parent 6b5709a commit da2fa94
File tree
4 files changed
+57
-25
lines changed- packages/@headlessui-react
- src
- components/listbox
- hooks
4 files changed
+57
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
Lines changed: 44 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
| |||
82 | 84 | | |
83 | 85 | | |
84 | 86 | | |
| 87 | + | |
85 | 88 | | |
86 | 89 | | |
87 | 90 | | |
| |||
137 | 140 | | |
138 | 141 | | |
139 | 142 | | |
| 143 | + | |
140 | 144 | | |
141 | 145 | | |
142 | 146 | | |
| |||
181 | 185 | | |
182 | 186 | | |
183 | 187 | | |
| 188 | + | |
184 | 189 | | |
185 | 190 | | |
186 | 191 | | |
| |||
338 | 343 | | |
339 | 344 | | |
340 | 345 | | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
341 | 362 | | |
342 | 363 | | |
343 | 364 | | |
| |||
436 | 457 | | |
437 | 458 | | |
438 | 459 | | |
| 460 | + | |
439 | 461 | | |
440 | 462 | | |
441 | 463 | | |
| |||
487 | 509 | | |
488 | 510 | | |
489 | 511 | | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
490 | 521 | | |
491 | 522 | | |
492 | 523 | | |
| |||
556 | 587 | | |
557 | 588 | | |
558 | 589 | | |
| 590 | + | |
559 | 591 | | |
560 | 592 | | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
567 | 598 | | |
568 | 599 | | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | 600 | | |
573 | | - | |
| 601 | + | |
| 602 | + | |
574 | 603 | | |
| 604 | + | |
575 | 605 | | |
576 | 606 | | |
577 | 607 | | |
| |||
600 | 630 | | |
601 | 631 | | |
602 | 632 | | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
603 | 637 | | |
604 | 638 | | |
605 | 639 | | |
| |||
Lines changed: 3 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
595 | 595 | | |
596 | 596 | | |
597 | 597 | | |
598 | | - | |
| 598 | + | |
599 | 599 | | |
600 | 600 | | |
601 | 601 | | |
| |||
671 | 671 | | |
672 | 672 | | |
673 | 673 | | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | | - | |
679 | | - | |
680 | | - | |
681 | | - | |
| 674 | + | |
682 | 675 | | |
683 | 676 | | |
684 | 677 | | |
| |||
872 | 865 | | |
873 | 866 | | |
874 | 867 | | |
875 | | - | |
876 | | - | |
877 | | - | |
878 | | - | |
879 | | - | |
| 868 | + | |
880 | 869 | | |
881 | 870 | | |
882 | 871 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
168 | | - | |
| 168 | + | |
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| |||
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
0 commit comments