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

[docs][system] Add documentation on how to augment custom theme types for the sx prop callback #39259

Merged
Merged
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
37 changes: 37 additions & 0 deletions docs/data/system/getting-started/the-sx-prop/the-sx-prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,43 @@ The `sx` prop can also receive a callback when you need to get theme values that
/>
```

In TypeScript, to use custom theme properties with the `sx` prop callback, extend the `Theme` type from the `@mui/system` library using [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation):

```tsx
import * as React from 'react';
import Box from '@mui/material/Box';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import { orange } from '@mui/material/colors';

declare module '@mui/system' {
interface Theme {
status: {
warning: string;
};
}
}

const theme = createTheme({
status: {
warning: orange[500],
},
});

export default function App() {
return (
<ThemeProvider theme={theme}>
<Box
sx={(theme) => ({
bgcolor: theme.status.warning,
})}
>
Example
</Box>
</ThemeProvider>
);
}
```

## Array values

Array types are useful when you want to partially override some styles in the former index:
Expand Down