1 | import * as React from 'react';
|
2 | import { getWindow } from './dom/getWindow';
|
3 | import { isDirectionalKeyCode } from './keyboard';
|
4 | import { setFocusVisibility } from './setFocusVisibility';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var mountCounters = new WeakMap();
|
10 | function setMountCounters(key, delta) {
|
11 | var newValue;
|
12 | var currValue = mountCounters.get(key);
|
13 | if (currValue) {
|
14 | newValue = currValue + delta;
|
15 | }
|
16 | else {
|
17 | newValue = 1;
|
18 | }
|
19 | mountCounters.set(key, newValue);
|
20 | return newValue;
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export function useFocusRects(rootRef) {
|
38 | React.useEffect(function () {
|
39 | var _a, _b;
|
40 | var win = getWindow((_a = rootRef) === null || _a === void 0 ? void 0 : _a.current);
|
41 | if (!win || ((_b = win.FabricConfig) === null || _b === void 0 ? void 0 : _b.disableFocusRects) === true) {
|
42 | return undefined;
|
43 | }
|
44 | var count = setMountCounters(win, 1);
|
45 | if (count <= 1) {
|
46 | win.addEventListener('mousedown', _onMouseDown, true);
|
47 | win.addEventListener('pointerdown', _onPointerDown, true);
|
48 | win.addEventListener('keydown', _onKeyDown, true);
|
49 | }
|
50 | return function () {
|
51 | var _a;
|
52 | if (!win || ((_a = win.FabricConfig) === null || _a === void 0 ? void 0 : _a.disableFocusRects) === true) {
|
53 | return;
|
54 | }
|
55 | count = setMountCounters(win, -1);
|
56 | if (count === 0) {
|
57 | win.removeEventListener('mousedown', _onMouseDown, true);
|
58 | win.removeEventListener('pointerdown', _onPointerDown, true);
|
59 | win.removeEventListener('keydown', _onKeyDown, true);
|
60 | }
|
61 | };
|
62 | }, [rootRef]);
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | export var FocusRects = function (props) {
|
69 | useFocusRects(props.rootRef);
|
70 | return null;
|
71 | };
|
72 | function _onMouseDown(ev) {
|
73 | setFocusVisibility(false, ev.target);
|
74 | }
|
75 | function _onPointerDown(ev) {
|
76 | if (ev.pointerType !== 'mouse') {
|
77 | setFocusVisibility(false, ev.target);
|
78 | }
|
79 | }
|
80 | function _onKeyDown(ev) {
|
81 |
|
82 | if (isDirectionalKeyCode(ev.which)) {
|
83 | setFocusVisibility(true, ev.target);
|
84 | }
|
85 | }
|
86 |
|
\ | No newline at end of file |