/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
packages/mui-material/src/Modal/Modal.js
341 строка
10 KB
Zeeshan Tamboli
[transitions] Prevent exit transitions from getting stuck (#48881)
05 авг 2026, 16:14
Не верифицирован
05 авг 2026, 16:14
a13824f
Код
Авторство
О чём код?
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import HTMLElementType from '@mui/utils/HTMLElementType'; import elementAcceptingRef from '@mui/utils/elementAcceptingRef'; import composeClasses from '@mui/utils/composeClasses'; import FocusTrap from '../Unstable_TrapFocus'; import Portal from '../Portal'; import { styled } from '../zero-styled'; import memoTheme from '../utils/memoTheme'; import { useDefaultProps } from '../DefaultPropsProvider'; import Backdrop from '../Backdrop'; import useModal from './useModal'; import { getModalUtilityClass } from './modalClasses'; import useSlot from '../utils/useSlot'; const useUtilityClasses = (ownerState) => { const { open, exited, classes } = ownerState; const slots = { root: ['root', !open && exited && 'hidden'], backdrop: ['backdrop'], }; return composeClasses(slots, getModalUtilityClass, classes); }; const ModalRoot = styled('div', { name: 'MuiModal', slot: 'Root', overridesResolver: (props, styles) => { const { ownerState } = props; return [styles.root, !ownerState.open && ownerState.exited && styles.hidden]; }, })( memoTheme(({ theme }) => ({ position: 'fixed', zIndex: (theme.vars || theme).zIndex.modal, right: 0, bottom: 0, top: 0, left: 0, variants: [ { props: ({ ownerState }) => !ownerState.open && ownerState.exited, style: { visibility: 'hidden', }, }, ], })), ); const ModalBackdrop = styled(Backdrop, { name: 'MuiModal', slot: 'Backdrop', })({ zIndex: -1, }); /** * Modal is a lower-level construct that is leveraged by the following components: * * - [Dialog](/material-ui/api/dialog/) * - [Drawer](/material-ui/api/drawer/) * - [Menu](/material-ui/api/menu/) * - [Popover](/material-ui/api/popover/) * * If you are creating a modal dialog, you probably want to use the [Dialog](/material-ui/api/dialog/) component * rather than directly using Modal. * * This component shares many concepts with [react-overlays](https://react-bootstrap.github.io/react-overlays/#modals). */ const Modal = React.forwardRef(function Modal(inProps, ref) { const props = useDefaultProps({ name: 'MuiModal', props: inProps }); const { classes: classesProp, className, closeAfterTransition = false, children, container, component, disableAutoFocus = false, disableEnforceFocus = false, disablePortal = false, disableRestoreFocus = false, disableScrollLock = false, hideBackdrop = false, keepMounted = false, onClose, onTransitionEnter, onTransitionExited, open, slotProps = {}, slots = {}, // eslint-disable-next-line react/prop-types theme, ...other } = props; const propsWithDefaults = { ...props, closeAfterTransition, disableAutoFocus, disableEnforceFocus, disablePortal, disableRestoreFocus, disableScrollLock, hideBackdrop, keepMounted, }; const { getRootProps, getBackdropProps, getTransitionProps, portalRef, portalContainer, isTopModal, exited, hasTransition, } = useModal({ ...propsWithDefaults, rootRef: ref, }); const ownerState = { ...propsWithDefaults, exited, }; const classes = useUtilityClasses(ownerState); const childProps = {}; if (children.props.tabIndex === undefined) { childProps.tabIndex = '-1'; } // It's a Transition like component if (hasTransition) { const { onEnter, onExited } = getTransitionProps(); childProps.onEnter = onEnter; childProps.onExited = onExited; } const externalForwardedProps = { slots, slotProps }; const [RootSlot, rootProps] = useSlot('root', { ref, elementType: ModalRoot, externalForwardedProps: { ...externalForwardedProps, ...other, component, }, getSlotProps: getRootProps, ownerState, className: clsx( className, classes?.root, !ownerState.open && ownerState.exited && classes?.hidden, ), }); const [BackdropSlot, backdropProps] = useSlot('backdrop', { elementType: ModalBackdrop, externalForwardedProps, shouldForwardComponentProp: true, getSlotProps: (otherHandlers) => { return getBackdropProps({ ...otherHandlers, onClick: (event) => { if (otherHandlers?.onClick) { otherHandlers.onClick(event); } }, }); }, className: classes?.backdrop, ownerState, }); if (!keepMounted && !open && (!hasTransition || exited)) { return null; } return ( <Portal ref={portalRef} container={portalContainer} disablePortal={disablePortal}> <RootSlot {...rootProps}> {!hideBackdrop ? <BackdropSlot {...backdropProps} /> : null} <FocusTrap disableEnforceFocus={disableEnforceFocus} disableAutoFocus={disableAutoFocus} disableRestoreFocus={disableRestoreFocus} isEnabled={isTopModal} open={open} > {React.cloneElement(children, childProps)} </FocusTrap> </RootSlot> </Portal> ); }); Modal.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the d.ts file and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * A single child content element. */ children: elementAcceptingRef.isRequired, /** * Override or extend the styles applied to the component. */ classes: PropTypes.object, /** * @ignore */ className: PropTypes.string, /** * When set to true the Modal waits until a nested Transition is completed before closing. * @default false */ closeAfterTransition: PropTypes.bool, /** * The component used for the root node. * Either a string to use a HTML element or a component. */ component: PropTypes.elementType, /** * An HTML element or function that returns one. * The `container` will have the portal children appended to it. * * You can also provide a callback, which is called in a React layout effect. * This lets you set the container from a ref, and also makes server-side rendering possible. * * By default, it uses the body of the top-level document object, * so it's simply `document.body` most of the time. */ container: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([ HTMLElementType, PropTypes.func, ]), /** * If `true`, the modal will not automatically shift focus to itself when it opens, and * replace it to the last focused element when it closes. * This also works correctly with any modal children that have the `disableAutoFocus` prop. * * Generally this should never be set to `true` as it makes the modal less * accessible to assistive technologies, like screen readers. * @default false */ disableAutoFocus: PropTypes.bool, /** * If `true`, the modal will not prevent focus from leaving the modal while open. * * Generally this should never be set to `true` as it makes the modal less * accessible to assistive technologies, like screen readers. * @default false */ disableEnforceFocus: PropTypes.bool, /** * The `children` will be under the DOM hierarchy of the parent component. * @default false */ disablePortal: PropTypes.bool, /** * If `true`, the modal will not restore focus to previously focused element once * modal is hidden or unmounted. * @default false */ disableRestoreFocus: PropTypes.bool, /** * Disable the scroll lock behavior. * @default false */ disableScrollLock: PropTypes.bool, /** * If `true`, the backdrop is not rendered. * @default false */ hideBackdrop: PropTypes.bool, /** * Always keep the children in the DOM. * This prop can be useful in SEO situation or * when you want to maximize the responsiveness of the Modal. * @default false */ keepMounted: PropTypes.bool, /** * Callback fired when the component requests to be closed. * The `reason` parameter can optionally be used to control the response to `onClose`. * * @param {object} event The event source of the callback. * @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`. */ onClose: PropTypes.func, /** * A function called when a transition enters. */ onTransitionEnter: PropTypes.func, /** * A function called when a transition has exited. */ onTransitionExited: PropTypes.func, /** * If `true`, the component is shown. */ open: PropTypes.bool.isRequired, /** * The props used for each slot inside the Modal. * @default {} */ slotProps: PropTypes.shape({ backdrop: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), }), /** * The components used for each slot inside the Modal. * Either a string to use a HTML element or a component. * @default {} */ slots: PropTypes.shape({ backdrop: PropTypes.elementType, root: PropTypes.elementType, }), /** * The system prop that allows defining system overrides as well as additional CSS styles. */ sx: PropTypes.oneOfType([ PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object, ]), }; export default Modal;