34 Commits

Author SHA1 Message Date
Robin Malfait bc76bbd3f1 2.2.4 - @headlessui/react 2025-05-20 16:16:43 +02:00
Robin Malfait d689bbc64f Fix Combobox issue when using virtual mode (#3734)
This PR fixes an issue with the `Combobox` component when using the
`virtual` mode.

We recently already fixed this issue
(https://github.com/tailwindlabs/headlessui/pull/3678) by applying a
patch on the `@tanstack/virtual-core` package. But this was only applied
in cjs builds, not in the esm builds.

Instead of trying to fix it in our build process, we decided to just fix
it upstream (https://github.com/TanStack/virtual/pull/1004) and this PR
bumps the version of `@tanstack/virtual-core` to the latest version
(which includes the fix).
2025-05-20 14:11:26 +00:00
Robin Malfait 8a24040316 2.2.3 - @headlessui/react 2025-05-12 23:48:06 +02:00
Robin Malfait 130b3d76d5 bump Tailwind CSS 2025-05-09 16:16:23 +02:00
Kamil Dzieniszewski 0b8deaf7a9 Update @react-aria/focus and @react-aria/interactions dependencies to latest versions (#3712)
Support React 19

Fixes: #3711
2025-05-04 15:18:25 +02:00
Robin Malfait df88f4c48b 2.2.2 - @headlessui/react 2025-04-17 15:16:30 +02:00
Robin Malfait 97cb20806a Improve Combobox component performance (#3697)
This PR improves the performance of the `Combobox` component. This is a
similar implementation as:

- https://github.com/tailwindlabs/headlessui/pull/3685
- https://github.com/tailwindlabs/headlessui/pull/3688

Before this PR, the `Combobox` component is built in a way where all the
state lives in the `Combobox` itself. If state changes, everything
re-renders and re-computes the necessary derived state.

However, if you have a 1000 items, then every time the active item
changes, all 1000 items have to re-render.

To solve this, we can move the state outside of the `Combobox`
component, and "subscribe" to state changes using the `useSlice` hook
introduced in https://github.com/tailwindlabs/headlessui/pull/3684.

This will allow us to subscribe to a slice of the state, and only
re-render if the computed slice actually changes.

If the active item changes, only 3 things will happen:

1. The `ComboboxOptions` will re-render and have an updated
`aria-activedescendant`
2. The `ComboboxOption` that _was_ active, will re-render and the
`data-focus` attribute wil be removed.
3. The `ComboboxOption` that is now active, will re-render and the
`data-focus` attribute wil be added.

The `Combobox` component already has a `virtual` option if you want to
render many many more items. This is a bit of a different model where
all the options are passed in via an array instead of rendering all
`ComboboxOption` components immediately.

Because of this, I didn't want to batch the registration of the options
as part of this PR (similar to what we do in the `Menu` and `Listbox`)
because it behaves differently compared to what mode you are using
(virtual or not). Since not all components will be rendered, batching
the registration until everything is registered doesn't really make
sense in the general case. However, it does make sense in non-virtual
mode. But because of this difference, I didn't want to implement this as
part of this PR and increase the complexity of the PR even more.

Instead I will follow up with more PRs with more improvements. But the
key improvement of looking at the slice of the data is what makes the
biggest impact. This also means that we can do another release once this
is merged.

Last but not least, recently we fixed a bug where the `Combobox` in
`virtual` mode could crash if you search for an item that doesn't exist.
To solve it, we implemented a workaround in:

- https://github.com/tailwindlabs/headlessui/pull/3678

Which used a double `requestAnimationFrame` to delay the scrolling to
the item. While this solved this issue, this also caused visual flicker
when holding down your arrow keys.

I also fixed it in this PR by introducing `patch-package` and work
around the issue in the `@tanstack/virtual-core` package itself.

More info: 96f4da70b16d5cf259643

Before:


https://github.com/user-attachments/assets/132520d3-b4d6-42f9-9152-57427de20639

After:


https://github.com/user-attachments/assets/41f198fe-9326-42d1-a09f-077b60a9f65d

## Test plan

1. All tests still pass
2. Tested this in the browser with a 1000 items. In the videos below the
only thing I'm doing is holding down the `ArrowDown` key.

Before:


https://github.com/user-attachments/assets/945692a3-96e6-4ac7-bee0-36a1fd89172b

After:


https://github.com/user-attachments/assets/98a151d0-16cc-4823-811c-fcee0019937a
2025-04-17 15:16:30 +02:00
Robin Malfait e10f54bc12 Migrate React playground to Tailwind CSS v4 (#3695)
This PR bumps the internal React playground to use Tailwind CSS v4
2025-04-11 19:28:04 +02:00
Robin Malfait e2a63760aa Prepare for performance improvements (#3684)
This PR is just a chore to prepare for future performance optimizations.
Essentially I want to improve the performance of the `Menu`, `Listbox`
and `Combobox` components but I want to do it in separate PRs such that
reverting the improvements can be done if needed.

This PR just sets up a `Machine` for state machines, and adds some
helpers such as a `useSlice` to calculate parts of the state machine.
Component using the `useSlice` will only re-render _if_ the slice
changes.

So apart from adding a library (`useSyncExternalStoreWithSelector`) and
adding some setup code. Nothing in this PR changes the behavior of the
components.
2025-04-10 22:26:12 +02:00
Robin Malfait ef9c17217e 2.2.1 - @headlessui/react 2025-04-04 16:45:09 +02:00
Robin Malfait 1be0e67c76 0.2.2 - @headlessui/tailwindcss 2025-02-06 14:21:42 +01:00
Philipp Spiess 4f5506ef99 Support installing with Tailwind CSS v4 (#3634)
Resolves #3633

Bumps the version range for `@headlessui/tailwindcss` to ensure support
with Tailwind CSS v4.
2025-02-06 14:19:45 +01:00
Kevin Chung 058a14b058 Bump @tanstack/react-virtual (#3588)
`@tanstack/react-virtual` added peer deps support for React 19 in
[v3.11.0](https://github.com/TanStack/virtual/releases/tag/v3.11.0)
(https://github.com/TanStack/virtual/pull/893)

This PR upgrades the packages. This can resolve some warnings on some
React 19 projects, e.g.:

```
npm warn ERESOLVE overriding peer dependency
npm warn While resolving: @tanstack/react-virtual@3.10.9
npm warn Found: react@19.0.0
npm warn node_modules/react
npm warn   peer react@"^18 || ^19 || ^19.0.0-rc" from @headlessui/react@2.2.0
npm warn   node_modules/@headlessui/react
npm warn     @headlessui/react@"^2.2.0" from the root project
npm warn   15 more (@floating-ui/react, @floating-ui/react-dom, ...)
npm warn
npm warn Could not resolve dependency:
npm warn peer react@"^16.8.0 || ^17.0.0 || ^18.0.0" from @tanstack/react-virtual@3.10.9
npm warn node_modules/@headlessui/react/node_modules/@tanstack/react-virtual
npm warn   @tanstack/react-virtual@"^3.8.1" from @headlessui/react@2.2.0
npm warn   node_modules/@headlessui/react
npm warn
npm warn Conflicting peer dependency: react@18.3.1
npm warn node_modules/react
npm warn   peer react@"^16.8.0 || ^17.0.0 || ^18.0.0" from @tanstack/react-virtual@3.10.9
npm warn   node_modules/@headlessui/react/node_modules/@tanstack/react-virtual
npm warn     @tanstack/react-virtual@"^3.8.1" from @headlessui/react@2.2.0
npm warn     node_modules/@headlessui/react
npm warn ERESOLVE overriding peer dependency
npm warn While resolving: @tanstack/react-virtual@3.10.9
npm warn Found: react-dom@19.0.0
npm warn node_modules/react-dom
npm warn   peer react-dom@"^18 || ^19 || ^19.0.0-rc" from @headlessui/react@2.2.0
npm warn   node_modules/@headlessui/react
npm warn     @headlessui/react@"^2.2.0" from the root project
npm warn   5 more (@floating-ui/react, @floating-ui/react-dom, ...)
npm warn
npm warn Could not resolve dependency:
npm warn peer react-dom@"^16.8.0 || ^17.0.0 || ^18.0.0" from @tanstack/react-virtual@3.10.9
npm warn node_modules/@headlessui/react/node_modules/@tanstack/react-virtual
npm warn   @tanstack/react-virtual@"^3.8.1" from @headlessui/react@2.2.0
npm warn   node_modules/@headlessui/react
npm warn
npm warn Conflicting peer dependency: react-dom@18.3.1
npm warn node_modules/react-dom
npm warn   peer react-dom@"^16.8.0 || ^17.0.0 || ^18.0.0" from @tanstack/react-virtual@3.10.9
npm warn   node_modules/@headlessui/react/node_modules/@tanstack/react-virtual
npm warn     @tanstack/react-virtual@"^3.8.1" from @headlessui/react@2.2.0
npm warn     node_modules/@headlessui/react
```
2024-12-12 17:38:38 +01:00
Robin Malfait d71fb9cd2e 2.2.0 - @headlessui/react 2024-10-25 15:51:43 +02:00
Robin Malfait 5eb3b12a95 2.1.10 - @headlessui/react 2024-10-10 20:56:58 +02:00
Robin Malfait 242225000f 2.1.9 - @headlessui/react 2024-10-03 11:57:46 +02:00
Robin Malfait 994303f936 2.1.8 - @headlessui/react 2024-09-12 12:35:23 +02:00
Robin Malfait dde00da9e7 2.1.7 - @headlessui/react 2024-09-11 17:29:20 +02:00
Robin Malfait 4737c6df97 Prevent crash in environments where Element.prototype.getAnimations is not available (#3473)
Recently we made improvements to the `Transition` component and internal
`useTransition` hook. We now use the `Element.prototype.getAnimations`
API to know whether or not all transitions are done.

This API has been available in browsers since 2020, however jsdom
doesn't have support for this. This results in a lot of failing tests
where users rely on jsdom (e.g. inside of Jest or Vitest).

In a perfect world, jsdom is not used because it's not a real browser
and there is a lot you need to workaround to even mimic a real browser.

I understand that just switching to real browser tests (using Playwright
for example) is not an easy task that can be done easily.

Even our tests still rely on jsdom…

So to make the development experience better, we polyfill the
`Element.prototype.getAnimations` API only in tests
(`process.env.NODE_ENV === 'test'`) and show a warning in the console on
how to proceed.

The polyfill we ship simply returns an empty array for
`node.getAnimations()`. This means that it will be _enough_ for most
tests to pass. The exception is if you are actually relying on
`transition-duration` and `transition-delay` CSS properties.


The warning you will get looks like this:
``````
Headless UI has polyfilled `Element.prototype.getAnimations` for your tests.
Please install a proper polyfill e.g. `jsdom-testing-mocks`, to silence these warnings.

Example usage:
```js
import { mockAnimationsApi } from 'jsdom-testing-mocks'
mockAnimationsApi()
```
``````

Fixes: #3470
Fixes: #3469
Fixes: #3468
2024-09-11 17:19:55 +02:00
Robin Malfait 5b365f5cae 2.1.6 - @headlessui/react 2024-09-09 21:14:18 +02:00
Robin Malfait cb86665f5b 2.1.5 - @headlessui/react 2024-09-04 16:36:30 +02:00
Robin Malfait 75619eef3b 2.1.4 - @headlessui/react 2024-09-03 17:23:03 +02:00
Robin Malfait d65829b08a Fix crash in Combobox component when in virtual mode when options are empty (#3356)
* bump `@tanstack/react-virtual`

* only enable the virtualizer when there are options

* update changelog
2024-07-03 00:37:02 +02:00
Robin Malfait 03c22b42b6 Cancel outside click behavior on touch devices when scrolling (#3266)
* make `handleOutsideClick` stable

* cancel "outside click" when "scrolling" on touch device

When on a touch device, then the `touchend` event will fire, even if you
scrolled a bit and scrolling was your intention.

This now tracks that touches were at least 30px apart in either the X or
Y direction. If that's the case, then we do not consider it an outside
click.

* add `enabled` parameter to `useDocumentEvent` and `useWindowEvent`

* update `useDocumentEvent` and `useWindowEvent` usages

This now takes the new `enabled` value into account.

* update changelog

* bump vue and vite in playground
2024-06-03 16:18:14 +02:00
Robin Malfait e8c766190d bump dependencies (#3247) 2024-05-28 12:33:09 +02:00
Robin Malfait 031b39d522 update @headlessui/vue version in package-lock.json 2024-05-08 13:35:04 +02:00
Jordan Pittman f513614ffe 2.0.3 - @headlessui/react 2024-05-07 14:14:50 -04:00
Jordan Pittman 2d5d35a533 2.0.1 - @headlessui/react 2024-05-06 15:07:53 -04:00
Robin Malfait f0e3e5b4a6 Bump dependencies (#3158)
* use `act` from `react` instead of `@testing-library/react`

* bump dependencies

* bump `@testing-library/react`

* bump `@react-aria/interactions`

* bump "@tanstack/react-virtual"

* add `ResizeObserver` polyfill, and enable it by default for tests

* mock `getBoundingClientRect`

Otherwise the virtualization tests don't work as expected because they
rely on the client rect which is not supported (or not correctly
measured) in JSDOM.
2024-05-02 14:41:58 +02:00
Robin Malfait 36616b217e Update minimal peer dependency version requirements for react and react-dom (#3131)
* require at least React 18

We already relied on React 18 for Headless UI v2, but now it's also
reflected in the package.json

* update changelog
2024-04-24 19:39:12 +02:00
Robin Malfait d03fbb19f5 Make the Combobox component nullable by default (#3064)
* remove `nullable` prop

* prevent selecting active option on blur

+ cleanup and adjust comments

* remove nullable from comments

* bump TypeScript to 5.4

This gives us `NoInfer<T>`!

* simplify types of `Combobox`

Now that `nullable` is gone, we can take another look at the type
definition. This in combination with the new `NoInfer` type makes types
drastically simpler and more correct.

* re-add `nullable` to prevent type issues

But let's mark it as deprecated to hint that something changed.

* update changelog

* improve `ByComparator` type

If we are just checking for `T extends null`, then
`{id:1,name:string}|null` will also be true and therefore we would
eventually return `string` instead of `"id" | "name"`.

To solve this, we first check if `NonNullable<T> extends never`, this
would be the case if `T` is `null`.

Otherwise, we know it's not just `null` but it can be something else
with or without `null`. To be sure, we use `keyof NonNullable<null>` to
get rid of the `null` part and to only keep the rest of the object (if
it's an object).

* ensure the `by` prop type handles `multiple` values correctly

This way the `by` prop will still compare single values that are present
inside the array.

This now also solves a pending TypeScript issue that we used to `//
@ts-expect-error` before.

* type uncontrolled `Combobox` components correctly

We have some tests that use uncontrolled components which means that we
can't infer the type from the `value` type.

* simplify `onChange` calls

Now that we don't infer the type when using the generic inside of
`onChange`, it means that we can use `onChange={setValue}` directly
because we don't have to worry about the updater function of `setValue`
anymore.

* correctly type `onChange`, by adding `null`

If you are in single value mode, then the `onChange` can (and will)
receive `null` as a value (when you clear the input field). We never
properly typed it so this fixes that.

In multiple value mode this won't happen, if anything the value will be
`[]` but not `null`.

* remove `nullable` prop from playground

* drop `nullable` mentions in tests
2024-03-29 15:41:12 +01:00
Jordan Pittman 680db08b00 Remove outdated esbuild deps 2024-02-06 15:04:02 -05:00
Robin Malfait a3276570d2 sync package-lock.json 2024-01-05 12:54:54 +01:00
Robin Malfait a73007388f Ensure playgrounds work + switch to npm workspaces (#2907)
* bump Next in playground

* convert legacy Link after Next.js bump

* update yarn.lock

* switch to npm workspaces

* move `packages/playground-*` to `playgrounds/*`

* use `npm` instead of `yarn`

* sync package-lock.json

* use node 20 for insiders releases
2024-01-03 14:26:12 +01:00