keyborg 2.4.0 → 2.4.1

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.
package/dist/esm/index.js CHANGED
@@ -222,18 +222,15 @@ var KeyborgCore = class {
222
222
  _state.setVal(false);
223
223
  };
224
224
  this._onKeyDown = (e) => {
225
- var _a, _b;
226
225
  const isNavigatingWithKeyboard = _state.getVal();
227
- const keyCode = e.keyCode;
228
- const triggerKeys = this._triggerKeys;
229
- if (!isNavigatingWithKeyboard && (!triggerKeys || triggerKeys.has(keyCode))) {
230
- const activeElement = (_a = this._win) == null ? void 0 : _a.document.activeElement;
231
- if (activeElement && (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA" || activeElement.contentEditable === "true")) {
232
- return;
226
+ if (isNavigatingWithKeyboard) {
227
+ if (this._shouldDismissKeyboardNavigation(e)) {
228
+ this._scheduleDismiss();
229
+ }
230
+ } else {
231
+ if (this._shouldTriggerKeyboardNavigation(e)) {
232
+ _state.setVal(true);
233
233
  }
234
- _state.setVal(true);
235
- } else if (isNavigatingWithKeyboard && ((_b = this._dismissKeys) == null ? void 0 : _b.has(keyCode))) {
236
- this._scheduleDismiss();
237
234
  }
238
235
  };
239
236
  this.id = "c" + ++_lastId;
@@ -290,6 +287,26 @@ var KeyborgCore = class {
290
287
  }
291
288
  }
292
289
  }
290
+ /**
291
+ * @returns whether the keyboard event should trigger keyboard navigation mode
292
+ */
293
+ _shouldTriggerKeyboardNavigation(e) {
294
+ var _a;
295
+ if (e.key === "Tab") {
296
+ return true;
297
+ }
298
+ const activeElement = (_a = this._win) == null ? void 0 : _a.document.activeElement;
299
+ const isTriggerKey = !this._triggerKeys || this._triggerKeys.has(e.keyCode);
300
+ const isEditable = activeElement && (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA" || activeElement.isContentEditable);
301
+ return isTriggerKey && !isEditable;
302
+ }
303
+ /**
304
+ * @returns whether the keyboard event should dismiss keyboard navigation mode
305
+ */
306
+ _shouldDismissKeyboardNavigation(e) {
307
+ var _a;
308
+ return (_a = this._dismissKeys) == null ? void 0 : _a.has(e.keyCode);
309
+ }
293
310
  _scheduleDismiss() {
294
311
  const win = this._win;
295
312
  if (win) {
@@ -391,7 +408,7 @@ function disposeKeyborg(instance) {
391
408
  }
392
409
 
393
410
  // src/index.ts
394
- var version = "2.4.0";
411
+ var version = "2.4.1";
395
412
  export {
396
413
  KEYBORG_FOCUSIN,
397
414
  createKeyborg,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/WeakRefInstance.ts","../../src/FocusEvent.ts","../../src/Keyborg.ts","../../src/index.ts"],"sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\n// IE11 compat, checks if WeakRef is supported\nexport const _canUseWeakRef = typeof WeakRef !== \"undefined\";\n\n/**\n * Allows disposable instances to be used\n */\nexport interface Disposable {\n isDisposed?(): boolean;\n}\n\n/**\n * WeakRef wrapper around a HTMLElement that also supports IE11\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef}\n * @internal\n */\nexport class WeakRefInstance<T extends Disposable | object> {\n private _weakRef?: WeakRef<T>;\n private _instance?: T;\n\n constructor(instance: T) {\n if (_canUseWeakRef && typeof instance === \"object\") {\n this._weakRef = new WeakRef(instance);\n } else {\n this._instance = instance;\n }\n }\n\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref}\n */\n deref(): T | undefined {\n let instance: T | undefined;\n\n if (this._weakRef) {\n instance = this._weakRef?.deref();\n\n if (!instance) {\n delete this._weakRef;\n }\n } else {\n instance = this._instance;\n if ((instance as Disposable)?.isDisposed?.()) {\n delete this._instance;\n }\n }\n\n return instance;\n }\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { WeakRefInstance } from \"./WeakRefInstance\";\n\nexport const KEYBORG_FOCUSIN = \"keyborg:focusin\";\n\ninterface KeyborgFocus {\n /**\n * This is the native `focus` function that is retained so that it can be restored when keyborg is disposed\n */\n __keyborgNativeFocus?: (options?: FocusOptions | undefined) => void;\n}\n\ninterface KeyborgFocusEventData {\n focusInHandler: (e: FocusEvent) => void;\n lastFocusedProgrammatically?: WeakRefInstance<HTMLElement>;\n}\n\n/**\n * Extends the global window with keyborg focus event data\n */\ninterface WindowWithKeyborgFocusEvent extends Window {\n HTMLElement: typeof HTMLElement;\n __keyborgData?: KeyborgFocusEventData;\n}\n\nfunction canOverrideNativeFocus(win: Window): boolean {\n const HTMLElement = (win as WindowWithKeyborgFocusEvent).HTMLElement;\n const origFocus = HTMLElement.prototype.focus;\n\n let isCustomFocusCalled = false;\n\n HTMLElement.prototype.focus = function focus(): void {\n isCustomFocusCalled = true;\n };\n\n const btn = win.document.createElement(\"button\");\n\n btn.focus();\n\n HTMLElement.prototype.focus = origFocus;\n\n return isCustomFocusCalled;\n}\n\nlet _canOverrideNativeFocus = false;\n\nexport interface KeyborgFocusInEventDetails {\n relatedTarget?: HTMLElement;\n isFocusedProgrammatically?: boolean;\n}\n\nexport interface KeyborgFocusInEvent\n extends CustomEvent<KeyborgFocusInEventDetails> {\n /**\n * @deprecated - used `event.detail`\n */\n details?: KeyborgFocusInEventDetails;\n}\n\n/**\n * Guarantees that the native `focus` will be used\n */\nexport function nativeFocus(element: HTMLElement): void {\n const focus = element.focus as KeyborgFocus;\n\n if (focus.__keyborgNativeFocus) {\n focus.__keyborgNativeFocus.call(element);\n } else {\n element.focus();\n }\n}\n\n/**\n * Overrides the native `focus` and setups the keyborg focus event\n */\nexport function setupFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n\n if (!_canOverrideNativeFocus) {\n _canOverrideNativeFocus = canOverrideNativeFocus(kwin);\n }\n\n const origFocus = kwin.HTMLElement.prototype.focus;\n\n if ((origFocus as KeyborgFocus).__keyborgNativeFocus) {\n // Already set up.\n return;\n }\n\n kwin.HTMLElement.prototype.focus = focus;\n\n const focusOutShadowRootHandler = (e: FocusEvent) => {\n const relatedTarget = e.relatedTarget as HTMLElement | null;\n const currentTarget = e.currentTarget as ShadowRoot;\n\n // cleanup polyfill event handlers once focus leaves the shadow root\n if (!currentTarget.contains(relatedTarget)) {\n currentTarget.removeEventListener(\"focusin\", focusInHandler, true);\n currentTarget.removeEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n }\n };\n\n const focusInHandler = (e: FocusEvent) => {\n const target = e.target as HTMLElement;\n\n if (!target) {\n return;\n }\n\n if (target.shadowRoot) {\n /**\n * https://bugs.chromium.org/p/chromium/issues/detail?id=1512028\n * focusin events don't bubble up through an open shadow root once focus is inside\n * once focus moves into a shadow root - we drop the same focusin handler there\n * keyborg's custom event will still bubble up since it is composed\n * event handlers should be cleaned up once focus leaves the shadow root.\n * \n * When a focusin event is dispatched from a shadow root, its target is the shadow root parent.\n * Each shadow root encounter requires a new capture listener.\n * Why capture? - we want to follow the focus event in order or descending nested shadow roots\n * When there are no more shadow root targets - dispatch the keyborg:focusin event\n * \n * 1. no focus event\n * > document - capture listener ✅\n * > shadow root 1\n * > shadow root 2\n * > shadow root 3\n * > focused element\n * \n * 2. focus event received by document listener\n * > document - capture listener ✅ (focus event here)\n * > shadow root 1 - capture listener ✅\n * > shadow root 2\n * > shadow root 3\n * > focused element\n\n * 3. focus event received by root l1 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅ (focus event here)\n * > shadow root 2 - capture listener ✅\n * > shadow root 3\n * > focused element\n *\n * 4. focus event received by root l2 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅ (focus event here)\n * > shadow root 3 - capture listener ✅ \n * > focused element\n * \n * 5. focus event received by root l3 listener, no more shadow root targets\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅\n * > shadow root 3 - capture listener ✅ (focus event here)\n * > focused element ✅ (no shadow root - dispatch keyborg event)\n */\n target.shadowRoot.addEventListener(\"focusin\", focusInHandler, true);\n target.shadowRoot.addEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n\n return;\n }\n\n const details: KeyborgFocusInEventDetails = {\n relatedTarget: (e.relatedTarget as HTMLElement) || undefined,\n };\n\n const event: KeyborgFocusInEvent = new CustomEvent(KEYBORG_FOCUSIN, {\n cancelable: true,\n bubbles: true,\n // Allows the event to bubble past an open shadow root\n composed: true,\n detail: details,\n });\n\n // Tabster (and other users) can still use the legacy details field - keeping for backwards compat\n event.details = details;\n\n if (_canOverrideNativeFocus || data.lastFocusedProgrammatically) {\n details.isFocusedProgrammatically =\n target === data.lastFocusedProgrammatically?.deref();\n\n data.lastFocusedProgrammatically = undefined;\n }\n\n target.dispatchEvent(event);\n };\n\n const data: KeyborgFocusEventData = (kwin.__keyborgData = {\n focusInHandler,\n });\n\n kwin.document.addEventListener(\n \"focusin\",\n kwin.__keyborgData.focusInHandler,\n true,\n );\n\n function focus(this: HTMLElement) {\n const keyborgNativeFocusEvent = (kwin as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n keyborgNativeFocusEvent.lastFocusedProgrammatically = new WeakRefInstance(\n this,\n );\n }\n\n // eslint-disable-next-line prefer-rest-params\n return origFocus.apply(this, arguments);\n }\n\n (focus as KeyborgFocus).__keyborgNativeFocus = origFocus;\n}\n\n/**\n * Removes keyborg event listeners and custom focus override\n * @param win The window that stores keyborg focus events\n */\nexport function disposeFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n const proto = kwin.HTMLElement.prototype;\n const origFocus = (proto.focus as KeyborgFocus).__keyborgNativeFocus;\n const keyborgNativeFocusEvent = kwin.__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n kwin.document.removeEventListener(\n \"focusin\",\n keyborgNativeFocusEvent.focusInHandler,\n true,\n );\n delete kwin.__keyborgData;\n }\n\n if (origFocus) {\n proto.focus = origFocus;\n }\n}\n\n/**\n * @param win The window that stores keyborg focus events\n * @returns The last element focused with element.focus()\n */\nexport function getLastFocusedProgrammatically(\n win: Window,\n): HTMLElement | null | undefined {\n const keyborgNativeFocusEvent = (win as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n return keyborgNativeFocusEvent\n ? keyborgNativeFocusEvent.lastFocusedProgrammatically?.deref() || null\n : undefined;\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n disposeFocusEvent,\n KeyborgFocusInEvent,\n KEYBORG_FOCUSIN,\n setupFocusEvent,\n} from \"./FocusEvent\";\nimport { Disposable, WeakRefInstance } from \"./WeakRefInstance\";\n\ninterface WindowWithKeyborg extends Window {\n __keyborg?: {\n core: KeyborgCore;\n refs: { [id: string]: Keyborg };\n };\n}\n\nconst _dismissTimeout = 500; // When a key from dismissKeys is pressed and the focus is not moved\n// during _dismissTimeout time, dismiss the keyboard navigation mode.\n\nlet _lastId = 0;\n\nexport interface KeyborgProps {\n // Keys to be used to trigger keyboard navigation mode. By default, any key will trigger\n // it. Could be limited to, for example, just Tab (or Tab and arrow keys).\n triggerKeys?: number[];\n // Keys to be used to dismiss keyboard navigation mode using keyboard (in addition to\n // mouse clicks which dismiss it). For example, Esc could be used to dismiss.\n dismissKeys?: number[];\n}\n\nexport type KeyborgCallback = (isNavigatingWithKeyboard: boolean) => void;\n\n/**\n * Source of truth for all the keyborg core instances and the current keyboard navigation state\n */\nexport class KeyborgState {\n private __keyborgCoreRefs: { [id: string]: WeakRefInstance<KeyborgCore> } =\n {};\n private _isNavigatingWithKeyboard = false;\n\n add(keyborg: KeyborgCore): void {\n const id = keyborg.id;\n\n if (!(id in this.__keyborgCoreRefs)) {\n this.__keyborgCoreRefs[id] = new WeakRefInstance<KeyborgCore>(keyborg);\n }\n }\n\n remove(id: string): void {\n delete this.__keyborgCoreRefs[id];\n\n if (Object.keys(this.__keyborgCoreRefs).length === 0) {\n this._isNavigatingWithKeyboard = false;\n }\n }\n\n setVal(isNavigatingWithKeyboard: boolean): void {\n if (this._isNavigatingWithKeyboard === isNavigatingWithKeyboard) {\n return;\n }\n\n this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;\n\n for (const id of Object.keys(this.__keyborgCoreRefs)) {\n const ref = this.__keyborgCoreRefs[id];\n const keyborg = ref.deref();\n\n if (keyborg) {\n keyborg.update(isNavigatingWithKeyboard);\n } else {\n this.remove(id);\n }\n }\n }\n\n getVal(): boolean {\n return this._isNavigatingWithKeyboard;\n }\n}\n\nconst _state = new KeyborgState();\n\n/**\n * Manages a collection of Keyborg instances in a window/document and updates keyborg state\n */\nclass KeyborgCore implements Disposable {\n readonly id: string;\n\n private _win?: WindowWithKeyborg;\n private _isMouseUsedTimer: number | undefined;\n private _dismissTimer: number | undefined;\n private _triggerKeys?: Set<number>;\n private _dismissKeys?: Set<number>;\n\n constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this.id = \"c\" + ++_lastId;\n this._win = win;\n const doc = win.document;\n\n if (props) {\n const triggerKeys = props.triggerKeys;\n const dismissKeys = props.dismissKeys;\n\n if (triggerKeys?.length) {\n this._triggerKeys = new Set(triggerKeys);\n }\n\n if (dismissKeys?.length) {\n this._dismissKeys = new Set(dismissKeys);\n }\n }\n\n doc.addEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.addEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.addEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n setupFocusEvent(win);\n\n _state.add(this);\n }\n\n dispose(): void {\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n this._isMouseUsedTimer = undefined;\n }\n\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n disposeFocusEvent(win);\n\n const doc = win.document;\n\n doc.removeEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.removeEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.removeEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n delete this._win;\n\n _state.remove(this.id);\n }\n }\n\n isDisposed(): boolean {\n return !!this._win;\n }\n\n /**\n * Updates all keyborg instances with the keyboard navigation state\n */\n update(isNavigatingWithKeyboard: boolean): void {\n const keyborgs = this._win?.__keyborg?.refs;\n\n if (keyborgs) {\n for (const id of Object.keys(keyborgs)) {\n Keyborg.update(keyborgs[id], isNavigatingWithKeyboard);\n }\n }\n }\n\n private _onFocusIn = (e: KeyborgFocusInEvent) => {\n // When the focus is moved not programmatically and without keydown events,\n // it is likely that the focus is moved by screen reader (as it might swallow\n // the events when the screen reader shortcuts are used). The screen reader\n // usage is keyboard navigation.\n\n if (this._isMouseUsedTimer) {\n // There was a mouse event recently.\n return;\n }\n\n if (_state.getVal()) {\n return;\n }\n\n const details = e.detail;\n\n if (!details.relatedTarget) {\n return;\n }\n\n if (\n details.isFocusedProgrammatically ||\n details.isFocusedProgrammatically === undefined\n ) {\n // The element is focused programmatically, or the programmatic focus detection\n // is not working.\n return;\n }\n\n _state.setVal(true);\n };\n\n private _onMouseDown = (e: MouseEvent): void => {\n if (\n e.buttons === 0 ||\n (e.clientX === 0 && e.clientY === 0 && e.screenX === 0 && e.screenY === 0)\n ) {\n // This is most likely an event triggered by the screen reader to perform\n // an action on an element, do not dismiss the keyboard navigation mode.\n return;\n }\n\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n }\n\n this._isMouseUsedTimer = win.setTimeout(() => {\n delete this._isMouseUsedTimer;\n }, 1000); // Keeping the indication of the mouse usage for some time.\n }\n\n _state.setVal(false);\n };\n\n private _onKeyDown = (e: KeyboardEvent): void => {\n const isNavigatingWithKeyboard = _state.getVal();\n\n const keyCode = e.keyCode;\n const triggerKeys = this._triggerKeys;\n\n if (\n !isNavigatingWithKeyboard &&\n (!triggerKeys || triggerKeys.has(keyCode))\n ) {\n const activeElement = this._win?.document.activeElement as\n | HTMLElement\n | null\n | undefined;\n\n if (\n activeElement &&\n (activeElement.tagName === \"INPUT\" ||\n activeElement.tagName === \"TEXTAREA\" ||\n activeElement.contentEditable === \"true\")\n ) {\n // We're inside an input, textarea or contenteditable, it's not\n // keyboard navigation, it is text editing scenario.\n return;\n }\n\n _state.setVal(true);\n } else if (isNavigatingWithKeyboard && this._dismissKeys?.has(keyCode)) {\n this._scheduleDismiss();\n }\n };\n\n private _scheduleDismiss(): void {\n const win = this._win;\n\n if (win) {\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n const was = win.document.activeElement;\n\n this._dismissTimer = win.setTimeout(() => {\n this._dismissTimer = undefined;\n\n const cur = win.document.activeElement;\n\n if (was && cur && was === cur) {\n // Esc was pressed, currently focused element hasn't changed.\n // Just dismiss the keyboard navigation mode.\n _state.setVal(false);\n }\n }, _dismissTimeout);\n }\n }\n}\n\n/**\n * Used to determine the keyboard navigation state\n */\nexport class Keyborg {\n private _id: string;\n private _win?: WindowWithKeyborg;\n private _core?: KeyborgCore;\n private _cb: KeyborgCallback[] = [];\n\n static create(win: WindowWithKeyborg, props?: KeyborgProps): Keyborg {\n return new Keyborg(win, props);\n }\n\n static dispose(instance: Keyborg): void {\n instance.dispose();\n }\n\n /**\n * Updates all subscribed callbacks with the keyboard navigation state\n */\n static update(instance: Keyborg, isNavigatingWithKeyboard: boolean): void {\n instance._cb.forEach((callback) => callback(isNavigatingWithKeyboard));\n }\n\n private constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this._id = \"k\" + ++_lastId;\n this._win = win;\n\n const current = win.__keyborg;\n\n if (current) {\n this._core = current.core;\n current.refs[this._id] = this;\n } else {\n this._core = new KeyborgCore(win, props);\n win.__keyborg = {\n core: this._core,\n refs: { [this._id]: this },\n };\n }\n }\n\n private dispose(): void {\n const current = this._win?.__keyborg;\n\n if (current?.refs[this._id]) {\n delete current.refs[this._id];\n\n if (Object.keys(current.refs).length === 0) {\n current.core.dispose();\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n delete this._win!.__keyborg;\n }\n } else if (process.env.NODE_ENV !== \"production\") {\n console.error(\n `Keyborg instance ${this._id} is being disposed incorrectly.`,\n );\n }\n\n this._cb = [];\n delete this._core;\n delete this._win;\n }\n\n /**\n * @returns Whether the user is navigating with keyboard\n */\n isNavigatingWithKeyboard(): boolean {\n return _state.getVal();\n }\n\n /**\n * @param callback - Called when the keyboard navigation state changes\n */\n subscribe(callback: KeyborgCallback): void {\n this._cb.push(callback);\n }\n\n /**\n * @param callback - Registered with subscribe\n */\n unsubscribe(callback: KeyborgCallback): void {\n const index = this._cb.indexOf(callback);\n\n if (index >= 0) {\n this._cb.splice(index, 1);\n }\n }\n\n /**\n * Manually set the keyboard navigtion state\n */\n setVal(isNavigatingWithKeyboard: boolean): void {\n _state.setVal(isNavigatingWithKeyboard);\n }\n}\n\nexport function createKeyborg(win: Window, props?: KeyborgProps): Keyborg {\n return Keyborg.create(win, props);\n}\n\nexport function disposeKeyborg(instance: Keyborg) {\n Keyborg.dispose(instance);\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport type { Keyborg, KeyborgCallback } from \"./Keyborg\";\nexport { createKeyborg, disposeKeyborg } from \"./Keyborg\";\n\nexport type {\n KeyborgFocusInEvent,\n KeyborgFocusInEventDetails,\n} from \"./FocusEvent\";\nexport {\n getLastFocusedProgrammatically,\n nativeFocus,\n KEYBORG_FOCUSIN,\n} from \"./FocusEvent\";\n\nexport const version = process.env.PKG_VERSION;\n"],"mappings":";AAMO,IAAM,iBAAiB,OAAO,YAAY;AAc1C,IAAM,kBAAN,MAAqD;AAAA,EAI1D,YAAY,UAAa;AACvB,QAAI,kBAAkB,OAAO,aAAa,UAAU;AAClD,WAAK,WAAW,IAAI,QAAQ,QAAQ;A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,KAAK,KAAK,GAAG;AAE5B,UAAI,OAAO,KAAK,QAAQ,IAAI,EAAE,WAAW,GAAG;AAC1C,gBAAQ,KAAK,QAAQ;AAErB,eAAO,KAAK,KAAM;AAAA,MACpB;AAAA,IACF,WAAW,QAAQ,IAAI,aAAa,cAAc;AAChD,cAAQ;AAAA,QACN,oBAAoB,KAAK,GAAG;AAAA,MAC9B;AAAA,IACF;AAEA,SAAK,MAAM,CAAC;AACZ,WAAO,KAAK;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,2BAAoC;AAClC,WAAO,OAAO,OAAO;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,UAAiC;AACzC,SAAK,IAAI,KAAK,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,UAAiC;AAC3C,UAAM,QAAQ,KAAK,IAAI,QAAQ,QAAQ;AAEvC,QAAI,SAAS,GAAG;AACd,WAAK,IAAI,OAAO,OAAO,CAAC;AAAA,IAC1B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,0BAAyC;AAC9C,WAAO,OAAO,wBAAwB;AAAA,EACxC;AACF;AAEO,SAAS,cAAc,KAAa,OAA+B;AACxE,SAAO,QAAQ,OAAO,KAAK,KAAK;AAClC;AAEO,SAAS,eAAe,UAAmB;AAChD,UAAQ,QAAQ,QAAQ;AAC1B;;;ACnXO,IAAM,UAAU;","names":[]}
1
+ {"version":3,"sources":["../../src/WeakRefInstance.ts","../../src/FocusEvent.ts","../../src/Keyborg.ts","../../src/index.ts"],"sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\n// IE11 compat, checks if WeakRef is supported\nexport const _canUseWeakRef = typeof WeakRef !== \"undefined\";\n\n/**\n * Allows disposable instances to be used\n */\nexport interface Disposable {\n isDisposed?(): boolean;\n}\n\n/**\n * WeakRef wrapper around a HTMLElement that also supports IE11\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef}\n * @internal\n */\nexport class WeakRefInstance<T extends Disposable | object> {\n private _weakRef?: WeakRef<T>;\n private _instance?: T;\n\n constructor(instance: T) {\n if (_canUseWeakRef && typeof instance === \"object\") {\n this._weakRef = new WeakRef(instance);\n } else {\n this._instance = instance;\n }\n }\n\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref}\n */\n deref(): T | undefined {\n let instance: T | undefined;\n\n if (this._weakRef) {\n instance = this._weakRef?.deref();\n\n if (!instance) {\n delete this._weakRef;\n }\n } else {\n instance = this._instance;\n if ((instance as Disposable)?.isDisposed?.()) {\n delete this._instance;\n }\n }\n\n return instance;\n }\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { WeakRefInstance } from \"./WeakRefInstance\";\n\nexport const KEYBORG_FOCUSIN = \"keyborg:focusin\";\n\ninterface KeyborgFocus {\n /**\n * This is the native `focus` function that is retained so that it can be restored when keyborg is disposed\n */\n __keyborgNativeFocus?: (options?: FocusOptions | undefined) => void;\n}\n\ninterface KeyborgFocusEventData {\n focusInHandler: (e: FocusEvent) => void;\n lastFocusedProgrammatically?: WeakRefInstance<HTMLElement>;\n}\n\n/**\n * Extends the global window with keyborg focus event data\n */\ninterface WindowWithKeyborgFocusEvent extends Window {\n HTMLElement: typeof HTMLElement;\n __keyborgData?: KeyborgFocusEventData;\n}\n\nfunction canOverrideNativeFocus(win: Window): boolean {\n const HTMLElement = (win as WindowWithKeyborgFocusEvent).HTMLElement;\n const origFocus = HTMLElement.prototype.focus;\n\n let isCustomFocusCalled = false;\n\n HTMLElement.prototype.focus = function focus(): void {\n isCustomFocusCalled = true;\n };\n\n const btn = win.document.createElement(\"button\");\n\n btn.focus();\n\n HTMLElement.prototype.focus = origFocus;\n\n return isCustomFocusCalled;\n}\n\nlet _canOverrideNativeFocus = false;\n\nexport interface KeyborgFocusInEventDetails {\n relatedTarget?: HTMLElement;\n isFocusedProgrammatically?: boolean;\n}\n\nexport interface KeyborgFocusInEvent\n extends CustomEvent<KeyborgFocusInEventDetails> {\n /**\n * @deprecated - used `event.detail`\n */\n details?: KeyborgFocusInEventDetails;\n}\n\n/**\n * Guarantees that the native `focus` will be used\n */\nexport function nativeFocus(element: HTMLElement): void {\n const focus = element.focus as KeyborgFocus;\n\n if (focus.__keyborgNativeFocus) {\n focus.__keyborgNativeFocus.call(element);\n } else {\n element.focus();\n }\n}\n\n/**\n * Overrides the native `focus` and setups the keyborg focus event\n */\nexport function setupFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n\n if (!_canOverrideNativeFocus) {\n _canOverrideNativeFocus = canOverrideNativeFocus(kwin);\n }\n\n const origFocus = kwin.HTMLElement.prototype.focus;\n\n if ((origFocus as KeyborgFocus).__keyborgNativeFocus) {\n // Already set up.\n return;\n }\n\n kwin.HTMLElement.prototype.focus = focus;\n\n const focusOutShadowRootHandler = (e: FocusEvent) => {\n const relatedTarget = e.relatedTarget as HTMLElement | null;\n const currentTarget = e.currentTarget as ShadowRoot;\n\n // cleanup polyfill event handlers once focus leaves the shadow root\n if (!currentTarget.contains(relatedTarget)) {\n currentTarget.removeEventListener(\"focusin\", focusInHandler, true);\n currentTarget.removeEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n }\n };\n\n const focusInHandler = (e: FocusEvent) => {\n const target = e.target as HTMLElement;\n\n if (!target) {\n return;\n }\n\n if (target.shadowRoot) {\n /**\n * https://bugs.chromium.org/p/chromium/issues/detail?id=1512028\n * focusin events don't bubble up through an open shadow root once focus is inside\n * once focus moves into a shadow root - we drop the same focusin handler there\n * keyborg's custom event will still bubble up since it is composed\n * event handlers should be cleaned up once focus leaves the shadow root.\n * \n * When a focusin event is dispatched from a shadow root, its target is the shadow root parent.\n * Each shadow root encounter requires a new capture listener.\n * Why capture? - we want to follow the focus event in order or descending nested shadow roots\n * When there are no more shadow root targets - dispatch the keyborg:focusin event\n * \n * 1. no focus event\n * > document - capture listener ✅\n * > shadow root 1\n * > shadow root 2\n * > shadow root 3\n * > focused element\n * \n * 2. focus event received by document listener\n * > document - capture listener ✅ (focus event here)\n * > shadow root 1 - capture listener ✅\n * > shadow root 2\n * > shadow root 3\n * > focused element\n\n * 3. focus event received by root l1 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅ (focus event here)\n * > shadow root 2 - capture listener ✅\n * > shadow root 3\n * > focused element\n *\n * 4. focus event received by root l2 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅ (focus event here)\n * > shadow root 3 - capture listener ✅ \n * > focused element\n * \n * 5. focus event received by root l3 listener, no more shadow root targets\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅\n * > shadow root 3 - capture listener ✅ (focus event here)\n * > focused element ✅ (no shadow root - dispatch keyborg event)\n */\n target.shadowRoot.addEventListener(\"focusin\", focusInHandler, true);\n target.shadowRoot.addEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n\n return;\n }\n\n const details: KeyborgFocusInEventDetails = {\n relatedTarget: (e.relatedTarget as HTMLElement) || undefined,\n };\n\n const event: KeyborgFocusInEvent = new CustomEvent(KEYBORG_FOCUSIN, {\n cancelable: true,\n bubbles: true,\n // Allows the event to bubble past an open shadow root\n composed: true,\n detail: details,\n });\n\n // Tabster (and other users) can still use the legacy details field - keeping for backwards compat\n event.details = details;\n\n if (_canOverrideNativeFocus || data.lastFocusedProgrammatically) {\n details.isFocusedProgrammatically =\n target === data.lastFocusedProgrammatically?.deref();\n\n data.lastFocusedProgrammatically = undefined;\n }\n\n target.dispatchEvent(event);\n };\n\n const data: KeyborgFocusEventData = (kwin.__keyborgData = {\n focusInHandler,\n });\n\n kwin.document.addEventListener(\n \"focusin\",\n kwin.__keyborgData.focusInHandler,\n true,\n );\n\n function focus(this: HTMLElement) {\n const keyborgNativeFocusEvent = (kwin as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n keyborgNativeFocusEvent.lastFocusedProgrammatically = new WeakRefInstance(\n this,\n );\n }\n\n // eslint-disable-next-line prefer-rest-params\n return origFocus.apply(this, arguments);\n }\n\n (focus as KeyborgFocus).__keyborgNativeFocus = origFocus;\n}\n\n/**\n * Removes keyborg event listeners and custom focus override\n * @param win The window that stores keyborg focus events\n */\nexport function disposeFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n const proto = kwin.HTMLElement.prototype;\n const origFocus = (proto.focus as KeyborgFocus).__keyborgNativeFocus;\n const keyborgNativeFocusEvent = kwin.__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n kwin.document.removeEventListener(\n \"focusin\",\n keyborgNativeFocusEvent.focusInHandler,\n true,\n );\n delete kwin.__keyborgData;\n }\n\n if (origFocus) {\n proto.focus = origFocus;\n }\n}\n\n/**\n * @param win The window that stores keyborg focus events\n * @returns The last element focused with element.focus()\n */\nexport function getLastFocusedProgrammatically(\n win: Window,\n): HTMLElement | null | undefined {\n const keyborgNativeFocusEvent = (win as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n return keyborgNativeFocusEvent\n ? keyborgNativeFocusEvent.lastFocusedProgrammatically?.deref() || null\n : undefined;\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n disposeFocusEvent,\n KeyborgFocusInEvent,\n KEYBORG_FOCUSIN,\n setupFocusEvent,\n} from \"./FocusEvent\";\nimport { Disposable, WeakRefInstance } from \"./WeakRefInstance\";\n\ninterface WindowWithKeyborg extends Window {\n __keyborg?: {\n core: KeyborgCore;\n refs: { [id: string]: Keyborg };\n };\n}\n\nconst _dismissTimeout = 500; // When a key from dismissKeys is pressed and the focus is not moved\n// during _dismissTimeout time, dismiss the keyboard navigation mode.\n\nlet _lastId = 0;\n\nexport interface KeyborgProps {\n // Keys to be used to trigger keyboard navigation mode. By default, any key will trigger\n // it. Could be limited to, for example, just Tab (or Tab and arrow keys).\n triggerKeys?: number[];\n // Keys to be used to dismiss keyboard navigation mode using keyboard (in addition to\n // mouse clicks which dismiss it). For example, Esc could be used to dismiss.\n dismissKeys?: number[];\n}\n\nexport type KeyborgCallback = (isNavigatingWithKeyboard: boolean) => void;\n\n/**\n * Source of truth for all the keyborg core instances and the current keyboard navigation state\n */\nexport class KeyborgState {\n private __keyborgCoreRefs: { [id: string]: WeakRefInstance<KeyborgCore> } =\n {};\n private _isNavigatingWithKeyboard = false;\n\n add(keyborg: KeyborgCore): void {\n const id = keyborg.id;\n\n if (!(id in this.__keyborgCoreRefs)) {\n this.__keyborgCoreRefs[id] = new WeakRefInstance<KeyborgCore>(keyborg);\n }\n }\n\n remove(id: string): void {\n delete this.__keyborgCoreRefs[id];\n\n if (Object.keys(this.__keyborgCoreRefs).length === 0) {\n this._isNavigatingWithKeyboard = false;\n }\n }\n\n setVal(isNavigatingWithKeyboard: boolean): void {\n if (this._isNavigatingWithKeyboard === isNavigatingWithKeyboard) {\n return;\n }\n\n this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;\n\n for (const id of Object.keys(this.__keyborgCoreRefs)) {\n const ref = this.__keyborgCoreRefs[id];\n const keyborg = ref.deref();\n\n if (keyborg) {\n keyborg.update(isNavigatingWithKeyboard);\n } else {\n this.remove(id);\n }\n }\n }\n\n getVal(): boolean {\n return this._isNavigatingWithKeyboard;\n }\n}\n\nconst _state = new KeyborgState();\n\n/**\n * Manages a collection of Keyborg instances in a window/document and updates keyborg state\n */\nclass KeyborgCore implements Disposable {\n readonly id: string;\n\n private _win?: WindowWithKeyborg;\n private _isMouseUsedTimer: number | undefined;\n private _dismissTimer: number | undefined;\n private _triggerKeys?: Set<number>;\n private _dismissKeys?: Set<number>;\n\n constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this.id = \"c\" + ++_lastId;\n this._win = win;\n const doc = win.document;\n\n if (props) {\n const triggerKeys = props.triggerKeys;\n const dismissKeys = props.dismissKeys;\n\n if (triggerKeys?.length) {\n this._triggerKeys = new Set(triggerKeys);\n }\n\n if (dismissKeys?.length) {\n this._dismissKeys = new Set(dismissKeys);\n }\n }\n\n doc.addEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.addEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.addEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n setupFocusEvent(win);\n\n _state.add(this);\n }\n\n dispose(): void {\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n this._isMouseUsedTimer = undefined;\n }\n\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n disposeFocusEvent(win);\n\n const doc = win.document;\n\n doc.removeEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.removeEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.removeEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n delete this._win;\n\n _state.remove(this.id);\n }\n }\n\n isDisposed(): boolean {\n return !!this._win;\n }\n\n /**\n * Updates all keyborg instances with the keyboard navigation state\n */\n update(isNavigatingWithKeyboard: boolean): void {\n const keyborgs = this._win?.__keyborg?.refs;\n\n if (keyborgs) {\n for (const id of Object.keys(keyborgs)) {\n Keyborg.update(keyborgs[id], isNavigatingWithKeyboard);\n }\n }\n }\n\n private _onFocusIn = (e: KeyborgFocusInEvent) => {\n // When the focus is moved not programmatically and without keydown events,\n // it is likely that the focus is moved by screen reader (as it might swallow\n // the events when the screen reader shortcuts are used). The screen reader\n // usage is keyboard navigation.\n\n if (this._isMouseUsedTimer) {\n // There was a mouse event recently.\n return;\n }\n\n if (_state.getVal()) {\n return;\n }\n\n const details = e.detail;\n\n if (!details.relatedTarget) {\n return;\n }\n\n if (\n details.isFocusedProgrammatically ||\n details.isFocusedProgrammatically === undefined\n ) {\n // The element is focused programmatically, or the programmatic focus detection\n // is not working.\n return;\n }\n\n _state.setVal(true);\n };\n\n private _onMouseDown = (e: MouseEvent): void => {\n if (\n e.buttons === 0 ||\n (e.clientX === 0 && e.clientY === 0 && e.screenX === 0 && e.screenY === 0)\n ) {\n // This is most likely an event triggered by the screen reader to perform\n // an action on an element, do not dismiss the keyboard navigation mode.\n return;\n }\n\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n }\n\n this._isMouseUsedTimer = win.setTimeout(() => {\n delete this._isMouseUsedTimer;\n }, 1000); // Keeping the indication of the mouse usage for some time.\n }\n\n _state.setVal(false);\n };\n\n private _onKeyDown = (e: KeyboardEvent): void => {\n const isNavigatingWithKeyboard = _state.getVal();\n\n if (isNavigatingWithKeyboard) {\n if (this._shouldDismissKeyboardNavigation(e)) {\n this._scheduleDismiss();\n }\n } else {\n if (this._shouldTriggerKeyboardNavigation(e)) {\n _state.setVal(true);\n }\n }\n };\n\n /**\n * @returns whether the keyboard event should trigger keyboard navigation mode\n */\n private _shouldTriggerKeyboardNavigation(e: KeyboardEvent) {\n // TODO Some rich text fields can allow Tab key for indentation so it doesn't\n // need to be a navigation key. If there is a bug regarding that we should revisit\n if (e.key === \"Tab\") {\n return true;\n }\n\n const activeElement = this._win?.document\n .activeElement as HTMLElement | null;\n const isTriggerKey = !this._triggerKeys || this._triggerKeys.has(e.keyCode);\n\n const isEditable =\n activeElement &&\n (activeElement.tagName === \"INPUT\" ||\n activeElement.tagName === \"TEXTAREA\" ||\n activeElement.isContentEditable);\n\n return isTriggerKey && !isEditable;\n }\n\n /**\n * @returns whether the keyboard event should dismiss keyboard navigation mode\n */\n private _shouldDismissKeyboardNavigation(e: KeyboardEvent) {\n return this._dismissKeys?.has(e.keyCode);\n }\n\n private _scheduleDismiss(): void {\n const win = this._win;\n\n if (win) {\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n const was = win.document.activeElement;\n\n this._dismissTimer = win.setTimeout(() => {\n this._dismissTimer = undefined;\n\n const cur = win.document.activeElement;\n\n if (was && cur && was === cur) {\n // Esc was pressed, currently focused element hasn't changed.\n // Just dismiss the keyboard navigation mode.\n _state.setVal(false);\n }\n }, _dismissTimeout);\n }\n }\n}\n\n/**\n * Used to determine the keyboard navigation state\n */\nexport class Keyborg {\n private _id: string;\n private _win?: WindowWithKeyborg;\n private _core?: KeyborgCore;\n private _cb: KeyborgCallback[] = [];\n\n static create(win: WindowWithKeyborg, props?: KeyborgProps): Keyborg {\n return new Keyborg(win, props);\n }\n\n static dispose(instance: Keyborg): void {\n instance.dispose();\n }\n\n /**\n * Updates all subscribed callbacks with the keyboard navigation state\n */\n static update(instance: Keyborg, isNavigatingWithKeyboard: boolean): void {\n instance._cb.forEach((callback) => callback(isNavigatingWithKeyboard));\n }\n\n private constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this._id = \"k\" + ++_lastId;\n this._win = win;\n\n const current = win.__keyborg;\n\n if (current) {\n this._core = current.core;\n current.refs[this._id] = this;\n } else {\n this._core = new KeyborgCore(win, props);\n win.__keyborg = {\n core: this._core,\n refs: { [this._id]: this },\n };\n }\n }\n\n private dispose(): void {\n const current = this._win?.__keyborg;\n\n if (current?.refs[this._id]) {\n delete current.refs[this._id];\n\n if (Object.keys(current.refs).length === 0) {\n current.core.dispose();\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n delete this._win!.__keyborg;\n }\n } else if (process.env.NODE_ENV !== \"production\") {\n console.error(\n `Keyborg instance ${this._id} is being disposed incorrectly.`,\n );\n }\n\n this._cb = [];\n delete this._core;\n delete this._win;\n }\n\n /**\n * @returns Whether the user is navigating with keyboard\n */\n isNavigatingWithKeyboard(): boolean {\n return _state.getVal();\n }\n\n /**\n * @param callback - Called when the keyboard navigation state changes\n */\n subscribe(callback: KeyborgCallback): void {\n this._cb.push(callback);\n }\n\n /**\n * @param callback - Registered with subscribe\n */\n unsubscribe(callback: KeyborgCallback): void {\n const index = this._cb.indexOf(callback);\n\n if (index >= 0) {\n this._cb.splice(index, 1);\n }\n }\n\n /**\n * Manually set the keyboard navigtion state\n */\n setVal(isNavigatingWithKeyboard: boolean): void {\n _state.setVal(isNavigatingWithKeyboard);\n }\n}\n\nexport function createKeyborg(win: Window, props?: KeyborgProps): Keyborg {\n return Keyborg.create(win, props);\n}\n\nexport function disposeKeyborg(instance: Keyborg) {\n Keyborg.dispose(instance);\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport type { Keyborg, KeyborgCallback } from \"./Keyborg\";\nexport { createKeyborg, disposeKeyborg } from \"./Keyborg\";\n\nexport type {\n KeyborgFocusInEvent,\n KeyborgFocusInEventDetails,\n} from \"./FocusEvent\";\nexport {\n getLastFocusedProgrammatically,\n nativeFocus,\n KEYBORG_FOCUSIN,\n} from \"./FocusEvent\";\n\nexport const version = process.env.PKG_VERSION;\n"],"mappings":";AAMO,IAAM,iBAAiB,OAAO,YAAY;AAc1C,IAAM,kBAAN,MAAqD;AAAA,EAI1D,YAAY,UAAa;AACvB,QAAI,kBAAkB,OAAO,aAAa,UAAU;AAClD,WAAK,WAAW,IAAI,QAAQ,QAAQ;AAAA,IACtC,OAAO;AACL,WAAK,YAAY;AAAA,IACnB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,QAAuB;AAnCzB;AAoCI,QAAI;AAEJ,QAAI,KAAK,UAAU;AACjB,kBAAW,UAAK,aAAL,mBAAe;AAE1B,UAAI,CAAC,UAAU;AACb,eAAO,KAAK;AAAA,MACd;AAAA,IACF,OAAO;AACL,iBAAW,KAAK;AAChB,WAAK,0CAAyB,eAAzB,mCAAyC;AAC5C,eAAO,KAAK;AAAA,MACd;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;;;AC/CO,IAAM,kBAAkB;AAsB/B,SA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M;AAAA,MACpB;AAAA,IACF,WAAW,QAAQ,IAAI,aAAa,cAAc;AAChD,cAAQ;AAAA,QACN,oBAAoB,KAAK,GAAG;AAAA,MAC9B;AAAA,IACF;AAEA,SAAK,MAAM,CAAC;AACZ,WAAO,KAAK;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,2BAAoC;AAClC,WAAO,OAAO,OAAO;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,UAAiC;AACzC,SAAK,IAAI,KAAK,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,UAAiC;AAC3C,UAAM,QAAQ,KAAK,IAAI,QAAQ,QAAQ;AAEvC,QAAI,SAAS,GAAG;AACd,WAAK,IAAI,OAAO,OAAO,CAAC;AAAA,IAC1B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,0BAAyC;AAC9C,WAAO,OAAO,wBAAwB;AAAA,EACxC;AACF;AAEO,SAAS,cAAc,KAAa,OAA+B;AACxE,SAAO,QAAQ,OAAO,KAAK,KAAK;AAClC;AAEO,SAAS,eAAe,UAAmB;AAChD,UAAQ,QAAQ,QAAQ;AAC1B;;;AC/XO,IAAM,UAAU;","names":[]}
package/dist/index.d.mts CHANGED
@@ -43,6 +43,14 @@ declare class KeyborgCore implements Disposable {
43
43
  private _onFocusIn;
44
44
  private _onMouseDown;
45
45
  private _onKeyDown;
46
+ /**
47
+ * @returns whether the keyboard event should trigger keyboard navigation mode
48
+ */
49
+ private _shouldTriggerKeyboardNavigation;
50
+ /**
51
+ * @returns whether the keyboard event should dismiss keyboard navigation mode
52
+ */
53
+ private _shouldDismissKeyboardNavigation;
46
54
  private _scheduleDismiss;
47
55
  }
48
56
  /**
package/dist/index.d.ts CHANGED
@@ -43,6 +43,14 @@ declare class KeyborgCore implements Disposable {
43
43
  private _onFocusIn;
44
44
  private _onMouseDown;
45
45
  private _onKeyDown;
46
+ /**
47
+ * @returns whether the keyboard event should trigger keyboard navigation mode
48
+ */
49
+ private _shouldTriggerKeyboardNavigation;
50
+ /**
51
+ * @returns whether the keyboard event should dismiss keyboard navigation mode
52
+ */
53
+ private _shouldDismissKeyboardNavigation;
46
54
  private _scheduleDismiss;
47
55
  }
48
56
  /**
package/dist/index.js CHANGED
@@ -252,18 +252,15 @@ var KeyborgCore = class {
252
252
  _state.setVal(false);
253
253
  };
254
254
  this._onKeyDown = (e) => {
255
- var _a, _b;
256
255
  const isNavigatingWithKeyboard = _state.getVal();
257
- const keyCode = e.keyCode;
258
- const triggerKeys = this._triggerKeys;
259
- if (!isNavigatingWithKeyboard && (!triggerKeys || triggerKeys.has(keyCode))) {
260
- const activeElement = (_a = this._win) == null ? void 0 : _a.document.activeElement;
261
- if (activeElement && (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA" || activeElement.contentEditable === "true")) {
262
- return;
256
+ if (isNavigatingWithKeyboard) {
257
+ if (this._shouldDismissKeyboardNavigation(e)) {
258
+ this._scheduleDismiss();
259
+ }
260
+ } else {
261
+ if (this._shouldTriggerKeyboardNavigation(e)) {
262
+ _state.setVal(true);
263
263
  }
264
- _state.setVal(true);
265
- } else if (isNavigatingWithKeyboard && ((_b = this._dismissKeys) == null ? void 0 : _b.has(keyCode))) {
266
- this._scheduleDismiss();
267
264
  }
268
265
  };
269
266
  this.id = "c" + ++_lastId;
@@ -320,6 +317,26 @@ var KeyborgCore = class {
320
317
  }
321
318
  }
322
319
  }
320
+ /**
321
+ * @returns whether the keyboard event should trigger keyboard navigation mode
322
+ */
323
+ _shouldTriggerKeyboardNavigation(e) {
324
+ var _a;
325
+ if (e.key === "Tab") {
326
+ return true;
327
+ }
328
+ const activeElement = (_a = this._win) == null ? void 0 : _a.document.activeElement;
329
+ const isTriggerKey = !this._triggerKeys || this._triggerKeys.has(e.keyCode);
330
+ const isEditable = activeElement && (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA" || activeElement.isContentEditable);
331
+ return isTriggerKey && !isEditable;
332
+ }
333
+ /**
334
+ * @returns whether the keyboard event should dismiss keyboard navigation mode
335
+ */
336
+ _shouldDismissKeyboardNavigation(e) {
337
+ var _a;
338
+ return (_a = this._dismissKeys) == null ? void 0 : _a.has(e.keyCode);
339
+ }
323
340
  _scheduleDismiss() {
324
341
  const win = this._win;
325
342
  if (win) {
@@ -421,7 +438,7 @@ function disposeKeyborg(instance) {
421
438
  }
422
439
 
423
440
  // src/index.ts
424
- var version = "2.4.0";
441
+ var version = "2.4.1";
425
442
  // Annotate the CommonJS export names for ESM import in node:
426
443
  0 && (module.exports = {
427
444
  KEYBORG_FOCUSIN,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/WeakRefInstance.ts","../src/FocusEvent.ts","../src/Keyborg.ts"],"sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport type { Keyborg, KeyborgCallback } from \"./Keyborg\";\nexport { createKeyborg, disposeKeyborg } from \"./Keyborg\";\n\nexport type {\n KeyborgFocusInEvent,\n KeyborgFocusInEventDetails,\n} from \"./FocusEvent\";\nexport {\n getLastFocusedProgrammatically,\n nativeFocus,\n KEYBORG_FOCUSIN,\n} from \"./FocusEvent\";\n\nexport const version = process.env.PKG_VERSION;\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\n// IE11 compat, checks if WeakRef is supported\nexport const _canUseWeakRef = typeof WeakRef !== \"undefined\";\n\n/**\n * Allows disposable instances to be used\n */\nexport interface Disposable {\n isDisposed?(): boolean;\n}\n\n/**\n * WeakRef wrapper around a HTMLElement that also supports IE11\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef}\n * @internal\n */\nexport class WeakRefInstance<T extends Disposable | object> {\n private _weakRef?: WeakRef<T>;\n private _instance?: T;\n\n constructor(instance: T) {\n if (_canUseWeakRef && typeof instance === \"object\") {\n this._weakRef = new WeakRef(instance);\n } else {\n this._instance = instance;\n }\n }\n\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref}\n */\n deref(): T | undefined {\n let instance: T | undefined;\n\n if (this._weakRef) {\n instance = this._weakRef?.deref();\n\n if (!instance) {\n delete this._weakRef;\n }\n } else {\n instance = this._instance;\n if ((instance as Disposable)?.isDisposed?.()) {\n delete this._instance;\n }\n }\n\n return instance;\n }\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { WeakRefInstance } from \"./WeakRefInstance\";\n\nexport const KEYBORG_FOCUSIN = \"keyborg:focusin\";\n\ninterface KeyborgFocus {\n /**\n * This is the native `focus` function that is retained so that it can be restored when keyborg is disposed\n */\n __keyborgNativeFocus?: (options?: FocusOptions | undefined) => void;\n}\n\ninterface KeyborgFocusEventData {\n focusInHandler: (e: FocusEvent) => void;\n lastFocusedProgrammatically?: WeakRefInstance<HTMLElement>;\n}\n\n/**\n * Extends the global window with keyborg focus event data\n */\ninterface WindowWithKeyborgFocusEvent extends Window {\n HTMLElement: typeof HTMLElement;\n __keyborgData?: KeyborgFocusEventData;\n}\n\nfunction canOverrideNativeFocus(win: Window): boolean {\n const HTMLElement = (win as WindowWithKeyborgFocusEvent).HTMLElement;\n const origFocus = HTMLElement.prototype.focus;\n\n let isCustomFocusCalled = false;\n\n HTMLElement.prototype.focus = function focus(): void {\n isCustomFocusCalled = true;\n };\n\n const btn = win.document.createElement(\"button\");\n\n btn.focus();\n\n HTMLElement.prototype.focus = origFocus;\n\n return isCustomFocusCalled;\n}\n\nlet _canOverrideNativeFocus = false;\n\nexport interface KeyborgFocusInEventDetails {\n relatedTarget?: HTMLElement;\n isFocusedProgrammatically?: boolean;\n}\n\nexport interface KeyborgFocusInEvent\n extends CustomEvent<KeyborgFocusInEventDetails> {\n /**\n * @deprecated - used `event.detail`\n */\n details?: KeyborgFocusInEventDetails;\n}\n\n/**\n * Guarantees that the native `focus` will be used\n */\nexport function nativeFocus(element: HTMLElement): void {\n const focus = element.focus as KeyborgFocus;\n\n if (focus.__keyborgNativeFocus) {\n focus.__keyborgNativeFocus.call(element);\n } else {\n element.focus();\n }\n}\n\n/**\n * Overrides the native `focus` and setups the keyborg focus event\n */\nexport function setupFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n\n if (!_canOverrideNativeFocus) {\n _canOverrideNativeFocus = canOverrideNativeFocus(kwin);\n }\n\n const origFocus = kwin.HTMLElement.prototype.focus;\n\n if ((origFocus as KeyborgFocus).__keyborgNativeFocus) {\n // Already set up.\n return;\n }\n\n kwin.HTMLElement.prototype.focus = focus;\n\n const focusOutShadowRootHandler = (e: FocusEvent) => {\n const relatedTarget = e.relatedTarget as HTMLElement | null;\n const currentTarget = e.currentTarget as ShadowRoot;\n\n // cleanup polyfill event handlers once focus leaves the shadow root\n if (!currentTarget.contains(relatedTarget)) {\n currentTarget.removeEventListener(\"focusin\", focusInHandler, true);\n currentTarget.removeEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n }\n };\n\n const focusInHandler = (e: FocusEvent) => {\n const target = e.target as HTMLElement;\n\n if (!target) {\n return;\n }\n\n if (target.shadowRoot) {\n /**\n * https://bugs.chromium.org/p/chromium/issues/detail?id=1512028\n * focusin events don't bubble up through an open shadow root once focus is inside\n * once focus moves into a shadow root - we drop the same focusin handler there\n * keyborg's custom event will still bubble up since it is composed\n * event handlers should be cleaned up once focus leaves the shadow root.\n * \n * When a focusin event is dispatched from a shadow root, its target is the shadow root parent.\n * Each shadow root encounter requires a new capture listener.\n * Why capture? - we want to follow the focus event in order or descending nested shadow roots\n * When there are no more shadow root targets - dispatch the keyborg:focusin event\n * \n * 1. no focus event\n * > document - capture listener ✅\n * > shadow root 1\n * > shadow root 2\n * > shadow root 3\n * > focused element\n * \n * 2. focus event received by document listener\n * > document - capture listener ✅ (focus event here)\n * > shadow root 1 - capture listener ✅\n * > shadow root 2\n * > shadow root 3\n * > focused element\n\n * 3. focus event received by root l1 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅ (focus event here)\n * > shadow root 2 - capture listener ✅\n * > shadow root 3\n * > focused element\n *\n * 4. focus event received by root l2 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅ (focus event here)\n * > shadow root 3 - capture listener ✅ \n * > focused element\n * \n * 5. focus event received by root l3 listener, no more shadow root targets\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅\n * > shadow root 3 - capture listener ✅ (focus event here)\n * > focused element ✅ (no shadow root - dispatch keyborg event)\n */\n target.shadowRoot.addEventListener(\"focusin\", focusInHandler, true);\n target.shadowRoot.addEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n\n return;\n }\n\n const details: KeyborgFocusInEventDetails = {\n relatedTarget: (e.relatedTarget as HTMLElement) || undefined,\n };\n\n const event: KeyborgFocusInEvent = new CustomEvent(KEYBORG_FOCUSIN, {\n cancelable: true,\n bubbles: true,\n // Allows the event to bubble past an open shadow root\n composed: true,\n detail: details,\n });\n\n // Tabster (and other users) can still use the legacy details field - keeping for backwards compat\n event.details = details;\n\n if (_canOverrideNativeFocus || data.lastFocusedProgrammatically) {\n details.isFocusedProgrammatically =\n target === data.lastFocusedProgrammatically?.deref();\n\n data.lastFocusedProgrammatically = undefined;\n }\n\n target.dispatchEvent(event);\n };\n\n const data: KeyborgFocusEventData = (kwin.__keyborgData = {\n focusInHandler,\n });\n\n kwin.document.addEventListener(\n \"focusin\",\n kwin.__keyborgData.focusInHandler,\n true,\n );\n\n function focus(this: HTMLElement) {\n const keyborgNativeFocusEvent = (kwin as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n keyborgNativeFocusEvent.lastFocusedProgrammatically = new WeakRefInstance(\n this,\n );\n }\n\n // eslint-disable-next-line prefer-rest-params\n return origFocus.apply(this, arguments);\n }\n\n (focus as KeyborgFocus).__keyborgNativeFocus = origFocus;\n}\n\n/**\n * Removes keyborg event listeners and custom focus override\n * @param win The window that stores keyborg focus events\n */\nexport function disposeFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n const proto = kwin.HTMLElement.prototype;\n const origFocus = (proto.focus as KeyborgFocus).__keyborgNativeFocus;\n const keyborgNativeFocusEvent = kwin.__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n kwin.document.removeEventListener(\n \"focusin\",\n keyborgNativeFocusEvent.focusInHandler,\n true,\n );\n delete kwin.__keyborgData;\n }\n\n if (origFocus) {\n proto.focus = origFocus;\n }\n}\n\n/**\n * @param win The window that stores keyborg focus events\n * @returns The last element focused with element.focus()\n */\nexport function getLastFocusedProgrammatically(\n win: Window,\n): HTMLElement | null | undefined {\n const keyborgNativeFocusEvent = (win as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n return keyborgNativeFocusEvent\n ? keyborgNativeFocusEvent.lastFocusedProgrammatically?.deref() || null\n : undefined;\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n disposeFocusEvent,\n KeyborgFocusInEvent,\n KEYBORG_FOCUSIN,\n setupFocusEvent,\n} from \"./FocusEvent\";\nimport { Disposable, WeakRefInstance } from \"./WeakRefInstance\";\n\ninterface WindowWithKeyborg extends Window {\n __keyborg?: {\n core: KeyborgCore;\n refs: { [id: string]: Keyborg };\n };\n}\n\nconst _dismissTimeout = 500; // When a key from dismissKeys is pressed and the focus is not moved\n// during _dismissTimeout time, dismiss the keyboard navigation mode.\n\nlet _lastId = 0;\n\nexport interface KeyborgProps {\n // Keys to be used to trigger keyboard navigation mode. By default, any key will trigger\n // it. Could be limited to, for example, just Tab (or Tab and arrow keys).\n triggerKeys?: number[];\n // Keys to be used to dismiss keyboard navigation mode using keyboard (in addition to\n // mouse clicks which dismiss it). For example, Esc could be used to dismiss.\n dismissKeys?: number[];\n}\n\nexport type KeyborgCallback = (isNavigatingWithKeyboard: boolean) => void;\n\n/**\n * Source of truth for all the keyborg core instances and the current keyboard navigation state\n */\nexport class KeyborgState {\n private __keyborgCoreRefs: { [id: string]: WeakRefInstance<KeyborgCore> } =\n {};\n private _isNavigatingWithKeyboard = false;\n\n add(keyborg: KeyborgCore): void {\n const id = keyborg.id;\n\n if (!(id in this.__keyborgCoreRefs)) {\n this.__keyborgCoreRefs[id] = new WeakRefInstance<KeyborgCore>(keyborg);\n }\n }\n\n remove(id: string): void {\n delete this.__keyborgCoreRefs[id];\n\n if (Object.keys(this.__keyborgCoreRefs).length === 0) {\n this._isNavigatingWithKeyboard = false;\n }\n }\n\n setVal(isNavigatingWithKeyboard: boolean): void {\n if (this._isNavigatingWithKeyboard === isNavigatingWithKeyboard) {\n return;\n }\n\n this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;\n\n for (const id of Object.keys(this.__keyborgCoreRefs)) {\n const ref = this.__keyborgCoreRefs[id];\n const keyborg = ref.deref();\n\n if (keyborg) {\n keyborg.update(isNavigatingWithKeyboard);\n } else {\n this.remove(id);\n }\n }\n }\n\n getVal(): boolean {\n return this._isNavigatingWithKeyboard;\n }\n}\n\nconst _state = new KeyborgState();\n\n/**\n * Manages a collection of Keyborg instances in a window/document and updates keyborg state\n */\nclass KeyborgCore implements Disposable {\n readonly id: string;\n\n private _win?: WindowWithKeyborg;\n private _isMouseUsedTimer: number | undefined;\n private _dismissTimer: number | undefined;\n private _triggerKeys?: Set<number>;\n private _dismissKeys?: Set<number>;\n\n constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this.id = \"c\" + ++_lastId;\n this._win = win;\n const doc = win.document;\n\n if (props) {\n const triggerKeys = props.triggerKeys;\n const dismissKeys = props.dismissKeys;\n\n if (triggerKeys?.length) {\n this._triggerKeys = new Set(triggerKeys);\n }\n\n if (dismissKeys?.length) {\n this._dismissKeys = new Set(dismissKeys);\n }\n }\n\n doc.addEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.addEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.addEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n setupFocusEvent(win);\n\n _state.add(this);\n }\n\n dispose(): void {\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n this._isMouseUsedTimer = undefined;\n }\n\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n disposeFocusEvent(win);\n\n const doc = win.document;\n\n doc.removeEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.removeEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.removeEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n delete this._win;\n\n _state.remove(this.id);\n }\n }\n\n isDisposed(): boolean {\n return !!this._win;\n }\n\n /**\n * Updates all keyborg instances with the keyboard navigation state\n */\n update(isNavigatingWithKeyboard: boolean): void {\n const keyborgs = this._win?.__keyborg?.refs;\n\n if (keyborgs) {\n for (const id of Object.keys(keyborgs)) {\n Keyborg.update(keyborgs[id], isNavigatingWithKeyboard);\n }\n }\n }\n\n private _onFocusIn = (e: KeyborgFocusInEvent) => {\n // When the focus is moved not programmatically and without keydown events,\n // it is likely that the focus is moved by screen reader (as it might swallow\n // the events when the screen reader shortcuts are used). The screen reader\n // usage is keyboard navigation.\n\n if (this._isMouseUsedTimer) {\n // There was a mouse event recently.\n return;\n }\n\n if (_state.getVal()) {\n return;\n }\n\n const details = e.detail;\n\n if (!details.relatedTarget) {\n return;\n }\n\n if (\n details.isFocusedProgrammatically ||\n details.isFocusedProgrammatically === undefined\n ) {\n // The element is focused programmatically, or the programmatic focus detection\n // is not working.\n return;\n }\n\n _state.setVal(true);\n };\n\n private _onMouseDown = (e: MouseEvent): void => {\n if (\n e.buttons === 0 ||\n (e.clientX === 0 && e.clientY === 0 && e.screenX === 0 && e.screenY === 0)\n ) {\n // This is most likely an event triggered by the screen reader to perform\n // an action on an element, do not dismiss the keyboard navigation mode.\n return;\n }\n\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n }\n\n this._isMouseUsedTimer = win.setTimeout(() => {\n delete this._isMouseUsedTimer;\n }, 1000); // Keeping the indication of the mouse usage for some time.\n }\n\n _state.setVal(false);\n };\n\n private _onKeyDown = (e: KeyboardEvent): void => {\n const isNavigatingWithKeyboard = _state.getVal();\n\n const keyCode = e.keyCode;\n const triggerKeys = this._triggerKeys;\n\n if (\n !isNavigatingWithKeyboard &&\n (!triggerKeys || triggerKeys.has(keyCode))\n ) {\n const activeElement = this._win?.document.activeElement as\n | HTMLElement\n | null\n | undefined;\n\n if (\n activeElement &&\n (activeElement.tagName === \"INPUT\" ||\n activeElement.tagName === \"TEXTAREA\" ||\n activeElement.contentEditable === \"true\")\n ) {\n // We're inside an input, textarea or contenteditable, it's not\n // keyboard navigation, it is text editing scenario.\n return;\n }\n\n _state.setVal(true);\n } else if (isNavigatingWithKeyboard && this._dismissKeys?.has(keyCode)) {\n this._scheduleDismiss();\n }\n };\n\n private _scheduleDismiss(): void {\n const win = this._win;\n\n if (win) {\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n const was = win.document.activeElement;\n\n this._dismissTimer = win.setTimeout(() => {\n this._dismissTimer = undefined;\n\n const cur = win.document.activeElement;\n\n if (was && cur && was === cur) {\n // Esc was pressed, currently focused element hasn't changed.\n // Just dismiss the keyboard navigation mode.\n _state.setVal(false);\n }\n }, _dismissTimeout);\n }\n }\n}\n\n/**\n * Used to determine the keyboard navigation state\n */\nexport class Keyborg {\n private _id: string;\n private _win?: WindowWithKeyborg;\n private _core?: KeyborgCore;\n private _cb: KeyborgCallback[] = [];\n\n static create(win: WindowWithKeyborg, props?: KeyborgProps): Keyborg {\n return new Keyborg(win, props);\n }\n\n static dispose(instance: Keyborg): void {\n instance.dispose();\n }\n\n /**\n * Updates all subscribed callbacks with the keyboard navigation state\n */\n static update(instance: Keyborg, isNavigatingWithKeyboard: boolean): void {\n instance._cb.forEach((callback) => callback(isNavigatingWithKeyboard));\n }\n\n private constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this._id = \"k\" + ++_lastId;\n this._win = win;\n\n const current = win.__keyborg;\n\n if (current) {\n this._core = current.core;\n current.refs[this._id] = this;\n } else {\n this._core = new KeyborgCore(win, props);\n win.__keyborg = {\n core: this._core,\n refs: { [this._id]: this },\n };\n }\n }\n\n private dispose(): void {\n const current = this._win?.__keyborg;\n\n if (current?.refs[this._id]) {\n delete current.refs[this._id];\n\n if (Object.keys(current.refs).length === 0) {\n current.core.dispose();\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n delete this._win!.__keyborg;\n }\n } else if (process.env.NODE_ENV !== \"production\") {\n console.error(\n `Keyborg instance ${this._id} is being disposed incorrectly.`,\n );\n }\n\n this._cb = [];\n delete this._core;\n delete this._win;\n }\n\n /**\n * @returns Whether the user is navigating with keyboard\n */\n isNavigatingWithKeyboard(): boolean {\n return _state.getVal();\n }\n\n /**\n * @param callback - Called when the keyboard navigation state changes\n */\n subscribe(callback: KeyborgCallback): void {\n this._cb.push(callback);\n }\n\n /**\n * @param callback - Registered with subscribe\n */\n unsubscribe(callback: KeyborgCallback): void {\n const index = this._cb.indexOf(callback);\n\n if (index >= 0) {\n this._cb.splice(index, 1);\n }\n }\n\n /**\n * Manually set the keyboard navigtion state\n */\n setVal(isNavigatingWithKeyboard: boolean): void {\n _state.setVal(isNavigatingWithKeyboard);\n }\n}\n\nexport function createKeyborg(win: Window, props?: KeyborgProps): Keyborg {\n return Keyborg.create(win, props);\n}\n\nexport function disposeKeyborg(instance: Keyborg) {\n Keyborg.dispose(instance);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AA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yC;AACxE,aAAS,IAAI,QAAQ,CAAC,aAAa,SAAS,wBAAwB,CAAC;AAAA,EACvE;AAAA,EAoBQ,UAAgB;AAxU1B;AAyUI,UAAM,WAAU,UAAK,SAAL,mBAAW;AAE3B,QAAI,mCAAS,KAAK,KAAK,MAAM;AAC3B,aAAO,QAAQ,KAAK,KAAK,GAAG;AAE5B,UAAI,OAAO,KAAK,QAAQ,IAAI,EAAE,WAAW,GAAG;AAC1C,gBAAQ,KAAK,QAAQ;AAErB,eAAO,KAAK,KAAM;AAAA,MACpB;AAAA,IACF,WAAW,QAAQ,IAAI,aAAa,cAAc;AAChD,cAAQ;AAAA,QACN,oBAAoB,KAAK,GAAG;AAAA,MAC9B;AAAA,IACF;AAEA,SAAK,MAAM,CAAC;AACZ,WAAO,KAAK;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,2BAAoC;AAClC,WAAO,OAAO,OAAO;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,UAAiC;AACzC,SAAK,IAAI,KAAK,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,UAAiC;AAC3C,UAAM,QAAQ,KAAK,IAAI,QAAQ,QAAQ;AAEvC,QAAI,SAAS,GAAG;AACd,WAAK,IAAI,OAAO,OAAO,CAAC;AAAA,IAC1B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,0BAAyC;AAC9C,WAAO,OAAO,wBAAwB;AAAA,EACxC;AACF;AAEO,SAAS,cAAc,KAAa,OAA+B;AACxE,SAAO,QAAQ,OAAO,KAAK,KAAK;AAClC;AAEO,SAAS,eAAe,UAAmB;AAChD,UAAQ,QAAQ,QAAQ;AAC1B;;;AHnXO,IAAM,UAAU;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/WeakRefInstance.ts","../src/FocusEvent.ts","../src/Keyborg.ts"],"sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport type { Keyborg, KeyborgCallback } from \"./Keyborg\";\nexport { createKeyborg, disposeKeyborg } from \"./Keyborg\";\n\nexport type {\n KeyborgFocusInEvent,\n KeyborgFocusInEventDetails,\n} from \"./FocusEvent\";\nexport {\n getLastFocusedProgrammatically,\n nativeFocus,\n KEYBORG_FOCUSIN,\n} from \"./FocusEvent\";\n\nexport const version = process.env.PKG_VERSION;\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\n// IE11 compat, checks if WeakRef is supported\nexport const _canUseWeakRef = typeof WeakRef !== \"undefined\";\n\n/**\n * Allows disposable instances to be used\n */\nexport interface Disposable {\n isDisposed?(): boolean;\n}\n\n/**\n * WeakRef wrapper around a HTMLElement that also supports IE11\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef}\n * @internal\n */\nexport class WeakRefInstance<T extends Disposable | object> {\n private _weakRef?: WeakRef<T>;\n private _instance?: T;\n\n constructor(instance: T) {\n if (_canUseWeakRef && typeof instance === \"object\") {\n this._weakRef = new WeakRef(instance);\n } else {\n this._instance = instance;\n }\n }\n\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef/deref}\n */\n deref(): T | undefined {\n let instance: T | undefined;\n\n if (this._weakRef) {\n instance = this._weakRef?.deref();\n\n if (!instance) {\n delete this._weakRef;\n }\n } else {\n instance = this._instance;\n if ((instance as Disposable)?.isDisposed?.()) {\n delete this._instance;\n }\n }\n\n return instance;\n }\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { WeakRefInstance } from \"./WeakRefInstance\";\n\nexport const KEYBORG_FOCUSIN = \"keyborg:focusin\";\n\ninterface KeyborgFocus {\n /**\n * This is the native `focus` function that is retained so that it can be restored when keyborg is disposed\n */\n __keyborgNativeFocus?: (options?: FocusOptions | undefined) => void;\n}\n\ninterface KeyborgFocusEventData {\n focusInHandler: (e: FocusEvent) => void;\n lastFocusedProgrammatically?: WeakRefInstance<HTMLElement>;\n}\n\n/**\n * Extends the global window with keyborg focus event data\n */\ninterface WindowWithKeyborgFocusEvent extends Window {\n HTMLElement: typeof HTMLElement;\n __keyborgData?: KeyborgFocusEventData;\n}\n\nfunction canOverrideNativeFocus(win: Window): boolean {\n const HTMLElement = (win as WindowWithKeyborgFocusEvent).HTMLElement;\n const origFocus = HTMLElement.prototype.focus;\n\n let isCustomFocusCalled = false;\n\n HTMLElement.prototype.focus = function focus(): void {\n isCustomFocusCalled = true;\n };\n\n const btn = win.document.createElement(\"button\");\n\n btn.focus();\n\n HTMLElement.prototype.focus = origFocus;\n\n return isCustomFocusCalled;\n}\n\nlet _canOverrideNativeFocus = false;\n\nexport interface KeyborgFocusInEventDetails {\n relatedTarget?: HTMLElement;\n isFocusedProgrammatically?: boolean;\n}\n\nexport interface KeyborgFocusInEvent\n extends CustomEvent<KeyborgFocusInEventDetails> {\n /**\n * @deprecated - used `event.detail`\n */\n details?: KeyborgFocusInEventDetails;\n}\n\n/**\n * Guarantees that the native `focus` will be used\n */\nexport function nativeFocus(element: HTMLElement): void {\n const focus = element.focus as KeyborgFocus;\n\n if (focus.__keyborgNativeFocus) {\n focus.__keyborgNativeFocus.call(element);\n } else {\n element.focus();\n }\n}\n\n/**\n * Overrides the native `focus` and setups the keyborg focus event\n */\nexport function setupFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n\n if (!_canOverrideNativeFocus) {\n _canOverrideNativeFocus = canOverrideNativeFocus(kwin);\n }\n\n const origFocus = kwin.HTMLElement.prototype.focus;\n\n if ((origFocus as KeyborgFocus).__keyborgNativeFocus) {\n // Already set up.\n return;\n }\n\n kwin.HTMLElement.prototype.focus = focus;\n\n const focusOutShadowRootHandler = (e: FocusEvent) => {\n const relatedTarget = e.relatedTarget as HTMLElement | null;\n const currentTarget = e.currentTarget as ShadowRoot;\n\n // cleanup polyfill event handlers once focus leaves the shadow root\n if (!currentTarget.contains(relatedTarget)) {\n currentTarget.removeEventListener(\"focusin\", focusInHandler, true);\n currentTarget.removeEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n }\n };\n\n const focusInHandler = (e: FocusEvent) => {\n const target = e.target as HTMLElement;\n\n if (!target) {\n return;\n }\n\n if (target.shadowRoot) {\n /**\n * https://bugs.chromium.org/p/chromium/issues/detail?id=1512028\n * focusin events don't bubble up through an open shadow root once focus is inside\n * once focus moves into a shadow root - we drop the same focusin handler there\n * keyborg's custom event will still bubble up since it is composed\n * event handlers should be cleaned up once focus leaves the shadow root.\n * \n * When a focusin event is dispatched from a shadow root, its target is the shadow root parent.\n * Each shadow root encounter requires a new capture listener.\n * Why capture? - we want to follow the focus event in order or descending nested shadow roots\n * When there are no more shadow root targets - dispatch the keyborg:focusin event\n * \n * 1. no focus event\n * > document - capture listener ✅\n * > shadow root 1\n * > shadow root 2\n * > shadow root 3\n * > focused element\n * \n * 2. focus event received by document listener\n * > document - capture listener ✅ (focus event here)\n * > shadow root 1 - capture listener ✅\n * > shadow root 2\n * > shadow root 3\n * > focused element\n\n * 3. focus event received by root l1 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅ (focus event here)\n * > shadow root 2 - capture listener ✅\n * > shadow root 3\n * > focused element\n *\n * 4. focus event received by root l2 listener\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅ (focus event here)\n * > shadow root 3 - capture listener ✅ \n * > focused element\n * \n * 5. focus event received by root l3 listener, no more shadow root targets\n * > document - capture listener ✅\n * > shadow root 1 - capture listener ✅\n * > shadow root 2 - capture listener ✅\n * > shadow root 3 - capture listener ✅ (focus event here)\n * > focused element ✅ (no shadow root - dispatch keyborg event)\n */\n target.shadowRoot.addEventListener(\"focusin\", focusInHandler, true);\n target.shadowRoot.addEventListener(\n \"focusout\",\n focusOutShadowRootHandler,\n true,\n );\n\n return;\n }\n\n const details: KeyborgFocusInEventDetails = {\n relatedTarget: (e.relatedTarget as HTMLElement) || undefined,\n };\n\n const event: KeyborgFocusInEvent = new CustomEvent(KEYBORG_FOCUSIN, {\n cancelable: true,\n bubbles: true,\n // Allows the event to bubble past an open shadow root\n composed: true,\n detail: details,\n });\n\n // Tabster (and other users) can still use the legacy details field - keeping for backwards compat\n event.details = details;\n\n if (_canOverrideNativeFocus || data.lastFocusedProgrammatically) {\n details.isFocusedProgrammatically =\n target === data.lastFocusedProgrammatically?.deref();\n\n data.lastFocusedProgrammatically = undefined;\n }\n\n target.dispatchEvent(event);\n };\n\n const data: KeyborgFocusEventData = (kwin.__keyborgData = {\n focusInHandler,\n });\n\n kwin.document.addEventListener(\n \"focusin\",\n kwin.__keyborgData.focusInHandler,\n true,\n );\n\n function focus(this: HTMLElement) {\n const keyborgNativeFocusEvent = (kwin as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n keyborgNativeFocusEvent.lastFocusedProgrammatically = new WeakRefInstance(\n this,\n );\n }\n\n // eslint-disable-next-line prefer-rest-params\n return origFocus.apply(this, arguments);\n }\n\n (focus as KeyborgFocus).__keyborgNativeFocus = origFocus;\n}\n\n/**\n * Removes keyborg event listeners and custom focus override\n * @param win The window that stores keyborg focus events\n */\nexport function disposeFocusEvent(win: Window): void {\n const kwin = win as WindowWithKeyborgFocusEvent;\n const proto = kwin.HTMLElement.prototype;\n const origFocus = (proto.focus as KeyborgFocus).__keyborgNativeFocus;\n const keyborgNativeFocusEvent = kwin.__keyborgData;\n\n if (keyborgNativeFocusEvent) {\n kwin.document.removeEventListener(\n \"focusin\",\n keyborgNativeFocusEvent.focusInHandler,\n true,\n );\n delete kwin.__keyborgData;\n }\n\n if (origFocus) {\n proto.focus = origFocus;\n }\n}\n\n/**\n * @param win The window that stores keyborg focus events\n * @returns The last element focused with element.focus()\n */\nexport function getLastFocusedProgrammatically(\n win: Window,\n): HTMLElement | null | undefined {\n const keyborgNativeFocusEvent = (win as WindowWithKeyborgFocusEvent)\n .__keyborgData;\n\n return keyborgNativeFocusEvent\n ? keyborgNativeFocusEvent.lastFocusedProgrammatically?.deref() || null\n : undefined;\n}\n","/*!\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n disposeFocusEvent,\n KeyborgFocusInEvent,\n KEYBORG_FOCUSIN,\n setupFocusEvent,\n} from \"./FocusEvent\";\nimport { Disposable, WeakRefInstance } from \"./WeakRefInstance\";\n\ninterface WindowWithKeyborg extends Window {\n __keyborg?: {\n core: KeyborgCore;\n refs: { [id: string]: Keyborg };\n };\n}\n\nconst _dismissTimeout = 500; // When a key from dismissKeys is pressed and the focus is not moved\n// during _dismissTimeout time, dismiss the keyboard navigation mode.\n\nlet _lastId = 0;\n\nexport interface KeyborgProps {\n // Keys to be used to trigger keyboard navigation mode. By default, any key will trigger\n // it. Could be limited to, for example, just Tab (or Tab and arrow keys).\n triggerKeys?: number[];\n // Keys to be used to dismiss keyboard navigation mode using keyboard (in addition to\n // mouse clicks which dismiss it). For example, Esc could be used to dismiss.\n dismissKeys?: number[];\n}\n\nexport type KeyborgCallback = (isNavigatingWithKeyboard: boolean) => void;\n\n/**\n * Source of truth for all the keyborg core instances and the current keyboard navigation state\n */\nexport class KeyborgState {\n private __keyborgCoreRefs: { [id: string]: WeakRefInstance<KeyborgCore> } =\n {};\n private _isNavigatingWithKeyboard = false;\n\n add(keyborg: KeyborgCore): void {\n const id = keyborg.id;\n\n if (!(id in this.__keyborgCoreRefs)) {\n this.__keyborgCoreRefs[id] = new WeakRefInstance<KeyborgCore>(keyborg);\n }\n }\n\n remove(id: string): void {\n delete this.__keyborgCoreRefs[id];\n\n if (Object.keys(this.__keyborgCoreRefs).length === 0) {\n this._isNavigatingWithKeyboard = false;\n }\n }\n\n setVal(isNavigatingWithKeyboard: boolean): void {\n if (this._isNavigatingWithKeyboard === isNavigatingWithKeyboard) {\n return;\n }\n\n this._isNavigatingWithKeyboard = isNavigatingWithKeyboard;\n\n for (const id of Object.keys(this.__keyborgCoreRefs)) {\n const ref = this.__keyborgCoreRefs[id];\n const keyborg = ref.deref();\n\n if (keyborg) {\n keyborg.update(isNavigatingWithKeyboard);\n } else {\n this.remove(id);\n }\n }\n }\n\n getVal(): boolean {\n return this._isNavigatingWithKeyboard;\n }\n}\n\nconst _state = new KeyborgState();\n\n/**\n * Manages a collection of Keyborg instances in a window/document and updates keyborg state\n */\nclass KeyborgCore implements Disposable {\n readonly id: string;\n\n private _win?: WindowWithKeyborg;\n private _isMouseUsedTimer: number | undefined;\n private _dismissTimer: number | undefined;\n private _triggerKeys?: Set<number>;\n private _dismissKeys?: Set<number>;\n\n constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this.id = \"c\" + ++_lastId;\n this._win = win;\n const doc = win.document;\n\n if (props) {\n const triggerKeys = props.triggerKeys;\n const dismissKeys = props.dismissKeys;\n\n if (triggerKeys?.length) {\n this._triggerKeys = new Set(triggerKeys);\n }\n\n if (dismissKeys?.length) {\n this._dismissKeys = new Set(dismissKeys);\n }\n }\n\n doc.addEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.addEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.addEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n setupFocusEvent(win);\n\n _state.add(this);\n }\n\n dispose(): void {\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n this._isMouseUsedTimer = undefined;\n }\n\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n disposeFocusEvent(win);\n\n const doc = win.document;\n\n doc.removeEventListener(KEYBORG_FOCUSIN, this._onFocusIn, true); // Capture!\n doc.removeEventListener(\"mousedown\", this._onMouseDown, true); // Capture!\n win.removeEventListener(\"keydown\", this._onKeyDown, true); // Capture!\n\n delete this._win;\n\n _state.remove(this.id);\n }\n }\n\n isDisposed(): boolean {\n return !!this._win;\n }\n\n /**\n * Updates all keyborg instances with the keyboard navigation state\n */\n update(isNavigatingWithKeyboard: boolean): void {\n const keyborgs = this._win?.__keyborg?.refs;\n\n if (keyborgs) {\n for (const id of Object.keys(keyborgs)) {\n Keyborg.update(keyborgs[id], isNavigatingWithKeyboard);\n }\n }\n }\n\n private _onFocusIn = (e: KeyborgFocusInEvent) => {\n // When the focus is moved not programmatically and without keydown events,\n // it is likely that the focus is moved by screen reader (as it might swallow\n // the events when the screen reader shortcuts are used). The screen reader\n // usage is keyboard navigation.\n\n if (this._isMouseUsedTimer) {\n // There was a mouse event recently.\n return;\n }\n\n if (_state.getVal()) {\n return;\n }\n\n const details = e.detail;\n\n if (!details.relatedTarget) {\n return;\n }\n\n if (\n details.isFocusedProgrammatically ||\n details.isFocusedProgrammatically === undefined\n ) {\n // The element is focused programmatically, or the programmatic focus detection\n // is not working.\n return;\n }\n\n _state.setVal(true);\n };\n\n private _onMouseDown = (e: MouseEvent): void => {\n if (\n e.buttons === 0 ||\n (e.clientX === 0 && e.clientY === 0 && e.screenX === 0 && e.screenY === 0)\n ) {\n // This is most likely an event triggered by the screen reader to perform\n // an action on an element, do not dismiss the keyboard navigation mode.\n return;\n }\n\n const win = this._win;\n\n if (win) {\n if (this._isMouseUsedTimer) {\n win.clearTimeout(this._isMouseUsedTimer);\n }\n\n this._isMouseUsedTimer = win.setTimeout(() => {\n delete this._isMouseUsedTimer;\n }, 1000); // Keeping the indication of the mouse usage for some time.\n }\n\n _state.setVal(false);\n };\n\n private _onKeyDown = (e: KeyboardEvent): void => {\n const isNavigatingWithKeyboard = _state.getVal();\n\n if (isNavigatingWithKeyboard) {\n if (this._shouldDismissKeyboardNavigation(e)) {\n this._scheduleDismiss();\n }\n } else {\n if (this._shouldTriggerKeyboardNavigation(e)) {\n _state.setVal(true);\n }\n }\n };\n\n /**\n * @returns whether the keyboard event should trigger keyboard navigation mode\n */\n private _shouldTriggerKeyboardNavigation(e: KeyboardEvent) {\n // TODO Some rich text fields can allow Tab key for indentation so it doesn't\n // need to be a navigation key. If there is a bug regarding that we should revisit\n if (e.key === \"Tab\") {\n return true;\n }\n\n const activeElement = this._win?.document\n .activeElement as HTMLElement | null;\n const isTriggerKey = !this._triggerKeys || this._triggerKeys.has(e.keyCode);\n\n const isEditable =\n activeElement &&\n (activeElement.tagName === \"INPUT\" ||\n activeElement.tagName === \"TEXTAREA\" ||\n activeElement.isContentEditable);\n\n return isTriggerKey && !isEditable;\n }\n\n /**\n * @returns whether the keyboard event should dismiss keyboard navigation mode\n */\n private _shouldDismissKeyboardNavigation(e: KeyboardEvent) {\n return this._dismissKeys?.has(e.keyCode);\n }\n\n private _scheduleDismiss(): void {\n const win = this._win;\n\n if (win) {\n if (this._dismissTimer) {\n win.clearTimeout(this._dismissTimer);\n this._dismissTimer = undefined;\n }\n\n const was = win.document.activeElement;\n\n this._dismissTimer = win.setTimeout(() => {\n this._dismissTimer = undefined;\n\n const cur = win.document.activeElement;\n\n if (was && cur && was === cur) {\n // Esc was pressed, currently focused element hasn't changed.\n // Just dismiss the keyboard navigation mode.\n _state.setVal(false);\n }\n }, _dismissTimeout);\n }\n }\n}\n\n/**\n * Used to determine the keyboard navigation state\n */\nexport class Keyborg {\n private _id: string;\n private _win?: WindowWithKeyborg;\n private _core?: KeyborgCore;\n private _cb: KeyborgCallback[] = [];\n\n static create(win: WindowWithKeyborg, props?: KeyborgProps): Keyborg {\n return new Keyborg(win, props);\n }\n\n static dispose(instance: Keyborg): void {\n instance.dispose();\n }\n\n /**\n * Updates all subscribed callbacks with the keyboard navigation state\n */\n static update(instance: Keyborg, isNavigatingWithKeyboard: boolean): void {\n instance._cb.forEach((callback) => callback(isNavigatingWithKeyboard));\n }\n\n private constructor(win: WindowWithKeyborg, props?: KeyborgProps) {\n this._id = \"k\" + ++_lastId;\n this._win = win;\n\n const current = win.__keyborg;\n\n if (current) {\n this._core = current.core;\n current.refs[this._id] = this;\n } else {\n this._core = new KeyborgCore(win, props);\n win.__keyborg = {\n core: this._core,\n refs: { [this._id]: this },\n };\n }\n }\n\n private dispose(): void {\n const current = this._win?.__keyborg;\n\n if (current?.refs[this._id]) {\n delete current.refs[this._id];\n\n if (Object.keys(current.refs).length === 0) {\n current.core.dispose();\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n delete this._win!.__keyborg;\n }\n } else if (process.env.NODE_ENV !== \"production\") {\n console.error(\n `Keyborg instance ${this._id} is being disposed incorrectly.`,\n );\n }\n\n this._cb = [];\n delete this._core;\n delete this._win;\n }\n\n /**\n * @returns Whether the user is navigating with keyboard\n */\n isNavigatingWithKeyboard(): boolean {\n return _state.getVal();\n }\n\n /**\n * @param callback - Called when the keyboard navigation state changes\n */\n subscribe(callback: KeyborgCallback): void {\n this._cb.push(callback);\n }\n\n /**\n * @param callback - Registered with subscribe\n */\n unsubscribe(callback: KeyborgCallback): void {\n const index = this._cb.indexOf(callback);\n\n if (index >= 0) {\n this._cb.splice(index, 1);\n }\n }\n\n /**\n * Manually set the keyboard navigtion state\n */\n setVal(isNavigatingWithKeyboard: boolean): void {\n _state.setVal(isNavigatingWithKeyboard);\n }\n}\n\nexport function createKeyborg(win: Window, props?: KeyborgProps): Keyborg {\n return Keyborg.create(win, props);\n}\n\nexport function disposeKeyborg(instance: Keyborg) {\n Keyborg.dispose(instance);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,IAAM,iBAAiB,OAAO,YAAY;AAc1C,IAAM,kBAAN,MAAqD;AAAA,EAI1D,YAAY,UAAa;AACvB,QAAI,kBAAkB,OAAO,aAAa,UAAU;AAClD,WAAK,WAAW,IAAI,QAAQ,QAAQ;AAAA,IACtC,OAAO;AACL,WAAK,YAAY;AAAA,IACnB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,QAAuB;AAnCzB;AAoCI,QAAI;AAEJ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,wBAAwB,CAAC;AAAA,EACvE;AAAA,EAoBQ,UAAgB;AApV1B;AAqVI,UAAM,WAAU,UAAK,SAAL,mBAAW;AAE3B,QAAI,mCAAS,KAAK,KAAK,MAAM;AAC3B,aAAO,QAAQ,KAAK,KAAK,GAAG;AAE5B,UAAI,OAAO,KAAK,QAAQ,IAAI,EAAE,WAAW,GAAG;AAC1C,gBAAQ,KAAK,QAAQ;AAErB,eAAO,KAAK,KAAM;AAAA,MACpB;AAAA,IACF,WAAW,QAAQ,IAAI,aAAa,cAAc;AAChD,cAAQ;AAAA,QACN,oBAAoB,KAAK,GAAG;AAAA,MAC9B;AAAA,IACF;AAEA,SAAK,MAAM,CAAC;AACZ,WAAO,KAAK;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,2BAAoC;AAClC,WAAO,OAAO,OAAO;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,UAAiC;AACzC,SAAK,IAAI,KAAK,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,UAAiC;AAC3C,UAAM,QAAQ,KAAK,IAAI,QAAQ,QAAQ;AAEvC,QAAI,SAAS,GAAG;AACd,WAAK,IAAI,OAAO,OAAO,CAAC;AAAA,IAC1B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,0BAAyC;AAC9C,WAAO,OAAO,wBAAwB;AAAA,EACxC;AACF;AAEO,SAAS,cAAc,KAAa,OAA+B;AACxE,SAAO,QAAQ,OAAO,KAAK,KAAK;AAClC;AAEO,SAAS,eAAe,UAAmB;AAChD,UAAQ,QAAQ,QAAQ;AAC1B;;;AH/XO,IAAM,UAAU;","names":[]}
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Allows disposable instances to be used
3
+ */
4
+ interface Disposable {
5
+ isDisposed?(): boolean;
6
+ }
7
+ /*!
8
+ * Copyright (c) Microsoft Corporation. All rights reserved.
9
+ * Licensed under the MIT License.
10
+ */
11
+ interface WindowWithKeyborg extends Window {
12
+ __keyborg?: {
13
+ core: KeyborgCore;
14
+ refs: {
15
+ [id: string]: Keyborg;
16
+ };
17
+ };
18
+ }
19
+ interface KeyborgProps {
20
+ triggerKeys?: number[];
21
+ dismissKeys?: number[];
22
+ }
23
+ type KeyborgCallback = (isNavigatingWithKeyboard: boolean) => void;
24
+ /**
25
+ * Manages a collection of Keyborg instances in a window/document and updates keyborg state
26
+ */
27
+ declare class KeyborgCore implements Disposable {
28
+ readonly id: string;
29
+ private _win?;
30
+ private _isMouseUsedTimer;
31
+ private _dismissTimer;
32
+ private _triggerKeys?;
33
+ private _dismissKeys?;
34
+ constructor(win: WindowWithKeyborg, props?: KeyborgProps);
35
+ dispose(): void;
36
+ isDisposed(): boolean;
37
+ /**
38
+ * Updates all keyborg instances with the keyboard navigation state
39
+ */
40
+ update(isNavigatingWithKeyboard: boolean): void;
41
+ private _onFocusIn;
42
+ private _onMouseDown;
43
+ private _onKeyDown;
44
+ /**
45
+ * @returns whether the keyboard event should trigger keyboard navigation mode
46
+ */
47
+ private _shouldTriggerKeyboardNavigation;
48
+ /**
49
+ * @returns whether the keyboard event should dismiss keyboard navigation mode
50
+ */
51
+ private _shouldDismissKeyboardNavigation;
52
+ private _scheduleDismiss;
53
+ }
54
+ /**
55
+ * Used to determine the keyboard navigation state
56
+ */
57
+ declare class Keyborg {
58
+ private _id;
59
+ private _win?;
60
+ private _core?;
61
+ private _cb;
62
+ static create(win: WindowWithKeyborg, props?: KeyborgProps): Keyborg;
63
+ static dispose(instance: Keyborg): void;
64
+ /**
65
+ * Updates all subscribed callbacks with the keyboard navigation state
66
+ */
67
+ static update(instance: Keyborg, isNavigatingWithKeyboard: boolean): void;
68
+ private constructor();
69
+ private dispose;
70
+ /**
71
+ * @returns Whether the user is navigating with keyboard
72
+ */
73
+ isNavigatingWithKeyboard(): boolean;
74
+ /**
75
+ * @param callback - Called when the keyboard navigation state changes
76
+ */
77
+ subscribe(callback: KeyborgCallback): void;
78
+ /**
79
+ * @param callback - Registered with subscribe
80
+ */
81
+ unsubscribe(callback: KeyborgCallback): void;
82
+ /**
83
+ * Manually set the keyboard navigtion state
84
+ */
85
+ setVal(isNavigatingWithKeyboard: boolean): void;
86
+ }
87
+ declare function createKeyborg(win: Window, props?: KeyborgProps): Keyborg;
88
+ declare function disposeKeyborg(instance: Keyborg): void;
89
+ declare const KEYBORG_FOCUSIN = "keyborg:focusin";
90
+ interface KeyborgFocusInEventDetails {
91
+ relatedTarget?: HTMLElement;
92
+ isFocusedProgrammatically?: boolean;
93
+ }
94
+ interface KeyborgFocusInEvent extends CustomEvent<KeyborgFocusInEventDetails> {
95
+ /**
96
+ * @deprecated - used `event.detail`
97
+ */
98
+ details?: KeyborgFocusInEventDetails;
99
+ }
100
+ /**
101
+ * Guarantees that the native `focus` will be used
102
+ */
103
+ declare function nativeFocus(element: HTMLElement): void;
104
+ /**
105
+ * @param win The window that stores keyborg focus events
106
+ * @returns The last element focused with element.focus()
107
+ */
108
+ declare function getLastFocusedProgrammatically(win: Window): HTMLElement | null | undefined;
109
+ /*!
110
+ * Copyright (c) Microsoft Corporation. All rights reserved.
111
+ * Licensed under the MIT License.
112
+ */
113
+ declare const version: string | undefined;
114
+ export { KEYBORG_FOCUSIN, Keyborg, KeyborgCallback, KeyborgFocusInEvent, KeyborgFocusInEventDetails, createKeyborg, disposeKeyborg, getLastFocusedProgrammatically, nativeFocus, version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyborg",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "description": "Keyboard Navigation Detection for Web",
5
5
  "author": "Marat Abdullin <marata@microsoft.com>",
6
6
  "license": "MIT",
@@ -8,6 +8,13 @@
8
8
  "main": "./dist/index.js",
9
9
  "module": "./dist/esm/index.js",
10
10
  "typings": "./dist/index.d.ts",
11
+ "typesVersions": {
12
+ "<4.0": {
13
+ "dist/index.d.ts": [
14
+ "dist/ts3.9/index.d.ts"
15
+ ]
16
+ }
17
+ },
11
18
  "files": [
12
19
  "dist"
13
20
  ],
@@ -16,7 +23,7 @@
16
23
  "url": "git+https://github.com/microsoft/keyborg"
17
24
  },
18
25
  "scripts": {
19
- "build": "tsup",
26
+ "build": "tsup && npx downlevel-dts ./dist ./dist/ts3.9",
20
27
  "bundle-size": "npm run build && monosize measure",
21
28
  "format": "prettier --check .",
22
29
  "format:fix": "prettier --write .",
@@ -32,6 +39,7 @@
32
39
  "@playwright/test": "^1.40.1",
33
40
  "@typescript-eslint/eslint-plugin": "^6.15.0",
34
41
  "@typescript-eslint/parser": "^6.15.0",
42
+ "downlevel-dts": "^0.11.0",
35
43
  "eslint": "^8.56.0",
36
44
  "eslint-config-prettier": "^8.10.0",
37
45
  "eslint-plugin-header": "^3.1.1",