Have one css class or theme for multiple boxes inside the stack - MUI

24 Views Asked by At

I am new to React js and MUI components.

I am working on the following design where stack contains multiple boxes. Each box has its own style which is same across all boxes within stack. Is there a way to have one style defined for a box which then be applied to all boxes?

https://codesandbox.io/p/sandbox/angry-gwen-c986mp?file=%2Fsrc%2FDemo.tsx%3A38%2C14&layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522club5ghcv0007356hy9tigkpm%2522%252C%2522sizes%2522%253A%255B100%252C0%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522club5ghcv0003356ht7gmhip2%2522%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522club5ghcv0004356hmdcvoml5%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522club5ghcv0006356h5qkqh7qn%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B44.86491665069937%252C55.13508334930063%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522club5ghcv0003356ht7gmhip2%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522club5ghcu0002356h82prmvsj%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522FILE%2522%252C%2522initialSelections%2522%253A%255B%257B%2522startLineNumber%2522%253A38%252C%2522startColumn%2522%253A14%252C%2522endLineNumber%2522%253A38%252C%2522endColumn%2522%253A14%257D%255D%252C%2522filepath%2522%253A%2522%252Fsrc%252FDemo.tsx%2522%252C%2522state%2522%253A%2522IDLE%2522%257D%255D%252C%2522id%2522%253A%2522club5ghcv0003356ht7gmhip2%2522%252C%2522activeTabId%2522%253A%2522club5ghcu0002356h82prmvsj%2522%257D%252C%2522club5ghcv0006356h5qkqh7qn%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522club5ghcv0005356hn5cg341m%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522UNASSIGNED_PORT%2522%252C%2522port%2522%253A0%252C%2522path%2522%253A%2522%252F%2522%257D%255D%252C%2522id%2522%253A%2522club5ghcv0006356h5qkqh7qn%2522%252C%2522activeTabId%2522%253A%2522club5ghcv0005356hn5cg341m%2522%257D%252C%2522club5ghcv0004356hmdcvoml5%2522%253A%257B%2522tabs%2522%253A%255B%255D%252C%2522id%2522%253A%2522club5ghcv0004356hmdcvoml5%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Afalse%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D

1

There are 1 best solutions below

0
Invulner On BEST ANSWER

You can use the sx prop:

const boxStyle = {
    my: 4,
    display: "flex",
    alignItems: "top",
    gap: 1,
    p: 2,
    ...
};

...

<Box sx={boxStyle}></Box>

Alternatively, you can create a separate component for your specific box, like:

const StyledBox = ({ children }) => (
  <Box
    my={4}
    display="flex"
    alignItems="top"
    gap={1}
    p={2}
  >
    {children}
  </Box>
);

...

<Stack direction="row" spacing={2} border={1} borderColor="divider"> 
    <StyledBox>
        <TargetMetricIcon fontSize="medium" /> Target Metric
    </StyledBox>
</Stack>