Skip to content

Commit d741a6a

Browse files
Saadnajmiclaude
andcommitted
Add onAuxClick test page in View example and docsite entry
- Add MouseClickEventsExample to ViewExample.js with both a plain View and a Pressable target to verify onAuxClick fires and onPress does not on right-click - Document onAuxClick in docsite view-events.md with event data spec and usage example - Add button property to onDoubleClick event data docs Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4218d51 commit d741a6a

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

docsite/api/view-events.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@ Example:
126126

127127
---
128128

129+
### `onAuxClick`
130+
131+
Fired when the user clicks on the view with a non-primary button (e.g., right-click or middle-click). This follows the [W3C `auxclick` event](https://developer.mozilla.org/en-US/docs/Web/API/Element/auxclick_event) specification.
132+
133+
**Event Data:** Mouse event with the following properties:
134+
- `clientX`: Horizontal position in the target view
135+
- `clientY`: Vertical position in the target view
136+
- `screenX`: Horizontal position in the window
137+
- `screenY`: Vertical position in the window
138+
- `altKey`: Whether Alt/Option key is pressed
139+
- `ctrlKey`: Whether Control key is pressed
140+
- `shiftKey`: Whether Shift key is pressed
141+
- `metaKey`: Whether Command key is pressed
142+
- `button`: The button number that was pressed (2 for right-click)
143+
144+
Example:
145+
```javascript
146+
<View onAuxClick={(event) => {
147+
console.log('Right clicked, button:', event.nativeEvent.button);
148+
}}>
149+
<Text>Right click me</Text>
150+
</View>
151+
```
152+
153+
> **Note:** Right-clicking a `Pressable` will fire `onAuxClick` but will **not** trigger `onPress`. Only primary (left) button clicks trigger `onPress`.
154+
155+
---
156+
129157
### `onDoubleClick`
130158

131159
Fired when the user double-clicks on the view.
@@ -139,6 +167,7 @@ Fired when the user double-clicks on the view.
139167
- `ctrlKey`: Whether Control key is pressed
140168
- `shiftKey`: Whether Shift key is pressed
141169
- `metaKey`: Whether Command key is pressed
170+
- `button`: The button number that was pressed (0 for left-click)
142171

143172
Example:
144173
```javascript

packages/rn-tester/js/examples/View/ViewExample.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,106 @@ function FocusBlurExample(): React.Node {
768768
);
769769
}
770770

771+
// [macOS
772+
function MouseClickEventsExample(): React.Node {
773+
const [eventLog, setEventLog] = useState<Array<string>>([]);
774+
775+
const appendEvent = (eventName: string) => {
776+
setEventLog(prev => [eventName, ...prev.slice(0, 19)]);
777+
};
778+
779+
return (
780+
<View>
781+
<View
782+
style={mouseClickStyles.targetBox}
783+
onAuxClick={e => appendEvent(`auxClick (button=${e.nativeEvent.button})`)}
784+
onDoubleClick={() => appendEvent('doubleClick')}
785+
onMouseEnter={() => appendEvent('mouseEnter')}
786+
onMouseLeave={() => appendEvent('mouseLeave')}>
787+
<RNTesterText style={mouseClickStyles.targetText}>
788+
Click Target
789+
</RNTesterText>
790+
<RNTesterText style={mouseClickStyles.hintText}>
791+
Left-click, right-click, or double-click this box
792+
</RNTesterText>
793+
</View>
794+
<Pressable
795+
style={mouseClickStyles.pressableBox}
796+
onAuxClick={e =>
797+
appendEvent(`pressable auxClick (button=${e.nativeEvent.button})`)
798+
}
799+
onPress={() => appendEvent('pressable onPress')}
800+
onDoubleClick={() => appendEvent('pressable doubleClick')}>
801+
<RNTesterText style={mouseClickStyles.targetText}>
802+
Pressable Target
803+
</RNTesterText>
804+
<RNTesterText style={mouseClickStyles.hintText}>
805+
Right-click should fire auxClick but NOT onPress
806+
</RNTesterText>
807+
</Pressable>
808+
<View style={mouseClickStyles.logBox}>
809+
<RNTesterText style={mouseClickStyles.logHeader}>
810+
Event Log:
811+
</RNTesterText>
812+
{eventLog.length === 0 ? (
813+
<RNTesterText style={mouseClickStyles.logEntry}>
814+
No events yet
815+
</RNTesterText>
816+
) : (
817+
eventLog.map((event, i) => (
818+
<RNTesterText key={i} style={mouseClickStyles.logEntry}>
819+
{event}
820+
</RNTesterText>
821+
))
822+
)}
823+
</View>
824+
</View>
825+
);
826+
}
827+
828+
const mouseClickStyles = StyleSheet.create({
829+
targetBox: {
830+
backgroundColor: '#527FE4',
831+
borderRadius: 5,
832+
padding: 20,
833+
marginBottom: 10,
834+
alignItems: 'center',
835+
},
836+
pressableBox: {
837+
backgroundColor: '#7B61FF',
838+
borderRadius: 5,
839+
padding: 20,
840+
marginBottom: 10,
841+
alignItems: 'center',
842+
},
843+
targetText: {
844+
color: 'white',
845+
fontWeight: 'bold',
846+
fontSize: 16,
847+
},
848+
hintText: {
849+
color: 'rgba(255, 255, 255, 0.7)',
850+
fontSize: 12,
851+
marginTop: 4,
852+
},
853+
logBox: {
854+
backgroundColor: '#f0f0f0',
855+
borderRadius: 5,
856+
padding: 10,
857+
maxHeight: 200,
858+
},
859+
logHeader: {
860+
fontWeight: 'bold',
861+
marginBottom: 5,
862+
},
863+
logEntry: {
864+
fontSize: 12,
865+
fontFamily: Platform.OS === 'macos' ? 'Menlo' : 'monospace',
866+
color: '#333',
867+
},
868+
});
869+
// macOS]
870+
771871
export default ({
772872
title: 'View',
773873
documentationURL: 'https://reactnative.dev/docs/view',
@@ -1437,5 +1537,11 @@ export default ({
14371537
name: 'focus-blur',
14381538
render: FocusBlurExample,
14391539
},
1540+
{
1541+
title: 'Mouse Click Events',
1542+
name: 'mouse-click-events',
1543+
platform: 'macos',
1544+
render: MouseClickEventsExample,
1545+
},
14401546
],
14411547
}: RNTesterModule);

0 commit comments

Comments
 (0)