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

Fixed Dock Component using ReactElement for NextJS #97

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Yaseen-Ejaz
Copy link

@Yaseen-Ejaz Yaseen-Ejaz commented Jan 2, 2025

What the old code did

{Children.map(children, (child) =>
  cloneElement(child as React.ReactElement, { width, isHovered })
)}

The code assumes every child is a valid React element and can accept the width and isHovered props.
If a child is not a valid React element or does not support width or isHovered, it could result in runtime errors.
TypeScript flags the call to cloneElement because it doesn't know whether width or isHovered are valid props for the given child. This weakens type safety.

What the updated code does

{Children.map(children, (child) =>
  React.isValidElement(child)
    ? cloneElement(
        child as React.ReactElement<{ width: MotionValue<number>; isHovered: MotionValue<number> }>, 
        { width, isHovered }
      )
    : child
)}

Validates the child at runtime:

Uses React.isValidElement(child) to check if the child is a valid React element before attempting to clone it.
If the child is not a React element (e.g., a string or number), it bypasses cloning and returns the child as-is, preventing runtime errors.

Explicitly defines the expected props for cloning:

  • Casts child as a React.ReactElement<{ width: MotionValue<number>; isHovered: MotionValue<number> }>.
  • This tells TypeScript that the child must support width and isHovered props.
  • Resolves the TypeScript error by aligning the cloneElement props with the child’s expected type.

Improves robustness and maintainability:

  • Prevents accidental runtime errors by ensuring only valid React elements are cloned.
  • Makes the function self-documenting—future contributors can clearly see that width and isHovered are being passed to valid React elements.

Copy link

vercel bot commented Jan 2, 2025

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

Name Status Preview Comments Updated (UTC)
motion-primitives ❌ Failed (Inspect) Jan 2, 2025 2:52am

@Yaseen-Ejaz Yaseen-Ejaz changed the title Fixed Dock Component using ReactElement Fixed Dock Component using ReactElement for NextJS Jan 2, 2025
@ibelick ibelick force-pushed the main branch 2 times, most recently from c9645e1 to b5d9338 Compare January 9, 2025 16:15
@@ -18,6 +18,7 @@ import {
useMemo,
useRef,
useState,
React
Copy link
Owner

Choose a reason for hiding this comment

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

isValidElement here, and update React.isValidElement to isValidElement below

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.

2 participants