899 Commits

Author SHA1 Message Date
Robin Malfait 6f8225c437 Next batch of Vue components (#282)
* add little editor hack

By adding a html`..` to the template strings editors can get syntax
highlighting for these template strings. Even better, prettier can even
format the contents inside those because now it is "aware" of what kind
of content is inside of these template strings.

You might notice that for the Menu component I have a jsx`..`, this is
another little hack, this only provides us with syntax highlighting and
not with prettier support.

The reason why we have this is that for some reason, when you have:

html`
  <MenuItem>
`

It will be formatted as:

html`
  <menuitem>
`

There might exist a better name we can use instead of jsx, but for now,
this will do. Having syntax highlighting is already 10x better than what
we had before!

* add Alert component

* update changelog

* update REACT readme for Alert component

* expose Alert component

* add Disclosure component

* expose Disclosure component

* add FocusTrap component

* add FocusTrap example

* expose FocusTrap

* update test utils

We've been making some changes in the React utils, so we have to update
them here as well!

* add Popover component

* expose Popover

* drop unused state

* type Disclosure's API object

* add Portal component

* add Portal example

* expose Portal component

* use correct containElement assertion

* add useInertOthers hook

* add Dialog component

* fix various typo's

* expose Dialog

* add Popover example

* force focus on the Popover button on click

* drop own id when using labels

We are nesting the Label and Description components, if we also add the
id of ourselves we get strange results when using Voice Over.

First you would hear the contents (which includes the labels and
descriptions) then you would hear the labels and descriptions again.

We don't want to hear things twice!

* add Dialog example

* ensure to stop propagation

Otherwise if you nest a Menu inside a Dialog and you press `Escape` the
Dialog will close as well, which is not the expected behaviour.

* improve focus management

When you trigger a Popover using a `click` event, then start using `Tab`
the next `a`-tags do not contain the default focus styles. These only
happen when you trigger it using the keyboard first.

Using a tabindex="0" does make it "focusable" and the default browser
styles will be visible. If we remove the tabindex in a
requestAnimationFrame or a setTimeout then the focus styles will be
removed as well.

This should not cause to many issues (fingers crossed) because the
document.activeElement was already referring to the correct element!

* remove Alert component

There are a lot of unknowns and context dependendant questions to
implement Alerts in a good way. The current Alert component just had a
role set, and it had no JS attached.

We will revisit this, once we start working on Alert Dialogs,
Notification center notifications (dismissable, hide after x time, ...)

* ensure Popover.Overlay auto shows/hides based on Popover state

* enable focus trapping based on `open` prop

Only enable focus trapping in the Dialog when `open` is true, regardless
of the `static` prop.

* handle attrs on Dialog manually

* add low level Description component

* add low level Label component

* add RadioGroup component

* expose RadioGroup

* update README with links to new components

* update changelog with all the changes

* add RadioGroup example

* improve type in test

* cleanup internal Dialog Description

We have a low level Description component abstraction that can be used
instead of the Dialog specifiction Description.

* refactor raw window events to a shared useWindowEvent

* passthrough prop bag via context for abstract Description

The Description component is a generic low level component that is
re-used. This causes an issue that the render prop "bag"/"slot" doesn't
contain the data from let's say a Dialog component.

This commit will ensure that you can specify a bag (React) and slot
(Vue) on the DescriptionProvider, so that the Description component can
read it from the context.

* improve render function in React

These contain a few changes that are purely internal changes. Nothing
changes / breaks in the public API of the components.

- Instead of using multiple arguments in your `render()` functions, we now
use an object.
- `propsBag` / `bag` is renamed to `slot`.
- We also provide a `name` to the render function, so that we can use
  that to improve error messages.

* use the new internal render api (React)

* improve render function in Vue

* use the new internal render api (Vue)
2021-04-04 17:49:44 +02:00
Robin Malfait bf27f88e84 fix codesandbox examples for Vue (#301) 2021-04-02 15:55:44 +02:00
Mudassar Ali 4646dadf93 change ensue to ensure 2021-04-02 15:55:44 +02:00
Robin Malfait 3c68a9c0f8 update changelog 2021-04-02 15:55:44 +02:00
Robin Malfait 00cc8c50e3 Add Alert & RadioGroup components (#274)
* add Alert component

* expose Alert

* rename forgotten FLYOUT to POPOVER

* use PopoverRenderPropArg

* organize imports in a consistent way

* ensure Portals behave as expected

Portals can be nested from a React perspective, however in the DOM they
are rendered as siblings, this is mostly fine.

However, when they are rendered inside a Dialog, the Dialog itself is
marked with `role="modal"` which makes all the other content inert. This
means that rendering Menu.Items in a Portal or an Alert in a portal
makes it non-interactable. Alerts are not even announced.

To fix  this, we ensure that we make the `root` of the Portal the actual
dialog. This allows you to still interact with it, because an open modal
is the "root" for the assistive technology.

But there is a catch, a Dialog in a Dialog *can* render as a sibling,
because you force the focus into the new Dialog. So we also ensured that
Dialogs are always rendered in the portal root, and not inside another
Dialog.

* add dialog with alert example

* add internal Description component

* add internal Label component

* add RadioGroup component

* expose RadioGroup

* add RadioGroup example

* ensure to include tha RadioGroup.Option own id

* update changelog

* split documentation
2021-04-02 15:55:40 +02:00
Robin Malfait e3cbcc43f9 fix Portal cleanup issue (#266)
When the last portal is unmounted we will
document.body.removeChild(target), but this crashes when something
tampered with it. This is reproduceable in the tests. Instead we now
ensure that the portal root always exists when mounting. We will also
ensure to target.parentElement.removeChild which will ensure that we can
reference the parent correctly.
2021-04-02 15:55:16 +02:00
Mo Sattler b3c0cbef48 Small cleanup in React Readme (#253)
remove unused import in example
2021-04-02 15:55:15 +02:00
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 7195eeed1a Fix ref element (#249)
* add small dom utility to resolve the dom node from a ref

* use dom() to resolve underlying DOM node

There is probably a better way to do this, the idea is that we apply a
ref to the component. However by default for html components
`yourRef.value` will be the underlying DOM node. However if you pass the
ref to another component, the actual DOM node will be located at
`yourRef.value.$el`.

Fixes: #21

* update changelog
2021-04-02 15:55:15 +02:00
Robin Malfait 6a5a181c17 Fix unique symbol error (#248)
* replace unique symbol with uuid v4

Ideally we can use a Symbol, however it seems that TypeScript or TSDX,
inlines the `unique symbol`, instead of referencing the actual __
placeholder type.

Ideally we add this back in the future, but the odds that somebody has
this specific uuid in production is close to 0. (Unless they
specifically use this uuid).

Fixes: #240

Thanks to =?UTF-8?q?Micha=C3=ABl=20Zasso?= <targos@protonmail.com> for
providing a reproduction repository!

* 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 5159367f7a update changelog 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 75417f24b7 fix codesandbox examples for Vue (#300) 2021-04-02 15:51:42 +02:00
Robin Malfait 67d09e1c27 bump versions 2021-04-02 15:22:35 +02:00
Robin Malfait 6b3c3cfc8d update CHANGELOG 2021-04-02 15:22:25 +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 054d90d899 fix github templates 2021-03-22 15:01:38 +01:00
Simon Vrachliotis c1fe403254 Move ISSUE_TEMPLATE config to the right directory (#288) 2021-03-22 13:59:11 +11:00
Simon Vrachliotis 0a4dd680a5 Add bug report issue template + issue hub YAML configs (#287)
* Add ISSUE_TEMPLATE config
* Add bug report template config
2021-03-22 13:47:47 +11:00
Robin Malfait 5ac4a4de62 change links from develop to main branch 2021-02-18 18:39:02 +01:00
Robin Malfait 71730fea12 bump version 2021-02-11 19:39:44 +01:00
Robin Malfait 16f44e0de1 update changlog for React 0.3.1 version 2021-02-11 19:38:56 +01:00
Robin Malfait 533a81a115 0.0.2 2021-02-11 19:36:20 +01:00
Robin Malfait 6d321ab02a update changelog 2021-02-11 19:23:33 +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 483b17a434 move Transition changes to a changes section 2021-02-06 17:22:29 +01:00
Robin Malfait 43effbbe29 v0.3.0 2021-02-06 16:30:08 +01:00
Robin Malfait b0a35f3960 update changelog 2021-02-06 16:27:46 +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 cf1e92d62f drop dependabot 2021-02-02 11:12:11 +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
Tobias Kündig de16c1bd80 fix: added emits property to Vue components (#199) 2021-01-22 13:13:20 +01:00