GeezerVison is showing github.com — [react-events] Rely on 'buttons' rather than 'button' (#16479) · react/react@2559111
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/DOMEventResponderSystem.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -445,7 +445,7 @@ function createDOMResponderEvent( | 445 | 445 | passive: boolean, | 446 | 446 | passiveSupported: boolean, | 447 | 447 | ): ReactDOMResponderEvent { | 448 | | - const {pointerType} = (nativeEvent: any); | | 448 | + const {buttons, pointerType} = (nativeEvent: any); | 449 | 449 | let eventPointerType = ''; | 450 | 450 | let pointerId = null; | 451 | 451 |
| @@ -454,7 +454,7 @@ function createDOMResponderEvent( | 454 | 454 | pointerId = (nativeEvent: any).pointerId; | 455 | 455 | } else if (nativeEvent.key !== undefined) { | 456 | 456 | eventPointerType = 'keyboard'; | 457 | | - } else if (nativeEvent.button !== undefined) { | | 457 | + } else if (buttons !== undefined) { | 458 | 458 | eventPointerType = 'mouse'; | 459 | 459 | } else if ((nativeEvent: any).changedTouches !== undefined) { | 460 | 460 | eventPointerType = 'touch'; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/docs/Press.md -10Lines changed: 0 additions & 10 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -78,11 +78,6 @@ type PressOffset = { | 78 | 78 |
| 79 | 79 | Disables all `Press` events. | 80 | 80 |
| 81 | | -### onContextMenu: (e: PressEvent) => void | 82 | | - | 83 | | -Called when the context menu is shown. When a press is active, the context menu | 84 | | -will only be shown (and the press cancelled) if `preventDefault` is `false`. | 85 | | - | 86 | 81 | ### onPress: (e: PressEvent) => void | 87 | 82 |
| 88 | 83 | Called immediately after a press is released, unless the press is released | @@ -115,11 +110,6 @@ down) can be moved back within the bounds of the element to reactivate it. | 115 | 110 | Ensure you pass in a constant to reduce memory allocations. Default is `20` for | 116 | 111 | each offset. | 117 | 112 |
| 118 | | -### preventContextMenu: boolean = false | 119 | | - | 120 | | -Prevents the native context menu from being shown, but `onContextMenu` | 121 | | -is still called. | 122 | | - | 123 | 113 | ### preventDefault: boolean = true | 124 | 114 |
| 125 | 115 | Whether to `preventDefault()` native events. Native behavior is prevented by | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/ContextMenu.js +15-16Lines changed: 15 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,20 +28,19 @@ type ContextMenuState = { | 28 | 28 | }; | 29 | 29 |
| 30 | 30 | type ContextMenuEvent = {| | 31 | | - target: Element | Document, | 32 | | - type: 'contextmenu', | 33 | | - pointerType: PointerType, | 34 | | - timeStamp: number, | 35 | | - clientX: null | number, | 36 | | - clientY: null | number, | 37 | | - pageX: null | number, | 38 | | - pageY: null | number, | 39 | | - x: null | number, | 40 | | - y: null | number, | 41 | 31 | altKey: boolean, | | 32 | + buttons: 0 | 1 | 2, | 42 | 33 | ctrlKey: boolean, | 43 | 34 | metaKey: boolean, | | 35 | + pageX: null | number, | | 36 | + pageY: null | number, | | 37 | + pointerType: PointerType, | 44 | 38 | shiftKey: boolean, | | 39 | + target: Element | Document, | | 40 | + timeStamp: number, | | 41 | + type: 'contextmenu', | | 42 | + x: null | number, | | 43 | + y: null | number, | 45 | 44 | |}; | 46 | 45 |
| 47 | 46 | const hasPointerEvents = | @@ -60,13 +59,13 @@ function dispatchContextMenuEvent( | 60 | 59 |
| 61 | 60 | const gestureState = { | 62 | 61 | altKey: nativeEvent.altKey, | 63 | | - button: nativeEvent.button === 0 ? 'primary' : 'auxillary', | 64 | | - ctrlKey: nativeEvent.altKey, | 65 | | - metaKey: nativeEvent.altKey, | 66 | | - pageX: nativeEvent.altKey, | 67 | | - pageY: nativeEvent.altKey, | | 62 | + buttons: nativeEvent.buttons != null ? nativeEvent.buttons : 0, | | 63 | + ctrlKey: nativeEvent.ctrlKey, | | 64 | + metaKey: nativeEvent.metaKey, | | 65 | + pageX: nativeEvent.pageX, | | 66 | + pageY: nativeEvent.pageY, | 68 | 67 | pointerType, | 69 | | - shiftKey: nativeEvent.altKey, | | 68 | + shiftKey: nativeEvent.shiftKey, | 70 | 69 | target, | 71 | 70 | timeStamp, | 72 | 71 | type: 'contextmenu', | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/Hover.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,16 +37,16 @@ type HoverState = { | 37 | 37 | type HoverEventType = 'hoverstart' | 'hoverend' | 'hoverchange' | 'hovermove'; | 38 | 38 |
| 39 | 39 | type HoverEvent = {| | 40 | | - pointerType: PointerType, | 41 | | - target: Element | Document, | 42 | | - type: HoverEventType, | 43 | | - timeStamp: number, | 44 | 40 | clientX: null | number, | 45 | 41 | clientY: null | number, | 46 | 42 | pageX: null | number, | 47 | 43 | pageY: null | number, | | 44 | + pointerType: PointerType, | 48 | 45 | screenX: null | number, | 49 | 46 | screenY: null | number, | | 47 | + target: Element | Document, | | 48 | + timeStamp: number, | | 49 | + type: HoverEventType, | 50 | 50 | x: null | number, | 51 | 51 | y: null | number, | 52 | 52 | |}; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/Press.js +36-30Lines changed: 36 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,24 +75,24 @@ type PressEventType = | 75 | 75 | | 'presschange'; | 76 | 76 |
| 77 | 77 | type PressEvent = {| | 78 | | - button: 'primary' | 'auxillary', | 79 | | - defaultPrevented: boolean, | 80 | | - target: Element | Document, | 81 | | - type: PressEventType, | 82 | | - pointerType: PointerType, | 83 | | - timeStamp: number, | | 78 | + altKey: boolean, | | 79 | + buttons: 0 | 1 | 4, | 84 | 80 | clientX: null | number, | 85 | 81 | clientY: null | number, | | 82 | + ctrlKey: boolean, | | 83 | + defaultPrevented: boolean, | | 84 | + metaKey: boolean, | 86 | 85 | pageX: null | number, | 87 | 86 | pageY: null | number, | | 87 | + pointerType: PointerType, | 88 | 88 | screenX: null | number, | 89 | 89 | screenY: null | number, | | 90 | + shiftKey: boolean, | | 91 | + target: Element | Document, | | 92 | + timeStamp: number, | | 93 | + type: PressEventType, | 90 | 94 | x: null | number, | 91 | 95 | y: null | number, | 92 | | - altKey: boolean, | 93 | | - ctrlKey: boolean, | 94 | | - metaKey: boolean, | 95 | | - shiftKey: boolean, | 96 | 96 | |}; | 97 | 97 |
| 98 | 98 | const hasPointerEvents = | @@ -151,7 +151,7 @@ function createPressEvent( | 151 | 151 | defaultPrevented: boolean, | 152 | 152 | ): PressEvent { | 153 | 153 | const timeStamp = context.getTimeStamp(); | 154 | | - let button = 'primary'; | | 154 | + let buttons = 1; | 155 | 155 | let clientX = null; | 156 | 156 | let clientY = null; | 157 | 157 | let pageX = null; | @@ -171,31 +171,36 @@ function createPressEvent( | 171 | 171 | let eventObject; | 172 | 172 | eventObject = (touchEvent: any) || (nativeEvent: any); | 173 | 173 | if (eventObject) { | 174 | | - ({clientX, clientY, pageX, pageY, screenX, screenY} = eventObject); | 175 | | - } | 176 | | - if (nativeEvent.button === 1) { | 177 | | - button = 'auxillary'; | | 174 | + ({ | | 175 | + buttons, | | 176 | + clientX, | | 177 | + clientY, | | 178 | + pageX, | | 179 | + pageY, | | 180 | + screenX, | | 181 | + screenY, | | 182 | + } = eventObject); | 178 | 183 | } | 179 | 184 | } | 180 | 185 | return { | 181 | | - button, | 182 | | - defaultPrevented, | 183 | | - target, | 184 | | - type, | 185 | | - pointerType, | 186 | | - timeStamp, | | 186 | + altKey, | | 187 | + buttons, | 187 | 188 | clientX, | 188 | 189 | clientY, | | 190 | + ctrlKey, | | 191 | + defaultPrevented, | | 192 | + metaKey, | 189 | 193 | pageX, | 190 | 194 | pageY, | | 195 | + pointerType, | 191 | 196 | screenX, | 192 | 197 | screenY, | | 198 | + shiftKey, | | 199 | + target, | | 200 | + timeStamp, | | 201 | + type, | 193 | 202 | x: clientX, | 194 | 203 | y: clientY, | 195 | | - altKey, | 196 | | - ctrlKey, | 197 | | - metaKey, | 198 | | - shiftKey, | 199 | 204 | }; | 200 | 205 | } | 201 | 206 |
| @@ -581,11 +586,12 @@ const pressResponderImpl = { | 581 | 586 | state.activePointerId = touchEvent.identifier; | 582 | 587 | } | 583 | 588 |
| 584 | | - // Ignore any device buttons except primary/auxillary and touch/pen contact. | | 589 | + // Ignore any device buttons except primary/secondary and touch/pen contact. | 585 | 590 | // Additionally we ignore primary-button + ctrl-key with Macs as that | 586 | 591 | // acts like right-click and opens the contextmenu. | 587 | 592 | if ( | 588 | | - nativeEvent.button > 1 || | | 593 | + nativeEvent.buttons === 2 || | | 594 | + nativeEvent.buttons > 4 || | 589 | 595 | (isMac && isMouseEvent && nativeEvent.ctrlKey) | 590 | 596 | ) { | 591 | 597 | return; | @@ -703,7 +709,7 @@ const pressResponderImpl = { | 703 | 709 | case 'mouseup': | 704 | 710 | case 'touchend': { | 705 | 711 | if (isPressed) { | 706 | | - const button = nativeEvent.button; | | 712 | + const buttons = nativeEvent.buttons; | 707 | 713 | let isKeyboardEvent = false; | 708 | 714 | let touchEvent; | 709 | 715 | if (type === 'pointerup' && activePointerId !== pointerId) { | @@ -722,7 +728,7 @@ const pressResponderImpl = { | 722 | 728 | } | 723 | 729 | isKeyboardEvent = true; | 724 | 730 | removeRootEventTypes(context, state); | 725 | | - } else if (button === 1) { | | 731 | + } else if (buttons === 4) { | 726 | 732 | // Remove the root events here as no 'click' event is dispatched when this 'button' is pressed. | 727 | 733 | removeRootEventTypes(context, state); | 728 | 734 | } | @@ -780,7 +786,7 @@ const pressResponderImpl = { | 780 | 786 | } | 781 | 787 | } | 782 | 788 |
| 783 | | - if (state.isPressWithinResponderRegion && button !== 1) { | | 789 | + if (state.isPressWithinResponderRegion && buttons !== 4) { | 784 | 790 | dispatchEvent( | 785 | 791 | event, | 786 | 792 | onPress, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/__tests__/ContextMenu-test.internal.js +21-4Lines changed: 21 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | 'use strict'; | 11 | 11 |
| 12 | | -import {createEventTarget, platform, setPointerEvent} from '../testing-library'; | | 12 | +import { | | 13 | + buttonsType, | | 14 | + createEventTarget, | | 15 | + platform, | | 16 | + setPointerEvent, | | 17 | +} from '../testing-library'; | 13 | 18 |
| 14 | 19 | let React; | 15 | 20 | let ReactFeatureFlags; | @@ -61,7 +66,11 @@ describe.each(table)('ContextMenu responder', hasPointerEvents => { | 61 | 66 | expect(preventDefault).toHaveBeenCalledTimes(1); | 62 | 67 | expect(onContextMenu).toHaveBeenCalledTimes(1); | 63 | 68 | expect(onContextMenu).toHaveBeenCalledWith( | 64 | | - expect.objectContaining({pointerType: 'mouse', type: 'contextmenu'}), | | 69 | + expect.objectContaining({ | | 70 | + buttons: buttonsType.secondary, | | 71 | + pointerType: 'mouse', | | 72 | + type: 'contextmenu', | | 73 | + }), | 65 | 74 | ); | 66 | 75 | }); | 67 | 76 |
| @@ -80,7 +89,11 @@ describe.each(table)('ContextMenu responder', hasPointerEvents => { | 80 | 89 | expect(preventDefault).toHaveBeenCalledTimes(1); | 81 | 90 | expect(onContextMenu).toHaveBeenCalledTimes(1); | 82 | 91 | expect(onContextMenu).toHaveBeenCalledWith( | 83 | | - expect.objectContaining({pointerType: 'touch', type: 'contextmenu'}), | | 92 | + expect.objectContaining({ | | 93 | + buttons: buttonsType.none, | | 94 | + pointerType: 'touch', | | 95 | + type: 'contextmenu', | | 96 | + }), | 84 | 97 | ); | 85 | 98 | }); | 86 | 99 |
| @@ -144,7 +157,11 @@ describe.each(table)('ContextMenu responder', hasPointerEvents => { | 144 | 157 | target.contextmenu({}, {modified: true}); | 145 | 158 | expect(onContextMenu).toHaveBeenCalledTimes(1); | 146 | 159 | expect(onContextMenu).toHaveBeenCalledWith( | 147 | | - expect.objectContaining({pointerType: 'mouse', type: 'contextmenu'}), | | 160 | + expect.objectContaining({ | | 161 | + buttons: buttonsType.primary, | | 162 | + pointerType: 'mouse', | | 163 | + type: 'contextmenu', | | 164 | + }), | 148 | 165 | ); | 149 | 166 | }); | 150 | 167 | }); | |
|