question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[theme] Allow custom typography variants

See original GitHub issue

MUI allows you to add custom keys to the theme configuration. However, there is no way to expand the design system unless you create a whole new component. Allowing MUI components to reference custom theme keys would bring customization to a whole nother level.

createMuiTheme({
  typography: {
    poster: {
     fontSize: '5.8rem'
    },
  },
});

Usage:

<Typography variant="poster">Super big headline</Typography>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:20
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

19reactions
oliviertassinaricommented, Dec 21, 2020

We have fixed the underlying technical issue in #23841. Now, developers can do 🎉:

import * as React from "react";
import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";

const theme = createMuiTheme({
  typography: {
    poster: {
      color: "red"
    },
    h3: null
  }
});

declare module "@material-ui/core/styles/createTypography" {
  interface Typography {
    poster: React.CSSProperties;
  }

  // allow configuration using `createMuiTheme`
  interface TypographyOptions {
    poster?: React.CSSProperties;
  }
}

declare module "@material-ui/core/Typography/Typography" {
  interface TypographyPropsVariantOverrides {
    poster: true;
    h3: false;
  }
}

export default function FontSizeTheme() {
  return (
    <ThemeProvider theme={theme}>
      {/* A new variant */}
      <Typography variant="poster">poster</Typography>
      {/* You can also disable variants */}
      <Typography variant="h3">h3</Typography>
    </ThemeProvider>
  );
}

https://codesandbox.io/s/fontsizetheme-material-demo-forked-l9u05?file=/demo.tsx

Capture d’écran 2020-12-18 à 13 00 25

I believe the last step is to properly document it. Maybe inside https://next.material-ui.com/customization/typography/? @mnajdova

10reactions
Lanecommented, Feb 9, 2021

if someone wants to do this in V4, you can wrap the Material UI component with some of your own variants:

import { Typography as MuiTypography, withStyles } from "@material-ui/core";
import clsx from "clsx";

const style = (theme) => ({
  number: {
    fontSize: theme.typography.pxToRem(20),
    fontWeight: 700,
  },
  label: {
    fontSize: theme.typography.pxToRem(11.5),
    letterSpacing: "0.05em",
    fontStyle: "italic",
  },
});

/**
 * Overrides default material UI typography with some additional variants
 */
const Typography = ({ variant, classes, className, ...props }) => {
  const isCustom = Object.keys(classes).indexOf(variant) > -1;
  return (
    <MuiTypography
      className={isCustom ? clsx(classes[variant], className) : className}
      variant={isCustom ? undefined : variant}
      {...props}
    />
  );
};

export default withStyles(style)(Typography);

then using the typography component above, you can use your own variants:

<Typography variant="number">42</Typography>

not as nice as being able to define in the theme, but it works 🤷

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Add Custom Typography Variants In Material UI v5.1.0
While doing project at company I am working I came across with problem, where I need it to add extra Typography variant.
Read more >
Typography - Material UI - MUI
The theme.typography.* variant properties map directly to the generated CSS. You can use media queries inside them: const theme = createTheme() ...
Read more >
How to add custom typography variants in React material UI
First, create a default theme object using the createTheme method and include your own custom variants inside the typography object. import { ...
Read more >
MUI v5 - Extending Typography variant in TypeScript creates ...
I want to extend the MUI theme with some custom properties which add to the existing default properties. My theme.ts config looks like...
Read more >
Variants - Theme UI
To add groups of styles based on theme values, you can take advantage of the Variants feature. Variants allow you to define the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found