-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathfocus.ts
30 lines (24 loc) · 916 Bytes
/
focus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import {findClosest, getActiveElementOrBody, isFocusable} from '../utils'
import {updateSelectionOnFocus} from './selection'
import {wrapEvent} from './wrapEvent'
/**
* Focus closest focusable element.
*/
export function focusElement(element: Element) {
const target = findClosest(element, isFocusable)
const activeElement = getActiveElementOrBody(element.ownerDocument)
if ((target ?? element.ownerDocument.body) === activeElement) {
return
} else if (target) {
wrapEvent(() => target.focus(), element)
} else {
wrapEvent(() => (activeElement as HTMLElement | null)?.blur(), element)
}
updateSelectionOnFocus(target ?? element.ownerDocument.body)
}
export function blurElement(element: Element) {
if (!isFocusable(element)) return
const wasActive = getActiveElementOrBody(element.ownerDocument) === element
if (!wasActive) return
wrapEvent(() => element.blur(), element)
}