Using React MUI to implement a custom Theme attribute within a component

I have a CircularProgress element that I want to center, and to make the styling reusable, I decided to create a theme.d.ts file:

import { Theme, ThemeOptions } from '@mui/material/styles'

declare module '@mui/material/styles' {
  interface CustomTheme extends Theme {
    marginAutoItem: {
      margin: string;
    };
  }
  // allow configuration using `createTheme`
  interface CustomThemeOptions extends ThemeOptions {
    marginAutoItem: {
      margin?: string;
    };
  }
  export function createTheme(options?: CustomThemeOptions): CustomTheme;
}

After creating the theme, I implemented it as follows:

const theme = responsiveFontSizes(createTheme({
  palette: {
    primary: {
      main: "#626262",
    },
    secondary: {
      main: "#DFDFDF",
    },
  },
  typography: {
    fontFamily: [
      'Arial Regular',
      'sans'
    ].join(','),
  },
  marginAutoItem: {
    margin: 'auto'
  }
}));

const container = document.getElementById('root');
const root = createRoot(container as Element);

root.render(
  <React.StrictMode>
    <StyledEngineProvider injectFirst>
      <ThemeProvider theme={theme}>
        <App />
      </ThemeProvider>
    </StyledEngineProvider>
  </React.StrictMode>
);

Now I want to apply this new marginAutoItem to a component; how should I proceed?

// pseudo code example
<CircularProgress marginAutoItem />

Answer №1

To customize the styling of your MUI component, you can utilize the sx property. Take a look at the detailed documentation for more information.

Here's an example of how to use it:

<CircularProgress
    sx={ {
        margin: ( theme ) => theme.marginAutoItem.margin
    } }
/>

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

How can I revert a date format using date-fns?

Greetings from Thailand! I have a question regarding the reverse formatting using date-fns. Is there a way to create a function that will change "saturday-9-september-2564" back to "2022-09-24" using date-fns? Any insights or methods on achieving this wo ...

Utilizing the Solana Wallet Key for ArweaveJS Transaction Signing

Is there a method to import a Solana wallet keypair into the JWKInterface according to the node_modules/arweave/node/lib/wallet.d.ts, and then generate an Arweave transaction using await arweave.createTransaction({ data }, jwk);? Metaplex utilizes an API ...

Retrieving rows from a MySQL table that contain a specified BIGINT from an array parameter

I've encountered a problem with mysql while using serverless-mysql in TypeScript. It seems like my query might be incorrect. Here is how I am constructing the query: export default async function ExcuteQuery(query: any, values: any) { try { ...

Packages required for plugins on Npm

I am fairly new to NPM dependencies and I'm currently transitioning from the Ruby world. Working on a Chrome extension utilizing React, in my content.js script, I have the following code at the top: var React = require("react"); var $ = require("jqu ...

Unable to use column formatting on a row using flexbox

I am working on creating a layout with rows and columns where the content in each column is vertically aligned. Here is an example of what I am trying to achieve: Currently, I am using react and have multiple components. The approach I believe will work ...

Steps for changing the language in KeyboardDatePicker material ui

Currently, I am utilizing material ui on my website and leveraging the KeyboardDatePicker API with successful results. The only issue is that the months' names and button text are displayed in English, whereas I would prefer them to be in Spanish. Des ...

Is there a way to prevent the Mui DatePicker clear action from automatically closing the modal window?

Is there a way to prevent the clear action from closing the modal while using @mui/x-date-pickers? import React, { useState } from "react"; import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { LocalizationProvider } ...

Tips for incorporating auth0 into a vue application with typescript

Being a beginner in TypeScript, I've successfully integrated Auth0 with JavaScript thanks to their provided sample code. However, I'm struggling to find any sample applications for using Vue with TypeScript. Any recommendations or resources would ...

24-Hour Format TextField Material UI: A Quick Guide

Can someone help me figure out how to set a textfield in material ui to display the time in a 24-hour format? I've been searching everywhere but can't seem to find a solution. Here is my current component: <TextField type="time" ...

Material-UI: Avoid onClick event firing when clicking on an element that is overlapped by another in a sticky Table

I have a unique setup in my table where each row, including the header row, begins with a checkbox. This header row has a sticky property. As I scroll through the table, rows start to move behind the header row. If I try to click the checkbox in the heade ...

Encountering unanticipated undefined elements while incorporating map in JSX

I'm attempting to dynamically generate <Audio> components using the map method: import React, { useState, useRef, createRef } from 'react'; const audios = [{ src: '/fire.mp3' }, { src: '/crickets.mp3' }]; function ...

The state hook in React is not matching up with the user interface

I am facing an issue with my React application where the UI updates correctly when removing items from a list using the state hook, but the underlying array of elements still shows all elements. To see this problem in action, you can check out this CodeSan ...

particular shade for button border and background

I'm looking to create a button using Material UI that has a specific design. Right now, I've only been able to achieve this: https://i.stack.imgur.com/xvv7s.png Here is the code I am currently using. If anyone can help me identify what I'm ...

The React component library we are using is encountering issues with rendering MUI Icons. An error message is popping up saying "Element type is invalid: expected a string or a class/function but got: object

After setting up a monorepo with a shared component library, I encountered an issue when trying to import components that include a MUI icon from @mui/icons-material. The error message I received was: Error: Element type is invalid: expected a string (for ...

Getting the value from the object that holds the Provider/Consumer using React's Context API

Below is a demonstration using the Context API object with a library called 'react-singleton-context'. Check it out here. In my Menu.js file, I have the code snippet console.log(useSharedDataContext()). This displays an object containing Consume ...

Struggling to link AWS S3 ReactApp with Heroku Node/Express API

I successfully deployed a react-app to AWS S3 and a node/express API on Heroku, but I am facing difficulties in connecting them together even with the cors configuration in the API or using proxy in the react-app. I have been unable to resolve this issue. ...

What advantages can I gain from serving my React application through my Express server?

After researching how to render a React app from an express server, I'm wondering what the advantages are of doing so? I've already created a React app and used an express server to communicate with my database. I would run my express server and ...

Error in ReactJS: Trying to access a property called 'maps' of an undefined value causing a TypeError with Google Map integration

While attempting to include the GeoCoder API code, I encountered an error stating "TypeError: Cannot read property 'maps' of undefined". This is the code snippet: import React from 'react'; import { compose, withProps,withHandlers } f ...

Access and play audio files from a blob storage using Azure Functions in a React application

I have been working on integrating an audio file from azure storage into my react app using the react-h5-audio-player library. Below is the code snippet of my azure function: import { AzureFunction, Context, HttpRequest } from "@azure/functions" ...

What is the best way to incorporate auto-completion into a browser-based editor using Monaco?

Recently, I embarked on a project to develop a browser-based editor using monaco and antlr for a unique programming language. Following an excellent guide, I found at , gave me a great start. While Monaco provides basic suggestions with ctrl + space, I am ...