What is the best way to define a default font color across all components in a React project that utilizes Material

I'm working on a React application that utilizes Material UI. I've implemented my own theme override (see snippet below), but the font color needs to be changed to purple #391960 instead of rgba(0, 0, 0, 0.87). How can I adjust the default font color for the entire site?

Here's a portion of my customized theme:

import { createMuiTheme } from "@material-ui/core";

const theme = createMuiTheme({
  palette: {
     text: {
         light: "#ffffff",
          main: "#391960",
          dark: "#140b1d",
    },
});

export default theme;

I expected adding the above code in my theme.js file to update all font colors to #391960 since I haven't applied any specific styles to the fonts within the components. For instance:

import theme from "./styles/theme";

<ThemeProvider theme={theme}>
        <Typography variant="h1" component="h1">
          Page Title
        </Typography>
</ThemeProvider>

However, the text inside the Typography component mentioned above remains a dark grey. While I am able to modify the size of this typography component using the following code, proving that the theme is being imported correctly:

  typography: {
    h1: {
      fontSize: "2rem",
      fontWeight: 700,
    },

If setting the site-wide color through the theme.js file isn't possible, I was considering creating a global stylesheet that somehow fetches the correct color from the theme. For example (though I know it won't work!)

body {
 color: theme.palette.main
}

Any assistance would be greatly appreciated!

Thank you,

Katie

Answer №1

Two key factors are hindering the effective application of your theme.

  1. By referring to the official documentation, you can see the current default properties for the palette.text object.
const theme = createMuiTheme({
  palette: {
    text: {
      primary: 'rgba(0, 0, 0, 0.87)',
      secondary: 'rgba(0, 0, 0, 0.54)',
      disabled: 'rgba(0, 0, 0, 0.38)',
      hint: 'rgba(0, 0, 0, 0.38)',
    },
  },
});

If you wish to set a different global primary color in your theme.js file, it should be structured as follows:

import { createMuiTheme } from '@material-ui/core/styles';

const theme = createMuiTheme({
  palette: {
    text: {
      primary: '#391960',
    },
  },
});

export default theme;

Keep in mind that you can also specify different colors for other properties like secondary, disabled, and hint if needed.

  1. To ensure both the MUI theme and components display correctly, integrating the CSS Baseline component is essential.

Material-UI offers a CssBaseline component to establish a clean, consistent baseline for your designs.

import React from "react";
import { ThemeProvider, CssBaseline, Typography } from "@material-ui/core";
import theme from "./theme";

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <Typography variant="h1" component="h1">
        Page Title
      </Typography>
    </ThemeProvider>
  );
}

For a practical demonstration, check out this CodeSandbox reproduction. Hopefully, it provides some assistance.

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

Exploring the evolution of React-quill in Next.js context

I am facing a challenge while integrating the Quill editor into my Next.js project alongside the History module to enable undo and redo functionalities. To resolve an issue with the 'document is not defined' error, I had to implement a dynamic im ...

Maintaining aspect ratio while resizing images: A foolproof guide

I've been working on image editing and encountered a bit of trouble with aspect ratios. <img src="big_image.jpg" width="900" height="600" alt="" /> As you can see, the height and width are already specifi ...

Make the Bootstrap navbar hidden by default

I am a beginner in using bootstrap and I am working on setting up a left side navigation bar that is closed by default when the page loads, requiring the user to click a menu link to open it. The page is a content-heavy dashboard that needs as much space a ...

What is the purpose of using "recaptcha_get_html" function to echo?

Hello everyone. I've been struggling to incorporate Google's reCAPTCHA into my project and I keep encountering an error when trying to use the recaptcha_get_html function. It keeps showing up as undefined and despite searching high and low, I can ...

The "Temporary Text" feature in an HTML form

Seeking assistance with implementing dynamic text in place of static text for the placeholder attribute, sourced from a key-value pair in a localization file. Currently developing on the Aurelia framework and would appreciate any guidance on this matter. T ...

Seeing extra CSS code in Chrome's Developer Tools?

