react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Tabs component, trigger onSelectionChange on hover

Open federicocappellotto97 opened this issue 1 year ago โ€ข 3 comments

Provide a general summary of the issue here

This is the component i'm using:

<Tabs onSelectionChange={(key) => {
			alert(key);
		}}>
  <TabList aria-label="Input settings">
    <Tab id="mouse">Mouse Settings</Tab>
    <Tab id="keyboard">Keyboard Settings</Tab>
  </TabList>
  <TabPanel id="mouse">Mouse Settings</TabPanel>
  <TabPanel id="keyboard">Keyboard Settings</TabPanel>
</Tabs>

The problem is the alert is appearing every time i hover the selected tab

๐Ÿค” Expected Behavior?

onSelectionChange should be triggered only on change, not on hover

๐Ÿ˜ฏ Current Behavior

onSelectionChange triggers also on hover on current tab (it looks only while the tab is focused)

๐Ÿ’ Possible Solution

No response

๐Ÿ”ฆ Context

No response

๐Ÿ–ฅ๏ธ Steps to Reproduce

https://codesandbox.io/p/sandbox/epic-bogdan-sjd6ky

Version

"react-aria-components": "^1.0.0",

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

Mac OS

๐Ÿงข Your Company/Team

No response

๐Ÿ•ท Tracking Issue

No response

federicocappellotto97 avatar Dec 20 '23 22:12 federicocappellotto97

It seems like this is due to our usePress code getting confused due to the alert message popping up: https://github.com/adobe/react-spectrum/blob/86b38c87868ce7f262e0df905e5ac4eb2653791d/packages/%40react-aria/interactions/src/usePress.ts#L462-L466. Ideally the above code shouldn't fire but I believe the window level listeners aren't being cleared because the pointer up isn't detected due to the alert. If you remove the alert, things seem to work just fine: https://codesandbox.io/p/sandbox/aged-resonance-3ly5rz?file=%2Fsrc%2FApp.js%3A13%2C1-14%2C1

LFDanLu avatar Dec 20 '23 22:12 LFDanLu

Yes, you are right, only the alerts are triggered even on hover. Thank you for your reply!

federicocappellotto97 avatar Dec 21 '23 07:12 federicocappellotto97

This appears to be an issue in some cases where a new tab is opened and receives focus in some browsers, such as Chrome. When you come back to the original tab, a hover event will trigger onSelectionChange. Likely for the same reason as the alert.

snowystinger avatar Apr 22 '24 20:04 snowystinger

Would a potential solution/workaround be to run cancel from here on the window 'blur' event? Since alert, prompt, and confirm all trigger that?

AndrewLeedham avatar Sep 20 '24 15:09 AndrewLeedham

Possibly, looks like a good avenue to explore. Would you like to open a PR to test it out? Thank you!

snowystinger avatar Sep 24 '24 02:09 snowystinger