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] Fix slotProps.transition types #45214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/pages/material-ui/api/speed-dial.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
{
"name": "transition",
"description": "The component that renders the transition.\n[Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.",
"default": "{}",
"default": "Zoom",
"class": null
}
],
Expand Down
26 changes: 14 additions & 12 deletions packages/mui-material/src/Accordion/Accordion.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TransitionProps } from '../transitions/transition';
import { AccordionClasses } from './accordionClasses';
import { OverridableComponent, OverrideProps } from '../OverridableComponent';
import { ExtendPaperTypeMap } from '../Paper/Paper';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
import { CreateSlotsAndSlotProps, SlotComponentProps, SlotProps } from '../utils/types';

export interface AccordionSlots {
/**
Expand All @@ -18,9 +18,7 @@ export interface AccordionSlots {
* [Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
* @default Collapse
*/
transition: React.JSXElementConstructor<
TransitionProps & { children?: React.ReactElement<unknown, any> }
>;
Comment on lines -21 to -23
Copy link
Member Author

Choose a reason for hiding this comment

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

any slot should be React.ElementType, otherwise user cannot provide their own props interface.

See this TS playground

transition: React.ElementType;
}

export interface AccordionTransitionSlotPropsOverrides {}
Expand All @@ -29,14 +27,18 @@ export interface AccordionHeadingSlotPropsOverrides {}
export type AccordionSlotsAndSlotProps = CreateSlotsAndSlotProps<
AccordionSlots,
{
heading: SlotProps<
React.ElementType<React.HTMLProps<HTMLHeadingElement>>,
AccordionHeadingSlotPropsOverrides,
AccordionOwnerState
>;
transition: SlotProps<
React.ElementType<TransitionProps>,
AccordionTransitionSlotPropsOverrides,
/**
* Props forwarded to the heading slot.
* By default, the avaible props are based on the h3 element.
*/
heading: SlotProps<'h3', AccordionHeadingSlotPropsOverrides, AccordionOwnerState>;
/**
* Props forwarded to the transition slot.
* By default, the avaible props are based on the [Collapse](https://mui.com/material-ui/api/collapse/#props) component.
*/
transition: SlotComponentProps<
React.ElementType,
TransitionProps & AccordionTransitionSlotPropsOverrides,
AccordionOwnerState
>;
}
Expand Down
28 changes: 27 additions & 1 deletion packages/mui-material/src/Accordion/Accordion.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { expectType } from '@mui/types';
import Accordion from '@mui/material/Accordion';
import Accordion, { AccordionProps } from '@mui/material/Accordion';

