reakit-utils
This is experimental and may have breaking changes in minor versions.
Installation
npm:
npm i reakit-utils
Yarn:
yarn add reakit-utils
API
Table of Contents
applyState
Receives a setState
argument and calls it with currentValue
if it's a
function. Otherwise return the argument as the new value.
Parameters
argument
React.SetStateAction<T>currentValue
T
Examples
import { applyState } from "reakit-utils";
applyState((value) => value + 1, 1);
applyState(2, 1);
closest
Ponyfill for Element.prototype.closest
Parameters
Examples
import { closest } from "reakit-utils";
closest(document.getElementById("id"), "div");
document.getElementById("id").closest("div");
contains
Similar to Element.prototype.contains
.
Parameters
Returns boolean
createEvent
Creates an Event
in a way that also works on IE 11.
Parameters
Examples
import { createEvent } from "reakit-utils";
const el = document.getElementById("id");
el.dispatchEvent(createEvent(el, "blur", { bubbles: false }));
Returns Event
createKeyboardEvent
Creates a KeyboardEvent
in a way that also works on IE 11.
Parameters
Examples
import { createKeyboardEvent } from "reakit-utils";
const el = document.getElementById("id");
el.dispatchEvent(createKeyboardEvent(el, "keydown", { key: "ArrowDown" }));
Returns KeyboardEvent
createOnKeyDown
Returns an onKeyDown
handler to be passed to a component.
Parameters
options
Options (optional, default {}
)
options.keyMap
options.onKey
options.stopPropagation
options.onKeyDown
options.shouldKeyDown
(optional, default ()=>true
)options.preventDefault
(optional, default true
)
Returns React.KeyboardEventHandler
fireEvent
Creates and dispatches Event
in a way that also works on IE 11.
Parameters
Examples
import { fireEvent } from "reakit-utils";
fireEvent(document.getElementById("id"), "blur", {
bubbles: true,
cancelable: true,
});
fireKeyboardEvent
Creates and dispatches KeyboardEvent
in a way that also works on IE 11.
Parameters
Examples
import { fireKeyboardEvent } from "reakit-utils";
fireKeyboardEvent(document.getElementById("id"), "keydown", {
key: "ArrowDown",
shiftKey: true,
});
flatten
Transforms an array with multiple levels into a flattened one.
Parameters
Examples
import { flatten } from "reakit-utils";
flatten([0, 1, [2, [3, 4], 5], 6]);
getActiveElement
Returns element.ownerDocument.activeElement
.
Parameters
getDefaultView
Returns element.ownerDocument.defaultView || window
.
Parameters
Returns Window
getDocument
Returns element.ownerDocument || window.document
.
Parameters
Returns Document
getNextActiveElementOnBlur
Cross-browser method that returns the next active element (the element that
is receiving focus) after a blur event is dispatched. It receives the blur
event object as the argument.
Parameters
Examples
import { getNextActiveElementOnBlur } from "reakit-utils";
const element = document.getElementById("id");
element.addEventListener("blur", (event) => {
const nextActiveElement = getNextActiveElementOnBlur(event);
...
});
hasFocusWithin
Checks if element
has focus.
Parameters
Examples
import { hasFocusWithin } from "reakit-utils";
hasFocusWithin(document.getElementById("id"));
Returns boolean
isButton
Checks whether element
is a native HTML button element or not.
Parameters
Examples
import { isButton } from "reakit-utils";
isButton(document.querySelector("button"));
isButton(document.querySelector("input[type='button']"));
isButton(document.querySelector("div"));
isButton(document.querySelector("input[type='text']"));
Returns boolean
isEmpty
Checks whether arg
is empty or not.
Parameters
Examples
import { isEmpty } from "reakit-utils";
isEmpty([]);
isEmpty(["a"]);
isEmpty({});
isEmpty({ a: "a" });
isEmpty();
isEmpty(null);
isEmpty(undefined);
isEmpty("");
Returns boolean
isInteger
Checks whether arg
is an integer or not.
Parameters
Examples
import { isInteger } from "reakit-utils";
isInteger(1);
isInteger(1.5);
isInteger("1");
isInteger("1.5");
Returns boolean
isObject
Checks whether arg
is an object or not.
Parameters
Returns boolean
isPlainObject
Checks whether arg
is a plain object or not.
Parameters
Returns boolean
isPortalEvent
Returns true
if event
has been fired within a React Portal element.
Parameters
Returns boolean
isPromise
Checks whether arg
is a promise or not.
Parameters
Returns boolean
isSelfTarget
Returns true
if event.target
and event.currentTarget
are the same.
Parameters
event
React.SyntheticEvent
Returns boolean
isTextField
Check whether the given element is a text field, where text field is defined
by the ability to select within the input, or that it is contenteditable.
Parameters
Returns boolean
matches
Ponyfill for Element.prototype.matches
Parameters
Returns boolean
omit
Omits specific keys from an object.
Parameters
object
Tpaths
(ReadonlyArray<K> | Array<K>)
Examples
import { omit } from "reakit-utils";
omit({ a: "a", b: "b" }, ["a"]);
Returns Omit<T, K>
pick
Picks specific keys from an object.
Parameters
object
Tpaths
(ReadonlyArray<K> | Array<K>)
Examples
import { pick } from "reakit-utils";
pick({ a: "a", b: "b" }, ["a"]);
removeIndexFromArray
Immutably removes an index from an array.
Parameters
Examples
import { removeIndexFromArray } from "reakit-utils";
removeIndexFromArray(["a", "b", "c"], 1);
Returns Array A new array without the item in the passed index.
removeItemFromArray
Immutably removes an item from an array.
Parameters
Examples
import { removeItemFromArray } from "reakit-utils";
removeItemFromArray(["a", "b", "c"], "b");
const obj = {};
removeItemFromArray([obj], {});
removeItemFromArray([obj], obj);
Returns Array A new array without the passed item.
shallowEqual
Compares two objects.
Parameters
objA
Record<any, any>?objB
Record<any, any>?
Examples
import { shallowEqual } from "reakit-utils";
shallowEqual({ a: "a" }, {});
shallowEqual({ a: "a" }, { b: "b" });
shallowEqual({ a: "a" }, { a: "a" });
shallowEqual({ a: "a" }, { a: "a", b: "b" });
Returns boolean
splitProps
Splits an object (props
) into a tuple where the first item is an object
with the passed keys
, and the second item is an object with these keys
omitted.
Parameters
props
Tkeys
(ReadonlyArray<K> | Array<K>)
Examples
import { splitProps } from "reakit-utils";
splitProps({ a: "a", b: "b" }, ["a"]);
Returns [any, Omit<T, K>]
tabbable
isFocusable
Checks whether element
is focusable or not.
Parameters
Examples
import { isFocusable } from "reakit-utils";
isFocusable(document.querySelector("input"));
isFocusable(document.querySelector("input[tabindex='-1']"));
isFocusable(document.querySelector("input[hidden]"));
isFocusable(document.querySelector("input:disabled"));
Returns boolean
isTabbable
Checks whether element
is tabbable or not.
Parameters
Examples
import { isTabbable } from "reakit-utils";
isTabbable(document.querySelector("input"));
isTabbable(document.querySelector("input[tabindex='-1']"));
isTabbable(document.querySelector("input[hidden]"));
isTabbable(document.querySelector("input:disabled"));
Returns boolean
getAllFocusableIn
Returns all the focusable elements in container
.
Parameters
Returns Array<Element>
getFirstFocusableIn
Returns the first focusable element in container
.
Parameters
Returns (Element | null)
getAllTabbableIn
Returns all the tabbable elements in container
, including the container
itself.
Parameters
container
ElementfallbackToFocusable
boolean? If true
, it'll return focusable elements if there are no tabbable ones.
Returns Array<Element>
getFirstTabbableIn
Returns the first tabbable element in container
, including the container
itself if it's tabbable.
Parameters
container
ElementfallbackToFocusable
boolean? If true
, it'll return the first focusable element if there are no tabbable ones.
Returns (Element | null)
getLastTabbableIn
Returns the last tabbable element in container
, including the container
itself if it's tabbable.
Parameters
container
ElementfallbackToFocusable
boolean? If true
, it'll return the last focusable element if there are no tabbable ones.
Returns (Element | null)
getNextTabbableIn
Returns the next tabbable element in container
.
Parameters
container
ElementfallbackToFocusable
boolean? If true
, it'll return the next focusable element if there are no tabbable ones.
Returns (Element | null)
getPreviousTabbableIn
Returns the previous tabbable element in container
.
Parameters
container
ElementfallbackToFocusable
boolean? If true
, it'll return the previous focusable element if there are no tabbable ones.
Returns (Element | null)
getClosestFocusable
Returns the closest focusable element.
Parameters
element
(T | null)?container
Element
Returns (Element | null)
ensureFocus
Ensures element
will receive focus if it's not already.
Parameters
element
HTMLElement$1
EnsureFocusOptions (optional, default {}
)
$1.isActive
(optional, default defaultIsActive
)$1.preventScroll
Examples
import { ensureFocus } from "reakit-utils";
ensureFocus(document.activeElement);
const element = document.querySelector("input");
ensureFocus(element);
ensureFocus(element, { preventScroll: true });
function isActive(el) {
return el.dataset.active === "true";
}
ensureFocus(document.querySelector("[data-active='true']"), { isActive });
Returns number requestAnimationFrame
call ID so it can be passed to cancelAnimationFrame
if needed.
toArray
Transforms arg
into an array if it's not already.
Parameters
Examples
import { toArray } from "reakit-utils";
toArray("a");
toArray(["a"]);
Returns Array<any>
types
RenderProp
Render prop type
Type: function (props: P): React.ReactElement<any>
As
"as" prop
Type: React.ElementType<P>
HTMLAttributesWithRef
Type: any
Returns only the HTML attributes inside P
type OnlyId = ExtractHTMLAttributes<{ id: string; foo: string }>;
type HTMLAttributes = ExtractHTMLAttributes<any>;
Type: Pick<HTMLAttributesWithRef, Extract<any, any>>
UnionToIntersection
Transforms "a" | "b"
into "a" & "b"
Type: any
PropsWithAs
Generic component props with "as" prop
Type: any
ArrayValue
Returns the type of the items in an array
Type: any
AnyFunction
Any function
Type: function (...args: Array<any>): any
SetState
State hook setter.
Type: React.Dispatch<React.SetStateAction<T>>
useAllCallbacks
React custom hook that combines multiple callbacks into one.
Parameters
Examples
import React from "react";
import { useAllCallbacks } from "reakit-utils";
function Component(props) {
const onClick = () => {};
return (
<button onClick={useAllCallbacks(onClick, props.onClick)}>Button</button>
);
}
Returns AnyFunction
useForkRef
Merges up to two React Refs into a single memoized function React Ref so you
can pass it to an element.
Parameters
refA
React.Ref<any>?refB
React.Ref<any>?
Examples
import React from "react";
import { useForkRef } from "reakit-utils";
const Component = React.forwardRef((props, ref) => {
const internalRef = React.useRef();
return <div {...props} ref={useForkRef(internalRef, ref)} />;
});
useIsomorphicEffect
React.useLayoutEffect
that fallbacks to React.useEffect
on server side
rendering.
useLiveRef
A React.Ref
that keeps track of the passed value
.
Parameters
Returns React.MutableRefObject<T>
usePipe
A React custom hook similar to useAllCallbacks
, but
it'll pass the returned value from a function to the next function.
Parameters
useSealedState
React custom hook that returns the very first value passed to initialState
,
even if it changes between re-renders.
Parameters
initialState
SealedInitialState<T>
useUpdateEffect
A React.useEffect
that will not run on the first render.
Parameters
effect
React.EffectCallbackdeps
(ReadonlyArray<any> | undefined)?
License
MIT © Diego Haz