Skip to content

Commit

Permalink
Merge pull request #29 from abhimanyu-singh-uber/abhi-focus-options
Browse files Browse the repository at this point in the history
Added FocusOptions to setFocus function to allow users to pass preventScroll to setFocus
  • Loading branch information
theKashey authored Dec 12, 2021
2 parents 79a32c1 + ef064a2 commit 08a9904
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .size-limit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
path: "dist/es2015/index.js",
limit: "2428 B"
limit: "2443 B"
}
]
2 changes: 1 addition & 1 deletion .size.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"name": "dist/es2015/index.js",
"passed": true,
"size": 2428
"size": 2443
}
]
13 changes: 9 additions & 4 deletions src/setFocus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { getFocusMerge } from './focusMerge';

export const focusOn = (target: HTMLInputElement | HTMLFrameElement) => {
target.focus();
type Options = {
focusOptions?: FocusOptions
};

export const focusOn = (target: HTMLInputElement | HTMLFrameElement, focusOptions?: FocusOptions) => {
target.focus(focusOptions);
if ('contentWindow' in target && target.contentWindow) {
target.contentWindow.focus();
}
Expand All @@ -10,8 +14,9 @@ export const focusOn = (target: HTMLInputElement | HTMLFrameElement) => {
let guardCount = 0;
let lockDisabled = false;

export const setFocus = (topNode: HTMLElement, lastNode: HTMLInputElement) => {
export const setFocus = (topNode: HTMLElement, lastNode: HTMLInputElement, options?: Options) => {
const focusable = getFocusMerge(topNode, lastNode);
const { focusOptions = undefined } = options || {};

if (lockDisabled) {
return;
Expand All @@ -31,7 +36,7 @@ export const setFocus = (topNode: HTMLElement, lastNode: HTMLInputElement) => {
return;
}
guardCount++;
focusOn(focusable.node);
focusOn(focusable.node, focusOptions);
guardCount--;
}
};

0 comments on commit 08a9904

Please sign in to comment.