function testOnChange() {
function handleAccordionChange(event: React.SyntheticEvent, tabsValue: unknown) {}
Expand Down Expand Up @@ -56,3 +56,29 @@ const AccordionComponentTest = () => {
<Accordion slotProps={{ heading: { component: 'h4' } }}>
<div />
</Accordion>;

function Custom(props: AccordionProps) {
const { slotProps, ...dialogProps } = props;
return (
<Accordion
slotProps={{
...slotProps,
transition: (ownerState) => {
const transitionProps =
typeof slotProps?.transition === 'function'
? slotProps.transition(ownerState)
: slotProps?.transition;
return {
...transitionProps,
onExited: (node) => {
transitionProps?.onExited?.(node);
},
};
},
aarongarciah marked this conversation as resolved.
Show resolved Hide resolved
}}
{...dialogProps}
>
test
</Accordion>
);
}
26 changes: 14 additions & 12 deletions packages/mui-material/src/Backdrop/Backdrop.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TransitionProps } from '../transitions/transition';
import { Theme } from '../styles';
import { BackdropClasses } from './backdropClasses';
import { OverridableComponent, OverrideProps } from '../OverridableComponent';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
import { CreateSlotsAndSlotProps, SlotComponentProps, SlotProps } from '../utils/types';

export interface BackdropSlots {
/**
Expand All @@ -18,9 +18,7 @@ export interface BackdropSlots {
* [Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
* @default Fade
*/
transition: React.JSXElementConstructor<
TransitionProps & { children: React.ReactElement<unknown, any> }
>;
transition: React.ElementType;
}
export interface BackdropComponentsPropsOverrides {}

Expand All @@ -29,14 +27,18 @@ export interface BackdropTransitionSlotPropsOverrides {}
export type BackdropSlotsAndSlotProps = CreateSlotsAndSlotProps<
BackdropSlots,
{
root: SlotProps<
React.ElementType<HTMLDivElement>,
BackdropComponentsPropsOverrides,
BackdropOwnerState
>;
transition: SlotProps<
React.JSXElementConstructor<TransitionProps>,
BackdropTransitionSlotPropsOverrides,
/**
* Props forwarded to the transition slot.
* By default, the avaible props are based on the div element.
*/
root: SlotProps<'div', BackdropComponentsPropsOverrides, BackdropOwnerState>;
/**
* Props forwarded to the transition slot.
* By default, the avaible props are based on the [Fade](https://mui.com/material-ui/api/fade/#props) component.
*/
transition: SlotComponentProps<
React.ElementType,
TransitionProps & BackdropTransitionSlotPropsOverrides,
BackdropOwnerState
>;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-material/src/Dialog/Dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PaperProps } from '../Paper';
import { ModalProps } from '../Modal';
import { TransitionProps } from '../transitions/transition';
import { DialogClasses } from './dialogClasses';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
import { CreateSlotsAndSlotProps, SlotComponentProps, SlotProps } from '../utils/types';

export interface DialogSlots {
/**
Expand Down Expand Up @@ -66,9 +66,9 @@ export type DialogSlotsAndSlotProps = CreateSlotsAndSlotProps<
* Props forwarded to the transition slot.
* By default, the avaible props are based on the [Fade](https://mui.com/material-ui/api/fade/#props) component.
*/
transition: SlotProps<
React.ElementType<TransitionProps>,
DialogTransitionSlotPropsOverrides,
transition: SlotComponentProps<
React.ElementType,
TransitionProps & DialogTransitionSlotPropsOverrides,
DialogOwnerState
>;
/**
Expand Down
28 changes: 27 additions & 1 deletion packages/mui-material/src/Dialog/Dialog.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { expectType } from '@mui/types';
import Dialog from '@mui/material/Dialog';
import Dialog, { DialogProps } from '@mui/material/Dialog';
import { PaperProps } from '@mui/material/Paper';

const paperProps: PaperProps<'span'> = {
Expand All @@ -17,3 +17,29 @@ function Test() {
</React.Fragment>
);
}

function Custom(props: DialogProps) {
const { slotProps, ...dialogProps } = props;
return (
<Dialog
slotProps={{
...slotProps,
transition: (ownerState) => {
const transitionProps =
typeof slotProps?.transition === 'function'
? slotProps.transition(ownerState)
: slotProps?.transition;
return {
...transitionProps,
onExited: (node) => {
transitionProps?.onExited?.(node);
},
};
},
}}
{...dialogProps}
>
test
</Dialog>
);
}
4 changes: 2 additions & 2 deletions packages/mui-material/src/Popover/Popover.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export type PopoverSlotsAndSlotProps = CreateSlotsAndSlotProps<
*/
transition: SlotComponentProps<
// use SlotComponentProps because transition slot does not support `component` and `sx` prop
React.ElementType<TransitionProps>,
PopoverTransitionSlotPropsOverrides,
React.ElementType,
TransitionProps & PopoverTransitionSlotPropsOverrides,
PopoverOwnerState
>;
/**
Expand Down
28 changes: 27 additions & 1 deletion packages/mui-material/src/Popover/Popover.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { expectType } from '@mui/types';
import { Popover, PaperProps } from '@mui/material';
import { Popover, PaperProps, PopoverProps } from '@mui/material';

const paperProps: PaperProps<'span'> = {
component: 'span',
Expand All @@ -25,3 +25,29 @@ function Test() {
},
}}
/>;

function Custom(props: PopoverProps) {
const { slotProps, ...dialogProps } = props;
return (
<Popover
slotProps={{
...slotProps,
transition: (ownerState) => {
const transitionProps =
typeof slotProps?.transition === 'function'
? slotProps.transition(ownerState)
: slotProps?.transition;
return {
...transitionProps,
onExited: (node) => {
transitionProps?.onExited?.(node);
},
};
},
}}
{...dialogProps}
>
test
</Popover>
);
}
6 changes: 3 additions & 3 deletions packages/mui-material/src/Snackbar/Snackbar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export type SnackbarSlotsAndSlotProps = CreateSlotsAndSlotProps<
>;
/**
* Props applied to the transition element.
* By default, the element is based on this [`Transition`](https://reactcommunity.org/react-transition-group/transition/) component.
* By default, the element is based on the [Grow](https://mui.com/material-ui/api/grow/#props) component.
*/
transition: SlotComponentProps<
React.ElementType<TransitionProps>,
SnackbarTransitionSlotPropsOverrides,
React.ElementType,
TransitionProps & SnackbarTransitionSlotPropsOverrides,
SnackbarOwnerState
>;
}
Expand Down
26 changes: 25 additions & 1 deletion packages/mui-material/src/Snackbar/Snackbar.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import Snackbar from '@mui/material/Snackbar';
import Snackbar, { SnackbarProps } from '@mui/material/Snackbar';
import { expectType } from '@mui/types';

<Snackbar
Expand Down Expand Up @@ -36,3 +36,27 @@ import { expectType } from '@mui/types';
},
}}
/>;

