Skip to content

Commit

Permalink
[material-ui][Alert] Add ability to override slot props (#42787)
Browse files Browse the repository at this point in the history
Co-authored-by: ZeeshanTamboli <[email protected]>
  • Loading branch information
alexey-kozlenkov and ZeeshanTamboli authored Jun 30, 2024
1 parent d238f98 commit 5a2327f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/mui-material/src/Alert/Alert.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
export type AlertColor = 'success' | 'info' | 'warning' | 'error';

export interface AlertPropsVariantOverrides {}

export interface AlertPropsColorOverrides {}
export interface AlertCloseButtonSlotPropsOverrides {}
export interface AlertCloseIconSlotPropsOverrides {}

export interface AlertSlots {
/**
Expand All @@ -28,8 +29,16 @@ export interface AlertSlots {
export type AlertSlotsAndSlotProps = CreateSlotsAndSlotProps<
AlertSlots,
{
closeButton: SlotProps<React.ElementType<IconButtonProps>, {}, AlertOwnerState>;
closeIcon: SlotProps<React.ElementType<SvgIconProps>, {}, AlertOwnerState>;
closeButton: SlotProps<
React.ElementType<IconButtonProps>,
AlertCloseButtonSlotPropsOverrides,
AlertOwnerState
>;
closeIcon: SlotProps<
React.ElementType<SvgIconProps>,
AlertCloseIconSlotPropsOverrides,
AlertOwnerState
>;
}
>;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as React from 'react';
import Alert from '@mui/material/Alert';
import { IconButton, IconButtonProps, svgIconClasses } from '@mui/material';

declare module '@mui/material/Alert' {
interface AlertCloseButtonSlotPropsOverrides {
iconSize: 'small' | 'medium';
}
}

type MyIconButtonProps = IconButtonProps<
'button',
{
iconSize?: 'small' | 'medium';
}
>;

const MyIconButton = ({ iconSize, ...rest }: MyIconButtonProps) => {
return (
<IconButton
{...rest}
sx={{
// whatever customization based on iconSize
[`.${svgIconClasses.root}`]: {
fontSize: iconSize === 'small' ? '1rem' : '1.5rem',
},
}}
/>
);
};

<Alert
severity="success"
slots={{
closeButton: MyIconButton,
}}
slotProps={{
closeButton: {
iconSize: 'medium',
},
}}
>
Here is a gentle confirmation that your action was successful.
</Alert>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../../../../tsconfig.json",
"files": ["alertCustomSlotProps.spec.tsx"]
}

0 comments on commit 5a2327f

Please sign in to comment.