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][Alert] Add ability to override slot props (@alexey-kozlenkov) #42808

Merged
merged 1 commit into from
Jun 30, 2024
Merged
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
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"]
}
Loading