feat: add Listbox component (#3)

* make jest monorepo aware

* add @testing-library/jest-dom for custom matchers

This way we can use expect(element).toHaveAttribute(key, value?)

* abstract keys enum

* change type to unknown, because we don't know the return value

* update use-id hook, make it suspense aware

Thanks Reach UI!

* hoist the disposables collection

* add accessbility assertions for listbox

Also made it consistent for the Menu component and simplified some of the assertions

* add use-computed hook

This allows us re-render when hooks change, but also return a value. So this is a combination of useEffect and a useState value.

* add Listbox component

* bump dependencies

* add listbox example

* add lint-staged

This way we will only lint the files that have been staged and ready to be committed instead of the whole codebase

* add missing prevent defaults

* improve tests to verify that we can actually update the value of the listbox

* scroll the active listbox item into view

* small optimization, only focus "Nothing" on pointer leave when we are the active item

We used to always go to "Nothing" on pointer leave. And while this code
doesn't get called often, it *gets* called if you are using your arrow
keys and the mouse pointer is still over the list.

* bump dependencies

Also moved the tailwind dependencies to the root

* fix typo

* drop the default Transition inside the Menu and Listbox components

* update examples to reflect drop of default Transition wrapper

* rename Listbox.{Items,Item} to Listbox.{Options,Option}

Also rename all instances of `item` to `option` in tests and comments
and what have you...

* fix typo

* drop disabled prop, use aria-disabled only
This commit is contained in:
Robin Malfait
2020-10-02 11:05:41 +02:00
committed by GitHub
parent 412cc950aa
commit 58ff88698b
36 changed files with 9873 additions and 1146 deletions
File diff suppressed because it is too large Load Diff
@@ -4,37 +4,17 @@ import * as React from 'react'
import { Props } from '../../types'
import { match } from '../../utils/match'
import { forwardRefWithAs, render } from '../../utils/render'
import { Transition, TransitionClasses } from '../transitions/transition'
import { useDisposables } from '../../hooks/use-disposables'
import { useIsoMorphicEffect } from '../../hooks/use-iso-morphic-effect'
import { useSyncRefs } from '../../hooks/use-sync-refs'
import { useId } from '../../hooks/use-id'
import { Keys } from '../keyboard'
enum MenuStates {
Open,
Closed,
}
// TODO: This must already exist somewhere, right? 🤔
// Ref: https://www.w3.org/TR/uievents-key/#named-key-attribute-values
enum Key {
Space = ' ',
Enter = 'Enter',
Escape = 'Escape',
Backspace = 'Backspace',
ArrowUp = 'ArrowUp',
ArrowDown = 'ArrowDown',
Home = 'Home',
End = 'End',
PageUp = 'PageUp',
PageDown = 'PageDown',
Tab = 'Tab',
}
type MenuItemDataRef = React.MutableRefObject<{ textValue?: string; disabled: boolean }>
type StateDefinition = {
@@ -286,9 +266,9 @@ const Button = forwardRefWithAs(function Button<
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-13
case Key.Space:
case Key.Enter:
case Key.ArrowDown:
case Keys.Space:
case Keys.Enter:
case Keys.ArrowDown:
event.preventDefault()
dispatch({ type: ActionTypes.OpenMenu })
d.nextFrame(() => {
@@ -297,7 +277,7 @@ const Button = forwardRefWithAs(function Button<
})
break
case Key.ArrowUp:
case Keys.ArrowUp:
event.preventDefault()
dispatch({ type: ActionTypes.OpenMenu })
d.nextFrame(() => {
@@ -369,20 +349,10 @@ type ItemsRenderPropArg = { open: boolean }
const Items = forwardRefWithAs(function Items<
TTag extends React.ElementType = typeof DEFAULT_ITEMS_TAG
>(
props: Props<TTag, ItemsRenderPropArg, ItemsPropsWeControl> &
TransitionClasses & { static?: boolean },
props: Props<TTag, ItemsRenderPropArg, ItemsPropsWeControl> & { static?: boolean },
ref: React.Ref<HTMLDivElement>
) {
const {
enter,
enterFrom,
enterTo,
leave,
leaveFrom,
leaveTo,
static: isStatic = false,
...passthroughProps
} = props
const { static: isStatic = false, ...passthroughProps } = props
const [state, dispatch] = useMenuContext([Menu.name, Items.name].join('.'))
const itemsRef = useSyncRefs(state.itemsRef, ref)
@@ -397,12 +367,14 @@ const Items = forwardRefWithAs(function Items<
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-12
// @ts-expect-error Falthrough is expected here
case Key.Space:
if (state.searchQuery !== '')
// @ts-expect-error Fallthrough is expected here
case Keys.Space:
if (state.searchQuery !== '') {
event.preventDefault()
return dispatch({ type: ActionTypes.Search, value: event.key })
}
// When in type ahead mode, fallthrough
case Key.Enter:
case Keys.Enter:
event.preventDefault()
dispatch({ type: ActionTypes.CloseMenu })
if (state.activeItemIndex !== null) {
@@ -412,31 +384,31 @@ const Items = forwardRefWithAs(function Items<
}
break
case Key.ArrowDown:
case Keys.ArrowDown:
event.preventDefault()
return dispatch({ type: ActionTypes.GoToItem, focus: Focus.NextItem })
case Key.ArrowUp:
case Keys.ArrowUp:
event.preventDefault()
return dispatch({ type: ActionTypes.GoToItem, focus: Focus.PreviousItem })
case Key.Home:
case Key.PageUp:
case Keys.Home:
case Keys.PageUp:
event.preventDefault()
return dispatch({ type: ActionTypes.GoToItem, focus: Focus.FirstItem })
case Key.End:
case Key.PageDown:
case Keys.End:
case Keys.PageDown:
event.preventDefault()
return dispatch({ type: ActionTypes.GoToItem, focus: Focus.LastItem })
case Key.Escape:
case Keys.Escape:
event.preventDefault()
dispatch({ type: ActionTypes.CloseMenu })
d.nextFrame(() => state.buttonRef.current?.focus())
break
case Key.Tab:
case Keys.Tab:
return event.preventDefault()
default:
@@ -461,36 +433,12 @@ const Items = forwardRefWithAs(function Items<
tabIndex: 0,
}
if (isStatic) {
return render(
{ ...passthroughProps, ...propsWeControl, ...{ ref: itemsRef } },
propsBag,
DEFAULT_ITEMS_TAG
)
}
if (!isStatic && state.menuState === MenuStates.Closed) return null
return (
<Transition
show={state.menuState === MenuStates.Open}
{...{ enter, enterFrom, enterTo, leave, leaveFrom, leaveTo }}
>
{ref =>
render(
{
...passthroughProps,
...propsWeControl,
...{
ref(elementRef: HTMLDivElement) {
ref.current = elementRef
itemsRef(elementRef)
},
},
},
propsBag,
DEFAULT_ITEMS_TAG
)
}
</Transition>
return render(
{ ...passthroughProps, ...propsWeControl, ...{ ref: itemsRef } },
propsBag,
DEFAULT_ITEMS_TAG
)
})
@@ -501,7 +449,6 @@ type MenuItemPropsWeControl =
| 'role'
| 'tabIndex'
| 'aria-disabled'
| 'onPointerEnter'
| 'onPointerLeave'
| 'onFocus'
@@ -563,8 +510,9 @@ function Item<TTag extends React.ElementType = typeof DEFAULT_ITEM_TAG>(
const handlePointerLeave = React.useCallback(() => {
if (disabled) return
if (!active) return
dispatch({ type: ActionTypes.GoToItem, focus: Focus.Nothing })
}, [disabled, dispatch])
}, [disabled, active, dispatch])
const propsBag = React.useMemo(() => ({ active, disabled }), [active, disabled])
const propsWeControl = {