mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
* Renamed 'events' package to 'legacy-events' * Updated 'events' references to point to 'legacy-events'
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
/**
|
|
* Translation from modifier key to the associated property in the event.
|
|
* @see http://www.w3.org/TR/DOM-Level-3-Events/#keys-Modifiers
|
|
*/
|
|
|
|
import type {AnyNativeEvent} from 'legacy-events/PluginModuleType';
|
|
|
|
const modifierKeyToProp = {
|
|
Alt: 'altKey',
|
|
Control: 'ctrlKey',
|
|
Meta: 'metaKey',
|
|
Shift: 'shiftKey',
|
|
};
|
|
|
|
// Older browsers (Safari <= 10, iOS Safari <= 10.2) do not support
|
|
// getModifierState. If getModifierState is not supported, we map it to a set of
|
|
// modifier keys exposed by the event. In this case, Lock-keys are not supported.
|
|
function modifierStateGetter(keyArg: string): boolean {
|
|
const syntheticEvent = this;
|
|
const nativeEvent = syntheticEvent.nativeEvent;
|
|
if (nativeEvent.getModifierState) {
|
|
return nativeEvent.getModifierState(keyArg);
|
|
}
|
|
const keyProp = modifierKeyToProp[keyArg];
|
|
return keyProp ? !!nativeEvent[keyProp] : false;
|
|
}
|
|
|
|
function getEventModifierState(
|
|
nativeEvent: AnyNativeEvent,
|
|
): (keyArg: string) => boolean {
|
|
return modifierStateGetter;
|
|
}
|
|
|
|
export default getEventModifierState;
|