Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 3 additions & 31 deletions src/FocusEvent.mts
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,6 @@ interface WindowWithKeyborgFocusEvent extends Window {
__keyborgData?: KeyborgFocusEventData;
}

function canOverrideNativeFocus(win: Window): boolean {
const HTMLElement = (win as WindowWithKeyborgFocusEvent).HTMLElement;
const origFocus = HTMLElement.prototype.focus;

let isCustomFocusCalled = false;

HTMLElement.prototype.focus = function focus(): void {
isCustomFocusCalled = true;
};

const btn = win.document.createElement("button");

btn.focus();

HTMLElement.prototype.focus = origFocus;

return isCustomFocusCalled;
}

let _canOverrideNativeFocus = false;

export interface KeyborgFocusInEventDetails {
relatedTarget?: HTMLElement;
isFocusedProgrammatically?: boolean;
Expand Down Expand Up @@ -97,11 +76,6 @@ export function setupFocusEvent(win: Window): void {
const kwin = win as WindowWithKeyborgFocusEvent;
const doc = kwin.document;
const proto = kwin.HTMLElement.prototype;

if (!_canOverrideNativeFocus) {
_canOverrideNativeFocus = canOverrideNativeFocus(kwin);
}

const origFocus = proto.focus;

if ((origFocus as KeyborgFocus).__keyborgNativeFocus) {
Expand Down Expand Up @@ -258,12 +232,10 @@ export function setupFocusEvent(win: Window): void {
// Tabster (and other users) can still use the legacy details field - keeping for backwards compat
event.details = details;

if (_canOverrideNativeFocus || data[LAST_FOCUSED_PROGRAMMATICALLY]) {
details.isFocusedProgrammatically =
target === data[LAST_FOCUSED_PROGRAMMATICALLY]?.deref();
details.isFocusedProgrammatically =
target === data[LAST_FOCUSED_PROGRAMMATICALLY]?.deref();

data[LAST_FOCUSED_PROGRAMMATICALLY] = undefined;
}
data[LAST_FOCUSED_PROGRAMMATICALLY] = undefined;

target.dispatchEvent(event);
};
Expand Down