Skip to content

Make ref optional in @mui/base#43792

Merged
Janpot merged 6 commits intomui:renovate/react-18.xfrom
Janpot:try-fix-types-react
Sep 26, 2024
Merged

Make ref optional in @mui/base#43792
Janpot merged 6 commits intomui:renovate/react-18.xfrom
Janpot:try-fix-types-react

Conversation

@Janpot
Copy link
Member

@Janpot Janpot commented Sep 17, 2024

Attempt at addressing issues in #43555

It seems to me like this should be optional, but I'm not quite sure if I'm creating some 2nd order effects. Putting it up on a separate PR for review.

@Janpot Janpot added the scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd). label Sep 17, 2024
@Janpot Janpot changed the base branch from renovate/react-18.x to master September 17, 2024 08:38
@mui-bot
Copy link

mui-bot commented Sep 17, 2024

Netlify deploy preview

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

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against ceae0a1

@Janpot Janpot changed the base branch from master to renovate/react-18.x September 17, 2024 09:44
@Janpot Janpot mentioned this pull request Sep 17, 2024
1 task
@michaldudak
Copy link
Member

michaldudak commented Sep 20, 2024

The types you're editing are types of props supplied to slot components.
So if you're doing <Button slot={{ root: MyButton }} />, MyButton will accept these props. Since we control these, we can be sure that refs are present, so they are not optional. I wonder if setting them as optional would actually be a breaking change.

@Janpot
Copy link
Member Author

Janpot commented Sep 20, 2024

Since we control these, we can be sure that refs are present, so they are not optional.

IIUC, forwardRef always turned the ref optional on its return type, regardless of the props. So this was already not enforced.

I wonder if setting them as optional would actually be a breaking change.

If this was only there to enforce it internally, then I don't think this would be a breaking change. And if the previous point is true, then it wasn't working as intended anyways.

@michaldudak
Copy link
Member

Side note: I wanted top verify the PR on the preview site's codesandbox, but for some reason there's no Netlify build for it

@Janpot Janpot changed the base branch from renovate/react-18.x to master September 20, 2024 08:56
@michaldudak
Copy link
Member

michaldudak commented Sep 20, 2024

IIUC, forwardRef always turned the ref optional on its return type, regardless of the props.

Not optional, but nullable:

ForwardedRef<T> = ((instance: T | null) => void) | MutableRefObject<T | null> | null;

Also, forwardRef won't be used in React 19, so we'll rely on what's defined in the props directly.


But anyway, you're right that it shouldn't be a problem today for React < 19 users.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Sep 20, 2024
@Janpot
Copy link
Member Author

Janpot commented Sep 20, 2024

Not optional, but nullable:

I meant the property in the resulting component after calling forwardRef. But I guess that's irrelevant when it comes to enforcing this on our side, we'd be satisfying the interface, not the component.

Alternatives that I could come up with:

  • Recommend usage of React.PropsWithoutRef
    const Root = React.forwardRef(function Root(
      props: React.PropsWithoutRef<BadgeRootSlotProps>,
      ref: React.ForwardedRef<HTMLSpanElement>,
    ) {
      // ...
  • Recommend specifying the generic types in React.forwardRef:
    const Root = React.forwardRef<HTMLSpanElement, BadgeRootSlotProps>(function Root(props, ref) {
      // ...

Both of which would be breaking on the user side. I can't think of a situation where the user would rely on this property being required.

I've changed base to master and pushed an empty commit to trigger a netlify build

@Janpot Janpot changed the base branch from master to renovate/react-18.x September 26, 2024 10:17
@Janpot Janpot marked this pull request as ready for review September 26, 2024 10:19
@Janpot Janpot merged commit bdc3374 into mui:renovate/react-18.x Sep 26, 2024
aarongarciah pushed a commit that referenced this pull request Oct 2, 2024
aarongarciah pushed a commit that referenced this pull request Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: out-of-date The pull request has merge conflicts and can't be merged. scope: code-infra Involves the code-infra product (https://www.notion.so/mui-org/5562c14178aa42af97bc1fa5114000cd). typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants