keyborg 1.0.0 → 1.1.0-alpha.2

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,502 +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
- */
78
-
79
- function nativeFocus(element) {
80
- var focus = element.focus;
81
-
82
- if (focus.__keyborgNativeFocus) {
83
- focus.__keyborgNativeFocus.call(element);
84
- } else {
85
- element.focus();
86
- }
87
- }
88
- /**
89
- * Overrides the native `focus` and setups the keyborg focus event
90
- */
91
-
92
- function setupFocusEvent(win) {
93
- var kwin = win;
94
-
95
- if (!_canOverrideNativeFocus) {
96
- _canOverrideNativeFocus = canOverrideNativeFocus(kwin);
97
- }
98
-
99
- var origFocus = kwin.HTMLElement.prototype.focus;
100
-
101
- if (origFocus.__keyborgNativeFocus) {
102
- // Already set up.
103
- return;
104
- }
105
-
106
- kwin.HTMLElement.prototype.focus = focus;
107
- var data = kwin.__keyborgData = {
108
- focusInHandler: function focusInHandler(e) {
109
- var _a;
110
-
111
- var target = e.target;
112
-
113
- if (!target) {
114
- return;
115
- }
116
-
117
- var event = document.createEvent('HTMLEvents');
118
- event.initEvent(KEYBORG_FOCUSIN, true, true);
119
- var details = {
120
- relatedTarget: e.relatedTarget || undefined
121
- };
122
-
123
- if (_canOverrideNativeFocus || data.lastFocusedProgrammatically) {
124
- details.isFocusedProgrammatically = target === ((_a = data.lastFocusedProgrammatically) === null || _a === void 0 ? void 0 : _a.deref());
125
- data.lastFocusedProgrammatically = undefined;
126
- }
127
-
128
- event.details = details;
129
- target.dispatchEvent(event);
130
- }
131
- };
132
- kwin.document.addEventListener('focusin', kwin.__keyborgData.focusInHandler, true);
133
-
134
- function focus() {
135
- var keyborgNativeFocusEvent = kwin.__keyborgData;
136
-
137
- if (keyborgNativeFocusEvent) {
138
- keyborgNativeFocusEvent.lastFocusedProgrammatically = new WeakRefInstance(this);
139
- }
140
-
141
- return origFocus.apply(this, arguments);
142
- }
143
-
144
- focus.__keyborgNativeFocus = origFocus;
145
- }
146
- /**
147
- * Removes keyborg event listeners and custom focus override
148
- * @param win The window that stores keyborg focus events
149
- */
150
-
151
- function disposeFocusEvent(win) {
152
- var kwin = win;
153
- var proto = kwin.HTMLElement.prototype;
154
- var origFocus = proto.focus.__keyborgNativeFocus;
155
- var keyborgNativeFocusEvent = kwin.__keyborgData;
156
-
157
- if (keyborgNativeFocusEvent) {
158
- kwin.document.removeEventListener('focusin', keyborgNativeFocusEvent.focusInHandler, true);
159
- delete kwin.__keyborgData;
160
- }
161
-
162
- if (origFocus) {
163
- proto.focus = origFocus;
164
- }
165
- }
166
- /**
167
- * @param win The window that stores keyborg focus events
168
- * @returns The last element focused with element.focus()
169
- */
170
-
171
- function getLastFocusedProgrammatically(win) {
172
- var _a;
173
-
174
- var keyborgNativeFocusEvent = win.__keyborgData;
175
- return keyborgNativeFocusEvent ? ((_a = keyborgNativeFocusEvent.lastFocusedProgrammatically) === null || _a === void 0 ? void 0 : _a.deref()) || null : undefined;
176
- }
177
-
178
- /*!
179
- * Copyright (c) Microsoft Corporation. All rights reserved.
180
- * Licensed under the MIT License.
181
- */
182
- var KeyTab = 9;
183
- var KeyEsc = 27;
184
- var _dismissTimeout = 500; // When Esc is pressed and the focused is not moved
185
- // during _dismissTimeout time, dismiss the keyboard
186
- // navigation mode.
187
-
188
- var _lastId = 0;
189
- /**
190
- * Source of truth for all the keyborg core instances and the current keyboard navigation state
191
- */
192
-
193
- var KeyborgState = /*#__PURE__*/function () {
194
- function KeyborgState() {
195
- this.__keyborgCoreRefs = {};
196
- this._isNavigatingWithKeyboard = false;
197
- }
198
-
199
- KeyborgState.prototype.add = function (keyborg) {
200
- var id = keyborg.id;
201
-
202
- if (!(id in this.__keyborgCoreRefs)) {
203
- this.__keyborgCoreRefs[id] = new WeakRefInstance(keyborg);
204
- }
205
- };
206
-
207
- KeyborgState.prototype.remove = function (id) {
208
- delete this.__keyborgCoreRefs[id];
209
-
210
- if (Object.keys(this.__keyborgCoreRefs).length === 0) {
211
- this._isNavigatingWithKeyboard = false;
212
- }
213
- };
214
-
215
- KeyborgState.prototype.setVal = function (isNavigatingWithKeyboard) {
216
- if (this._isNavigatingWithKeyboard === isNavigatingWithKeyboard) {
217
- return;
218
- }
219
-
220
- this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;
221
-
222
- for (var _i = 0, _a = Object.keys(this.__keyborgCoreRefs); _i < _a.length; _i++) {
223
- var id = _a[_i];
224
- var ref = this.__keyborgCoreRefs[id];
225
- var keyborg = ref.deref();
226
-
227
- if (keyborg) {
228
- keyborg.update(isNavigatingWithKeyboard);
229
- } else {
230
- this.remove(id);
231
- }
232
- }
233
- };
234
-
235
- KeyborgState.prototype.getVal = function () {
236
- return this._isNavigatingWithKeyboard;
237
- };
238
-
239
- return KeyborgState;
240
- }();
241
-
242
- var _state = /*#__PURE__*/new KeyborgState();
243
- /**
244
- * Manages a collection of Keyborg instances in a window/document and updates keyborg state
245
- */
246
-
247
-
248
- var KeyborgCore = /*#__PURE__*/function () {
249
- function KeyborgCore(win) {
250
- var _this = this;
251
-
252
- this._isMouseUsed = false;
253
-
254
- this._onFocusIn = function (e) {
255
- if (_this._isMouseUsed) {
256
- _this._isMouseUsed = false;
257
- return;
258
- }
259
-
260
- if (_state.getVal()) {
261
- return;
262
- }
263
-
264
- var details = e.details;
265
-
266
- if (!details.relatedTarget) {
267
- return;
268
- }
269
-
270
- if (details.isFocusedProgrammatically || details.isFocusedProgrammatically === undefined) {
271
- // The element is focused programmatically, or the programmatic focus detection
272
- // is not working.
273
- return;
274
- }
275
-
276
- _state.setVal(true);
277
- };
278
-
279
- this._onMouseDown = function (e) {
280
- if (e.buttons === 0 || e.clientX === 0 && e.clientY === 0 && e.screenX === 0 && e.screenY === 0) {
281
- // This is most likely an event triggered by the screen reader to perform
282
- // an action on an element, do not dismiss the keyboard navigation mode.
283
- return;
284
- }
285
-
286
- _this._isMouseUsed = true;
287
-
288
- _state.setVal(false);
289
- };
290
-
291
- this._onKeyDown = function (e) {
292
- var isNavigatingWithKeyboard = _state.getVal();
293
-
294
- if (!isNavigatingWithKeyboard && e.keyCode === KeyTab) {
295
- _state.setVal(true);
296
- } else if (isNavigatingWithKeyboard && e.keyCode === KeyEsc) {
297
- _this._scheduleDismiss();
298
- }
299
- };
300
-
301
- this.id = 'c' + ++_lastId;
302
- this._win = win;
303
- var doc = win.document;
304
- doc.addEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!
305
-
306
- doc.addEventListener('mousedown', this._onMouseDown, true); // Capture!
307
-
308
- win.addEventListener('keydown', this._onKeyDown, true); // Capture!
309
-
310
- setupFocusEvent(win);
311
-
312
- _state.add(this);
313
- }
314
-
315
- KeyborgCore.prototype.dispose = function () {
316
- var win = this._win;
317
-
318
- if (win) {
319
- if (this._dismissTimer) {
320
- win.clearTimeout(this._dismissTimer);
321
- this._dismissTimer = undefined;
322
- }
323
-
324
- disposeFocusEvent(win);
325
- var doc = win.document;
326
- doc.removeEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!
327
-
328
- doc.removeEventListener('mousedown', this._onMouseDown, true); // Capture!
329
-
330
- win.removeEventListener('keydown', this._onKeyDown, true); // Capture!
331
-
332
- delete this._win;
333
-
334
- _state.remove(this.id);
335
- }
336
- };
337
-
338
- KeyborgCore.prototype.isDisposed = function () {
339
- return !!this._win;
340
- };
341
- /**
342
- * Updates all keyborg instances with the keyboard navigation state
343
- */
344
-
345
-
346
- KeyborgCore.prototype.update = function (isNavigatingWithKeyboard) {
347
- var _a, _b;
348
-
349
- var keyborgs = (_b = (_a = this._win) === null || _a === void 0 ? void 0 : _a.__keyborg) === null || _b === void 0 ? void 0 : _b.refs;
350
-
351
- if (keyborgs) {
352
- for (var _i = 0, _c = Object.keys(keyborgs); _i < _c.length; _i++) {
353
- var id = _c[_i];
354
- Keyborg.update(keyborgs[id], isNavigatingWithKeyboard);
355
- }
356
- }
357
- };
358
-
359
- KeyborgCore.prototype._scheduleDismiss = function () {
360
- var _this = this;
361
-
362
- var win = this._win;
363
-
364
- if (win) {
365
- if (this._dismissTimer) {
366
- win.clearTimeout(this._dismissTimer);
367
- this._dismissTimer = undefined;
368
- }
369
-
370
- var was_1 = win.document.activeElement;
371
- this._dismissTimer = win.setTimeout(function () {
372
- _this._dismissTimer = undefined;
373
- var cur = win.document.activeElement;
374
-
375
- if (was_1 && cur && was_1 === cur) {
376
- // Esc was pressed, currently focused element hasn't changed.
377
- // Just dismiss the keyboard navigation mode.
378
- _state.setVal(false);
379
- }
380
- }, _dismissTimeout);
381
- }
382
- };
383
-
384
- return KeyborgCore;
385
- }();
386
- /**
387
- * Used to determine the keyboard navigation state
388
- */
389
-
390
-
391
- var Keyborg = /*#__PURE__*/function () {
392
- function Keyborg(win) {
393
- var _a;
394
-
395
- this._cb = [];
396
- this._id = 'k' + ++_lastId;
397
- this._win = win;
398
- var current = win.__keyborg;
399
-
400
- if (current) {
401
- this._core = current.core;
402
- current.refs[this._id] = this;
403
- } else {
404
- this._core = new KeyborgCore(win);
405
- win.__keyborg = {
406
- core: this._core,
407
- refs: (_a = {}, _a[this._id] = this, _a)
408
- };
409
- }
410
- }
411
-
412
- Keyborg.create = function (win) {
413
- return new Keyborg(win);
414
- };
415
-
416
- Keyborg.dispose = function (instance) {
417
- instance.dispose();
418
- };
419
- /**
420
- * Updates all subscribed callbacks with the keyboard navigation state
421
- */
422
-
423
-
424
- Keyborg.update = function (instance, isNavigatingWithKeyboard) {
425
- instance._cb.forEach(function (callback) {
426
- return callback(isNavigatingWithKeyboard);
427
- });
428
- };
429
-
430
- Keyborg.prototype.dispose = function () {
431
- var _a;
432
-
433
- var current = (_a = this._win) === null || _a === void 0 ? void 0 : _a.__keyborg;
434
-
435
- if (current === null || current === void 0 ? void 0 : current.refs[this._id]) {
436
- delete current.refs[this._id];
437
-
438
- if (Object.keys(current.refs).length === 0) {
439
- current.core.dispose();
440
- delete this._win.__keyborg;
441
- }
442
- } else {
443
- console.error("Keyborg instance " + this._id + " is being disposed incorrectly.");
444
- }
445
-
446
- this._cb = [];
447
- delete this._core;
448
- delete this._win;
449
- };
450
- /**
451
- * @returns Whether the user is navigating with keyboard
452
- */
453
-
454
-
455
- Keyborg.prototype.isNavigatingWithKeyboard = function () {
456
- return _state.getVal();
457
- };
458
- /**
459
- * @param callback - Called when the keyboard navigation state changes
460
- */
461
-
462
-
463
- Keyborg.prototype.subscribe = function (callback) {
464
- this._cb.push(callback);
465
- };
466
- /**
467
- * @param callback - Registered with subscribe
468
- */
469
-
470
-
471
- Keyborg.prototype.unsubscribe = function (callback) {
472
- var index = this._cb.indexOf(callback);
473
-
474
- if (index >= 0) {
475
- this._cb.splice(index, 1);
476
- }
477
- };
478
- /**
479
- * Manually set the keyboard navigtion state
480
- */
481
-
482
-
483
- Keyborg.prototype.setVal = function (isNavigatingWithKeyboard) {
484
- _state.setVal(isNavigatingWithKeyboard);
485
- };
486
-
487
- return Keyborg;
488
- }();
489
- function createKeyborg(win) {
490
- return Keyborg.create(win);
491
- }
492
- function disposeKeyborg(instance) {
493
- Keyborg.dispose(instance);
494
- }
495
-
496
- exports.KEYBORG_FOCUSIN = KEYBORG_FOCUSIN;
497
- exports.Keyborg = Keyborg;
498
- exports.createKeyborg = createKeyborg;
499
- exports.disposeKeyborg = disposeKeyborg;
500
- exports.getLastFocusedProgrammatically = getLastFocusedProgrammatically;
501
- exports.nativeFocus = nativeFocus;
502
- //# 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":""}