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][styles] Convert createPalette code to typescript #44173

Closed
wants to merge 24 commits into from

Conversation

sai6855
Copy link
Contributor

@sai6855 sai6855 commented Oct 21, 2024

Was going through this PR comments and this comment was the motivation to open the PR

@sai6855 sai6855 marked this pull request as draft October 21, 2024 11:28
@mui-bot
Copy link

mui-bot commented Oct 21, 2024

Netlify deploy preview

https://deploy-preview-44173--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 916e703

@sai6855 sai6855 marked this pull request as ready for review October 21, 2024 12:47
@sai6855 sai6855 requested a review from Janpot October 21, 2024 12:48
@sai6855 sai6855 added typescript package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. package: material-ui Specific to @mui/material and removed package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. labels Oct 21, 2024
@sai6855 sai6855 requested a review from aarongarciah October 25, 2024 10:28
A700: string;
}

export {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have a purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure, i just copy pasted entire code from createPalette.d.ts to this file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this. Don't see the point of having it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed here 5819055

Copy link
Contributor Author

@sai6855 sai6855 Nov 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thoughts, many files does have export {} in their ts files. it looks like this is used to disabled automatic export. i'm reverting it back. reverted here 858a0e6

// disable automatic export
export {};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by disabling automatic export?

lightShade = 300,
darkShade = 700,
}: PaletteAugmentColorOptions): PaletteColor => {
const colorInput = { ...color } as PaletteColor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 4 or 5 places with type casting, especially this one and the createPalette return type. Can we try avoiding type casting without breaking public types?

Copy link
Contributor Author

@sai6855 sai6855 Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i agree, there are lot of type castings, initially i tried to avoid castings but it seemed impossible without breaking public api's.

For example: let's take this type here. I applied casting to shade type here to make sure type of shade is one of keys in intent. So to remove this casting, we need to change type of shade to number | keyof PaletteColor from number | string.

So if we change shade type, we need to change type of lightShade and darkShade from number | string to number | keyof PaletteColor as both are passed as shade here and this results in changing PaletteAugmentColorOptions type which is a public api.

Let me know, if you need explainations for other type castings

@ZeeshanTamboli
Copy link
Member

Not sure why TypeScript throws an error in CI here after removing string casting, but not locally.

@sai6855
Copy link
Contributor Author

sai6855 commented Nov 30, 2024

Not sure why TypeScript throws an error in CI here after removing string casting, but not locally.

Yes even in TypeScript playground I'm not seeing error , check here

added as string back here

@@ -318,7 +473,7 @@ export default function createPalette(palette) {
...modeHydrated,
},
other,
);
) as Palette;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To maintain performance (#44075), avoid unnecessary casting here, and address this: #44059 (comment), we can make the following changes:

--- a/packages/mui-material/src/styles/createPalette.ts
+++ b/packages/mui-material/src/styles/createPalette.ts
@@ -419,17 +419,17 @@ export default function createPalette(palette: PaletteOptions): Palette {
     return colorInput;
   };

-  let modeHydrated;
+  let modeHydrated: ReturnType<typeof getLight> | ReturnType<typeof getDark>;
   if (mode === 'light') {
     modeHydrated = getLight();
   } else if (mode === 'dark') {
     modeHydrated = getDark();
-  }
-
-  if (process.env.NODE_ENV !== 'production') {
-    if (!modeHydrated) {
+  } else {
+    if (process.env.NODE_ENV !== 'production') {
       console.error(`MUI: The palette mode \`${mode}\` is not supported.`);
     }
+    // Should never reach here due to the default value, but safeguard
+    modeHydrated = getLight();
   }

   const paletteOutput = deepmerge(
@@ -473,7 +473,7 @@ export default function createPalette(palette: PaletteOptions): Palette {
       ...modeHydrated,
     },
     other,
-  ) as Palette;
+  );

   return paletteOutput;
 }

Copy link
Contributor Author

@sai6855 sai6855 Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got confused by this diff, but I’ve removed the casting with slightly different approach here: 1812c9f. I mostly followed your approach.

changes in this commit are making CI to fail but working fine locally.

check: https://circleci.com/gh/mui/material-ui/775454

I'm suspecting Module augmentation in these files are making CI to fail due to above commit. shall i revert the commit?

declare module '@mui/material/styles/createPalette' {

declare module '@mui/material/styles/createPalette' {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied the diff I mentioned earlier in commit 260488a. However, TypeScript compilation still fails in CI (but not locally). As you noted, this is likely due to TypeScript module augmentation modifying the types. I suspect the issue might be related to CI running TypeScript tasks in parallel, where the augmented types from docs get compiled first and affect the mui-material package, though I'm not entirely sure.

That said, casting to the Palette type doesn’t seem like an ideal solution, and having to cast types in multiple places makes me question if this PR is worth the effort. What’s your take?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, adding multiple assertions doesn't seem like an ideal solution. We can revisit this in future

@aarongarciah aarongarciah changed the title [material-ui][styles] convert createPalette code to typescript [material-ui][styles] Convert createPalette code to typescript Dec 2, 2024
@sai6855 sai6855 closed this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: material-ui Specific to @mui/material typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants