Commit Graph

93 Commits

Author SHA1 Message Date
Robin Malfait b949b4bbfa add missing imports
Closes: #255

Co-authored-by: Pier-Luc Gendreau <Zertz@users.noreply.github.com>
2021-04-02 15:55:15 +02:00
Robin Malfait 958e3ea8c6 bug fixes (#261)
* apply re-focus bug fix to Popover

* force focus in Menu.Items from within Menu.Items component itself

* force focus in Listbox.Options from within Listbox.Options component itself

* fix undefined values in id's

We were setting the element in state, but updates to the id were not taken into account

* update the caniuse db

* ensure useInertOthers works in multiple places

Previously each hook call would take care of the whole tree. However
when multiple calls to this hook are happening we need to make sure that
you are not removing the aria-hidden when another hook is still used.

This will fix that by keeping track of a list of "interactable" items,
and updating the parents (root of the body) accordingly.

* add the concept of a Stack

When you are rendering a Dialog, we will make sure that this Dialog is
rendered inside a Portal. However, when you are also rendering a Menu,
there is a chance that your Menu doesn't fit within the Dialog,
therefore you will likely render the Menu.Items inside a Portal so that
you can style it as if it is rendered inside but overflows the Dialog
correctly.

This introduces an interesting/annoying problem. Your Menu.Items are now
rendered in a Portal, as a *sibling* to the Dialog. This means that
autoFocus, focusTrap, ... all these features don't work as expected.

Introducing this Stack will allow us to register DOM nodes into a list
of contains that we consider being part of the main container. In other
words, the sibling Menu.Items will now be considered part of the Dialog.
Even though it is rendered *outside* of the Dialog.

This concept also allows for some fun stuff, for example, nesting
Dialog's is no problem with this approach. Dialogs are technically
rendered as siblings in the Portal, but the FocusTrap, and all that just
works as expected.

* capture keyboard events in the capturing phase

This will allow us to use event.stopPropagation() in the code (which
will be required, probably) but still see the keystrokes in the
playground.

* stop propagating keyboard events

This looks a bit silly, and ideally we can solve this in a more elegant
way. However when you nest a Menu inside a Dialog, both of those
components have a `close on escape` functionality built in. However when
your Menu is open, and you press escape, you only want to close the
Menu, not the Dialog. Therefore if we `event.stopPropagation()` it
allows us to stop the `escape` keystroke in the Menu from reaching all
the way to the Dialog itself.

* update Dialog example that showcases nested Dialogs, and nested Menu

* update changelog
2021-04-02 15:55:15 +02:00
Robin Malfait f4291112c5 Class name functions (#257)
* allow className to be a function

Every component that accepts a className should be able to pass in a
function. This function will retrieve the render prop arg for this
component. The function should resolve to a string in the end.

This makes the API a bit nicer if you just need to change the classNames
based on some internal state.

E.g.:

```js
// Before
<Menu.Button as={Fragment}>
  {({ open }) => (
    <button className={open ? 'font-bold' : 'font-normal'}>
      Hello
    </button>
  )}
</Menu.Button>

// After
<Menu.Button className={({ open }) => open ? 'font-bold' : 'font-normal'}>
  Hello
</Menu.Button>
```

* cleanup types

* merge React imports

* update changelog
2021-04-02 15:55:15 +02:00
Robin Malfait 4e5190d65d Fix refocus button bug (#256)
* fix outside click on span inside button works as expected

We have `outside click` behaviour implemented. Whenever the target
element is focusable we make sure that the newly clicked/focused element
stays focused. If it is not a focusable element we will make sure that
the Menu/Listbox button is re-focused so that screenreader users don't
get confused.

This is all fine, but it turns out that when you have a button with a
span, and you click on the span, then the event.target will be that
span. The span itself is not focusable of course, but the button will
get the focus. This results in the Menu/Listbox button being re-focused
which is incorrect.

For this we will introduce a FocusableMode on the `isFocusableElement`,
we will have a `Strict` mode, which means the actual element should be
focusable. And a `Loose` mode, which means that the actual element can
be inside a focusable element. E.g.: A span within a button.

* rename menu to listbox

Copy paste can be fun sometimes

* update changelog
2021-04-02 15:55:15 +02:00
Robin Malfait e43dd5b012 assign displayName instead of name (#247)
This allows us to read a custom `displayName`, and we can default to a
`name`. React Devtools will still be able to read this information.

Fixes: #246
2021-04-02 15:55:15 +02:00
Alexander Bluhm e53a562d9f Added new examples for switch components (React, Vue) (#245)
* Added new example for switch component (React)

* Added new example for switch component (Vue)
2021-04-02 15:55:15 +02:00
Robin Malfait 3222548bab prevent unnecessary re-renders when the state is already correct 2021-04-02 15:55:15 +02:00
Robin Malfait 648a2843e6 Multiple new components (#220)
* add Disclosure component

* expose the Disclosure component

* add Disclosure example component page

* temporary fix selector because of JSDOM bug

* add useFocusTrap hook

* add FocusTrap component

* expose FocusTrap

* add Dialog component

* add Dialog example component page

* expose Dialog

* random cleanup

* make TypeScript a bit more happy

* add Switch.Description component for React

* add Switch.Description component for Vue

* ensure focus event is triggered on click when element is focusable

* remove Dialog.Button and Dialog.Panel from accessibility assertions

* add Portal component

* expose Portal

* always render Dialog in a Portal

* add useInertOthers hook

This will allow us to mark everything but the current ref as "inert".
This is important for screenreaders, to ensure that screenreaders and
assistive technology can't interact with other content but the current
ref.

This implementation is not ideal yet. It doesn't take into account that
you can use the hook in 2 different components. For now this is fine,
since we only use it in a Dialog and you should also probably only have
a single Dialog open at a time.

Will improve this in the future!

* use the useInertOthers hook

* add scroll lock to the dialog

* ensure we respect autoFocus on form elements within the Dialog

If we have an autoFocus on an input, that input will receive focus. Once
we try to focus the first focusable element in the Dialog this could be
lead to unwanted behaviour. Therefore we check if the focus already is
within the Dialog, if it is, keep it like that.

* only mark aria-modal when Dialog is open

* add initialFocus option to Dialog, FocusTrap & useFocusTrap

* add tests and a few fixes for the initialFocusRef functionality

* forward ref to underlying Dialog component

* close Dialog when it becomes hidden

Could happen when this is in md:hidden for example

* prevent infinite loop

When we `Tab` in a FocusTrap it will try and focus the Next element. If
we are in a state where none of the elements inside the FocusTrap can be
focused, then we keep trying to focus the next one in line. This results
in an infinite loop...

To mitigate this issue, we check if we looped around, if we did, it
means that we tried all the other focusable elements, therefore we can
stop.

* isIntersecting doesn't work in every scenario

When page is scrollable, when dialog is translated of the page. Now just checking for sizes, which should be enough for md:hiden cases

* render Portal contents in a div

Otherwise you can't use multiple Portal components if you render multiple children inside each Portal

* ensure the props bag is typed

* add getByText and assertContainsActiveElement helpers

* add Popover component

* expose Popover

* add Popover example component page

* add quick checks to prevent useless renders

* drop incorrect close function

* update Changelog

* make test error more readable when comparing DOM nodes

* actually call .focus() on the element

This ensures that the document.activeElement becomes the focused element.

* improve useSyncRefs, because ...refs is *always* different

* add dedicated focus management utilities

* refactor useFocusTrap, use focus management utilities

* fix regression while using outside click

There might be a chance that you didn't even notice this *bug*. The idea
is that when you click outside, that the Menu or Listbox closes. However
there is another step that happens:

1. When you click on a focusable item, keep the focus on that item.
2. When you click on a non-focusable item, move focus back to the
   Menu.Button or Listbox.Button

We broke part 2, we never returned to the Menu.Button or Listbox.Button.
This is (might) be important for screenreaders so that they don't "get lost",
because if you click on a non-focusable item, the document.body becomes
the active element. Confusing.

* add outside-click to Dialog itself

* update docs
2021-04-02 15:55:14 +02:00
Robin Malfait 67d09e1c27 bump versions 2021-04-02 15:22:35 +02:00
Robin Malfait 5da253b831 backfill fixes (#299)
* fix unique symbol error (#248)

* Vue breaking change (#279)

* bump Vue

* ensure we reference the vite.config.js

* fix event name casing

Vue broke this in a 3.0.5 release, it still worked in 3.0.4.

Fixes: #267

* handle throwing while rendering a better in tests
2021-04-02 15:18:47 +02:00
Robin Malfait 71730fea12 bump version 2021-02-11 19:39:44 +01:00
Robin Malfait bb68793f08 correctly handle TypeScript render abstractions 2021-02-11 19:19:12 +01:00
Robin Malfait d557d50139 ensure correct path to types 2021-02-07 12:08:53 +01:00
Robin Malfait 43effbbe29 v0.3.0 2021-02-06 16:30:08 +01:00
Robin Malfait 3171f4f701 add changelog (#232)
* update docs

* add CHANGELOG
2021-02-06 16:24:43 +01:00
Robin Malfait e9c92924cf Add disabled to listbox (#229)
* allow to press on an element without focusing it first

* add disabled option to the Listbox component
2021-02-06 00:09:13 +01:00
Robin Malfait 9e0df9ee39 ensure valid Menu accessibility tree (#228) 2021-02-05 21:01:28 +01:00
Robin Malfait 9891fa31b9 add better implementation to detect the FPS 2021-02-05 18:15:51 +01:00
Robin Malfait 9e05bbe8d8 ensure the active MenuItem is scrolled into view
Fixes: #227
2021-02-05 18:15:07 +01:00
Robin Malfait ef00732685 cleanup and consistency (#213)
- Made the use of `const` and `let` consistent
- import required functions and types from 'react' instead of using the
  `React.` namespace.
- Added `Expand` type, which can expand complex types to their "final"
  result.
- Ensured that we use `as const` for DEFAULT_XXX_TAG where we used a
  string. So that we have the type of `div` instead of `string` for
  example.
- Used `interface` over `type` where possible. I'm personally more of a
  `type` fan. But the TypeScript recommends `interfaces` where possible
  because they are faster, yield better error messages and so on.
2021-01-30 14:46:54 +01:00
Robin Malfait da179ca72b trigger "outside click" behaviour on mousedown (#212)
Fixes: #95
2021-01-29 21:47:56 +01:00
Robin Malfait 80402e70e1 Fix various event bugs (#211)
* add right click option to the interactions

* add tests to ensure right click behaves as expected

Fixes: #142
Fixes: #167

* fallback to mouse events if pointer events are not supported

When the pointer events are not supported, then this is essentially a
no-op. When they *are* supported, then both the pointer *and* mouse
events will fire.
To mitigate potential issues, we make sure that state changes (and
potential re-renders) are idempotent (we bail out on potential state
updates when we are already ina certain state).

Fixes: #173
Fixes: #167
2021-01-29 20:43:40 +01:00
Robin Malfait 9b0d9e136a bump dependencies (#177)
* bump root dependencies

* bump react related dependencies

* update browserslist db

* remove obsolete shared dependency
2021-01-29 15:14:59 +01:00
Robin Malfait 4459689beb handle keyboard interactions in a more robust way
Browsers. Are. Crazy.

In JSDOM, when you fire an event, you only get that specific event. You
don't get all the magic that the browser gives you. For example, when
you are focused on a button and press to "Tab" then in JSDOM you would
only get a keydown event. However in the browser you get this chain of
events:

1. `keydown` on the current element
2. `blur` on the current element
3. `focus` on the new element
4. `keyup` on the new element

I implemented this "magic", for the `Tab`, `Enter` and `Space` key for
now. Those are the most important currently. `Enter` and `Space` also
trigger `click` events for example.

I also have a "generic" implementation, where a normal press results in:

1. `keydown`
2. `keypress` (in case it has a `charCode` and is "printable", so `alt`
   is ignored)
3. `keyup`

I also ensured that the cancelation when you use an
`event.preventDefault()` happens correctly.

Here is a fun summary: https://twitter.com/malfaitrobin/status/1354472678128820234

Press "Enter" on a button
  -> keydown, keypress, click, keyup

Press "Space" on a button
  -> keydown, keypress, keyup, click

Press "Enter" or "Space" on a button, with event.preventDefault() in the keydown listener
  -> keydown, keyup

Press "Enter" on a button, with event.preventDefault() in the keypress listener
  -> keydown, keypress, keyup

Press "Space" on a button, with event.preventDefault() in the keypress listener
  -> keydown, keypress, keyup, click
2021-01-29 12:23:14 +01:00
Robin Malfait b6212b9d44 ensure that we regenerate the id when it is still null 2021-01-29 12:23:10 +01:00
Robin Malfait ab820ded09 update React Transition docs (#203) 2021-01-22 18:35:26 +01:00
Robin Malfait 5fb605205d apply disabled fix when inside a disabled fieldset (#202)
And if we are in a disabled fieldset, double check that we are not in
the first legend. Because in that case we are visually outside of the
fieldset and according to the spec those elements should **not** be
considered disabled.

Fixes: #194
2021-01-22 15:41:09 +01:00
Robin Malfait 17a853501d ensure Switch has type="button" (#192)
Closes: #178

Co-authored-by: =?UTF-8?q?Oskar=20L=C3=B6fgren?= <islander.abroad@gmail.com>

Co-authored-by: =?UTF-8?q?Oskar=20L=C3=B6fgren?= <islander.abroad@gmail.com>
2021-01-08 23:00:39 +01:00
ellreka 037079af61 docs: rename class to className (#139) 2020-12-16 15:06:28 +01:00
Matthew Hailwood c79aa36c14 Add displayName to all contexts in react (#175) 2020-12-16 15:02:38 +01:00
Robin Malfait 78ebd3eb23 prevent scrolling when refocusing items
Fixes #161
2020-11-30 13:12:41 +01:00
Robin Malfait 96e0fa96bd remove unused d dependency 2020-11-30 12:35:53 +01:00
Robin Malfait 24725216e4 fix: outside click refocus bug (#114)
* add watch script

* make interactions in Vue and React consistent

* re-work focus restoration

When we click outside of the Menu or Listbox, we want to
restore the focus to the Button, *unless* we clicked on/in an element
that is focusable in itself. For example, when the Menu is open and you
click in an input field, the input field should stay focused. We should
also close the Menu itself at this point.

* add examples with multiple elements

* bump dependencies
2020-10-20 15:38:12 +02:00
Robin Malfait 47b3ad1387 chore: Cleanup duplication (#109)
* remove duplicate calculate-active-index calculation

* make codebase a bit more consistent

* remove duplicate resolve-prop-value
2020-10-19 12:21:12 +02:00
Robin Malfait aab23c9077 feat: add render features + render strategy (#106)
* add unmount strategy to README (React)

* add unmount strategy to README (Vue)

* add different render features (React)

* use render features in Menu and Listbox (React)

* add different render features (Vue)

* use render features in Menu and Listbox (Vue)

* bump dependencies

* add ability to change the ref property using `refName`

Example use case:

```tsx
// Some components have this API with an `innerRef`. The suggested approach is to use
// `React.forwardRef` so that you get the actual `ref` value. However if you already have this
// `innerRef` API than we can use the `refName="innerRef"` to give the `ref` prop a good name. It
// defaults to `ref` so that it still works everywhere else.

function MyButton({ innerRef, ...props }) {
  return <button ref={innerRef} {...props} />
}

<Menu.Button as={MyButton} refName="innerRef" />
```

* small cleanup, move refs to props we control

* add tests for the render abstraction (Render)

+ use the unique __ symbol as a default value in the Props type for the
  omitable props.

* use render features in Transition (React)

* add/update Transition examples to also showcase the `unmount={false}` render strategy

* bump dependencies

* add example with nested unmount/hide transitions

* add unmount to Transition documentation
2020-10-18 15:34:05 +02:00
Robin Malfait 7d4af1ad3a chore: bump dependencies (#100) 2020-10-14 12:51:10 +02:00
Robin Malfait 53776af89e docs: improve documentation (#91)
* format README's with Prettier

* hoist people list

otherwise the reference will never be the same when you select a new item. Alternative could be to put it in a ref or useMemo or something.

* make whitespace consistent
2020-10-12 13:58:54 +02:00
Robin Malfait 5bb7df9d93 chore: bump dependencies (#90) 2020-10-12 12:27:11 +02:00
Robin Malfait deee4dfa20 bump dependencies 2020-10-08 15:33:47 +02:00
Robin Malfait a5089d07b1 feat: Add Transition events (#57)
* fix wrong class in tests

* add Transition event callbacks

* add Transition Modal example with Event callbacks

* update props table of Transition component
2020-10-08 15:30:10 +02:00
Robin Malfait 7701a48c64 fix Listbox.Option value typing 2020-10-08 15:15:30 +02:00
Tobias Reich c33fc2568f docs: fix variable name in README (#71)
Looks like an old variable that hasn't been renamed
2020-10-07 11:43:47 +02:00
Robin Malfait c7b91dc731 fix: allow disabling the Menu/Listbox button (#56)
* make sure the Menu.Button can be disabled (React)

* make sure the MenuButton can be disabled (Vue)

* make sure the Listbox.Button can be disabled (React)

* make sure the ListboxButton can be disabled (Vue)
2020-10-06 14:41:54 +02:00
Robin Malfait fecd61dff6 ensure that you can't use Enter to invoke the Switch
And a bunch of keyPress and keyboard related shenanigans
2020-10-06 14:00:01 +02:00
Robin Malfait 6ea5c93457 chore: bump dependencies 2020-10-06 11:01:46 +02:00
Manaia Junior 738ae3a9dd docs: Update table of contents and fix code snippet of Listbox (#50) 2020-10-05 22:49:16 -04:00
Adam Wathan b07a298188 v0.2.0 2020-10-05 21:50:45 -04:00
Adam Wathan b761bd3203 docs: Update table of contents and other navigation 2020-10-05 21:45:24 -04:00
Adam Wathan d0720c27c5 docs: Add switch documentation 2020-10-05 21:41:26 -04:00
Adam Wathan a1fcf52443 docs: Fix mistake in docs 2020-10-05 15:44:16 -04:00