Skip to content

Commit de11dc7

Browse files
authored
[Snackbar][base] Drop component prop (#37041)
1 parent 146ba10 commit de11dc7

File tree

6 files changed

+24
-39
lines changed

6 files changed

+24
-39
lines changed

docs/data/base/components/snackbar/snackbar.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,29 @@ The Snackbar component is composed of a single root `<div>` slot with no interio
5858
<div role="presentation" className="BaseSnackbar-root">snackbar content</div>
5959
```
6060

61-
### Slot props
61+
### Custom structure
62+
63+
```jsx
64+
<Snackbar slots={{ root: 'span' }} />
65+
```
6266

6367
:::info
64-
The following props are available on all non-utility Base components.
65-
See [Usage](/base/getting-started/usage/) for full details.
68+
The `slots` prop is available on all non-utility Base components.
69+
See [Overriding component structure](/base/guides/overriding-component-structure/) for full details.
6670
:::
6771

68-
Use the `component` prop to override the root slot with a custom element:
72+
#### Usage with TypeScript
6973

70-
```jsx
71-
<Snackbar component="span" />
74+
In TypeScript, you can specify the custom component type used in the `slots.root` as a generic to the unstyled component. This way, you can safely provide the custom component's props directly on the component:
75+
76+
```tsx
77+
<Snackbar<typeof CustomComponent> slots={{ root: CustomComponent }} customProp />
78+
```
79+
80+
The same applies for props specific to custom primitive elements:
81+
82+
```tsx
83+
<Snackbar<'button'> slots={{ root: 'button' }} onClick={() => {}} />
7284
```
7385

7486
## Hook

docs/pages/base/api/snackbar.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"props": {
33
"autoHideDuration": { "type": { "name": "number" }, "default": "null" },
4-
"component": { "type": { "name": "elementType" } },
54
"disableWindowBlurListener": { "type": { "name": "bool" }, "default": "false" },
65
"exited": { "type": { "name": "bool" }, "default": "true" },
76
"onClose": { "type": { "name": "func" } },

docs/translations/api-docs-base/snackbar/snackbar.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"componentDescription": "",
33
"propDescriptions": {
44
"autoHideDuration": "The number of milliseconds to wait before automatically calling the <code>onClose</code> function. <code>onClose</code> should then set the state of the <code>open</code> prop to hide the Snackbar. This behavior is disabled by default with the <code>null</code> value.",
5-
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
65
"disableWindowBlurListener": "If <code>true</code>, the <code>autoHideDuration</code> timer will expire even if the window is not focused.",
76
"exited": "The prop used to handle exited transition and unmount the component.",
87
"onClose": "Callback fired when the component requests to be closed. Typically <code>onClose</code> is used to set state in the parent component, which is used to control the <code>Snackbar</code> <code>open</code> prop. The <code>reason</code> parameter can optionally be used to control the response to <code>onClose</code>, for example ignoring <code>clickaway</code>.<br><br><strong>Signature:</strong><br><code>function(event: React.SyntheticEvent&lt;any&gt; | Event, reason: string) =&gt; void</code><br><em>event:</em> The event source of the callback.<br><em>reason:</em> Can be: <code>&quot;timeout&quot;</code> (<code>autoHideDuration</code> expired), <code>&quot;clickaway&quot;</code>, or <code>&quot;escapeKeyDown&quot;</code>.",

packages/mui-base/src/Snackbar/Snackbar.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe('<Snackbar />', () => {
4646
expectedClassName: classes.root,
4747
},
4848
},
49+
skip: ['componentProp'],
4950
}),
5051
);
5152

