I'm just starting with Material UI. Thanks for bearing with me.
I know you can use things like <Box mx={2}>
out-of-the-box (ha). So if I wanted to put a margin around, say, a TextField
, I could wrap it in a box.
Is there a simple way to set up my app's theme so that any component can use those style function props? (m
, p
, display
, etc)
So that I could to <TextField mx={2}/>
without having to wrap it in a Box
.
The docs imply that you can do this:
(the example uses ThemeProvider
from styled-components
but I'm assuming that MUI's ThemeProvider
works the same way???)
import React from 'react'
import { ThemeProvider } from 'styled-components'
const theme = {
spacing: 4,
palette: {
primary: '#007bff',
},
};
export default function App() {
return (
<ThemeProvider theme={theme}>
{/* children */}
</ThemeProvider>
)
}
I've tried this but it crashes from the TextField's
my
prop:
import { createMuiTheme, TextField, ThemeProvider } from '@material-ui/core';
// Greatly simplified version of my component
const App = () => <TextField my={2}/>
let theme = createMuiTheme({})
export default () =>
<ThemeProvider theme={ theme }>
<App/>
</ThemeProvider>;
I can do something like this and it works:
function App() {
const Input = styled(TextField)(compose(spacing))
return <Input my={3}/>
}
But then I'd have to compose
my components every time I want to do use the style functions.