UNPKG

@renderlesskit/react

Version:

Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit

57 lines (45 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFocusOnHide = useFocusOnHide; var _reakitUtils = require("reakit-utils"); var _reakitWarning = require("reakit-warning"); function hidByFocusingAnotherElement(dialogRef) { var dialog = dialogRef.current; if (!dialog) return false; var activeElement = (0, _reakitUtils.getActiveElement)(dialog); if (!activeElement) return false; if ((0, _reakitUtils.contains)(dialog, activeElement)) return false; if ((0, _reakitUtils.isTabbable)(activeElement)) return true; if (activeElement.getAttribute("data-dialog") === "true") return true; return false; } function useFocusOnHide(dialogRef, disclosureRef, options) { var shouldFocus = options.unstable_autoFocusOnHide && !options.visible; (0, _reakitUtils.useUpdateEffect)(function () { var _options$unstable_fin; if (!shouldFocus) return; if (options.animating) return; console.log("%canimating", "color: #ffa280", options.animating); // Hide was triggered by a click/focus on a tabbable element outside // the dialog or on another dialog. We won't change focus then. if (hidByFocusingAnotherElement(dialogRef)) { return; } var finalFocusEl = ((_options$unstable_fin = options.unstable_finalFocusRef) === null || _options$unstable_fin === void 0 ? void 0 : _options$unstable_fin.current) || disclosureRef.current; if (finalFocusEl) { if (finalFocusEl.id) { var document = (0, _reakitUtils.getDocument)(finalFocusEl); var compositeElement = document.querySelector("[aria-activedescendant='".concat(finalFocusEl.id, "']")); if (compositeElement) { (0, _reakitUtils.ensureFocus)(compositeElement); return; } } (0, _reakitUtils.ensureFocus)(finalFocusEl); return; } (0, _reakitWarning.warning)(true, "Can't return focus after closing dialog. Either render a disclosure component or provide a `unstable_finalFocusRef` prop.", dialogRef.current); }, [shouldFocus, options.animating, dialogRef, disclosureRef]); } //# sourceMappingURL=useFocusOnHide.js.map