-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
660 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,29 @@ | ||
import { isFocusable, getActiveElement } from "reakit-utils"; | ||
import { DirtiableElement } from "./__utils/types"; | ||
import { fireEvent } from "./fireEvent"; | ||
import { act } from "./act"; | ||
import { blur } from "./blur"; | ||
|
||
import "./mockClientRects"; | ||
|
||
export function focus(element: Element) { | ||
if (getActiveElement(element) === element) return; | ||
if (!isFocusable(element)) return; | ||
blur(); | ||
|
||
const activeElement = getActiveElement(element) as DirtiableElement | null; | ||
|
||
if (activeElement?.dirty) { | ||
fireEvent.change(activeElement); | ||
activeElement.dirty = false; | ||
} | ||
|
||
if (activeElement) { | ||
fireEvent.focusOut(activeElement, { relatedTarget: element }); | ||
} | ||
|
||
act(() => { | ||
if (element instanceof HTMLElement || element instanceof SVGElement) { | ||
element.focus(); | ||
} | ||
}); | ||
fireEvent.focusIn(element); | ||
fireEvent.focusIn(element, { relatedTarget: activeElement }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getDocument } from "./getDocument"; | ||
|
||
/** | ||
* Creates an `Event` in a way that also works on IE 11. | ||
* | ||
* @example | ||
* import { createEvent } from "reakit-utils"; | ||
* | ||
* const el = document.getElementById("id"); | ||
* el.dispatchEvent(createEvent(el, "blur", { bubbles: false })); | ||
*/ | ||
export function createEvent( | ||
element: HTMLElement, | ||
type: string, | ||
eventInit: EventInit = {} | ||
): Event { | ||
if (typeof Event === "function") { | ||
return new Event(type, eventInit); | ||
} | ||
// IE 11 doesn't support Event constructors | ||
const event = getDocument(element).createEvent("Event"); | ||
event.initEvent(type, eventInit?.bubbles, eventInit?.cancelable); | ||
return event; | ||
} |
Oops, something went wrong.