Skip to content

How to pre-configure a basic animation in a polymorphic component that uses Slot? #2930

Closed Answered by letelete
letelete asked this question in Help
Discussion options

You must be logged in to vote

Ok, so I've managed to implement something that works. Link to my original answer: https://stackoverflow.com/a/78655398/8997321

interface PolymorphicButtonProps extends ComponentPropsWithoutRef<'button'> {
  asChild?: boolean;
}

// Forwarding the ref is mandatory for using the `motion` function,
// ensuring proper animation handling.
const PolymorphicButton = forwardRef<HTMLButtonElement, PolymorphicButtonProps>(
  ({ asChild = false, ...rest }, ref) => {
    const Comp = asChild ? Slot : 'button';

    return (
      <Comp
        ref={ref}
        {...rest}
      />
    );
  }
);

PolymorphicButton.displayName = 'PolymorphicButton';

/* -------------------------------------------------…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by letelete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
1 participant