Skip to content

Commit

Permalink
Cleanup process in Combobox component when using virtualization (#3495
Browse files Browse the repository at this point in the history
)

This PR is a different approach compared to #3487. 

Instead of checking whether we are in a test environment (specifically
in a Jest environment), I think we can just get rid of the check
entirely and use the virtualizer in all environments.

This will remove an unnecessary check for `process` being available and
gets rid of `process` entirely. It also fixes an issue that #3487 tries
to solve where `process` is available, but `process.env` is not.

Closes: #3487
  • Loading branch information
RobinMalfait authored Sep 27, 2024
1 parent 63daa86 commit 02b43d0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Ensure `Element` is available before polyfilling to prevent crashes in non-browser environments ([#3493](https://github.com/tailwindlabs/headlessui/pull/3493))
- Fix crash when using `instanceof HTMLElement` in some environments ([#3494](https://github.com/tailwindlabs/headlessui/pull/3494))
- Cleanup `process` in Combobox component when using virtualization ([#3495](https://github.com/tailwindlabs/headlessui/pull/3495))

## [2.1.8] - 2024-09-12

Expand Down
21 changes: 7 additions & 14 deletions packages/@headlessui-react/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -532,21 +532,14 @@ function VirtualProvider(props: {
return
}

// Scroll to the active index
{
// Ignore this when we are in a test environment
if (typeof process !== 'undefined' && process.env.JEST_WORKER_ID !== undefined) {
return
}

// Do not scroll when the mouse/pointer is being used
if (data.activationTrigger === ActivationTrigger.Pointer) {
return
}
// Do not scroll when the mouse/pointer is being used
if (data.activationTrigger === ActivationTrigger.Pointer) {
return
}

if (data.activeOptionIndex !== null && options.length > data.activeOptionIndex) {
virtualizer.scrollToIndex(data.activeOptionIndex)
}
// Scroll to the active index
if (data.activeOptionIndex !== null && options.length > data.activeOptionIndex) {
virtualizer.scrollToIndex(data.activeOptionIndex)
}
}}
>
Expand Down
1 change: 1 addition & 0 deletions packages/@headlessui-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Cancel outside click behavior on touch devices when scrolling ([#3266](https://github.com/tailwindlabs/headlessui/pull/3266))
- Fix restoring focus to correct element when closing `Dialog` component ([#3365](https://github.com/tailwindlabs/headlessui/pull/3365))
- Cleanup `process` in Combobox component when using virtualization ([#3495](https://github.com/tailwindlabs/headlessui/pull/3495))

## [1.7.22] - 2024-05-08

Expand Down
27 changes: 10 additions & 17 deletions packages/@headlessui-vue/src/components/combobox/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,17 @@ let VirtualProvider = defineComponent({
return
}

// Scroll to the active index
{
// Ignore this when we are in a test environment
if (typeof process !== 'undefined' && process.env.JEST_WORKER_ID !== undefined) {
return
}

// Do not scroll when the mouse/pointer is being used
if (api.activationTrigger.value === ActivationTrigger.Pointer) {
return
}
// Do not scroll when the mouse/pointer is being used
if (api.activationTrigger.value === ActivationTrigger.Pointer) {
return
}

if (
api.activeOptionIndex.value !== null &&
api.virtual.value!.options.length > api.activeOptionIndex.value
) {
virtualizer.value.scrollToIndex(api.activeOptionIndex.value)
}
// Scroll to the active index
if (
api.activeOptionIndex.value !== null &&
api.virtual.value!.options.length > api.activeOptionIndex.value
) {
virtualizer.value.scrollToIndex(api.activeOptionIndex.value)
}
},
},
Expand Down

0 comments on commit 02b43d0

Please sign in to comment.