Commit Graph

421 Commits

Author SHA1 Message Date
Jordan Pittman 2f99644ed7 Don't break overflow when multiple dialogs are open at the same time (#2215)
* Fix overflow when swapping dialogs that use transition

* Refactor

* refactor

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Inline shim for ESM support

Until the official package adds an ESM version with a wildcard import we can’t use it. This version was copied from Remix Router

* Add dialog shadow root examples

* Fix SSR error

* Add repro for iOS scrolling issue

* Try to fix vercel build

idk what’s wrong here

* Update repro

A transition is required to delay closing enough to demonstrate the bug

* Port global dialog state to Vue

* Add dialog test to Vue

* wip

* wip

* Workaround bug

This shouldn’t happen at all and we need to find the source of the bug but this should “fix” things for the time being

* wip

* Rebuild overflow locking with simpler API

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update deps

* wip

* simplify

* Port to Vue

* wip

* wip

* Tweak tests

* Update changelog

* Ensure meta callbacks are cleaned up

* cleanup

* wip
2023-02-01 16:08:34 -05:00
Jordan Pittman e95f664a36 Fix SSR tab hydration when using Strict Mode in development (#2231)
* Work on SSR tests for react

* Use React internals to count tabs and panels

React’s double rendering in strict mode in development makes SSR + hydration matching impossible without reaching into internals. This is unfortunate but the way react works. Production builds of React are unaffected by this but still require a consistent mechanism that works so in that case we use Symbols just like we do in SSR.

* Update changelog
2023-01-31 15:40:13 -05:00
Adventune f2d9ea08b6 fix(@headless-react / @headless-vue): update WAI-ARIA reference links (#2230)
* fix(@headlessui/react): WAI-ARIA reference links

* fix(@headlessui-vue): Fix WAI-ARIA reference links

---------

Co-authored-by: lumilno <nooa.lumilaakso@gmail.com>
2023-01-31 11:42:02 -05:00
Robin Malfait d8b263cb42 Fix shadow-root bug closing Dialog containers (#2217)
* ensure we consider `html > *` as valid containers as well

* update changelog
2023-01-27 14:49:43 +01:00
Robin Malfait 2f13496acb Allow setting tabIndex on the Tab.Panel (#2214)
* allow setting `tabIndex` on the `Tab.Panel`

* update changelog
2023-01-26 17:12:11 +01:00
Robin Malfait e2294f5280 Improve Tabs wrapping around when controlling the component and overflowing the selectedIndex (#2213)
* ensure chaning the `selectedIndex` tabs properly wraps around

We never want to use and index that doesn't map to a proper tab.

This commit also makes the implementation similar for both React and
Vue.

* add tests to prove the underflow and overflow wrapping

* drop updating the index manually

This is already adjusted when tabs change internally. You can still
manually change it of course, but for these tests that doesn't matter
and cause different results.

* update changelog
2023-01-26 15:55:55 +01:00
Robin Malfait dbcfb23bc3 Fix FocusTrap in Dialog when there is only 1 focusable element (#2172)
* add tests to guarantee `FocusTrap` with a single element works as expected

* it should keep the focus in the Dialog

Even if there is only 1 element. We were skipping the current active
element so the container didn't have any elements anymore and just
continued to the next focusable element in line. This will prevent that
and ensure that we can only skip elements if there are multiple ones.

* update changelog
2023-01-25 13:18:51 +01:00
Robin Malfait 6f205f07d2 Fix crash when reading headlessuiFocusGuard of relatedTarget in the FocusTrap component (#2203)
* ensure `relatedTarget` is an `HTMLElement`

Or in other words, Robin trust the type system...

I was assuming that this was always an `HTMLElement` or `null` but
that's not the case. Just using `e.relatedTarget` shows that `dataset`
is not always available.

* update changelog
2023-01-24 20:08:38 +01:00
Robin Malfait 1d94d15c79 Improve Combobox accessibility (#2153)
* add the `aria-autocomplete` attribute

* drop the `aria-activedescendant` attribute on the `Combobox.Options` component

It is only required on the `Combobox.Input` component.

* improve triggering VoiceOver when opening the `Combobox`

We do this by mutating the `input` value for a split second to trigger a
change that VoiceOver will pick up. We will also ensure to restore the
value and the selection / cursor position so that the end user won't
notice a difference at all.

* update changelog

Fixes: #2129
Co-authored-by: Andrea Fercia <a.fercia@gmail.com>
2023-01-24 18:10:14 +01:00
Thet Naing Tun 676de1668b cleanup unused import (#2184) 2023-01-16 12:54:17 +01:00
Jordan Pittman aac78d52b7 Don’t overwrite classes during SSR when rendering fragments (#2173)
* Refactor SSR test helpers

* Add SSR tests for transition

* Don’t overwrite classes during SSR when rendering fragments

* Update changelog
2023-01-12 11:17:51 -05:00
Robin Malfait 08c083768f Fix failed to removeChild on Node bug (#2164)
* fix `failed to removeChild on Node` bug

Let's introduce a bit more defensive code to make sure that the code
doesn't crash when we don't pass a `Node` to `removeChild`

* update changelog
2023-01-09 12:11:45 +01:00
Robin Malfait c687c2e165 Fix false positive warning when using <Popover.Button /> in React 17 (#2163)
* fix false positive warning when using <Popover.Button /> in React 17

* update changelog
2023-01-09 11:43:12 +01:00
Robin Malfait 6fa6074cd5 Fix Tab key with non focusable elements in Popover.Panel (#2147)
* fix `Tab` key with non focusable elements in `Popover.Panel`

Fixes: #2112

* ensure all Dialog tests are running

* update changelog
2023-01-04 19:08:47 +01:00
Robin Malfait 69b953ae9d Fix false positive warning about using multiple <Popover.Button> components (#2146)
* fix false positive warning about using multiple `<Popover.Button>` components

Fixes: #2143

* update changelog
2023-01-04 16:26:35 +01:00
Robin Malfait e8b7b7fe60 Fix arrow key handling in Tab (after DOM order changes) (#2145)
* detect change in `Tab` order

This will guarantee that when you are using your arrow keys that the
previous / next values are the correct ones instead of the "old" values
before the order change happened.

Fixes: #2131

* update changelog
2023-01-04 14:59:30 +01:00
Jordan Pittman 865bd57357 Fix SSR tab rendering on React 17 (#2102)
* Allow clicks inside dialog panel when target is inside shadow root

* Introduce resettable “server” state

This will aid in testing

* Add SSR and hydration tests for react

* Fix server rendering of Tabs on React 17

* Fix CS

* Skip hydration tests

* Tweak SSR implementation in Vue

* Update changelog
2022-12-16 12:55:51 -05:00
Robin Malfait 599428373e improve scrolling to in-page location 2022-12-16 17:31:55 +01:00
Robin Malfait 45fde141d2 improve scroll offset 2022-12-15 16:46:13 +01:00
Robin Malfait 3cb80795ae update aria-haspopup to use the correct role (#2101)
`aria-haspopup` should now contain the corresponding role instead of
just true or false. The `aria-haspopup="true"` is considered a `menu`
now.

Context: https://w3c.github.io/aria/#aria-haspopup

Fixes: #2099
2022-12-15 16:32:14 +01:00
Robin Malfait 962528c216 Improve scroll locking on iOS (#2100)
* improve types for addEventListener inside disposables

* improve scroll locking

Instead of using the "simple" hack with the `position: fixed;` we now
went back to the `touchmove` implementation.

The `position: fixed;` causes some annoying issues. For starters, on iOS
you will now get a strange gap (due to safe areas). Some applications
also saw "blank" screens based on how the page was implemented.

We also saw some issues internally, where clicking changing the scroll
position on the main page from within the Dialog.

Think about something along the lines of:
```html
<a href="#interesting-link-on-the-current-page">Interesting link on the page</a>
```

This doesn't work becauase the page is now fixed, and there is nothing
to scroll...

Instead, we now use the `touchmove` again. The problem with this last
time was that this disabled _all_ touch move events. This is obviously
not good.

Luckily, we already have a concept of "safe containers". This is what we
use for the `outside click` behaviour as well. Basically in a Dialog,
your `Dialog.Panel` is the safe container. But also third party DOM
elements that are rendered inside that Panel (or as a sibling of the
Dialog, but not your main app).

We can re-use this knowledge of "safe containers", and only cancel the
`touchmove` behaviour if this didn't happen in any of the safe
containers.

* update changelog
2022-12-15 16:09:33 +01:00
Robin Malfait d31bb5c08e Fix FocusTrap escape due to strange tabindex values (#2093)
* sort DOM nodes using tabIndex first

It will still keep the same DOM order if tabIndex matches, thanks to
stable sorts!

* refactor `focusIn` API

All the arguments resulted in usage like `focusIn(container,
Focus.First, true, null)`, and to make things worse, we need to add
something else to this list in the future.

Instead, let's keep the `container` and the type of `Focus` as known
params, all the other things can sit in an options object.

* fix FocusTrap escape due to strange tabindex values

This code will now ensure that we can't escape the FocusTrap if you use
`<tab>` and you happen to tab to an element outside of the FocusTrap
because the next item in line happens to be outside of the FocusTrap and
we never hit any of the focus guard elements.

How it works is as follows:

1. The `onBlur` is implemented on the `FocusTrap` itself, this will give
   us some information in the event itself.
   - `e.target` is the element that is being blurred (think of it as `from`)
   - `e.currentTarget` is the element with the event listener (the dialog)
   - `e.relatedTarget` is the element we are going to (think of it as `to`)
2. If the blur happened due to a `<tab>` or `<shift>+<tab>`, then we
   will move focus back inside the FocusTrap, and go from the `e.target`
   to the next or previous value.
3. If the blur happened programmatically (so no tab keys are involved,
   aka no direction is known), then the focus is restored to the
   `e.target` value.

Fixes: #1656

* update changelog
2022-12-14 16:26:38 +01:00
Robin Malfait 1f2de63b40 Fix displayValue syncing when Combobox.Input is unmounted and re-mounted in different trees (#2090)
* simplify `currentDisplayValue` calculation

Always calculate the currentDisplayValue, and only apply it if the user
is not typing. In all other cases it can be applied (e.g.: when the
value changes from the outside, inside or on reset)

* update changelog
2022-12-12 19:16:46 +01:00
Robin Malfait 46754e637c Fix regression where displayValue crashes (#2087)
* fix regression where `displayValue` crashes

It regressed in the sense that it now uses `displayValue` for the
`defaultValue` as well, but if nothing is passed it would crash.

Right now, it makes sure to only run the displayValue value on the
actual value and the actual default value if they are not undefined.

Note: if your displayValue is implemented like `(value) => value.name`,
and your `value` is passed as `null`, it will still crash (as expected)
because then you are in charge of rendering something else than null. If
we would "fix" this, then no value can be rendered instead of `null`.

Fixes: #2084

* update changelog
2022-12-12 16:43:57 +01:00
Jordan Pittman 724ee378fc Allow clicks inside dialog panel when target is inside shadow root (#2079)
* Allow clicks inside dialog panel when target is inside shadow root

* fixup

* Update changelog
2022-12-08 16:37:01 -05:00
Jordan Pittman 2e941f85dd Ignore mouse move/leave events when the cursor hasn’t moved (#2069)
* Ignore mouse move/leave events when the cursor hasn’t moved

A mouse enter / leave event where the cursor hasn’t moved happen only because of:
- Scrolling
- The container moved

* Fix linting errors

* Update changelog

* wip

* Fix tests

* fix linting error

* Tweak tests to bypass tracked pointer checks

* Fixup

* Add stuff

* Fix build script

* fix stuff

* wip
2022-12-07 13:50:57 -05:00
Robin Malfait a6dea8af4b Fix Dialog unmounting problem due to incorrect transitioncancel event in the Transition component on Android (#2071)
* remove `transitioncancel` logic

On Desktop Sarai, Chrome, and on mobile iOS Safari the
`transitioncancel` is never called on outside click of the Dialog.
However, on mobile Android Chrome it _is_ called, and the
`transitionend` is never triggered for _some_ reason.

According to the MDN docs:
> If the transitioncancel event is fired, the transitionend event will not fire.
>
> — https://developer.mozilla.org/en-US/docs/Web/API/Element/transitioncancel_event

When testing this, I never got into the `transitionend` when I got into
the `transitioncancel` first. But, once I removed the `transitioncancel`
logic, the `transitionend` code _was_ being called.

The code is now both simpler, and works again. The nice part is that we
never did anything with the `cancel` event. We marked it as done using
the `Reason.Cancelled` and that's about it.

* cleanup transition completion `Reason`

* update changelog
2022-12-06 17:22:27 +01:00
Robin Malfait 9ef269e936 Add null as a valid type for Listbox and Combobox in Vue (#2064)
* add `null` as a valid type for Listbox and Combobox in Vue

* update changelog
2022-12-05 15:40:19 +01:00
Robin Malfait 219901c84f Allow passing in your own id prop (#2060)
* accept `id` as a prop where it is currently hardcoded (React)

Continuation of #2020

Co-authored-by: Olivier Louvignes <olivier@mgcrea.io>

* accept `id` as a prop where it is currently hardcoded (Vue)

* update changelog

* apply React's hook rules

Co-authored-by: Olivier Louvignes <olivier@mgcrea.io>
2022-12-02 23:56:33 +01:00
Robin Malfait 7509124e56 Apply enter and enterFrom classes in SSR for Transition component (#2059)
* apply `enter` and `enterFrom` classes in SSR

This only happens on the server when `show=true` and `appear=true`. This
will guarantee that the `class` is already set to the correct value
before the transition happens.

It worked before if you moved your classes from `enterFrom` to
`className` because that prop was SSR'd.

* update changelog
2022-12-02 16:00:44 +01:00
Robin Malfait a0bcbae93a Fix crash when using multiple mode without value prop (uncontrolled) for Listbox and Combobox components (#2058)
* ensure `value` in `multiple` mode is always an array

In case nothing or `undefined` was passed.

* update changelog
2022-12-02 15:18:31 +01:00
Robin Malfait 4da0b3aba9 Improve syncing of the Combobox.Input value (#2042)
* make combobox playgrounds in React and Vue similar

* syncing of the input should happen when the value changes internally or externally

I also got rid of the manually dispatching of the change event if the
value changes from internally.

I think the correct mental model is:
- That the `Combobox.Input` value should change if the selected value
  changes from the outside or from the inside.
  - Note: It should _not_ do that if you are currently typing (once you
    choose a new value it will re-sync, once you reset (escape / outside
    click) it will also sync again).
- The `onChange`/`onInput` of the `Combobox.Input` itself should only be
  called if you as the user type something. Not when the value is
  "synced" based on the selected value. We were currently manually
  dispatching events which works (to a certain extend) but smelled a bit
  fishy to me.

The manual dispatching of events tried to solve an issue
(https://github.com/tailwindlabs/headlessui/issues/1875), but I think
this can be solved in 2 other ways that make a bit more sense:

1. (Today) Use the `onBlur` on the input to reset the `query` value to filter
   options.
2. (In the future)  Use an exposed `onClose` (or similar) event to reset
   your `query` value.

* update changelog

* ignore flakey test
2022-11-23 16:16:10 +01:00
Robin Malfait 8b4363d7a4 Ensure shift+home and shift+end works as expected in the Combobox.Input component (#2024)
* ensure shift+home and shift+end works as expected

While testing with a normal input, the Home and End keys don't do
anything on their own, so therefore using them to go to the first or
last item respectively is still a good solution.

However, shift+home and shift+end will do _something_, it will select
the text in the input.

* update changelog
2022-11-17 15:00:26 +01:00
Robin Malfait a4c7cab8a7 Ensure Popover doesn't crash when focus is going to window (#2019)
* ensure Popover doesn't crash when `focus` is going to `window`

* update changelog
2022-11-16 15:11:01 +01:00
Robin Malfait f0dd25fbab Add warning when using <Popover.Button /> multiple times (#2007)
* add warning when using `<Popover.Button />` multiple times

* update changelog
2022-11-10 15:21:24 +01:00
Robin Malfait c0f0d43383 Reset form-like components when the parent <form> resets (#2004)
* add reset button to form example

* refactor React Listbox

This splitsup the raw `[state, dispatch]` to separate `useActions` and `useData` hooks.

This allows us to make the actions themselves simpler and include logic
that doesn't really belong in the reducer itself.

This also allows us to expose data via the `useData` hook that doesn't
belong in the state exposed from the `useReducer` hook.

E.g.: we used to store a `propsRef` from the root `Listbox`, and update
the ref with the new props in a `useEffect`. Now, we will just expose
that information directly via the `useData` hook. This simplifies the
code, removes useEffect's and so on.

* refactor Tabs, ensure function reference stays the same

If the `isControlled` value changes, then the references to all the
functions changed. Now they won't because of the `useEvent` hooks.

* type the actions abg similar to how we type the data bag

* refactor RadioGroup to use useData/useActions hooks

* reset Listbox to defaultValue on form reset

* reset Combobox to defaultValue on form reset

* reset RadioGroup to defaultValue on form reset

* reset Switch to defaultChecked on form reset

* port combinations/form playground example to Vue

* update changelog
2022-11-09 23:39:23 +01:00
Robin Malfait f424aa20db Add client-only to mark everything as client components (#1981)
* add `client-only` to mark everything as client components

This should improve the error messages when using Headless UI in a
Next.js 13+ repo instead of getting a cryptic error message that
`createContext` doesn't exist.

* update changelog
2022-11-03 16:17:22 +01:00
Ernest dce2a1af07 fix: the order of compositionend events varies by browser (#1890)
* fix: the order of compositionend events varies by browser

* apply our style conventions

Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2022-11-03 16:10:28 +01:00
Kristóf Poduszló f66f4926c4 fix(RadioGroup): defaultValue type definition (#1920) 2022-10-14 15:11:52 +02:00
Jordan Pittman ab78fbd91e Fire user’s onChange handler when we update the combobox input value internally (#1916)
* Fire user’s onChange handler when we update the input value internally

* Update changelog

* Fix CS
2022-10-10 15:17:52 -04:00
Robin Malfait 0e147a0c75 Fix useOutsideClick, add improvements for ShadowDOM (#1914)
* Fix `useOutsideClick` not closing when clicking in ShadowDOM

https://github.com/tailwindlabs/headlessui/pull/1876#issuecomment-1264742366

* use `getRootNode` in `useOutsideClick` for Vue

* update changelog

* run prettier

Co-authored-by: Theodore Messinezis <7229472+theomessin@users.noreply.github.com>
2022-10-10 17:02:51 +02:00
Jordan Pittman 1127a55a76 Warn when changing Combobox between controlled and uncontrolled (#1878) 2022-10-10 10:41:15 -04:00
Robin Malfait 83a5f456c3 Expose close function for Menu and Menu.Item components (#1897)
* expose `close` function for `Menu` and `Menu.Item` components

The `Menu` will already automatically close if you invoke the
`Menu.Item` (which is typically an `a` or a `button`). However you have
control over this, so if you add an explicit `onClick={e =>
e.preventDefault()}` then we respect that and don't execute the default
behavior, ergo closing the menu.

The problem occurs when you are using another component like the Inertia
`Link` component, that does have this `e.preventDefault()` built-in to
guarantee SPA-like page transitions without refreshing the browser.
Because of this, the menu will never close (unless you go to a totally
different page where the menu is not present of course).

This is where the explicit `close` function comes in, now you can use
that function to "force" close a menu, if your 3rd party tool already
bypassed the default behaviour.

This API is also how we do it in the `Popover` component for scenario's
where you can't rely on the default behaviour.

* update changelog
2022-10-04 15:49:20 +02:00
Robin Malfait af68a347fe Fix <Popover.Button as={Fragment} /> crash (#1889)
* prevent infinite loop

When you use `as={Fragment}` an unmount and remount can happen. This
means that the `ref` gets called with `null` for the unmount and
`HTMLButtonElement` for the mount.

This keeps toggling which results in an infinite loop and eventually a
Maximum callback size exceeded issue.

This ensures that we only set the button if we have a button.

* update changelog
2022-09-30 21:48:35 +02:00
Robin Malfait 9e9c08c7cc Fix Tab incorrectly activating on focus event (#1887)
* rework Tabs so that they don't change on focus

The "change on focus" was an incorrect implementation detail that made
it a bit easier but this causes a problem as seen in #1858.

If you want to conditionally check if you want to change the tab or note
(e.g. by using `window.confirm`) then the focus is lost while the popup
is shown. Regardless of your choice, the browser will re-focus the Tab
therefore asking you *again* what you want to do.

This fixes that by only activating the tab if needed while using arrow
keys or when you click the tab (not when it is focused).

* update changelog
2022-09-30 13:51:14 +02:00
Robin Malfait e7cfb05036 Fix useOutsideClick swallowing events inside ShadowDOM (#1886)
* check inside shadow dom for use-outside-click

* update changelog

Co-authored-by: Raphael Melloni <raphael.melloni@nortal.com>
2022-09-29 12:14:01 +02:00
Robin Malfait b032679ed0 Improve Portal detection for Popover components (#1842)
* improve Popover heuristics for detecting being inside a Portal

* improve performance of checks

* make it work in Vue

* verify behaviour in tests
2022-09-28 14:16:59 +02:00
Jordan Pittman 060f37b667 Fix use of undefined and displayValue in Combobox (#1865)
* Work around Vue multi-source + undefined bug

* Update changelog
2022-09-19 09:56:57 -04:00
Robin Malfait b346736659 Ensure we handle the static prop in Tab.Panel components correctly (#1856)
* ensure we handle `static` panels

* update changelog
2022-09-15 18:26:13 +02:00
Ernest e1f3ef862a Prevent option selection in ComboboxInput while composing (#1850)
* Fix should do nothing when event is fired within a composing session

* update changelog

* link to PR instead of issue

Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2022-09-14 15:01:34 +02:00