How can I adjust the font size property for Material UI Icons through typing?

I put together the code snippet below, and I'm trying to define a specific type for the custom iconFontSize prop. Can someone guide me on how to achieve this?

import { SvgIconComponent } from '@mui/icons-material'
import { Typography, TypographyProps} from '@mui/material'

type Props = TypographyProps & {
  Icon: SvgIconComponent
  iconFontSize: /* need help defining type here! */
}

export const IconTypography = ({
  Icon,
  iconFontSize = 'inherit',
  columnGap = 1,
  children,
  ...props
}: Props) => {
  return (
    <Typography display="flex" alignItems="center" columnGap={columnGap} {...props}>
      <Icon fontSize={iconFontSize} />
      {children}
    </Typography>
  )
}

Appreciate any assistance you can provide!

Answer №1

To specify the font size for an icon, you can use the iconFontSize property as a union type of 'inherit' | 'large' | 'medium' | 'small':

type Props = TypographyProps & {
  Icon: SvgIconComponent
  iconFontSize: "inherit" | "small" | "medium" | "large" 
}

If you prefer to use the exact type from the Material-UI type definition file, you have another option:


import { OverridableStringUnion } from '@mui/types';

fontSize?: OverridableStringUnion<
  'inherit' | 'large' | 'medium' | 'small',
  SvgIconPropsSizeOverrides
>;

The OverridableStringUnion type is defined as follows:

export type OverridableStringUnion<T extends string | number, U = {}> = GenerateStringUnion<
  Overwrite<Record<T, true>, U>
>;

You can find more information about this type in the Material-UI documentation here: https://mui.com/material-ui/api/svg-icon/

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

When transitioning between steps in Material UI React, the Vertical Stepper component should automatically scroll to the top of

When switching steps in Material UI's vertical stepper, it should automatically scroll to the beginning of the selected step. One potential solution is to utilize a ref to scroll to the stepper titles. More information on scrolling to specific elemen ...

Application Initialization Error: appInits is not a valid function

When my Angular v17 application starts, I need to set some important values right away. This is how it's done in app.config.ts: export const appConfig: ApplicationConfig = { providers: [ ConfigService, ... { pr ...

The subscription function in observables may result in values that are undefined

I integrated a new angular 2 library into my application called "angular2-grid". This library is located within the node_modules folder. Furthermore, I created a service as shown below: import { Injectable } from '@angular/core'; import { Htt ...

Ways to resolve the error message "TypeError: 'setOption' is not a function on type 'MutableRefObject' in React"

CODE export default function EChart({ option, config, resize }) { let chart = useRef(null) let [chartEl, setChartEl] = useState(chart) useEffect(() => { if (resize) { chartEl.resize() } if (!chartEl.cu ...

Creating personalized breakpoints in Material UI using TypeScript

When using the createMuiTheme() function, you have the ability to update breakpoint values like this. const theme = createMuiTheme({ breakpoints: { values: { xs: 0, sm: 600, md: 960, lg: 1280, xl: 1920, }, }, }) ...

The PhotoService (?) is facing difficulties in resolving dependencies according to Nest

Just started diving into Nest.js and encountering an issue right after setting up a service: Nest is throwing an error while trying to resolve dependencies of the PhotoService (?). Make sure that [0] argument is accessible in the current context. I' ...

What is the material-ui component that mirrors the functionality of <input type="color"><datalist>?

I'm just starting out with javascript, react, and Material-UI, so my question (along with the code sample) might show my lack of experience. Within a Material-UI TableCell (not a form), I have the following code: <input type="color" name ...

What is the best way to implement an onClick event listener in a TypeScript React application?

Is there a way to properly add an onClick event listener to a div element in my code snippet below? useEffect(() => { if (ref.current === null) { return; } const handleClick = (el: HTMLDivElement, e: MouseEvent) = ...

Attempting to call a function with a template variable is not allowed

@Component({ selector: 'modal', ... }) export class SimpleModal { modalOpen: boolean; isModalOpen(): boolean { return this.modalOpen; } } <modal #modalRef> <div *ngIf="modalRef.isModalOpen()">...</div> </mo ...

The auto-sizing feature of MUIX DataGridPremium's columns does not function properly unless the setTimeout function is utilized

I am currently working with DataGridPremium and I have a requirement to set the grid column widths automatically. Despite using the autosizeOptions property, the grid columns do not adjust according to their contents as expected. Interestingly, when I encl ...

TypeScript - ESBuild - Encountered an unexpected '<' token

When compiling TypeScript files for a React app with esbuild, everything goes smoothly. However, upon checking the browser console, an error pops up: An unexpected token '<' is causing errors after the return statement // components/editor/ ...

Is there a way to dynamically create a property and assign a value to it on the fly?

When retrieving data from my API, I receive two arrays - one comprising column names and the other containing corresponding data. In order to utilize ag-grid effectively, it is necessary to map these columns to properties of a class. For instance, if ther ...

Tips for defining the types of nested arrays in useState

Looking for guidance on how to define types for nested arrays in a useState array This is my defined interface: interface ToyProps { car: string | null; doll: number | null; } interface SettingsProps { [key: string]: ToyProps[]; } Here is the stat ...

What is the method for selecting an item on Material-UI using the tab key?

I'm currently working with the MenuItem component from material-UI. I am attempting to allow users to select an item by pressing the 'tab' key, similar to this: {Object.keys(Countries).map(key => ( <MenuItem key={key} value={Countr ...

Combining react-draggable and material-ui animations through react-transition group: a comprehensive guide

Trying to incorporate react-draggable with material-UI animations. One approach is as follows: <Draggable> <Grow in={checked}> <Card className={clsx(classes.abs, classes.paper)}> <svg classN ...

What is the best way to customize the legend on a mui5 PIE Chart to display a specific behavior?

I am new to React and have been given the task of creating a pie chart with specific behavior. The desired functionality includes displaying legend items in columns at the bottom of the chart, allowing for expanding rows upon clicking "expand more" to reve ...

The Next.js 13 function getQueriesData does not have any matching overloads for TypeError

Having a TypeScript error issue while using the getQueriesData function in Next.js 13 with React Query. Below is my code snippet: // app/products.tsx import { getQueryClient } from "@/app/providers/getQueryClient"; import { useQuery, useQueryCli ...

Customizing the Style of Page Numbers in Material UI Pagination – A Tailored Approach

As a newbie to using material-ui, I am currently working on creating this component. https://i.stack.imgur.com/2BkYM.png I have managed to style the next and previous buttons in a similar way as shown in the image. The default style displays the page num ...

Required attributes not found for data type in TypeScript

When the following code snippet is executed: @Mutation remove_bought_products(productsToBeRemoved: Array<I.Product>) { const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine); const reducedPro ...

When the tab is switched, the URL remains the same. However, if the URL is changed,

The tabs are changing content but the URL is not being updated. Please refer to this link for a clearer explanation of my issue: https://codesandbox.io/s/mui-tabs-58y4gk Whenever I click on a tab, the URL should change accordingly (for example, clicking ...