Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FocusZone: remove keydown event only when needed. #10941

Merged
merged 2 commits into from Oct 23, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,9 @@
{
"type": "patch",
"comment": "FocusZone: unhooking capture keydown handler at the right time to avoid a race condition.",
"packageName": "office-ui-fabric-react",
"email": "dzearing@hotmail.com",
"commit": "01728e3082bf41ea216f9f25a745c202a7489372",
"date": "2019-10-23T05:16:09.514Z",
"file": "C:\\fabric\\branch3\\change\\office-ui-fabric-react-2019-10-22-22-16-09-focuszone-keydown.json"
}
Expand Up @@ -23,7 +23,8 @@ import {
shouldWrapFocus,
warnDeprecations,
portalContainsElement,
IPoint
IPoint,
getWindow
} from '../../Utilities';
import { mergeStyles } from '@uifabric/merge-styles';

Expand Down Expand Up @@ -62,6 +63,9 @@ const _allInstances: {
} = {};
const _outerZones: Set<FocusZone> = new Set();

// Track the 1 global keydown listener we hook to window.
let _disposeGlobalKeyDownListener: () => void | undefined;

const ALLOWED_INPUT_TYPES = ['text', 'number', 'password', 'email', 'tel', 'url', 'search'];

const ALLOW_VIRTUAL_ELEMENTS = false;
Expand Down Expand Up @@ -135,8 +139,7 @@ export class FocusZone extends React.Component<IFocusZoneProps> implements IFocu
_allInstances[this._id] = this;

if (root) {
const windowElement = root.ownerDocument!.defaultView!;

const windowElement = getWindow(root);
let parentElement = getParent(root, ALLOW_VIRTUAL_ELEMENTS);

while (parentElement && parentElement !== this._getDocument().body && parentElement.nodeType === 1) {
Expand All @@ -152,7 +155,7 @@ export class FocusZone extends React.Component<IFocusZoneProps> implements IFocu
}

if (windowElement && _outerZones.size === 1) {
this._disposables.push(on(windowElement, 'keydown', this._onKeyDownCapture, true));
_disposeGlobalKeyDownListener = on(windowElement, 'keydown', this._onKeyDownCapture, true);
}
this._disposables.push(on(root, 'blur', this._onBlur, true));

Expand Down Expand Up @@ -195,6 +198,11 @@ export class FocusZone extends React.Component<IFocusZoneProps> implements IFocu

// Dispose all events.
this._disposables.forEach(d => d());

// If this is the last outer zone, remove the keydown listener.
if (_outerZones.size === 0 && _disposeGlobalKeyDownListener) {
_disposeGlobalKeyDownListener();
}
}

public render() {
Expand Down