|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +// [macOS] |
| 12 | +// Legacy validKeysDown/validKeysUp/passthroughAllKeyEvents compat layer. |
| 13 | +// When removing legacy support, delete this file and its call sites. |
| 14 | + |
| 15 | +import type {HandledKeyEvent, KeyEvent} from '../Types/CoreEventTypes'; |
| 16 | + |
| 17 | +type LegacyHandledKeyEvent = string | HandledKeyEvent; |
| 18 | + |
| 19 | +function expandKey(entry: LegacyHandledKeyEvent): Array<HandledKeyEvent> { |
| 20 | + if (typeof entry !== 'string') { |
| 21 | + return [entry]; |
| 22 | + } |
| 23 | + const out: Array<HandledKeyEvent> = []; |
| 24 | + const bools: Array<boolean> = [false, true]; |
| 25 | + for (const metaKey of bools) { |
| 26 | + for (const ctrlKey of bools) { |
| 27 | + for (const altKey of bools) { |
| 28 | + for (const shiftKey of bools) { |
| 29 | + out.push({altKey, ctrlKey, key: entry, metaKey, shiftKey}); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + return out; |
| 35 | +} |
| 36 | + |
| 37 | +function normalize( |
| 38 | + legacy: ?$ReadOnlyArray<LegacyHandledKeyEvent>, |
| 39 | +): void | Array<HandledKeyEvent> { |
| 40 | + if (legacy == null) { |
| 41 | + return undefined; |
| 42 | + } |
| 43 | + const result: Array<HandledKeyEvent> = []; |
| 44 | + for (const entry of legacy) { |
| 45 | + result.push(...expandKey(entry)); |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +function matchesEvent( |
| 51 | + events: $ReadOnlyArray<HandledKeyEvent>, |
| 52 | + event: KeyEvent, |
| 53 | +): boolean { |
| 54 | + return events.some( |
| 55 | + ({key, metaKey, ctrlKey, altKey, shiftKey}: HandledKeyEvent) => |
| 56 | + event.nativeEvent.key === key && |
| 57 | + Boolean(metaKey) === event.nativeEvent.metaKey && |
| 58 | + Boolean(ctrlKey) === event.nativeEvent.ctrlKey && |
| 59 | + Boolean(altKey) === event.nativeEvent.altKey && |
| 60 | + Boolean(shiftKey) === event.nativeEvent.shiftKey, |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +export type LegacyKeyResult = { |
| 65 | + keyDownEvents: void | Array<HandledKeyEvent>, |
| 66 | + keyUpEvents: void | Array<HandledKeyEvent>, |
| 67 | + onKeyDown: void | ((event: KeyEvent) => void), |
| 68 | + onKeyUp: void | ((event: KeyEvent) => void), |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Returns true if the props contain legacy key props that need processing. |
| 73 | + */ |
| 74 | +export function hasLegacyKeyProps(props: mixed): boolean { |
| 75 | + // $FlowFixMe[unclear-type] |
| 76 | + const p = (props: any); |
| 77 | + return ( |
| 78 | + p.validKeysDown != null || |
| 79 | + p.validKeysUp != null || |
| 80 | + p.passthroughAllKeyEvents != null |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Strips legacy props from a props object (mutates). |
| 86 | + */ |
| 87 | +export function stripLegacyKeyProps(props: {+[string]: mixed}): void { |
| 88 | + // $FlowFixMe[unclear-type] |
| 89 | + const p = (props: any); |
| 90 | + delete p.validKeysDown; |
| 91 | + delete p.validKeysUp; |
| 92 | + delete p.passthroughAllKeyEvents; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Processes legacy validKeysDown/validKeysUp/passthroughAllKeyEvents props |
| 97 | + * and returns the equivalent modern keyDownEvents/keyUpEvents and wrapped |
| 98 | + * onKeyDown/onKeyUp handlers. |
| 99 | + * |
| 100 | + * Usage in component: |
| 101 | + * if (hasLegacyKeyProps(props)) { |
| 102 | + * const legacy = processLegacyKeyProps(props); |
| 103 | + * // use legacy.keyDownEvents, legacy.onKeyDown, etc. |
| 104 | + * } |
| 105 | + */ |
| 106 | +export default function processLegacyKeyProps( |
| 107 | + // $FlowFixMe[unclear-type] |
| 108 | + props: any, |
| 109 | +): LegacyKeyResult { |
| 110 | + const validKeysDown: ?$ReadOnlyArray<LegacyHandledKeyEvent> = |
| 111 | + props.validKeysDown; |
| 112 | + const validKeysUp: ?$ReadOnlyArray<LegacyHandledKeyEvent> = props.validKeysUp; |
| 113 | + const passthroughAllKeyEvents: ?boolean = props.passthroughAllKeyEvents; |
| 114 | + |
| 115 | + const hasModernKeyDown = props.keyDownEvents != null; |
| 116 | + const hasModernKeyUp = props.keyUpEvents != null; |
| 117 | + const legacyPassthrough = |
| 118 | + passthroughAllKeyEvents === true && !hasModernKeyDown; |
| 119 | + |
| 120 | + const gateKeyDown = |
| 121 | + !hasModernKeyDown && validKeysDown != null && !legacyPassthrough; |
| 122 | + const gateKeyUp = |
| 123 | + !hasModernKeyUp && validKeysUp != null && !legacyPassthrough; |
| 124 | + |
| 125 | + const normalizedDown = props.keyDownEvents ?? normalize(validKeysDown); |
| 126 | + const normalizedUp = props.keyUpEvents ?? normalize(validKeysUp); |
| 127 | + |
| 128 | + const keyDownEvents = legacyPassthrough ? undefined : normalizedDown; |
| 129 | + const keyUpEvents = legacyPassthrough ? undefined : normalizedUp; |
| 130 | + |
| 131 | + const onKeyDown = |
| 132 | + props.onKeyDown != null |
| 133 | + ? (event: KeyEvent) => { |
| 134 | + let isHandled = false; |
| 135 | + if (normalizedDown != null && !event.isPropagationStopped()) { |
| 136 | + isHandled = matchesEvent(normalizedDown, event); |
| 137 | + if (isHandled && hasModernKeyDown) { |
| 138 | + event.stopPropagation(); |
| 139 | + } |
| 140 | + } |
| 141 | + if (!gateKeyDown || isHandled) { |
| 142 | + props.onKeyDown?.(event); |
| 143 | + } |
| 144 | + } |
| 145 | + : undefined; |
| 146 | + |
| 147 | + const onKeyUp = |
| 148 | + props.onKeyUp != null |
| 149 | + ? (event: KeyEvent) => { |
| 150 | + let isHandled = false; |
| 151 | + if (normalizedUp != null && !event.isPropagationStopped()) { |
| 152 | + isHandled = matchesEvent(normalizedUp, event); |
| 153 | + if (isHandled && hasModernKeyUp) { |
| 154 | + event.stopPropagation(); |
| 155 | + } |
| 156 | + } |
| 157 | + if (!gateKeyUp || isHandled) { |
| 158 | + props.onKeyUp?.(event); |
| 159 | + } |
| 160 | + } |
| 161 | + : undefined; |
| 162 | + |
| 163 | + return {keyDownEvents, keyUpEvents, onKeyDown, onKeyUp}; |
| 164 | +} |
0 commit comments