@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
99 lines • 4.76 kB
JavaScript
;
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.FocusScope = void 0;
const React = __importStar(require("react"));
const react_1 = require("react");
const Slot_1 = require("../../../slot/Slot");
const hooks_1 = require("../../../util/hooks");
const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
/**
* FocusScope manages focus on mount and unmount of container.
* This is used to better handle autofocus of elements when mounted and unmounted.
* Example usage:
* - Focus first item in a list when mounted
* - Focus a button when unmounted
*/
const FocusScope = (0, react_1.forwardRef)((_a, ref) => {
var { onMountHandler: onMountHandlerCallback, onUnmountHandler: onUnmountHandlerCallback } = _a, rest = __rest(_a, ["onMountHandler", "onUnmountHandler"]);
const [container, setContainer] = (0, react_1.useState)(null);
const onMountHandler = (0, hooks_1.useCallbackRef)(onMountHandlerCallback);
const onUnmountHandler = (0, hooks_1.useCallbackRef)(onUnmountHandlerCallback);
const composedRefs = (0, hooks_1.useMergeRefs)(ref, setContainer);
(0, react_1.useEffect)(() => {
var _a;
if (!container)
return;
const ownerDocument = (_a = container.ownerDocument) !== null && _a !== void 0 ? _a : globalThis === null || globalThis === void 0 ? void 0 : globalThis.document;
const hasFocus = container.contains(ownerDocument.activeElement);
if (!hasFocus) {
const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountHandler);
container.dispatchEvent(mountEvent);
}
return () => {
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountHandler);
/**
* https://github.com/facebook/react/issues/17894
* As usual when dealing with focus and useEffect,
* we need to defer the focus to the next event-loop
* setTimeout makes sure the code is ran after the next render-cycle
*/
setTimeout(() => {
const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountHandler);
container.dispatchEvent(unmountEvent);
// we need to remove the listener after we `dispatchEvent`
container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountHandler);
}, 0);
};
}, [container, onMountHandler, onUnmountHandler]);
return React.createElement(Slot_1.Slot, Object.assign({ tabIndex: -1 }, rest, { ref: composedRefs }));
});
exports.FocusScope = FocusScope;
//# sourceMappingURL=FocusScope.js.map