Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@uifabric/utilities",
"comment": "asyncFocus: passing a component which has a focus method, but no `ownerDocument`, should still call focus.",
"type": "patch"
}
],
"packageName": "@uifabric/utilities",
"email": "dzearing@microsoft.com"
}
12 changes: 11 additions & 1 deletion packages/utilities/src/focus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ describe('focusAsync', () => {
);

const container = ReactDOM.findDOMNode(component as React.ReactInstance) as Element;

const buttonA = container.querySelector('.a') as HTMLElement;
const buttonB = container.querySelector('.b') as HTMLElement;
const buttonC = container.querySelector('.c') as HTMLElement;
Expand All @@ -161,4 +160,15 @@ describe('focusAsync', () => {

jest.runAllTimers();
});

it('can focus a component which implements focus()', () => {
let calledFocus = false;
const fakeComponent = {
focus: () => calledFocus = true
};

focusAsync(fakeComponent);
jest.runAllTimers();
expect(calledFocus).toEqual(true);
});
});
12 changes: 9 additions & 3 deletions packages/utilities/src/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,15 @@ export function shouldWrapFocus(element: HTMLElement, noWrapDataAttribute: 'data
return elementContainsAttribute(element, noWrapDataAttribute) === 'true' ? false : true;
}

let targetToFocusOnNextRepaint: HTMLElement | null | undefined = undefined;
let targetToFocusOnNextRepaint: HTMLElement | { focus: () => void } | null | undefined = undefined;

/**
* Sets focus to an element asynchronously. The focus will be set at the next browser repaint,
* meaning it won't cause any extra recalculations. If more than one focusAsync is called during one frame,
* only the latest called focusAsync element will actually be focused
* @param element The element to focus
*/
export function focusAsync(element: HTMLElement | undefined | null): void {
export function focusAsync(element: HTMLElement | { focus: () => void } | undefined | null): void {
if (element) {
// An element was already queued to be focused, so replace that one with the new element
if (targetToFocusOnNextRepaint) {
Expand All @@ -370,8 +370,14 @@ export function focusAsync(element: HTMLElement | undefined | null): void {

targetToFocusOnNextRepaint = element;

const htmlElement = element as HTMLElement;
const view = ((
htmlElement.ownerDocument &&
htmlElement.ownerDocument.defaultView
) ? htmlElement.ownerDocument.defaultView : window);

// element.focus() is a no-op if the element is no longer in the DOM, meaning this is always safe
element.ownerDocument.defaultView.requestAnimationFrame(() => {
view.requestAnimationFrame(() => {
targetToFocusOnNextRepaint && targetToFocusOnNextRepaint.focus();

// We are done focusing for this frame, so reset the queued focus element
Expand Down