Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Typography] Enforce responsive typography type checking in sx prop #42918

Open
goodwin64 opened this issue Jul 12, 2024 · 7 comments · May be fixed by #42945
Open

[material-ui][Typography] Enforce responsive typography type checking in sx prop #42918

goodwin64 opened this issue Jul 12, 2024 · 7 comments · May be fixed by #42945
Assignees
Labels
component: Typography The React component. good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted typescript

Comments

@goodwin64
Copy link
Contributor

goodwin64 commented Jul 12, 2024

Summary

Here the typography shorthand has a string type as it's later being spread into the element.

We use responsive typography as in the following example:

<Typography
  sx={{
    typography: {
      xs: 'Body S',
      sm: 'Body M',
      md: 'any string is valid here',
    },
  }}
>
  The size of this text changes based on the screen size. Check it out! <br />
  In most cases, we will reuse the same typography variant across breakpoints. But
  in case you need to override it, refer to the code in this example.
</Typography>

and there's no way for TypeScript to catch the types inconsistency early in the process.

I tried a bunch of things to redeclare typography prop but with no luck. E.g.:

// attempt 1
declare module '@mui/system' {
  interface SxProps<Theme = MUITheme> extends MUISxProps<Theme> {
    typography?: {
      [key in Breakpoint]?: FlashPackTypographyVariant;
    } | FlashPackTypographyVariant;
  }
}

// attempt 2
declare module '@mui/system' {
  interface TypographyPropsVariantOverrides {
    // Your custom typography variants (as before)
  }

  interface AliasesCSSProperties {
    // Add your custom property here
    typography?: {
      [key in Breakpoint]?: FlashPackTypographyVariant;
    } | FlashPackTypographyVariant;
  }

  // This ensures the custom property is recognized in the theme
  interface ThemeOptions {
    typography?: TypographyOptions | ((theme: Theme) => TypographyOptions);
  }
}

// attempt 3
declare module '@mui/material/styles' {
  interface CSSProperties {
    typography?: {
      [key in Breakpoint]?: FlashPackTypographyVariant;
    } | FlashPackTypographyVariant;
  }
}

I'd love to get some tips if there's a way to override typography in this case!

Examples

No response

Motivation

No response

Search keywords: typography, typescript, override, generic

@goodwin64 goodwin64 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jul 12, 2024
@zannager zannager added the component: Typography The React component. label Jul 12, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title AliasesCSSProperties['typography'] types override [material-ui][Typography] AliasesCSSProperties['typography'] types override Jul 13, 2024
@ZeeshanTamboli ZeeshanTamboli added the package: material-ui Specific to @mui/material label Jul 13, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Typography] AliasesCSSProperties['typography'] types override [material-ui][Typography] Support AliasesCSSProperties['typography'] types override Jul 13, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Typography] Support AliasesCSSProperties['typography'] types override [material-ui][Typography] Allow responsive typography type checking in sx prop typography key Jul 13, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Typography] Allow responsive typography type checking in sx prop typography key [material-ui][Typography] Allow responsive typography type checking in sx prop Jul 13, 2024
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Jul 13, 2024

Instead of allowing type overrides in the sx prop's typography property, we should enforce type checking. For the Typography component, we can make the following changes:

--- a/packages/mui-material/src/Typography/Typography.d.ts
+++ b/packages/mui-material/src/Typography/Typography.d.ts
@@ -1,6 +1,6 @@
 import * as React from 'react';
 import { OverridableStringUnion } from '@mui/types';
-import { SxProps, SystemProps } from '@mui/system';
+import { SxProps, SystemProps, Breakpoint } from '@mui/system';
 import { Theme } from '../styles';
 import { OverrideProps, OverridableComponent } from '../OverridableComponent';
 import { Variant } from '../styles/createTypography';
@@ -43,7 +43,9 @@ export interface TypographyOwnProps extends SystemProps<Theme> {
   /**
    * The system prop that allows defining system overrides as well as additional CSS styles.
    */
-  sx?: SxProps<Theme>;
+  sx?: SxProps<Theme> & {
+    typography?: string | Partial<Record<Breakpoint, TypographyProps['variant']>>;
+  };
   /**
    * Applies the theme typography styles.
    * @default 'body1'

We cannot implement this in AliasesCSSProperties because the Variant type from Material UI is not available in the MUI System. While we may need this change for all sx types across all Material UI components, applying it to the Typography component is a good start.

In-case anybody is creating a new PR, make sure to add a TypeScript test case.

@ZeeshanTamboli ZeeshanTamboli added good first issue Great for first contributions. Enable to learn the contribution process. ready to take Help wanted. Guidance available. There is a high chance the change will be accepted and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 13, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Typography] Allow responsive typography type checking in sx prop [material-ui][Typography] Enforce responsive typography type checking in sx prop Jul 13, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Typography] Enforce responsive typography type checking in sx prop [material-ui][Typography] Enforce responsive typography type checking in sx prop Jul 14, 2024
@Sergio16T
Copy link

Hello @ZeeshanTamboli! I'm beginning to work on a fix for this issue. Could you please assign it to me?

@ZeeshanTamboli
Copy link
Member

@Sergio16T Assigned!

@Sergio16T
Copy link

Sergio16T commented Jul 15, 2024

Hello @ZeeshanTamboli! I opened a PR with fix. Ready for review

@rkumar261
Copy link

Is this issue already resolved? If not, I'm interested in working on it.

@Sergio16T
Copy link

Hi @rkumar261 the dev work is complete. PR is awaiting final review from maintainers.

@rkumar261
Copy link

rkumar261 commented Aug 13, 2024

Ok @Sergio16T, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: Typography The React component. good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted typescript
Projects
None yet
6 participants