/
Tabbable.ts
198 lines (178 loc) · 5.33 KB
/
Tabbable.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import * as React from "react";
import { createComponent } from "reakit-system/createComponent";
import { createHook } from "reakit-system/createHook";
import { mergeRefs } from "reakit-utils/mergeRefs";
import { isFocusable } from "reakit-utils/tabbable";
import { hasFocusWithin } from "reakit-utils/hasFocusWithin";
import { isButton } from "reakit-utils/isButton";
import { BoxOptions, BoxHTMLProps, useBox } from "../Box/Box";
export type TabbableOptions = BoxOptions & {
/**
* Same as the HTML attribute.
*/
disabled?: boolean;
/**
* When an element is `disabled`, it may still be `focusable`. It works
* similarly to `readOnly` on form elements. In this case, only
* `aria-disabled` will be set.
*/
focusable?: boolean;
/**
* Whether or not trigger click on pressing <kbd>Enter</kbd>.
* @private
*/
unstable_clickOnEnter?: boolean;
/**
* Whether or not trigger click on pressing <kbd>Space</kbd>.
* @private
*/
unstable_clickOnSpace?: boolean;
};
export type TabbableHTMLProps = BoxHTMLProps & {
disabled?: boolean;
};
export type TabbableProps = TabbableOptions & TabbableHTMLProps;
function isNativeTabbable(element: Element) {
return (
element.tagName === "BUTTON" ||
element.tagName === "INPUT" ||
element.tagName === "SELECT" ||
element.tagName === "TEXTAREA" ||
element.tagName === "A" ||
element.tagName === "AUDIO" ||
element.tagName === "VIDEO"
);
}
// https://twitter.com/diegohaz/status/1176998102139572225
function isUserAgent(string: string) {
if (typeof window === "undefined") return false;
return window.navigator.userAgent.indexOf(string) !== -1;
}
const isSafariOrFirefoxOnMac =
isUserAgent("Mac") && (isUserAgent("Safari") || isUserAgent("Firefox"));
export const useTabbable = createHook<TabbableOptions, TabbableHTMLProps>({
name: "Tabbable",
compose: useBox,
keys: [
"disabled",
"focusable",
"unstable_clickOnEnter",
"unstable_clickOnSpace"
],
useOptions(
{ unstable_clickOnEnter = true, unstable_clickOnSpace = true, ...options },
{ disabled }
) {
return {
disabled,
unstable_clickOnEnter,
unstable_clickOnSpace,
...options
};
},
useProps(
options,
{
ref: htmlRef,
tabIndex: htmlTabIndex,
onClick: htmlOnClick,
onMouseDown: htmlOnMouseDown,
onKeyDown: htmlOnKeyDown,
style: htmlStyle,
...htmlProps
}
) {
const ref = React.useRef<HTMLElement>(null);
const trulyDisabled = options.disabled && !options.focusable;
const [nativeTabbable, setNativeTabbable] = React.useState(true);
const tabIndex = nativeTabbable ? htmlTabIndex : htmlTabIndex || 0;
const style =
options.disabled && !nativeTabbable
? { pointerEvents: "none" as const, ...htmlStyle }
: htmlStyle;
React.useEffect(() => {
if (ref.current && !isNativeTabbable(ref.current)) {
setNativeTabbable(false);
}
}, []);
const onClick = React.useCallback(
(event: React.MouseEvent) => {
if (options.disabled) {
event.stopPropagation();
event.preventDefault();
} else if (htmlOnClick) {
htmlOnClick(event);
}
},
[options.disabled, htmlOnClick]
);
const onMouseDown = React.useCallback(
(event: React.MouseEvent) => {
if (options.disabled) {
event.stopPropagation();
event.preventDefault();
return;
}
const self = event.currentTarget as HTMLElement;
const target = event.target as HTMLElement;
if (isSafariOrFirefoxOnMac && isButton(self) && self.contains(target)) {
event.preventDefault();
const isFocusControl =
isFocusable(target) || target instanceof HTMLLabelElement;
if (!hasFocusWithin(self) || self === target || !isFocusControl) {
self.focus();
}
}
if (htmlOnMouseDown) {
htmlOnMouseDown(event);
}
},
[options.disabled, htmlOnMouseDown]
);
const onKeyDown = React.useCallback(
(event: React.KeyboardEvent) => {
if (htmlOnKeyDown) {
htmlOnKeyDown(event);
}
if (options.disabled || isNativeTabbable(event.currentTarget)) return;
// Per the spec, space only triggers button click on key up.
// On key down, it triggers the :active state.
// Since we can't mimic this behavior, we trigger click on key down.
if (
(options.unstable_clickOnEnter && event.key === "Enter") ||
(options.unstable_clickOnSpace && event.key === " ")
) {
event.preventDefault();
event.target.dispatchEvent(
new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: false
})
);
}
},
[
options.disabled,
options.unstable_clickOnEnter,
options.unstable_clickOnSpace,
htmlOnKeyDown
]
);
return {
ref: mergeRefs(ref, htmlRef),
disabled: trulyDisabled,
tabIndex: trulyDisabled ? undefined : tabIndex,
"aria-disabled": options.disabled,
onClick,
onMouseDown,
onKeyDown,
style,
...htmlProps
};
}
});
export const Tabbable = createComponent({
as: "button",
useHook: useTabbable
});