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

[core] Offer alternative to OverridableComponent via module augmentation for better performance #32735

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/mui-material/src/OverridableComponentAugmentation.ts
@@ -0,0 +1,44 @@
import * as React from 'react';
import { DistributiveOmit } from '@mui/types';

declare module '@mui/material/OverridableComponent' {
/**
* A component whose root component can be controlled via a `component` prop.
*
* Adjusts valid props based on the type of `component`.
*/
interface OverridableComponent<M extends OverridableTypeMap> {
<C extends React.ElementType>(
props: {
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: C;
} & OverridePropsVer2<M, C>,
): JSX.Element;
(props: DefaultComponentPropsVer2<M>): JSX.Element;
}

/**
* Props of the component if `component={Component}` is used.
*/
// prettier-ignore
type OverridePropsVer2<
M extends OverridableTypeMap,
C extends React.ElementType,
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

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

If we're going to use these types across the codebase at some point, it would be great to rename the generic props to something more meaningful.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am keeping it as it was before, so that it is visible what exactly are the changes between the two, but yes, we can rename the generics at any point in time :)

Can be done in a dedicated PR where both the existing and the new types will be renamed.

> = (
& BaseProps<M>
& DistributiveOmit<React.ComponentPropsWithoutRef<C>, keyof BaseProps<M>>
Copy link
Member

Choose a reason for hiding this comment

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

Do you think the usage of DistributiveOmit here could be another bottleneck?

The reason I ask is that the React.ComponentPropsWithoutRef has a lot of keys and omitting some of the keys sounds like it requires expensive computation.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we can get away without DistributiveOmit here (but I wouldn't mind being proven wrong). We need the props defined on the component (BaseProps) to always be present on the resulting type, even if they're not compatible with props defined on C.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you think the usage of DistributiveOmit here could be another bottleneck?

Nope, it was one of the first things I tried replacing, this is why I left it.

& { ref?: React.Ref<Element> }
Copy link
Member

Choose a reason for hiding this comment

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

Would this be better?

Suggested change
& { ref?: React.Ref<Element> }
& { ref?: C extends keyof JSX.IntrinsicElements ? React.Ref<C> : React.Ref<Element> }

Copy link
Member

Choose a reason for hiding this comment

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

Other than this, looks good to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me compare the time it takes to do the type checks and will report back :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually these is a big difference:

image ~ 100ms for checking App.tsx

vs

image ~ 1344md for checking App.tsx

I would ship the initial version without this check.

);

/**
* Props if `component={Component}` is NOT used.
*/
// prettier-ignore
type DefaultComponentPropsVer2<M extends OverridableTypeMap> =
& BaseProps<M>
& DistributiveOmit<React.ComponentPropsWithoutRef<M['defaultComponent']>, keyof BaseProps<M>>
& { ref?: React.Ref<Element> };
Copy link
Member Author

Choose a reason for hiding this comment

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

}
2 changes: 1 addition & 1 deletion packages/mui-material/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig",
"include": ["src/**/*", "test/**/*"],
"exclude": ["test/typescript/moduleAugmentation"]
"exclude": ["test/typescript/moduleAugmentation", "src/OverridableComponentAugmentation.ts"]
}
44 changes: 44 additions & 0 deletions packages/mui-types/OverridableComponentAugmentation.ts
@@ -0,0 +1,44 @@
import * as React from 'react';

declare module '@mui/types' {
/**
* A component whose root component can be controlled via a `component` prop.
*
* Adjusts valid props based on the type of `component`.
*/
interface OverridableComponent<M extends OverridableTypeMap> {
<C extends React.ElementType>(
props: {
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: C;
} & OverridePropsVer2<M, C>,
): JSX.Element;
(props: DefaultComponentPropsVer2<M>): JSX.Element;
propTypes?: any;
}

/**
* Props of the component if `component={Component}` is used.
*/
// prettier-ignore
type OverridePropsVer2<
M extends OverridableTypeMap,
C extends React.ElementType
> = (
& BaseProps<M>
& DistributiveOmit<React.ComponentPropsWithoutRef<C>, keyof BaseProps<M>>
& { ref?: React.Ref<Element> }
);

/**
* Props if `component={Component}` is NOT used.
*/
// prettier-ignore
type DefaultComponentPropsVer2<M extends OverridableTypeMap> =
& BaseProps<M>
& DistributiveOmit<React.ComponentPropsWithoutRef<M['defaultComponent']>, keyof BaseProps<M>>
& { ref?: React.Ref<Element> };
}
5 changes: 3 additions & 2 deletions packages/mui-types/package.json
Expand Up @@ -6,7 +6,8 @@
"description": "Utility types for MUI.",
"types": "./index.d.ts",
"files": [
"index.d.ts"
"index.d.ts",
"OverridableComponentAugmentation.ts"
],
"keywords": [
"react",
Expand All @@ -25,7 +26,7 @@
},
"homepage": "https://github.com/mui/material-ui/tree/master/packages/mui-types",
"scripts": {
"build": "mkdir build && cpy index.d.ts build/ && yarn build:copy-files",
"build": "mkdir build && cpy index.d.ts build/ && cpy OverridableComponentAugmentation.ts build/ && yarn build:copy-files",
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
"build:copy-files": "node ../../scripts/copy-files.js",
"prebuild": "rimraf build",
"release": "yarn build && npm publish build",
Expand Down
3 changes: 2 additions & 1 deletion packages/mui-types/tsconfig.json
@@ -1,3 +1,4 @@
{
"extends": "../../tsconfig"
"extends": "../../tsconfig",
"exclude": ["OverridableComponentAugmentation.ts"]
}