Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

trizotti
Copy link
Contributor

@trizotti trizotti commented Jan 6, 2023

mui/base-ui#15

@mui-bot
Copy link

mui-bot commented Jan 6, 2023

Messages
📖 Netlify deploy preview: https://deploy-preview-35744--material-ui.netlify.app/

@material-ui/unstyled: parsed: +0.34% , gzip: +0.45%

Details of bundle changes

Generated by 🚫 dangerJS against 82a3e56

@trizotti trizotti changed the title Handle focus, keyboard interactions with same element Use root element to handle focus, clicks and keyboard interactions Jan 6, 2023
@mnajdova mnajdova added component: switch This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base labels Jan 17, 2023
@mnajdova mnajdova requested a review from michaldudak January 17, 2023 10:20
@trizotti trizotti marked this pull request as ready for review January 23, 2023 12:43
@trizotti
Copy link
Contributor Author

@michaldudak , could you take a look?

Comment on lines +136 to +154
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);
};
Copy link
Member

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).

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Feb 21, 2023
@ZeeshanTamboli
Copy link
Member

Since there are no updates after Michał's review, I am closing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: switch This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base PR: out-of-date The pull request has merge conflicts and can't be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants