Styling
Rule 1: don't use styles
In Nantral Platform, we use the MUI library to style our
components. This means that all styling things are managed by the library,
and we only configure it through the theme.ts
file.
If you need to style something, first use this check-list to ensure you are not doing something wrong:
- Check if the component you need does not already exist:
- in the MUI Components library;
- or in the
frontend/src/shared/components
folder.
- If you want to modify the styles of one component:
- you are breaking the design system => try to use the current theme, or update the theme (you can modify it or extend it);
- If the component does not exist, then create it in the shared folder (but use theme variables to style it)!
The exception rule: use sx
props for padding
only
Well, this rule is pretty clear 😁 But why is that, you might ask?
- for
margins
, it is better to replace them with<Spacer>
components, or by usinggap
in<FlexRow>
or<FlexColumn>
components; width
andheight
should be fixed by the grid your component is in, not on the component itself;- other styling properties (like
color
,background
,border
, etc.) should be managed by the theme.
Can I use HTML tags (div
, h1
...)?
❌ No.
Ideally, you should always use the MUI
equivalents of HTML tags:
div
=>Box
,Container
,FlexRow
, ...a
=>Link
, withcomponent={ReactRouterLink}
(cf React Router) if the link is internalp
=>Typography
h1
=>Typography
withvariant="h1"
The reason of that is to ensure that the components are styled correctly, and respect the MUI theme.
The only exception to this principle is <img>
, because there is no equivalent
in MUI (and also no need).
Ok I understand, but I need custom styles
Ok, if you need custom styles, you can use these options:
-
preferably, use the
sx
props with theme variables from theuseTheme
hook to add CSS properties:import { useTheme } from '@mui/material/styles';
const theme = useTheme();
const sx: SxProps = {
color: theme.palette.primary.main,
padding: theme.spacing(2),
}; -
if the component does not have a
sx
prop (it exists only on MUI components), you can use thestyle
prop; -
if your CSS is too complex, you can create a new SCSS file and add your styles there. However, it has some limitations:
- you can't use theme variables from MUI;
- on the build phase, all
CSS
andSCSS
files will be merged together, so you should be very careful with the class names you use.