Accessibility
This set of utilities is meant to help developers create accessible components without making assumptions about how it's UI will display
ID Generation
File | Function |
---|---|
genNewId | genId |
This function has no parameters, but will return a number that's unique to that instance. We use it to generate new IDs that can be used by an id
attribute
Keyboard Detection
There may be an instance where you want to check if the user last used a keyboard on an element in order to render different UI (or highlight items when the user is using a keyboard), this hook may be of help
File | Function |
---|---|
useUsedKeyboardLast | useUsedKeyboardLast |
This hook takes two parameters:
elRef
: A reference to an element used to attach the event listeners tooptions
: An optional object of options for the hook
Options Object
options.enable
: A boolean of if it should be enabled or not. If disabled, theusedKeyboardLast
value will remain the same
Returns
This hook returns an object with two keys:
usedKeyboardLast
: A boolean of if the user had last used a keyboardresetLastUsedKeyboard
: A function that takes no parameters and setsusedKeyboardLast
to false
Keyboard List Navigation
There are various UI elements that may want to use keyboard arrow key navigation on. This hook will assist you in handling this, although it will not handle the "selection" element of a UI element that might expect that.
By default, it handles up arrow, down arrow, end and home button navigation. The up and down arrow moves by one and the home and end go to the first and last index item
File | Function |
---|---|
useKeyboardListNavigation | useKeyboardListNavigation |
This hook takes two parameters:
parentRef
: A reference to an element used to attach the event listeners tooptions
: An optional object of options for the hook
Options Object
options.maxLength
: The maximum number that should be allowed. By default,- If this is not defined, home and end buttons will not work
options.enable
: A boolean of if it should be enabled or notoptions.runOnIndexChange
: A callback that should be ran whenever the selected index is changedoptions.wrapOnOverflow
: A boolean. When false, we force numbers within set min/max constraints, otherwise if true, we allow negative numbers to set the focused index to the minimum and numbers larger than the maximum to set the focused index to the minimum value
Run On Index Change Callback
The runOnIndexChange
callback allows a developer the ability to add additional key listeners to the existing keypress listener, or run other custom code when the user changes the index of the currently selected item
event
: The keyboard event that's used on the element, useful for extending functionality of the keyboard listener for more keypress checks- Be aware that if you use
selectIndex
, this may beundefined
if you do not manually pass the keyboard event
- Be aware that if you use
focusedIndex
: The previous index that was selected before user interactionnewIndex
: The new index that's now being selected by user interaction- Be aware that this may also be
undefined
if the user selects a key that is not being bound to any specific index value
- Be aware that this may also be
It's useful to have both the old and new index for scenarios like allowing a user to have a selection that's toggled when the user holds shift and selects two indexes
Returns
The useKeyboardListNavigation
hook returns an object with two keys:
focusedIndex
: The index of the currently focused itemselectIndex
: A function that takes two parameters:index
: The new index that you'd like to have selectedevent
: The event that was used to trigger the selection. This will get passed torunOnIndexChange
, so it's suggested to pass it when the user makes keyboard events at least
Selectable Array
In many UI elements, there are times where having a selectable array with unique elements and a state of if an item is selected or not can be greatly useful. This hook assists in scenarios of the sort
File | Function |
---|---|
useSelectableArray | useSelectableArray |
This base hook takes two parameters:
valArr
: The original value array that you want to make selectablerunAfterSelectChange
: An optional function that can be ran after selection changes are made- This is present as the
ref.current
value changes may not trigger a detection change, so this allows you to do so manually
- This is present as the
Returns
This hook returns an object with three keys:
internalArr
: An array the same length asvalArr
, but with more metadata on each item in the array- Each item of the array will have the following values:
id
: A unique ID that can be safely placed in the DOMval
: The original value in thevalArr
arrayindex
: The original index in thevalArr
arrayselected
: If this item is selected in the data
- Each item of the array will have the following values:
selectAll
: A function with no parameters that will mark all items in the array withselected: true
markAsSelected
: A function that takes two parameters - a from index and a to index. It will then select all the items from one value to another- If the
from
index is lower than theto
index, it will simply swap the two numbers and mark the items in between asselected: true
0
- If the
Popover
This hook is meant to provide a utility that can be used to compose functionality for a popover component. A popover component is one that has a trigger button and a popup after the button is pressed. This hook handles the expanded
property as well as the props to be added to the trigger button
File | Function |
---|---|
usePopover | usePopover |
This hook takes three parameters:
parentRef
: The div that contains the popoverArea and the trigger buttonpopoverAreaRef
: The div that will be used as the popover area to focus on when the popover is openedoptions
: An optional object of options for the hook
Options Object
options.onBtnClick
options.onBtnKeyDown
Because the return object expects you to pass the buttonProps
onClick
and onKeyDown
to the button used to trigger a popover, passing these functions will run after the usePopover
hook's implementation of each of these functions, allowing you to configure the behavior of them manually without ejecting from the object property spread.
Returns
The usePopover
hook returns an object with three keys:
buttonProps
: The set of props that should be expanded on into a button. IE:<button {...buttonProps}/>
- The two included props in this object are
onClick
andonKeyDown
. Your own implementations of these will be overwritten if you follow the above code
- The two included props in this object are
expanded
: A boolean of if the popover is expanded or notsetExpanded
: A function that takes a single boolean parameter and sets the value ofexpanded
Popover Combobox
A combobox being a way of selecting multiple items in a list at once, a popover combobox is just what it sounds like: A popup multi-select. This hook is meant to serve as a general utility for multi-select, pop-over combo-box. It composes the ability to have a popover, keyboard navigation multi-selects, and tests to see if the user had last used a keyboard
File | Function |
---|---|
usePopoverCombobox | usePopoverCombobox |
This hook take a single parameter:
- An array of values that can be selected that should be used to display the combobox
Returns
The usePopoverCombobox
hook returns an object with three keys:
selected
active
comboBoxListRef
parentRef
values
selectIndex
expanded
setExpanded
usedKeyboardLast
buttonProps