UNPKG

2.44 kBJavaScriptView Raw
1const _excluded = ["className", "tabIndex", "focused", "open", "dropUp", "disabled", "readOnly", "autofilling"];
2
3function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
4
5function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
7import cn from 'classnames';
8import React, { useState } from 'react';
9import useGlobalListener from '@restart/hooks/useGlobalListener';
10
11function useKeyboardNavigationCheck() {
12 const [isNavigatingViaKeyboard, setIsNavigatingViaKeyboard] = useState(false);
13 useGlobalListener('keydown', ({
14 key
15 }) => {
16 if (key == ' ' || key === 'Tab' || key == 'Enter' || key && key.indexOf('Arrow') !== -1) {
17 setIsNavigatingViaKeyboard(true);
18 }
19 }); // TODO: use pointerdown
20
21 useGlobalListener('mousedown', () => {
22 setIsNavigatingViaKeyboard(false);
23 });
24 return isNavigatingViaKeyboard;
25}
26
27export function useWidgetProps(props) {
28 const tabIndex = props.tabIndex != null ? props.tabIndex : -1;
29 const isKeyboardNavigating = useKeyboardNavigationCheck();
30 return {
31 tabIndex: tabIndex,
32 'data-intent': isKeyboardNavigating ? 'keyboard' : 'mouse',
33 className: cn(props.className, 'rw-widget', props.disabled && 'rw-state-disabled', props.readOnly && 'rw-state-readonly', props.focused && 'rw-state-focus', props.autofilling && 'rw-webkit-autofill', props.open && `rw-open${props.dropUp ? '-up' : ''}`)
34 };
35}
36const Widget = /*#__PURE__*/React.forwardRef((_ref, ref) => {
37 let {
38 className,
39 tabIndex,
40 focused,
41 open,
42 dropUp,
43 disabled,
44 readOnly,
45 autofilling
46 } = _ref,
47 props = _objectWithoutPropertiesLoose(_ref, _excluded);
48
49 const widgetProps = useWidgetProps({
50 className,
51 tabIndex,
52 focused,
53 open,
54 dropUp,
55 disabled,
56 readOnly,
57 autofilling
58 });
59 return /*#__PURE__*/React.createElement("div", _extends({
60 ref: ref
61 }, props, widgetProps));
62});
63Widget.displayName = 'Widget';
64export default Widget;
\No newline at end of file