UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

122 lines 5.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RovingFocus = void 0; const react_1 = __importStar(require("react")); const Slot_1 = require("../../../slot/Slot"); const composeEventHandlers_1 = require("../../../util/composeEventHandlers"); const hooks_1 = require("../../../util/hooks"); const owner_1 = require("../../../util/owner"); const ENTRY_FOCUS = "rovingFocusGroup.onEntryFocus"; const EVENT_OPTIONS = { bubbles: false, cancelable: true }; const RovingFocus = (0, react_1.forwardRef)((_a, ref) => { var { children, asChild, descendants, onKeyDown, onEntryFocus, onMouseDown, onFocus } = _a, rest = __rest(_a, ["children", "asChild", "descendants", "onKeyDown", "onEntryFocus", "onMouseDown", "onFocus"]); const _ref = react_1.default.useRef(null); const composedRefs = (0, hooks_1.useMergeRefs)(ref, _ref); const handleEntryFocus = (0, hooks_1.useCallbackRef)(onEntryFocus); const isMouseFocusRef = (0, react_1.useRef)(false); (0, react_1.useEffect)(() => { const node = _ref.current; if (node) { node.addEventListener(ENTRY_FOCUS, handleEntryFocus); return () => node.removeEventListener(ENTRY_FOCUS, handleEntryFocus); } }, [handleEntryFocus]); const handleKeyDown = (0, react_1.useCallback)((event) => { const loop = false; const ownerDoc = (0, owner_1.ownerDocument)(_ref === null || _ref === void 0 ? void 0 : _ref.current); const idx = descendants .values() .findIndex((x) => x.node.isSameNode(ownerDoc.activeElement)); const nextItem = () => { var _a; const next = descendants.nextEnabled(idx, loop); (_a = next === null || next === void 0 ? void 0 : next.node) === null || _a === void 0 ? void 0 : _a.focus(); }; const prevItem = () => { var _a; const prev = descendants.prevEnabled(idx, loop); (_a = prev === null || prev === void 0 ? void 0 : prev.node) === null || _a === void 0 ? void 0 : _a.focus(); }; const firstItem = () => { var _a; const first = descendants.firstEnabled(); (_a = first === null || first === void 0 ? void 0 : first.node) === null || _a === void 0 ? void 0 : _a.focus(); }; const lastItem = () => { var _a; const last = descendants.lastEnabled(); (_a = last === null || last === void 0 ? void 0 : last.node) === null || _a === void 0 ? void 0 : _a.focus(); }; const keyMap = { ArrowUp: prevItem, ArrowDown: nextItem, Home: firstItem, End: lastItem, }; const action = keyMap[event.key]; if (action) { event.preventDefault(); action(event); } }, [descendants]); const Comp = asChild ? Slot_1.Slot : "div"; return (react_1.default.createElement(Comp, Object.assign({ ref: composedRefs }, rest, { tabIndex: descendants.enabledCount() === 0 ? -1 : 0, style: Object.assign({ outline: "none" }, rest.style), onKeyDown: (0, composeEventHandlers_1.composeEventHandlers)(onKeyDown, handleKeyDown), onMouseDown: (0, composeEventHandlers_1.composeEventHandlers)(onMouseDown, () => { isMouseFocusRef.current = true; }), onFocus: (0, composeEventHandlers_1.composeEventHandlers)(onFocus, (event) => { var _a; if (event.target === event.currentTarget) { const entryFocusEvent = new CustomEvent(ENTRY_FOCUS, EVENT_OPTIONS); event.currentTarget.dispatchEvent(entryFocusEvent); if (!entryFocusEvent.defaultPrevented) { (_a = descendants.firstEnabled()) === null || _a === void 0 ? void 0 : _a.node.focus({ preventScroll: true }); } } isMouseFocusRef.current = false; }) }), children)); }); exports.RovingFocus = RovingFocus; //# sourceMappingURL=RovingFocus.js.map