The issue of changing the body font size in MUI v5 with React Styleguidist, ScopedCSSBaseline, and createTheme style overrides remains unresolved

Recently, I successfully migrated two MUI-powered UIs from MUI v4 to v5. In the process, I had to override their body fontSize according to the MUI migration guide to maintain the v4 design default of Typography body2 font size. This adjustment has worked perfectly.

Now, I want to ensure that the proper MUI v4 font size defaults are also reflected in my Styleguidist component style guide. To achieve this, I have developed a MuiThemeWrapper and integrated it into styleguide.config.js:

styleguideComponents: {
    Wrapper: path.join(__dirname, 'styleguidist/MuiThemeWrapper.tsx')
},

The wrapper essentially accomplishes the following:

const appTheme = createTheme(
    {
        components: {
            MuiCssBaseline: {
                styleOverrides: {
                    body: {
                        fontSize: '0.875rem', // ...reverting back to typography body2 font size as in MUI v4.
                        lineHeight: 1.43,
                        letterSpacing: '0.01071em',
                    },
                },
            },
        },
        palette: {
            mode: 'light',
            primary: { main: '#009999' },
            secondary: { main: '#ffc400' },
        },
    })

return (
    <ThemeProvider theme={appTheme}>
        <ScopedCssBaseline>
            {children}
        </ScopedCssBaseline>
    </ThemeProvider>
  );

However, despite implementing this setup, the font size is not being displayed as expected at 14px but rather as 16px. What could be causing this discrepancy, and is the body styleOverride configured correctly in this scenario?

https://codesandbox.io/s/kind-chihiro-oet1f?fontsize=14&hidenavigation=1&theme=dark

Answer №1

Upon further exploration in ScopedCssBaseline.js, I stumbled upon the realization that the MuiScopedCssBaseline is actually defined as a valid member of components when utilizing createTheme(). Instead of targeting the body, it's essential to specify the styleOverrides for the root element.

Therefore:

const appTheme = createTheme(
    {
        components: {
            MuiScopedCssBaseline: {
                styleOverrides: {
                    root: {
                        fontSize: '0.875rem', // Restore typography body2 font size back to MUI v4.
                        lineHeight: 1.43,
                        letterSpacing: '0.01071em',
                    },
                },
            },
        },
        palette: {
            mode: 'light',
            primary: { main: '#009999' },
            secondary: { main: '#ffc400' },
        },
    })

The revised codesandbox demo now functions as intended.

This update also aligns with the answer provided in Setup react-styleguidist, Create React App, Typescript, Material UI and styled-components and brings it in line with MUI v5.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Automatically insert personalized CSS design markers into Material-UI elements using code

Within my react-redux app, I am utilizing material-ui components. The initial UI prototype was established using adobe-xd, which has the capability to export character styles (design tokens) in a CSS file format: :root { /* Colors: */ --text-color: #F8E29 ...

The color scheme detection feature for matching media is malfunctioning on Safari

As I strive to incorporate a Dark Mode feature based on the user's system preferences, I utilize the @media query prefers-color-scheme: dark. While this approach is effective, I also find it necessary to conduct additional checks using JavaScript. de ...

Issue with Material UI: Styles flicker on screen and then vanish

Strange issue with styles flashing for a brief moment and then disappearing in this SSR app. I'm perplexed as to what might be causing this behavior. // This component is a child of index.tsx in the /pages folder <Button color="primary" ...

Ways to showcase the Item's name in the Material-UI Data Grid

Using the DataGrid component from Material UI React to showcase my data. This is how my database looks: https://i.stack.imgur.com/l4au3.png This is the code for my columns const columns = [ { field: 'user_id', headerName: 'User&apos ...

a fixed width layout with two columns aligned at the top

I've been struggling to design a two-column, top-aligned layout with fixed width for data that will be inserted into text in a WebView for an iPhone app. The first column needs to have a set width so the items in the second column can align perfectly. ...

When using both React's forwardRef and callback ref, the ref can sometimes become undefined

This issue is not related to Mapbox, and no map or geographical knowledge is required to assist. The problem at hand pertains to React refs. Currently, I am utilizing react-map-gl (Mapbox for React) in conjunction with @mapbox/mapbox-gl-draw to enable use ...

Is it possible for me to identify the quantity of children in CSS when the number of children surpasses four?

Is it necessary to style each child when the number of children exceeds four, within a parent element? I am aware of the nth-child(4) ~ child method, but this only targets the siblings that come after the fourth child. In this scenario, do I need to style ...

The issue of overflowing scroll functionality appears to be malfunctioning

.main-content{ height: 100%; width: 60%; min-height: 800px; background-color: white; border-radius: 5px; margin-left: 1%; border: solid 1px black; } .profile-banner{ display: flex; flex-direction: row; justify-content: space-between; ...

Why is my map state turning null on the Google Maps API?

Can anyone provide assistance? I seem to be stuck! My Objective: I am trying to display a Google map with a set of markers. When a marker is clicked, I want to add a Google circle to the map. The Issue: Upon clicking the first marker, no circle appears. ...

Tips for managing and authenticating communication between the backend and the frontend

I've built a backend system for user registration and login, but I'm struggling with handling and verifying sessions on the server side. Although I've read some guides on generating session tokens, I'm unsure about how to validate thes ...

Creating a Sudoku puzzle grid with HTML and CSS - a step-by-step guide

Apologies for the basic inquiry, but I have created a sudoku grid with the following structure. Modified(Using Tables): <table id="grid" border="1" style="border-collapse: collapse;"> <tr class="row"> ...

Utilizing React: assigning distinctive React elements as properties

What is the recommended approach for passing down unique React components as props? Here is an example of pseudocode: import IconOne; import IconTwo; import IconThree; const arr = [ { icon: IconOne }, { icon: IconTwo }, { icon: IconThree }, ...

Arranging elements in a row and column to ensure proper alignment

https://i.stack.imgur.com/LMsTg.png I'm having trouble aligning the UI elements in my Bootstrap 5 project. I can't seem to pinpoint the issue despite trying various solutions. Here's the code snippet: <div class="container"> ...

Using HTML and CSS to create a Contact Form

Greetings! I have come across this contact form code: /* Custom Contact Form Styling */ input[type=text], [type=email], select, textarea { width: 100%; padding: 12px; border: 1px solid #555; margin-top: 6px; margin-bottom: 16px; resize: v ...

What is the best way to insert images into a div in Ionic framework?

I am facing an issue with fitting an image inside a div container. Here is my code structure: <div style="background-color: red; height: 200px; width: 200px;"> <ion-img src="{{kategori[i].img}}"></ion-img> & ...

Tips for structuring an object map with flow type to ensure that each value includes a corresponding key

In an attempt to correctly type (using Flow) a helper function called createReducer for Redux, I have utilized the code from redux-immutablejs as my starting point. I am following the recommendations from the flow documentation on typing Redux reducers, b ...

Is there a way to arrange my bootstrap navigation tabs vertically on my mobile device?

I have a group of five bootstrap navigation tabs that are evenly spaced and visually appealing on desktop screens. However, when viewed on a mobile device, the text within the tabs becomes cramped together. How can I make the tabs stack vertically on mobil ...

Items within a shared container are currently displaying stacked on top of each other

I am facing an issue with my bike images and manufacturer names appearing on top of each other instead of next to each other. Despite using "col-md-12", the grid columns are not aligning horizontally as expected. How can I adjust the code so that each imag ...

Encountered an issue with the [email protected] postinstall script and unable to successfully execute it

I've scoured all the questions related to this issue, but I can't find a solution that works for me. The error I'm encountering is: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! <a href="/cdn-cgi/l/email-protection" class="__cf_ema ...

What is the best way to organize an array based on string, numerical, and date properties in a reactjs application

I have created a project where I need to sort by date, title, and number, but for some reason, none of them are working and there are no bug notifications. I am not sure how to go about debugging this issue. Any help would be greatly appreciated. Thank you ...