keyborg 1.0.7 → 1.1.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,506 +0,0 @@
1
- 'use strict';
2
-
3
- /*!
4
- * Copyright (c) Microsoft Corporation. All rights reserved.
5
- * Licensed under the MIT License.
6
- */
7
- // IE11 compat, checks if WeakRef is supported
8
- var _canUseWeakRef = typeof WeakRef !== 'undefined';
9
- /**
10
- * WeakRef wrapper around a HTMLElement that also supports IE11
11
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef}
12
- * @internal
13
- */
14
-
15
- var WeakRefInstance = /*#__PURE__*/function () {
16
- function WeakRefInstance(instance) {
17
- if (_canUseWeakRef) {
18
- this._weakRef = new WeakRef(instance);
19
- } else {
20
- this._instance = instance;
21
- }
22
- }
23
- /**
24
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref}
25
- */
26
-
27
-
28
- WeakRefInstance.prototype.deref = function () {
29
- var _a, _b, _c;
30
-
31
- var instance;
32
-
33
- if (this._weakRef) {
34
- instance = (_a = this._weakRef) === null || _a === void 0 ? void 0 : _a.deref();
35
-
36
- if (!instance) {
37
- delete this._weakRef;
38
- }
39
- } else {
40
- instance = this._instance;
41
-
42
- if ((_c = (_b = instance) === null || _b === void 0 ? void 0 : _b.isDisposed) === null || _c === void 0 ? void 0 : _c.call(_b)) {
43
- delete this._instance;
44
- }
45
- }
46
-
47
- return instance;
48
- };
49
-
50
- return WeakRefInstance;
51
- }();
52
-
53
- /*!
54
- * Copyright (c) Microsoft Corporation. All rights reserved.
55
- * Licensed under the MIT License.
56
- */
57
- var KEYBORG_FOCUSIN = 'keyborg:focusin';
58
-
59
- function canOverrideNativeFocus(win) {
60
- var HTMLElement = win.HTMLElement;
61
- var origFocus = HTMLElement.prototype.focus;
62
- var isCustomFocusCalled = false;
63
-
64
- HTMLElement.prototype.focus = function focus() {
65
- isCustomFocusCalled = true;
66
- };
67
-
68
- var btn = win.document.createElement('button');
69
- btn.focus();
70
- HTMLElement.prototype.focus = origFocus;
71
- return isCustomFocusCalled;
72
- }
73
-
74
- var _canOverrideNativeFocus = false;
75
- /**
76
- * Guarantees that the native `focus` will be used
77
- * Use this when trying to simulate a native keyboard focus since it does not
78
- * the programmatic focus flag.
79
- *
80
- * ⚠️⚠️ This focus will always activate the keyboard navigation state.
81
- */
82
-
83
- function nativeFocus(element) {
84
- var focus = element.focus;
85
-
86
- if (focus.__keyborgNativeFocus) {
87
- focus.__keyborgNativeFocus.call(element);
88
- } else {
89
- element.focus();
90
- }
91
- }
92
- /**
93
- * Overrides the native `focus` and setups the keyborg focus event
94
- */
95
-
96
- function setupFocusEvent(win) {
97
- var kwin = win;
98
-
99
- if (!_canOverrideNativeFocus) {
100
- _canOverrideNativeFocus = canOverrideNativeFocus(kwin);
101
- }
102
-
103
- var origFocus = kwin.HTMLElement.prototype.focus;
104
-
105
- if (origFocus.__keyborgNativeFocus) {
106
- // Already set up.
107
- return;
108
- }
109
-
110
- kwin.HTMLElement.prototype.focus = focus;
111
- var data = kwin.__keyborgData = {
112
- focusInHandler: function focusInHandler(e) {
113
- var _a;
114
-
115
- var target = e.target;
116
-
117
- if (!target) {
118
- return;
119
- }
120
-
121
- var event = document.createEvent('HTMLEvents');
122
- event.initEvent(KEYBORG_FOCUSIN, true, true);
123
- var details = {
124
- relatedTarget: e.relatedTarget || undefined
125
- };
126
-
127
- if (_canOverrideNativeFocus || data.lastFocusedProgrammatically) {
128
- details.isFocusedProgrammatically = target === ((_a = data.lastFocusedProgrammatically) === null || _a === void 0 ? void 0 : _a.deref());
129
- data.lastFocusedProgrammatically = undefined;
130
- }
131
-
132
- event.details = details;
133
- target.dispatchEvent(event);
134
- }
135
- };
136
- kwin.document.addEventListener('focusin', kwin.__keyborgData.focusInHandler, true);
137
-
138
- function focus() {
139
- var keyborgNativeFocusEvent = kwin.__keyborgData;
140
-
141
- if (keyborgNativeFocusEvent) {
142
- keyborgNativeFocusEvent.lastFocusedProgrammatically = new WeakRefInstance(this);
143
- }
144
-
145
- return origFocus.apply(this, arguments);
146
- }
147
-
148
- focus.__keyborgNativeFocus = origFocus;
149
- }
150
- /**
151
- * Removes keyborg event listeners and custom focus override
152
- * @param win The window that stores keyborg focus events
153
- */
154
-
155
- function disposeFocusEvent(win) {
156
- var kwin = win;
157
- var proto = kwin.HTMLElement.prototype;
158
- var origFocus = proto.focus.__keyborgNativeFocus;
159
- var keyborgNativeFocusEvent = kwin.__keyborgData;
160
-
161
- if (keyborgNativeFocusEvent) {
162
- kwin.document.removeEventListener('focusin', keyborgNativeFocusEvent.focusInHandler, true);
163
- delete kwin.__keyborgData;
164
- }
165
-
166
- if (origFocus) {
167
- proto.focus = origFocus;
168
- }
169
- }
170
- /**
171
- * @param win The window that stores keyborg focus events
172
- * @returns The last element focused with element.focus()
173
- */
174
-
175
- function getLastFocusedProgrammatically(win) {
176
- var _a;
177
-
178
- var keyborgNativeFocusEvent = win.__keyborgData;
179
- return keyborgNativeFocusEvent ? ((_a = keyborgNativeFocusEvent.lastFocusedProgrammatically) === null || _a === void 0 ? void 0 : _a.deref()) || null : undefined;
180
- }
181
-
182
- /*!
183
- * Copyright (c) Microsoft Corporation. All rights reserved.
184
- * Licensed under the MIT License.
185
- */
186
- var KeyTab = 9;
187
- var KeyEsc = 27;
188
- var _dismissTimeout = 500; // When Esc is pressed and the focused is not moved
189
- // during _dismissTimeout time, dismiss the keyboard
190
- // navigation mode.
191
-
192
- var _lastId = 0;
193
- /**
194
- * Source of truth for all the keyborg core instances and the current keyboard navigation state
195
- */
196
-
197
- var KeyborgState = /*#__PURE__*/function () {
198
- function KeyborgState() {
199
- this.__keyborgCoreRefs = {};
200
- this._isNavigatingWithKeyboard = false;
201
- }
202
-
203
- KeyborgState.prototype.add = function (keyborg) {
204
- var id = keyborg.id;
205
-
206
- if (!(id in this.__keyborgCoreRefs)) {
207
- this.__keyborgCoreRefs[id] = new WeakRefInstance(keyborg);
208
- }
209
- };
210
-
211
- KeyborgState.prototype.remove = function (id) {
212
- delete this.__keyborgCoreRefs[id];
213
-
214
- if (Object.keys(this.__keyborgCoreRefs).length === 0) {
215
- this._isNavigatingWithKeyboard = false;
216
- }
217
- };
218
-
219
- KeyborgState.prototype.setVal = function (isNavigatingWithKeyboard) {
220
- if (this._isNavigatingWithKeyboard === isNavigatingWithKeyboard) {
221
- return;
222
- }
223
-
224
- this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;
225
-
226
- for (var _i = 0, _a = Object.keys(this.__keyborgCoreRefs); _i < _a.length; _i++) {
227
- var id = _a[_i];
228
- var ref = this.__keyborgCoreRefs[id];
229
- var keyborg = ref.deref();
230
-
231
- if (keyborg) {
232
- keyborg.update(isNavigatingWithKeyboard);
233
- } else {
234
- this.remove(id);
235
- }
236
- }
237
- };
238
-
239
- KeyborgState.prototype.getVal = function () {
240
- return this._isNavigatingWithKeyboard;
241
- };
242
-
243
- return KeyborgState;
244
- }();
245
-
246
- var _state = /*#__PURE__*/new KeyborgState();
247
- /**
248
- * Manages a collection of Keyborg instances in a window/document and updates keyborg state
249
- */
250
-
251
-
252
- var KeyborgCore = /*#__PURE__*/function () {
253
- function KeyborgCore(win) {
254
- var _this = this;
255
-
256
- this._isMouseUsed = false;
257
-
258
- this._onFocusIn = function (e) {
259
- if (_this._isMouseUsed) {
260
- _this._isMouseUsed = false;
261
- return;
262
- }
263
-
264
- if (_state.getVal()) {
265
- return;
266
- }
267
-
268
- var details = e.details;
269
-
270
- if (!details.relatedTarget) {
271
- return;
272
- }
273
-
274
- if (details.isFocusedProgrammatically || details.isFocusedProgrammatically === undefined) {
275
- // The element is focused programmatically, or the programmatic focus detection
276
- // is not working.
277
- return;
278
- }
279
-
280
- _state.setVal(true);
281
- };
282
-
283
- this._onMouseDown = function (e) {
284
- if (e.buttons === 0 || e.clientX === 0 && e.clientY === 0 && e.screenX === 0 && e.screenY === 0) {
285
- // This is most likely an event triggered by the screen reader to perform
286
- // an action on an element, do not dismiss the keyboard navigation mode.
287
- return;
288
- }
289
-
290
- _this._isMouseUsed = true;
291
-
292
- _state.setVal(false);
293
- };
294
-
295
- this._onKeyDown = function (e) {
296
- var isNavigatingWithKeyboard = _state.getVal();
297
-
298
- if (!isNavigatingWithKeyboard && e.keyCode === KeyTab) {
299
- _state.setVal(true);
300
- } else if (isNavigatingWithKeyboard && e.keyCode === KeyEsc) {
301
- _this._scheduleDismiss();
302
- }
303
- };
304
-
305
- this.id = 'c' + ++_lastId;
306
- this._win = win;
307
- var doc = win.document;
308
- doc.addEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!
309
-
310
- doc.addEventListener('mousedown', this._onMouseDown, true); // Capture!
311
-
312
- win.addEventListener('keydown', this._onKeyDown, true); // Capture!
313
-
314
- setupFocusEvent(win);
315
-
316
- _state.add(this);
317
- }
318
-
319
- KeyborgCore.prototype.dispose = function () {
320
- var win = this._win;
321
-
322
- if (win) {
323
- if (this._dismissTimer) {
324
- win.clearTimeout(this._dismissTimer);
325
- this._dismissTimer = undefined;
326
- }
327
-
328
- disposeFocusEvent(win);
329
- var doc = win.document;
330
- doc.removeEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!
331
-
332
- doc.removeEventListener('mousedown', this._onMouseDown, true); // Capture!
333
-
334
- win.removeEventListener('keydown', this._onKeyDown, true); // Capture!
335
-
336
- delete this._win;
337
-
338
- _state.remove(this.id);
339
- }
340
- };
341
-
342
- KeyborgCore.prototype.isDisposed = function () {
343
- return !!this._win;
344
- };
345
- /**
346
- * Updates all keyborg instances with the keyboard navigation state
347
- */
348
-
349
-
350
- KeyborgCore.prototype.update = function (isNavigatingWithKeyboard) {
351
- var _a, _b;
352
-
353
- var keyborgs = (_b = (_a = this._win) === null || _a === void 0 ? void 0 : _a.__keyborg) === null || _b === void 0 ? void 0 : _b.refs;
354
-
355
- if (keyborgs) {
356
- for (var _i = 0, _c = Object.keys(keyborgs); _i < _c.length; _i++) {
357
- var id = _c[_i];
358
- Keyborg.update(keyborgs[id], isNavigatingWithKeyboard);
359
- }
360
- }
361
- };
362
-
363
- KeyborgCore.prototype._scheduleDismiss = function () {
364
- var _this = this;
365
-
366
- var win = this._win;
367
-
368
- if (win) {
369
- if (this._dismissTimer) {
370
- win.clearTimeout(this._dismissTimer);
371
- this._dismissTimer = undefined;
372
- }
373
-
374
- var was_1 = win.document.activeElement;
375
- this._dismissTimer = win.setTimeout(function () {
376
- _this._dismissTimer = undefined;
377
- var cur = win.document.activeElement;
378
-
379
- if (was_1 && cur && was_1 === cur) {
380
- // Esc was pressed, currently focused element hasn't changed.
381
- // Just dismiss the keyboard navigation mode.
382
- _state.setVal(false);
383
- }
384
- }, _dismissTimeout);
385
- }
386
- };
387
-
388
- return KeyborgCore;
389
- }();
390
- /**
391
- * Used to determine the keyboard navigation state
392
- */
393
-
394
-
395
- var Keyborg = /*#__PURE__*/function () {
396
- function Keyborg(win) {
397
- var _a;
398
-
399
- this._cb = [];
400
- this._id = 'k' + ++_lastId;
401
- this._win = win;
402
- var current = win.__keyborg;
403
-
404
- if (current) {
405
- this._core = current.core;
406
- current.refs[this._id] = this;
407
- } else {
408
- this._core = new KeyborgCore(win);
409
- win.__keyborg = {
410
- core: this._core,
411
- refs: (_a = {}, _a[this._id] = this, _a)
412
- };
413
- }
414
- }
415
-
416
- Keyborg.create = function (win) {
417
- return new Keyborg(win);
418
- };
419
-
420
- Keyborg.dispose = function (instance) {
421
- instance.dispose();
422
- };
423
- /**
424
- * Updates all subscribed callbacks with the keyboard navigation state
425
- */
426
-
427
-
428
- Keyborg.update = function (instance, isNavigatingWithKeyboard) {
429
- instance._cb.forEach(function (callback) {
430
- return callback(isNavigatingWithKeyboard);
431
- });
432
- };
433
-
434
- Keyborg.prototype.dispose = function () {
435
- var _a;
436
-
437
- var current = (_a = this._win) === null || _a === void 0 ? void 0 : _a.__keyborg;
438
-
439
- if (current === null || current === void 0 ? void 0 : current.refs[this._id]) {
440
- delete current.refs[this._id];
441
-
442
- if (Object.keys(current.refs).length === 0) {
443
- current.core.dispose();
444
- delete this._win.__keyborg;
445
- }
446
- } else {
447
- console.error("Keyborg instance " + this._id + " is being disposed incorrectly.");
448
- }
449
-
450
- this._cb = [];
451
- delete this._core;
452
- delete this._win;
453
- };
454
- /**
455
- * @returns Whether the user is navigating with keyboard
456
- */
457
-
458
-
459
- Keyborg.prototype.isNavigatingWithKeyboard = function () {
460
- return _state.getVal();
461
- };
462
- /**
463
- * @param callback - Called when the keyboard navigation state changes
464
- */
465
-
466
-
467
- Keyborg.prototype.subscribe = function (callback) {
468
- this._cb.push(callback);
469
- };
470
- /**
471
- * @param callback - Registered with subscribe
472
- */
473
-
474
-
475
- Keyborg.prototype.unsubscribe = function (callback) {
476
- var index = this._cb.indexOf(callback);
477
-
478
- if (index >= 0) {
479
- this._cb.splice(index, 1);
480
- }
481
- };
482
- /**
483
- * Manually set the keyboard navigtion state
484
- */
485
-
486
-
487
- Keyborg.prototype.setVal = function (isNavigatingWithKeyboard) {
488
- _state.setVal(isNavigatingWithKeyboard);
489
- };
490
-
491
- return Keyborg;
492
- }();
493
- function createKeyborg(win) {
494
- return Keyborg.create(win);
495
- }
496
- function disposeKeyborg(instance) {
497
- Keyborg.dispose(instance);
498
- }
499
-
500
- exports.KEYBORG_FOCUSIN = KEYBORG_FOCUSIN;
501
- exports.Keyborg = Keyborg;
502
- exports.createKeyborg = createKeyborg;
503
- exports.disposeKeyborg = disposeKeyborg;
504
- exports.getLastFocusedProgrammatically = getLastFocusedProgrammatically;
505
- exports.nativeFocus = nativeFocus;
506
- //# sourceMappingURL=keyborg.cjs.development.js.map
@@ -1,2 +0,0 @@
1
- "use strict";var e="undefined"!=typeof WeakRef,t=function(){function t(t){e?this._weakRef=new WeakRef(t):this._instance=t}return t.prototype.deref=function(){var e,t,i,o;return this._weakRef?(o=null===(e=this._weakRef)||void 0===e?void 0:e.deref())||delete this._weakRef:(null===(i=null===(t=o=this._instance)||void 0===t?void 0:t.isDisposed)||void 0===i?void 0:i.call(t))&&delete this._instance,o},t}(),i=!1,o=0,s=new(function(){function e(){this.__keyborgCoreRefs={},this._isNavigatingWithKeyboard=!1}return e.prototype.add=function(e){var i=e.id;i in this.__keyborgCoreRefs||(this.__keyborgCoreRefs[i]=new t(e))},e.prototype.remove=function(e){delete this.__keyborgCoreRefs[e],0===Object.keys(this.__keyborgCoreRefs).length&&(this._isNavigatingWithKeyboard=!1)},e.prototype.setVal=function(e){if(this._isNavigatingWithKeyboard!==e){this._isNavigatingWithKeyboard=e;for(var t=0,i=Object.keys(this.__keyborgCoreRefs);t<i.length;t++){var o=i[t],s=this.__keyborgCoreRefs[o].deref();s?s.update(e):this.remove(o)}}},e.prototype.getVal=function(){return this._isNavigatingWithKeyboard},e}()),n=function(){function e(e){var n=this;this._isMouseUsed=!1,this._onFocusIn=function(e){if(n._isMouseUsed)n._isMouseUsed=!1;else if(!s.getVal()){var t=e.details;t.relatedTarget&&(t.isFocusedProgrammatically||void 0===t.isFocusedProgrammatically||s.setVal(!0))}},this._onMouseDown=function(e){0===e.buttons||0===e.clientX&&0===e.clientY&&0===e.screenX&&0===e.screenY||(n._isMouseUsed=!0,s.setVal(!1))},this._onKeyDown=function(e){var t=s.getVal();t||9!==e.keyCode?t&&27===e.keyCode&&n._scheduleDismiss():s.setVal(!0)},this.id="c"+ ++o,this._win=e;var r=e.document;r.addEventListener("keyborg:focusin",this._onFocusIn,!0),r.addEventListener("mousedown",this._onMouseDown,!0),e.addEventListener("keydown",this._onKeyDown,!0),function(e){var o=e;i||(i=function(e){var t=e.HTMLElement,i=t.prototype.focus,o=!1;return t.prototype.focus=function(){o=!0},e.document.createElement("button").focus(),t.prototype.focus=i,o}(o));var s=o.HTMLElement.prototype.focus;if(!s.__keyborgNativeFocus){o.HTMLElement.prototype.focus=r;var n=o.__keyborgData={focusInHandler:function(e){var t,o=e.target;if(o){var s=document.createEvent("HTMLEvents");s.initEvent("keyborg:focusin",!0,!0);var r={relatedTarget:e.relatedTarget||void 0};(i||n.lastFocusedProgrammatically)&&(r.isFocusedProgrammatically=o===(null===(t=n.lastFocusedProgrammatically)||void 0===t?void 0:t.deref()),n.lastFocusedProgrammatically=void 0),s.details=r,o.dispatchEvent(s)}}};o.document.addEventListener("focusin",o.__keyborgData.focusInHandler,!0),r.__keyborgNativeFocus=s}function r(){var e=o.__keyborgData;return e&&(e.lastFocusedProgrammatically=new t(this)),s.apply(this,arguments)}}(e),s.add(this)}return e.prototype.dispose=function(){var e=this._win;if(e){this._dismissTimer&&(e.clearTimeout(this._dismissTimer),this._dismissTimer=void 0),function(e){var t=e,i=t.HTMLElement.prototype,o=i.focus.__keyborgNativeFocus,s=t.__keyborgData;s&&(t.document.removeEventListener("focusin",s.focusInHandler,!0),delete t.__keyborgData),o&&(i.focus=o)}(e);var t=e.document;t.removeEventListener("keyborg:focusin",this._onFocusIn,!0),t.removeEventListener("mousedown",this._onMouseDown,!0),e.removeEventListener("keydown",this._onKeyDown,!0),delete this._win,s.remove(this.id)}},e.prototype.isDisposed=function(){return!!this._win},e.prototype.update=function(e){var t,i,o=null===(i=null===(t=this._win)||void 0===t?void 0:t.__keyborg)||void 0===i?void 0:i.refs;if(o)for(var s=0,n=Object.keys(o);s<n.length;s++)r.update(o[n[s]],e)},e.prototype._scheduleDismiss=function(){var e=this,t=this._win;if(t){this._dismissTimer&&(t.clearTimeout(this._dismissTimer),this._dismissTimer=void 0);var i=t.document.activeElement;this._dismissTimer=t.setTimeout((function(){e._dismissTimer=void 0;var o=t.document.activeElement;i&&o&&i===o&&s.setVal(!1)}),500)}},e}(),r=function(){function e(e){var t;this._cb=[],this._id="k"+ ++o,this._win=e;var i=e.__keyborg;i?(this._core=i.core,i.refs[this._id]=this):(this._core=new n(e),e.__keyborg={core:this._core,refs:(t={},t[this._id]=this,t)})}return e.create=function(t){return new e(t)},e.dispose=function(e){e.dispose()},e.update=function(e,t){e._cb.forEach((function(e){return e(t)}))},e.prototype.dispose=function(){var e,t=null===(e=this._win)||void 0===e?void 0:e.__keyborg;(null==t?void 0:t.refs[this._id])&&(delete t.refs[this._id],0===Object.keys(t.refs).length&&(t.core.dispose(),delete this._win.__keyborg)),this._cb=[],delete this._core,delete this._win},e.prototype.isNavigatingWithKeyboard=function(){return s.getVal()},e.prototype.subscribe=function(e){this._cb.push(e)},e.prototype.unsubscribe=function(e){var t=this._cb.indexOf(e);t>=0&&this._cb.splice(t,1)},e.prototype.setVal=function(e){s.setVal(e)},e}();exports.KEYBORG_FOCUSIN="keyborg:focusin",exports.Keyborg=r,exports.createKeyborg=function(e){return r.create(e)},exports.disposeKeyborg=function(e){r.dispose(e)},exports.getLastFocusedProgrammatically=function(e){var t,i=e.__keyborgData;return i?(null===(t=i.lastFocusedProgrammatically)||void 0===t?void 0:t.deref())||null:void 0},exports.nativeFocus=function(e){var t=e.focus;t.__keyborgNativeFocus?t.__keyborgNativeFocus.call(e):e.focus()};
2
- //# sourceMappingURL=keyborg.cjs.production.min.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"keyborg.cjs.production.min.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}