UNPKG

2.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var getWindow_1 = require("./dom/getWindow");
4var keyboard_1 = require("./keyboard");
5var setFocusVisibility_1 = require("./setFocusVisibility");
6/**
7 * Initializes the logic which:
8 *
9 * 1. Subscribes keydown and mousedown events. (It will only do it once per window,
10 * so it's safe to call this method multiple times.)
11 * 2. When the user presses directional keyboard keys, adds the 'ms-Fabric--isFocusVisible' classname
12 * to the document body, removes the 'ms-Fabric-isFocusHidden' classname.
13 * 3. When the user clicks a mouse button, adds the 'ms-Fabric-isFocusHidden' classname to the
14 * document body, removes the 'ms-Fabric--isFocusVisible' classname.
15 *
16 * This logic allows components on the page to conditionally render focus treatments based on
17 * the existence of global classnames, which simplifies logic overall.
18 *
19 * @param window - the window used to add the event listeners
20 * @deprecated Use useFocusRects hook or FocusRects component instead.
21 */
22function initializeFocusRects(window) {
23 var _a;
24 var win = (window || getWindow_1.getWindow());
25 if (!win || ((_a = win.FabricConfig) === null || _a === void 0 ? void 0 : _a.disableFocusRects) === true) {
26 return;
27 }
28 if (!win.__hasInitializeFocusRects__) {
29 win.__hasInitializeFocusRects__ = true;
30 win.addEventListener('mousedown', _onMouseDown, true);
31 win.addEventListener('pointerdown', _onPointerDown, true);
32 win.addEventListener('keydown', _onKeyDown, true);
33 }
34}
35exports.initializeFocusRects = initializeFocusRects;
36function _onMouseDown(ev) {
37 setFocusVisibility_1.setFocusVisibility(false, ev.target);
38}
39function _onPointerDown(ev) {
40 if (ev.pointerType !== 'mouse') {
41 setFocusVisibility_1.setFocusVisibility(false, ev.target);
42 }
43}
44function _onKeyDown(ev) {
45 // eslint-disable-next-line deprecation/deprecation
46 keyboard_1.isDirectionalKeyCode(ev.which) && setFocusVisibility_1.setFocusVisibility(true, ev.target);
47}
48//# sourceMappingURL=initializeFocusRects.js.map
\No newline at end of file