Commit Graph

101 Commits

Author SHA1 Message Date
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
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 8814b0eecd Add React 19 support (#3543)
This PR fixes an issue where `@headlessui/react` was not compatible with
React 19.

We made sure that accessing `ref`s is safe and works in React 18 and
React 19. We also made sure to include React 19 as a valid version in
the peer dependencies. For now, we also allowed the RC versions of React
and React DOM.
2024-10-25 13:50:58 +00: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
Philipp Spiess 49c081d199 2.1.3 - @headlessui/react 2024-08-23 15:49:35 +02:00
Robin Malfait a36380fdb6 2.1.2 - @headlessui/react 2024-07-05 18:13:14 +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 abd86fca3e 2.1.1 - @headlessui/react 2024-06-26 15:10:43 +02:00
Robin Malfait d60ed6a670 2.1.0 - @headlessui/react 2024-06-21 16:13:50 +02:00
Robin Malfait e8c766190d bump dependencies (#3247) 2024-05-28 12:33:09 +02:00
Robin Malfait 7be23e5c7e 2.0.4 - @headlessui/react 2024-05-25 00:30:50 +02:00
Jordan Pittman f513614ffe 2.0.3 - @headlessui/react 2024-05-07 14:14:50 -04:00
Robin Malfait 48cf712d80 2.0.2 - @headlessui/react 2024-05-07 19:19:19 +02:00
Jordan Pittman 2d5d35a533 2.0.1 - @headlessui/react 2024-05-06 15:07:53 -04:00
Robin Malfait fb131905b4 2.0.0 - @headlessui/react 2024-05-06 17:41:56 +02: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
Jordan Pittman 680db08b00 Remove outdated esbuild deps 2024-02-06 15:04:02 -05:00
Robin Malfait 33286d0c80 2.0.0-alpha.4 — @headlessui/react 2024-01-03 15:06:09 +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
Robin Malfait d94038f198 2.0.0-alpha.3 — @headlessui/react 2023-12-21 02:51:25 +01:00
Robin Malfait 13f4cd5a7f 2.0.0-alpha.2 — @headlessui/react 2023-12-21 00:32:50 +01:00
Robin Malfait 9f0b19f41f 2.0.0-alpha.1 — @headlessui/react 2023-12-20 20:12:39 +01:00
Robin Malfait e662f12398 2.0.0 Alpha prep (#2887)
* bump React & React DOM dependencies

* fix typo `TOmitableProps` → `TOmittableProps`

* bump prettier

* run prettier after prettier version bump

* bump TypeScript

* run prettier after TypeScript version bump

* enable `verbatimModuleSyntax`

This ensures all imported types are using the `type` keyword.

* add `type` to type related imports

* add common testing scenarios

Will be used in the new and existing components.

* add script to make Next.js happy

Right now Next.js does barrel file optimization and re-writing imports
to a real path in the `dist` folder. Most of those rewrites don't
actually exist because they have an assumption:

```js
import { FooBar } from '@headlessui/react'
```

is rewritten as:
```js
import { FooBar } from '@headlessui/react/dist/components/foo-bar/foo-bar'
```

This script will make sure these paths exist...

* improve `by` prop, introduce `useByComparator`

This hook has a default implementation when comparing objects. If the
object contains an `id`, then we will compare the objects by their
`id`'s without the user of the library needing to specify `by="id"`.

If the objects don't have an `id` prop, then the default is still to
compare by reference (unless specicified otherwise).

* sync yarn.lock

* rename `Features` to `HiddenFeatures` for `Hidden` component

* rename `Features` to `FocusTrapFeatures` in `FocusTrap` component

* rename `Features` to `RenderFeatures` in `render` util

* add `floating-ui` as a dependency + introduce internal floating related components

* bump Vue dependencies

* ensure scroll bar calculations can't go negative

* improve types in `@headlessui/vue`

* use snapshot tests for `Transition` tests in `@headlessui/vue`

* use snapshot tests for `portal` tests in `@headlessui/vue`

* rename `src/components/transitions/` to `src/components/transition/` (singular)

This is so that we can be consistent with the other components.

* drop custom `toMatchFormattedCss`, prefer snapshot tests instead

* use snapshot tests for `Label` tests in `@headlessui/vue`

* use snapshot tests for `Description` tests in `@headlessui/vue`

* sort exported components in tests for `@headlessui/vue`

* use snapshot tests in `@headlessui/tailwindcss`

* rename `mergeProps` to `mergePropsAdvanced`

This is a more complex version of a soon to be exported `mergeProps`
that we will be using in our components.

* do not expose `aria-labelledby` if it is only referencing itself

* expose boolean state as `kebab-case` instead of `camelCase`

These are the ones being exposed inside `data-headlessui-state="..."`

* expose boolean data attributes

A slot with `{active,focus,hover}` will be exposed as:
```html
<span data-headlessui-state="active focus hover"></span>
```

But also as boolean attributes:
```html
<span data-active data-focus data-hover></span>
```

* improve internal types for `className` in `render` util

* ensure we keep exposed data attributes into account when trying to forward them to the component inside the `Fragment`

* add small typescript type fix

This is internal code, and the public API is not influenced by this
`:any`. It does make TypeScript happy.

* introduce `mergeProps` util to be used in our components

This will help us to merge props, when event handlers are available they
will be merged by wrapping them in a function such that both (or more)
event handlers are called for the same `event`.

* add new internal `Modal` component

* fix: when using `Focus.Previous` with `activeIndex = -1` should start at the end

* prefer `window.scrollY` instead of `window.pageYOffset`

Because `window.pageYOffset` is deprecated.

* add `'use client'` directives on client only components

These components use hooks that won't work in server components and you
will receive an error otherwise.

* drop `import 'client-only'` in favor of the `'use client'` directive

* add React Aria dependencies

* pin beta dependencies

* prettier bump formatting

* improve TypeScript types in tests

* use new Jest matchers instead of deprecated ones

* improve typescript types in Vue

* prefer `useLabelledBy` and `useDescribedBy`

* add internal `DisabledProvider`

* add internal `IdProvider`

* add internal `useDidElementMove` hook

* add internal `useElementSize` hook

* add internal `useIsTouchDevice` hook

* add internal `useActivePress` hook

* use snapshot tests for `Description` tests

* use snapshot tests for `Label` tests

* use snapshot tests for `Portal` tests

* use snapshot tests for `render` tests

* add (private) `Tooltip` component

Currently this one is not ready yet, so its not publicly exposed yet.

* add internal `FormFields` component

This one adds a component to render (hidden) inputs for native form
support. It also ensures that form fields can be hoisted to the end of
the nearest `Field`. If the components are not inside a `Field` they
will be rendered in place.

* add new `Button` component

* add new `Checkbox` component

* add new `DataInteractive` component

* add new `Field` component

* add new `Fieldset` component

* add new `Legend` component

* add new `Input` component

* add new `Select` component

* add new `Textarea` component

* export new components

* WIP

* remove `within: true`

This only makes sense if anything inside the current element receives
focus, which is not the case for `input`, `select`, `textarea` or
`Radio/RadioOption`.

* group focus/hover/active hooks together

* conditionally link anchor panel

* immediately focus the container

* prevent premature disabling of `Listbox`'s floating integration

+ Track whether the button moved or not when disabling such that we can
  disable the transitions earlier.

* improve scroll locking on iOS

* skip hydration tests for now

* skip certain focus trap tests for now

* update CHANGELOG.md

* add missing requires

* drop unused `@ts-expect-error`

* ignore type issues in playgrounds

These playgrounds are mainly test playgrounds. Lower priority for now,
we will get back to them.

* add yarn resolutions to solve swc bug
2023-12-20 19:57:57 +01:00
Jordan Pittman c25e2e6036 Fix CJS types (#2880)
* Fix Vue type error

* Add separate CTS types

* Add “Are The Types Wrong” CLI

* wip

* Bump node versions in workflows

* wip

* wip

* wip

* yolo

* yolo (again?)

* wip

* wip
2023-12-12 11:43:34 -05:00
Robin Malfait f016dc51db Add virtual prop to Combobox component (#2740)
* type timezones in playground data

* add `@tanstack/react-virtual` and `@tanstack/vue-virtual`

* use latest stable Tailwind CSS version

* add Combobox with virtual prop example

* add `virtual` prop to `Combobox`

Co-authored-by: Jordan Pittman <jordan@cryptica.me>

* add tests for `virtual` prop

- Also wrap `click` helpers in `act` for React (use `rawClick` without
  `act` in tests related to `Transition`)

* update changelog

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
2023-09-15 14:29:18 +02:00
Jordan Pittman 0b8bd32865 1.7.17 - @headlessui/react 2023-08-17 10:58:55 -04:00
Robin Malfait 0501475575 1.7.16 - @headlessui/react 2023-07-27 16:23:46 +02:00
Robin Malfait a1d7c69b69 1.7.15 - @headlessui/react 2023-06-01 12:40:00 +02:00
Mateusz Burzyński 0505e92b83 Move types condition to the front (#2469)
* move `types` condition to the front

* Update changelog

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
2023-05-01 12:21:28 -04:00
Robin Malfait c5d2e3b053 1.7.14 - @headlessui/react 2023-04-12 19:05:04 +02:00
Robin Malfait df61edcf3e 1.7.13 - @headlessui/react 2023-03-03 21:09:06 +01:00
Robin Malfait 885f446e4e 1.7.12 - @headlessui/react 2023-02-24 16:29:14 +01:00
Robin Malfait 2c2a22bd4f 1.7.11 - @headlessui/react 2023-02-15 15:10:39 +01:00
Robin Malfait 597256bd9e 1.7.10 - @headlessui/react 2023-02-06 12:33:23 +01:00
Robin Malfait 885fa6aedd 1.7.9 - @headlessui/react 2023-02-03 17:40:08 +01:00
Robin Malfait b3a0ccb2f8 1.7.8 - @headlessui/react 2023-01-27 17:24:04 +01:00
Robin Malfait b7c9e57343 1.7.7 - @headlessui/react 2022-12-16 17:38:32 +01:00
Robin Malfait 92e9302020 1.7.6 - @headlessui/react 2022-12-15 17:35:18 +01:00
Robin Malfait 326a43f73f 1.7.5 - @headlessui/react 2022-12-08 23:17:01 +01:00
Robin Malfait 74e7b43781 1.7.4 2022-11-03 16:21:15 +01:00