function Custom(props: SnackbarProps) {
const { slotProps, ...dialogProps } = props;
return (
<Snackbar
slotProps={{
...slotProps,
transition: (ownerState) => {
const transitionProps =
typeof slotProps?.transition === 'function'
? slotProps.transition(ownerState)
: slotProps?.transition;
return {
...transitionProps,
onExited: (node) => {
transitionProps?.onExited?.(node);
},
};
},
}}
{...dialogProps}
/>
);
}
14 changes: 8 additions & 6 deletions packages/mui-material/src/SpeedDial/SpeedDial.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InternalStandardProps as StandardProps } from '..';
import { FabProps } from '../Fab';
import { TransitionProps } from '../transitions';
import { SpeedDialClasses } from './speedDialClasses';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
import { CreateSlotsAndSlotProps, SlotComponentProps } from '../utils/types';

export type CloseReason = 'toggle' | 'blur' | 'mouseLeave' | 'escapeKeyDown';
export type OpenReason = 'toggle' | 'focus' | 'mouseEnter';
Expand All @@ -14,17 +14,19 @@ export interface SpeedDialSlots {
/**
* The component that renders the transition.
* [Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
* @default {}
* @default Zoom
*/
transition: React.JSXElementConstructor<
TransitionProps & { children: React.ReactElement<unknown, any> }
>;
transition: React.ElementType;
}

export type SpeedDialSlotsAndSlotProps = CreateSlotsAndSlotProps<
SpeedDialSlots,
{
transition: SlotProps<React.JSXElementConstructor<TransitionProps>, {}, SpeedDialOwnerState>;
/**
* Props forwarded to the transition slot.
* By default, the avaible props are based on the [Zoom](https://mui.com/material-ui/api/zoom/#props) component.
*/
transition: SlotComponentProps<React.ElementType, TransitionProps, SpeedDialOwnerState>;
}
>;

Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/StepContent/StepContent.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CollapseProps, InternalStandardProps as StandardProps } from '..';
import { Theme } from '../styles';
import { TransitionProps } from '../transitions/transition';
import { StepContentClasses } from './stepContentClasses';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
import { CreateSlotsAndSlotProps, SlotComponentProps } from '../utils/types';

export interface StepContentSlots {
/**
Expand All @@ -24,7 +24,7 @@ export type StepContentSlotsAndSlotProps = CreateSlotsAndSlotProps<
* Props forwared to the transition slot.
* By default, the available props are based on the [Collapse](https://mui.com/material-ui/api/collapse/#props) component
*/
transition: SlotProps<React.ElementType<CollapseProps>, {}, StepContentOwnerState>;
transition: SlotComponentProps<React.ElementType, CollapseProps, StepContentOwnerState>;
}
>;

Expand Down
28 changes: 27 additions & 1 deletion packages/mui-material/src/StepContent/StepContent.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import StepContent from '@mui/material/StepContent';
import StepContent, { StepContentProps } from '@mui/material/StepContent';
import Fade from '@mui/material/Fade';
import Collapse from '@mui/material/Collapse';
import Grow from '@mui/material/Grow';
Expand All @@ -11,3 +11,29 @@ import Zoom from '@mui/material/Zoom';
<StepContent TransitionComponent={Grow}>Step Content</StepContent>;
<StepContent TransitionComponent={Slide}>Step Content</StepContent>;
<StepContent TransitionComponent={Zoom}>Step Content</StepContent>;

function Custom(props: StepContentProps) {
const { slotProps, ...dialogProps } = props;
return (
<StepContent
slotProps={{
...slotProps,
transition: (ownerState) => {
const transitionProps =
typeof slotProps?.transition === 'function'
? slotProps.transition(ownerState)
: slotProps?.transition;
return {
...transitionProps,
onExited: (node) => {
transitionProps?.onExited?.(node);
},
};
},
}}
{...dialogProps}
>
test
</StepContent>
);
}
Loading