/
Rover.ts
145 lines (133 loc) · 4.17 KB
/
Rover.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as React from "react";
import { createComponent } from "reakit-system/createComponent";
import { createHook } from "reakit-system/createHook";
import { createOnKeyDown } from "reakit-utils/createOnKeyDown";
import { warning } from "reakit-utils/warning";
import { mergeRefs } from "reakit-utils/mergeRefs";
import { hasFocusWithin } from "reakit-utils/hasFocusWithin";
import { useAllCallbacks } from "reakit-utils/useAllCallbacks";
import {
TabbableOptions,
TabbableHTMLProps,
useTabbable
} from "../Tabbable/Tabbable";
import {
unstable_useId,
unstable_IdOptions,
unstable_IdHTMLProps
} from "../Id/Id";
import { RoverStateReturn, useRoverState } from "./RoverState";
export type RoverOptions = TabbableOptions &
unstable_IdOptions &
Pick<Partial<RoverStateReturn>, "orientation" | "unstable_moves"> &
Pick<
RoverStateReturn,
| "stops"
| "currentId"
| "register"
| "unregister"
| "move"
| "next"
| "previous"
| "first"
| "last"
> & {
/**
* Element ID.
*/
stopId?: string;
};
export type RoverHTMLProps = TabbableHTMLProps & unstable_IdHTMLProps;
export type RoverProps = RoverOptions & RoverHTMLProps;
export const useRover = createHook<RoverOptions, RoverHTMLProps>({
name: "Rover",
compose: [useTabbable, unstable_useId],
useState: useRoverState,
keys: ["stopId"],
useProps(
options,
{
ref: htmlRef,
tabIndex: htmlTabIndex = 0,
onFocus: htmlOnFocus,
onKeyDown: htmlOnKeyDown,
...htmlProps
}
) {
const ref = React.useRef<HTMLElement>(null);
const stopId = options.stopId || options.id || htmlProps.id;
const trulyDisabled = options.disabled && !options.focusable;
const noFocused = options.currentId == null;
const focused = options.currentId === stopId;
const isFirst = (options.stops || [])[0] && options.stops[0].id === stopId;
const shouldTabIndex = focused || (isFirst && noFocused);
React.useEffect(() => {
if (trulyDisabled || !stopId) return undefined;
options.register && options.register(stopId, ref);
return () => options.unregister && options.unregister(stopId);
}, [stopId, trulyDisabled, options.register, options.unregister]);
React.useEffect(() => {
if (!ref.current) {
warning(
true,
"[reakit/Rover]",
"Can't focus rover component because `ref` wasn't passed to component.",
"See https://reakit.io/docs/rover"
);
return;
}
if (options.unstable_moves && focused && !hasFocusWithin(ref.current)) {
ref.current.focus();
}
}, [focused, options.unstable_moves]);
const onFocus = React.useCallback(
(event: React.FocusEvent) => {
if (!stopId || !event.currentTarget.contains(event.target)) return;
// this is already focused, so we move silently
options.move(stopId, true);
},
[options.move, stopId]
);
const onKeyDown = React.useMemo(
() =>
createOnKeyDown({
onKeyDown: htmlOnKeyDown,
stopPropagation: true,
// Ignore portals
shouldKeyDown: event =>
// https://github.com/facebook/react/issues/11387
event.currentTarget.contains(event.target as Node),
keyMap: {
ArrowUp: options.orientation !== "horizontal" && options.previous,
ArrowRight: options.orientation !== "vertical" && options.next,
ArrowDown: options.orientation !== "horizontal" && options.next,
ArrowLeft: options.orientation !== "vertical" && options.previous,
Home: options.first,
End: options.last,
PageUp: options.first,
PageDown: options.last
}
}),
[
htmlOnKeyDown,
options.orientation,
options.previous,
options.next,
options.first,
options.last
]
);
return {
ref: mergeRefs(ref, htmlRef),
id: stopId,
tabIndex: shouldTabIndex ? htmlTabIndex : -1,
onFocus: useAllCallbacks(onFocus, htmlOnFocus),
onKeyDown,
...htmlProps
};
}
});
export const Rover = createComponent({
as: "button",
useHook: useRover
});