packages/mui-base/src/Snackbar/Snackbar.tsx

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react';
22
import PropTypes from 'prop-types';
3-
import { OverridableComponent } from '@mui/types';
43
import ClickAwayListener from '../ClickAwayListener';
54
import {
65
SnackbarOwnerState,
@@ -12,7 +11,7 @@ import {
1211
import composeClasses from '../composeClasses';
1312
import { getSnackbarUtilityClass } from './snackbarClasses';
1413
import useSnackbar from '../useSnackbar';
15-
import { useSlotProps, WithOptionalOwnerState } from '../utils';
14+
import { PolymorphicComponent, useSlotProps, WithOptionalOwnerState } from '../utils';
1615
import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
1716

1817
const useUtilityClasses = () => {
@@ -40,7 +39,6 @@ const Snackbar = React.forwardRef(function Snackbar<RootComponentType extends Re
4039
const {
4140
autoHideDuration = null,
4241
children,
43-
component,
4442
disableWindowBlurListener = false,
4543
exited = true,
4644
onBlur,
@@ -68,7 +66,7 @@ const Snackbar = React.forwardRef(function Snackbar<RootComponentType extends Re
6866

6967
const ownerState: SnackbarOwnerState = props;
7068

71-
const Root = component || slots.root || 'div';
69+
const Root = slots.root || 'div';
7270

7371
const rootProps: WithOptionalOwnerState<SnackbarRootSlotProps> = useSlotProps({
7472
elementType: Root,
@@ -106,7 +104,7 @@ const Snackbar = React.forwardRef(function Snackbar<RootComponentType extends Re
106104
<Root {...rootProps}>{children}</Root>
107105
</ClickAwayListener>
108106
);
109-
}) as OverridableComponent<SnackbarTypeMap>;
107+
}) as PolymorphicComponent<SnackbarTypeMap>;
110108

111109
Snackbar.propTypes /* remove-proptypes */ = {
112110
// ----------------------------- Warning --------------------------------
@@ -125,11 +123,6 @@ Snackbar.propTypes /* remove-proptypes */ = {
125123
* @ignore
126124
*/
127125
children: PropTypes.node,
128-
/**
129-
* The component used for the root node.
130-
* Either a string to use a HTML element or a component.
131-
*/
132-
component: PropTypes.elementType,
133126
/**
134127
* If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
135128
* @default false
@@ -140,10 +133,6 @@ Snackbar.propTypes /* remove-proptypes */ = {
140133
* @default true
141134
*/
142135
exited: PropTypes.bool,
143-
/**
144-
* @ignore
145-
*/
146-
onBlur: PropTypes.func,
147136
/**
148137
* Callback fired when the component requests to be closed.
149138
* Typically `onClose` is used to set state in the parent component,
@@ -155,18 +144,6 @@ Snackbar.propTypes /* remove-proptypes */ = {
155144
* @param {string} reason Can be: `"timeout"` (`autoHideDuration` expired), `"clickaway"`, or `"escapeKeyDown"`.
156145
*/
157146
onClose: PropTypes.func,
158-
/**
159-
* @ignore
160-
*/
161-
onFocus: PropTypes.func,
162-
/**
163-
* @ignore
164-
*/
165-
onMouseEnter: PropTypes.func,
166-
/**
167-
* @ignore
168-
*/
169-
onMouseLeave: PropTypes.func,
170147
/**
171148
* If `true`, the component is shown.
172149
*/

packages/mui-base/src/Snackbar/Snackbar.types.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as React from 'react';
2-
import { OverrideProps } from '@mui/types';
32
import ClickAwayListener, { ClickAwayListenerProps } from '../ClickAwayListener';
43
import { UseSnackbarParameters } from '../useSnackbar';
5-
import { SlotComponentProps } from '../utils';
4+
import { PolymorphicProps, SlotComponentProps } from '../utils';
65

76
export interface SnackbarRootSlotPropsOverrides {}
87
export interface SnackbarClickAwayListenerSlotPropsOverrides {}
@@ -52,9 +51,7 @@ export interface SnackbarTypeMap<
5251

5352
export type SnackbarProps<
5453
RootComponentType extends React.ElementType = SnackbarTypeMap['defaultComponent'],
55-
> = OverrideProps<SnackbarTypeMap<{}, RootComponentType>, RootComponentType> & {
56-
component?: RootComponentType;
57-
};
54+
> = PolymorphicProps<SnackbarTypeMap<{}, RootComponentType>, RootComponentType>;
5855

5956
export type SnackbarOwnerState = SnackbarOwnProps;
6057

0 commit comments

Comments
 (0)