Material-ui #
- To use spacing utilities you wrap components in a
Boxcomponent- Only fully in
v4
- Only fully in
- Designed on the font
Roboto- Must include this yourself
- Colors can be imported and modified with
blue[500]for example - You can configure the theme through
createMuiTheme - Icon names are in PascalCase
- Should use default exports if you don’t have tree-shaking configured (though webpack in production should do this for you)
Component Details #
Typography: A container component that you can put sub-components inside. This is used to set the font characteristics of the childrenHidden: A component that allows you to hide components based on breakpointsPopoverandPopper: Open popup menusClickAwayListener: Listen to click events not on the component
Transitions #
Material UI provides transition components that you wrap around the components you want to apply the transition to
FadeCollapseGrowSlideZoom
Layout #
<Grid>: Responsive UI is based on a 12 column grid layout- Has
xs,sm,md,lg, andxlbreakpoints - Spacing is automatically taken into account when setting the number of columns
- Can define multiple widths on a
<Grid item>for resizing at different breakpoints - Built on Flexbox
- Split into two types:
containeranditembut you can have both, e.g.<Grid container item>for it to be a flex container and flex item
- Has
Breakpoints #
Values #
xs: 0-599pxsm: 600-959pxmd: 960-1279pxlg: 1280-1919pxxl: 1920px or larger
API #
useMediaQuery: CSS media Query hook for React
Styling #
- Inline styling vs classes:
- Only use inline style for dynamic styling
- Classes are much faster, have auto-prefixing, media queries, better debugging, keyframes, etc.
withStyles,withTheme, are HOC’s for styling- CSS in JS is the current standard
- 3 different APIs for this:
- Hook API (
makeStyles) - Styled Components API
- Higher order component (HOC) API (
withStyles)
- Hook API (
- 3 different APIs for this:
- To modify/obfuscate class names in production you can use a class name generator
ThemeProvidertakes a theme property and makes the theme available down the React treeuseTheme(): A hook to return the theme of the stylesheet in a functional componentwithStyles(): Link a stylesheet with a component using HOCs- `withTheme()(Component): Class-based API for providing the theme to a component as a prop
- Can add
media queriesinto with styles as a string (e.g.@media print: {})
Classes #
- Can use with withStyles and className
- Class names follow the pattern
Mui[<component_name>]-[<style_rule_name>]-UUID - Can use the
classnameslibrary to apply multiple class names to an element or just use string interpolation - All components accept a
classesproperty for customization- I need to read more about this
Interesting/Appealing Components (For Potential Design Use) #
Breadcrumbswith iconsToggleButtonAvatarBadgesTabsfor navbar navigationIconButtonsButtonwithcomponent={Link}ChipsDialog- A modal like components that interrupts the user and prompts them to make a decision
DividerDrawerfor a side navExpansionPanelfor an accordian like expansion effectMenuFor profile, login/logout account avatarPaper- A flat, opaque component representing paper
ProgressFormGroupSwitchSnackbarSteppersTablesandTablePaginationTextField- Wraps
Label,Input, andHelpTextcomponents to make it easier to display a nice field
- Wraps
Inputwith adornments (prepended and appended text/icons)- Can pass the adornments in to
TextFieldas well
- Can pass the adornments in to
Tooltip- Must pass props down to child elements if it is a custom react component
- Otherwise, it will handle the hovering and subsequent activation for you
Customization #
- Inject theme with
MuiThemeProvider - The overrides object in
createMuiThemeallows you to override every instance of a material-ui component - You can also apply properties on all instances of a given component with the
propskeyword incrateMuiTheme
Testing #
createShallow(): Shallow render a componentcreateMount(): Fully mount the component on the DOMcreateRender(): Render component to string
General Tips #
- In general, material-ui is a library implementing Google’s material design
- You can code split by moving CSS and JS into different files
- Property spread: Properties flow down from Mui components to their children.
e.g.
<MenuItem disableRipple>
disableRipple will flow from MenuItem -> ListItem -> ButtonBase thus disabling the ripple at ButtonBase
- The
componentproperty of material-ui components allows you to change what the root node renders as. This is how you can integrate with something likereact-routercomponentas a prop should always be passed statically, not with an arrow function directly in the prop, otherwise a new component will have to be created and rendered on every re-render, regardless of whether it changes
- You can use
material-tablefor great table components with a lot of functionality