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

Feat: NewRefProp #3496

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

yishayhaz
Copy link

@yishayhaz yishayhaz commented Sep 27, 2024

Resolves #3483

Playground: playgrounds/react/pages/menu/test-menu.tsx

image

How

I created a new NewRefProp type, but we could easily refactor the existing RefProp type.

export type NewRefProp<T extends ElementType> = unknown extends ComponentPropsWithRef<T>['ref']
  ? {}
  : { ref?: ComponentPropsWithRef<T>['ref'] }
  1. While checking for unknown helps to hide ref from potential props when it's not applicable, this approach might not cover all cases. If ref appears as unknown but the component actually expects it, our method might not work as intended. In such scenarios, we may need to consider removing the condition from the type. I personally think it's okay to force typing components properly.

  2. I'm currently using React.ComponentWithRef. While I could manually construct the type, adding support for ref callbacks triggers a warning from React.

Important to note

For cases where ref is used as a regular prop (e.g., ({ ref }: { ref: boolean }) => null), React overrides the prop to be optional ref?: boolean. Additionally, using ref as a string causes issues. This problem also exists with the current implementation. As a best practice, users should avoid naming props ref unless they're used as refs.

Copy link

vercel bot commented Sep 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 5:48pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 5:48pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

React & TypeScript Support For ref With as Prop
1 participant