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

[material-ui][Modal] migrate useSlotProps to useSLot #42150

Merged
merged 22 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions packages/mui-material/src/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import HTMLElementType from '@mui/utils/HTMLElementType';
import elementAcceptingRef from '@mui/utils/elementAcceptingRef';
import { useSlotProps } from '@mui/base/utils';
import { unstable_useModal as useModal } from '@mui/base/unstable_useModal';
import composeClasses from '@mui/utils/composeClasses';
import FocusTrap from '../Unstable_TrapFocus';
import Portal from '../Portal';
import { styled, createUseThemeProps } from '../zero-styled';
import Backdrop from '../Backdrop';
import { getModalUtilityClass } from './modalClasses';
import useSlot from '../utils/useSlot';
import { useForkRef } from '../utils';

const useThemeProps = createUseThemeProps('MuiModal');

Expand Down Expand Up @@ -100,8 +101,8 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
onTransitionEnter,
onTransitionExited,
open,
slotProps,
slots,
slotProps = {},
slots = {},
// eslint-disable-next-line react/prop-types
theme,
...other
Expand Down Expand Up @@ -152,16 +153,23 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
childProps.onExited = onExited;
}

const RootSlot = slots?.root ?? components.Root ?? ModalRoot;
const BackdropSlot = slots?.backdrop ?? components.Backdrop ?? BackdropComponent;

const rootSlotProps = slotProps?.root ?? componentsProps.root;
const backdropSlotProps = slotProps?.backdrop ?? componentsProps.backdrop;
const backdropSlotProps = slotProps.backdrop ?? componentsProps?.backdrop;

const externalForwardedProps = {
slots: {
root: slots.root ?? components.Root,
backdrop: slots.backdrop ?? components.Backdrop,
},
slotProps: {
root: slotProps.root ?? componentsProps.root,
backdrop: backdropSlotProps,
},
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be?:

Suggested change
const externalForwardedProps = {
slots: {
root: slots.root ?? components.Root,
backdrop: slots.backdrop ?? components.Backdrop,
},
slotProps: {
root: slotProps.root ?? componentsProps.root,
backdrop: backdropSlotProps,
},
};
const externalForwardedProps = {
slots: {
root: components.Root,
backdrop: components.Backdrop,
...slots,
},
slotProps: {
...componentsProps,
...slots
},
};

This would allow us to remove the rootSlotProps and backdropSlotPropsvariables.

Copy link
Contributor Author

@sai6855 sai6855 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DiegoAndai refactored in this commit f7a2329, PR is now ready for next review


const rootProps = useSlotProps({
elementType: RootSlot,
externalSlotProps: rootSlotProps,
externalForwardedProps: other,
const [RootSlot, rootProps] = useSlot('root', {
elementType: ModalRoot,
externalForwardedProps,
getSlotProps: getRootProps,
additionalProps: {
ref,
Expand All @@ -176,9 +184,9 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
),
});

const backdropProps = useSlotProps({
elementType: BackdropSlot,
externalSlotProps: backdropSlotProps,
const [BackdropSlot, backdropProps] = useSlot('backdrop', {
elementType: BackdropComponent,
externalForwardedProps,
additionalProps: BackdropProps,
getSlotProps: (otherHandlers) => {
return getBackdropProps({
Expand All @@ -197,6 +205,8 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
ownerState,
});

const backdropRef = useForkRef(BackdropProps?.ref, backdropProps.ref);
DiegoAndai marked this conversation as resolved.
Show resolved Hide resolved

if (!keepMounted && !open && (!hasTransition || exited)) {
return null;
}
Expand All @@ -209,8 +219,10 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
* is not meant for humans to interact with directly.
* https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-static-element-interactions.md
*/}
<RootSlot {...rootProps}>
{!hideBackdrop && BackdropComponent ? <BackdropSlot {...backdropProps} /> : null}
<RootSlot {...rootProps} {...other}>
{!hideBackdrop && BackdropComponent ? (
<BackdropSlot {...backdropProps} ref={backdropRef} />
) : null}
<FocusTrap
disableEnforceFocus={disableEnforceFocus}
disableAutoFocus={disableAutoFocus}
Expand Down
1 change: 0 additions & 1 deletion packages/mui-material/src/Modal/Modal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('<Modal />', () => {
'themeDefaultProps', // portal, can't determine the root
'themeStyleOverrides', // portal, can't determine the root
'reactTestRenderer', // portal https://github.com/facebook/react/issues/11565
'slotPropsCallback', // not supported yet
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proof of slotPropsCallback is working

],
}),
);
Expand Down
Loading