Each time I launch a website in Chrome, even ones that I have designed myself, they all contain the following snippet of HTML/CSS code when examined in Developer Tools. <style> .tb_button {padding:1px;cursor:pointer;border-right: 1px solid #8b8b8b; ...

Safari keyframe animation causing wobbly off-center rotation

When viewed on Safari, the spinning animation appears off-center. How can this issue be resolved? The CSS code responsible for the animation is as follows: html, body { height: 100%; } body { background: #a6b8b1; display: grid; place ...

The visibility of the button is dependent on refreshing the page with the F5

I'm currently experiencing an issue with my code. The backoffice button is not showing up when I log in as an admin, unless I refresh the page by pressing F5. useEffect(() => { // Ensure window.FB and window.FB.XFBML are defined before calling ...

Differences in color rendering between XAML WPF and HTML

After migrating my app from WPF to HTML, CSS, and JavaScript, I noticed a discrepancy in color representation. Despite using the same hex value, the HTML color appears lighter than in WPF. See the comparison image here: https://i.sstatic.net/vB7rR.png In ...

Optimize your bootstrap carousel for mobile viewing by ensuring that the image and text are

My bootstrap carousel is displaying perfectly on desktop screens with images sized at 1200x400. However, when viewed on mobile devices, the images shrink so much that the text on them becomes unreadable. Additionally, the navigation dots at the bottom of t ...

The responsive push menus refuse to open automatically and fail to function

Before we go any further, take a moment to explore the demo here: http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/ I decided to work with the source code from the demo by creating new files for Javascript, CSS, and HTML. To simplify things, I ...

Utilizing CSS-in-JS to eliminate arrow buttons from a TextField

I'm currently developing a webpage using React and Material-UI. One of the components I have is a TextField element, and I need to remove the arrow buttons that typically appear on number input fields. If I were utilizing CSS, I could easily achieve t ...

What is the issue with undefined params in Next.js?

I have come across an issue with the function in app/api/hello/[slug]/route.ts When I try to log the output, it keeps showing as undefined. Why is this happening? The code snippet from app/api/hello/[slug]/route.ts is shown below: export async function G ...

Ensuring Bootstrap Cards (version 4.4.1) Blend Seamlessly with Body Text for a Cohes

https://i.sstatic.net/Dqywt.png I have implemented a card similar to the one displayed on my homepage, but I would like it to be aligned left and take up less horizontal space. Additionally, I want the text to wrap around the card rather than being comple ...

React Native is failing to dispatch the action

I have encountered an issue while trying to update my state by dispatching an action. I am using a picker in my component, and although I don't receive any errors, my state remains unchanged. Any suggestions on how to resolve this? onChange = e => ...

Encountering multiple re-renders error while trying to fetch data from API route using SWR on click

Whenever a button inside a component is clicked, an id is sent back to the Page using the const readUrl function. Currently, the goal is to then send that client-side id to an /api/dat/[id].js route which will search for the corresponding id in mongodb and ...

Developing an Easy-to-Use PDF Popup Interface

I need a simple PDF modal where I can input the text: 'The sky is blue' and have it link to a PDF that opens up when clicked. I found a similar sample online, but it includes an image plus an image modal that pops up. I want to replace the imag ...

React-router: Issue with ProtectedRoute authentication mechanism not functioning as expected

I'm currently working on setting up protected routes that can only be accessed after the user has logged in. There are two main routes to consider: /login, which is the Login component (public), and /, which is the Dashboard component (protected). Wh ...

The issue of the responsive navigation bar overlapping the header and navigation links not functioning properly

Hello everyone, I need some help with my website! I can't seem to find the answer on my own. I'm currently working on making my website responsive, but I've run into an issue with the mobile navigation on phones. The navigation links are ov ...

Ways to Remove Focus from a Button in Blazor/C# by Triggering the Escape Key without JavaScript

I am in the process of developing a tooltip feature within the Blazor framework. The main goal I have is to make the tooltip disappear when the escape key is pressed while focusing on the button. Currently, I have successfully implemented this feature usi ...