-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use root element to handle focus, clicks and keyboard interactions #35744
Conversation
@material-ui/unstyled: parsed: +0.34% , gzip: +0.45% |
@michaldudak , could you take a look? |
const handleOnClick = (event: React.BaseSyntheticEvent) => { | ||
setCheckedState(!checked); | ||
setFocusVisible(false); | ||
const nativeEvent = event.nativeEvent || event; | ||
// @ts-ignore | ||
const clonedEvent = new nativeEvent.constructor(nativeEvent.type, nativeEvent); | ||
Object.defineProperty(clonedEvent, 'target', inputRef.current); | ||
onChange?.(clonedEvent); | ||
}; | ||
|
||
const handleOnKeyPress = (e: React.KeyboardEvent) => { | ||
if (e.code === 'Space' && inputRef.current) { | ||
setCheckedState(!checked); | ||
} | ||
}; | ||
|
||
const handleOnFocus = () => { | ||
setFocusVisible(true); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, these event handlers should be specified in useSwitch
, so they won't have to be reimplemented in userland each time someone uses out hook. Plus, this will allow not to export the setCheckedState
, as the API should be declarative if only possible (value
/ defaultValue
instead of setValue
).
Since there are no updates after Michał's review, I am closing this PR. |
mui/base-ui#15