60 lines
2.5 KiB
JavaScript
60 lines
2.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
Object.defineProperty(exports, "useRovingTabIndex", {
|
|
enumerable: true,
|
|
get: function() {
|
|
return useRovingTabIndex;
|
|
}
|
|
});
|
|
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
|
|
const _reactsharedcontexts = require("@fluentui/react-shared-contexts");
|
|
const _reacttabster = require("@fluentui/react-tabster");
|
|
const _reactutilities = require("@fluentui/react-utilities");
|
|
function useRovingTabIndex() {
|
|
const currentElementRef = _react.useRef(null);
|
|
const walkerRef = _react.useRef(null);
|
|
const { targetDocument } = (0, _reactsharedcontexts.useFluent_unstable)();
|
|
_react.useEffect(()=>{
|
|
if ((currentElementRef.current === null || !(targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.body.contains(currentElementRef.current))) && walkerRef.current) {
|
|
initialize(walkerRef.current);
|
|
}
|
|
});
|
|
(0, _reacttabster.useFocusedElementChange)((element)=>{
|
|
if ((element === null || element === void 0 ? void 0 : element.getAttribute('role')) === 'treeitem' && walkerRef.current && (0, _reactutilities.elementContains)(walkerRef.current.root, element)) {
|
|
rove(element);
|
|
}
|
|
});
|
|
const initialize = _react.useCallback((walker)=>{
|
|
walkerRef.current = walker;
|
|
walker.currentElement = walker.root;
|
|
let tabbableChild = walker.firstChild((element)=>element.tabIndex === 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP);
|
|
walker.currentElement = walker.root;
|
|
tabbableChild !== null && tabbableChild !== void 0 ? tabbableChild : tabbableChild = walker.firstChild();
|
|
if (!tabbableChild) {
|
|
return;
|
|
}
|
|
tabbableChild.tabIndex = 0;
|
|
currentElementRef.current = tabbableChild;
|
|
let nextElement = null;
|
|
while((nextElement = walker.nextElement()) && nextElement !== tabbableChild){
|
|
nextElement.tabIndex = -1;
|
|
}
|
|
}, []);
|
|
const rove = _react.useCallback((nextElement, focusOptions)=>{
|
|
if (!currentElementRef.current) {
|
|
return;
|
|
}
|
|
currentElementRef.current.tabIndex = -1;
|
|
nextElement.tabIndex = 0;
|
|
nextElement.focus(focusOptions);
|
|
currentElementRef.current = nextElement;
|
|
}, []);
|
|
return {
|
|
rove,
|
|
initialize
|
|
};
|
|
}
|