Skip to content

Commit 83544dd

Browse files
alexey-kozlenkovZeeshanTamboli
authored andcommitted
[material-ui][Alert] Add ability to override slot props (#42787)
Co-authored-by: ZeeshanTamboli <[email protected]>
1 parent 95f7219 commit 83544dd

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

packages/mui-material/src/Alert/Alert.d.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
99
export type AlertColor = 'success' | 'info' | 'warning' | 'error';
1010

1111
export interface AlertPropsVariantOverrides {}
12-
1312
export interface AlertPropsColorOverrides {}
13+
export interface AlertCloseButtonSlotPropsOverrides {}
14+
export interface AlertCloseIconSlotPropsOverrides {}
1415

1516
export interface AlertSlots {
1617
/**
@@ -28,8 +29,16 @@ export interface AlertSlots {
2829
export type AlertSlotsAndSlotProps = CreateSlotsAndSlotProps<
2930
AlertSlots,
3031
{
31-
closeButton: SlotProps<React.ElementType<IconButtonProps>, {}, AlertOwnerState>;
32-
closeIcon: SlotProps<React.ElementType<SvgIconProps>, {}, AlertOwnerState>;
32+
closeButton: SlotProps<
33+
React.ElementType<IconButtonProps>,
34+
AlertCloseButtonSlotPropsOverrides,
35+
AlertOwnerState
36+
>;
37+
closeIcon: SlotProps<
38+
React.ElementType<SvgIconProps>,
39+
AlertCloseIconSlotPropsOverrides,
40+
AlertOwnerState
41+
>;
3342
}
3443
>;
3544

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as React from 'react';
2+
import Alert from '@mui/material/Alert';
3+
import { IconButton, IconButtonProps, svgIconClasses } from '@mui/material';
4+
5+
declare module '@mui/material/Alert' {
6+
interface AlertCloseButtonSlotPropsOverrides {
7+
iconSize: 'small' | 'medium';
8+
}
9+
}
10+
11+
type MyIconButtonProps = IconButtonProps<
12+
'button',
13+
{
14+
iconSize?: 'small' | 'medium';
15+
}
16+
>;
17+
18+
const MyIconButton = ({ iconSize, ...rest }: MyIconButtonProps) => {
19+
return (
20+
<IconButton
21+
{...rest}
22+
sx={{
23+
// whatever customization based on iconSize
24+
[`.${svgIconClasses.root}`]: {
25+
fontSize: iconSize === 'small' ? '1rem' : '1.5rem',
26+
},
27+
}}
28+
/>
29+
);
30+
};
31+
32+
<Alert
33+
severity="success"
34+
slots={{
35+
closeButton: MyIconButton,
36+
}}
37+
slotProps={{
38+
closeButton: {
39+
iconSize: 'medium',
40+
},
41+
}}
42+
>
43+
Here is a gentle confirmation that your action was successful.
44+
</Alert>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../../../../tsconfig.json",
3+
"files": ["alertCustomSlotProps.spec.tsx"]
4+
}

0 commit comments

Comments
 (0)