react-spectrum
react-spectrum copied to clipboard
Tabs component, trigger onSelectionChange on hover
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
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
Yes, you are right, only the alerts are triggered even on hover. Thank you for your reply!
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.
Would a potential solution/workaround be to run cancel
from here on the window
'blur' event? Since alert, prompt, and confirm all trigger that?
Possibly, looks like a good avenue to explore. Would you like to open a PR to test it out? Thank you!