Skip to content

Commit

Permalink
Hide focus race condition bug
Browse files Browse the repository at this point in the history
Potentially described in #21
  • Loading branch information
fregante committed Feb 19, 2024
1 parent c1b9715 commit b19e72c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ function getState(field: HTMLElement) {
}

for (const type of ['textarea', 'input', 'contenteditable'] as const) {
test(`${type}: harness test`, t => {
test(`${type}: harness test`, async t => {
// Reset the document focus
(document.activeElement as HTMLElement)?.blur?.();
document.body.focus();

t.equal(getState(getField(type)), '|');
t.equal(getState(getField(type, '|')), '|');
t.equal(getState(getField(type, 'A|')), 'A|');
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function withFocus<T>(field: HTMLElement, callback: () => T): T {
field.focus();
return callback();
} finally {
field.blur(); // Supports `intialFocus === body`
if (initialFocus instanceof HTMLElement) {
field.blur(); // Supports `intialFocus === body`
initialFocus.focus();
}
}
Expand Down

0 comments on commit b19e72c

Please sign in to comment.