@uncinc/uncinc-react-kitchen-sink 2.16.6 → 2.16.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MutableRefObject } from 'react';
|
|
2
|
+
interface UseProgrammaticFocusOptions {
|
|
3
|
+
querySelector?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Hook to help programmatically focus a non-interactive element.
|
|
7
|
+
* To ensure that an element becomes focusable, we add a -1 to the tabindex,
|
|
8
|
+
* and remove it once blurred.
|
|
9
|
+
*/
|
|
10
|
+
export default function useProgrammaticFocus<T extends HTMLElement>(refOrElement: T | MutableRefObject<T> | null, options?: UseProgrammaticFocusOptions): () => void;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
+
import { useCallback, useEffect } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to help programmatically focus a non-interactive element.
|
|
5
|
+
* To ensure that an element becomes focusable, we add a -1 to the tabindex,
|
|
6
|
+
* and remove it once blurred.
|
|
7
|
+
*/
|
|
8
|
+
export default function useProgrammaticFocus(refOrElement) {
|
|
9
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
10
|
+
var resolveElement = useCallback(function () {
|
|
11
|
+
var element = null;
|
|
12
|
+
if (!refOrElement) {
|
|
13
|
+
return element;
|
|
14
|
+
}
|
|
15
|
+
if (_typeof(refOrElement) === 'object' && 'current' in refOrElement) {
|
|
16
|
+
element = refOrElement.current;
|
|
17
|
+
} else {
|
|
18
|
+
element = refOrElement;
|
|
19
|
+
}
|
|
20
|
+
if (element && options.querySelector) {
|
|
21
|
+
element = element.querySelector(options.querySelector);
|
|
22
|
+
}
|
|
23
|
+
return element;
|
|
24
|
+
}, [options.querySelector, refOrElement]);
|
|
25
|
+
useEffect(function () {
|
|
26
|
+
var element = resolveElement();
|
|
27
|
+
if (!element) return;
|
|
28
|
+
function onBlur() {
|
|
29
|
+
element === null || element === void 0 ? void 0 : element.removeAttribute('tabindex');
|
|
30
|
+
}
|
|
31
|
+
element.addEventListener('blur', onBlur);
|
|
32
|
+
// eslint-disable-next-line consistent-return
|
|
33
|
+
return function () {
|
|
34
|
+
element.removeEventListener('blur', onBlur);
|
|
35
|
+
};
|
|
36
|
+
}, [resolveElement]);
|
|
37
|
+
function isFocusableElement(element) {
|
|
38
|
+
var focusableElementTags = ['a[href]', 'button', 'input', 'textarea', 'select', 'details', '[tabindex]:not([tabindex="-1"])'];
|
|
39
|
+
return focusableElementTags.some(function (selector) {
|
|
40
|
+
return element.matches(selector);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
var focus = useCallback(function () {
|
|
44
|
+
var element = resolveElement();
|
|
45
|
+
if (!element) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (!isFocusableElement(element)) {
|
|
49
|
+
element.setAttribute('tabindex', '-1');
|
|
50
|
+
}
|
|
51
|
+
element.focus();
|
|
52
|
+
}, [resolveElement]);
|
|
53
|
+
return focus;
|
|
54
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -79,6 +79,7 @@ export { default as useScrollPosition } from "./hooks/useScrollPosition";
|
|
|
79
79
|
export { default as useWindowDimensions } from "./hooks/useWindowDimensions";
|
|
80
80
|
export { default as useTrapFocus } from "./hooks/useTrapFocus";
|
|
81
81
|
export { default as useCookieStore } from "./hooks/useCookieStore";
|
|
82
|
+
export { default as useProgrammaticFocus } from "./hooks/useProgrammaticFocus";
|
|
82
83
|
export { default as gtm } from "./helpers/gtm";
|
|
83
84
|
export { default as useValueForCurrentViewPort } from "./helpers/useValueForCurrentViewPort";
|
|
84
85
|
export { default as getApiAutoCompleteSuggestions } from "./helpers/apiAutoComplete";
|
package/dist/index.js
CHANGED
|
@@ -92,6 +92,7 @@ export { default as useScrollPosition } from './hooks/useScrollPosition';
|
|
|
92
92
|
export { default as useWindowDimensions } from './hooks/useWindowDimensions';
|
|
93
93
|
export { default as useTrapFocus } from './hooks/useTrapFocus';
|
|
94
94
|
export { default as useCookieStore } from './hooks/useCookieStore';
|
|
95
|
+
export { default as useProgrammaticFocus } from './hooks/useProgrammaticFocus';
|
|
95
96
|
|
|
96
97
|
// Helpers
|
|
97
98
|
export { default as gtm } from './helpers/gtm';
|