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

fix: button types and component export standard improvement #492

Merged
merged 4 commits into from
Jan 16, 2025

Conversation

itsarghyadas
Copy link
Collaborator

  • all button components are now normally exported not with default
  • all button components now have default HTMLButtonProps and forwardRef support.

Copy link

vercel bot commented Jan 8, 2025

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

Name Status Preview Comments Updated (UTC)
magicui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 16, 2025 7:40am

Copy link
Collaborator

@dillionverma dillionverma left a comment

Choose a reason for hiding this comment

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

some changes

@@ -7,7 +7,7 @@ interface InteractiveHoverButtonProps
text?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

we could use children instead of props

registry/default/magicui/pulsating-button.tsx Show resolved Hide resolved
buttonColor: string;
buttonTextColor?: string;
subscribeStatus: boolean;
initialText: React.ReactElement | string;
changeText: React.ReactElement | string;
ref?: React.Ref<HTMLButtonElement>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this won't work since it overrides the forwardref

example:

"use client";
import { PulsatingButton } from "@/registry/default/magicui/pulsating-button";
import { useRef } from "react";

export default function PulsatingButtonDemo() {
  const ref = useRef<HTMLButtonElement>(null);

  const handleClick = () => {
    if (ref.current) {
      ref.current.style.backgroundColor = "red";
    }
  };
  return (
    <PulsatingButton ref={ref} onClick={handleClick}>
      Join Affiliate Program
    </PulsatingButton>
  );
}

import { AnimatePresence, motion } from "motion/react";
import React, { useState } from "react";

interface AnimatedSubscribeButtonProps {
interface AnimatedSubscribeButtonProps extends HTMLMotionProps<"button"> {
buttonColor: string;
buttonTextColor?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's remove the buttoncolor and buttontext color props and prefer tailwind classes instead

@@ -1,33 +1,33 @@
import React from "react";
"use client";
Copy link
Collaborator

Choose a reason for hiding this comment

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

we dno't need this here

Copy link
Collaborator

Choose a reason for hiding this comment

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

we still don't need this here

@@ -35,39 +35,38 @@ interface ShinyButtonProps extends HTMLMotionProps<"button"> {
ref?: React.Ref<HTMLButtonElement>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this ref prop is override the forwardref ref, and thus wont work

<motion.button
ref={ref}
{...animationProps}
{...props}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should be at the end of the list of props for this component

Copy link
Collaborator

@dillionverma dillionverma left a comment

Choose a reason for hiding this comment

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

some minor fixes

@@ -1,9 +1,9 @@
import InteractiveHoverButton from "@/registry/default/magicui/interactive-hover-button";
import { InteractiveHoverButton } from "@/registry/default/magicui/interactive-hover-button";

export default function InteractiveHoverButtonDemo() {
return (
<div className="relative justify-center">
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can remove this surrounding div to make it even simpler

check out shiny button demo for example

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes we can.

Copy link
Collaborator

Choose a reason for hiding this comment

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

awesome

@@ -1,33 +1,33 @@
import React from "react";
"use client";
Copy link
Collaborator

Choose a reason for hiding this comment

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

we still don't need this here

@@ -29,45 +29,43 @@ const animationProps = {
},
} as AnimationProps;

interface ShinyButtonProps extends HTMLMotionProps<"button"> {
interface ShinyButtonProps extends Omit<HTMLMotionProps<"button">, "ref"> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you explain the reasoning for omitting the 'ref' again?

Copy link
Collaborator Author

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 need to omit it, but the main issue arises when adding a ref to the button. The motion.button accepts a ref that is a different type from a standard one, causing a type conflict.

Declaring the ref in the interface like ref?: React.Ref<HTMLButtonElement>; solves the issue. Essentially, you can either declare the type yourself or omit the ref type from the HTMLMotionProps so it doesn’t validate the ref and avoids throwing errors.

Let me know.

Copy link
Collaborator

Choose a reason for hiding this comment

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

kk lets go with omitting the ref then

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

interface AnimatedSubscribeButtonProps extends HTMLMotionProps<"button"> {
  subscribeStatus: boolean;
  children: React.ReactNode;
  className?: string;
  ref?: React.Ref<HTMLButtonElement>;
}

This could be another solution - to explicitly declare the type.

Copy link
Collaborator

Choose a reason for hiding this comment

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

wouldn't this result in the ref overriding issue whenever you forwardref?

import { AnimatedSubscribeButton } from "@/registry/default/magicui/animated-subscribe-button";

export default function AnimatedSubscribeButtonDemo() {
return (
<AnimatedSubscribeButton
buttonColor="#000000"
buttonTextColor="#ffffff"
className="w-36"
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it possible to remove these props from the demo to simplify it further?

Ideally it's like this

<AnimatedButton>test</AnimatedButton>

Very similar to every component in shadcn

<Button>test</Button>

It also helps when making AI generated code

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I’ve thought about this and came up with a workaround, but I’m not sure if it’s the best solution.

The main use case for initialText and changeText is that this button can be used for any text, like follow/followed or subscribe/subscribed.

However, using children for it can be a bit tricky while still maintaining the text change functionality.

 <span className="group inline-flex items-center">
     Follow
    <ChevronRightIcon className="ml-1 size-4 transition-transform duration-300 group-hover:translate-x-1" />
 </span>
 <span className="group inline-flex items-center">
    <CheckIcon className="mr-2 size-4" />
     Subscribed
 </span>

That being said, I want to keep the subscribeStatus={false} prop, as it can also be used externally. For example, in an app, if you want to indicate that something is already subscribed, this provides a great out-of-the-box solution.

Copy link
Collaborator

@dillionverma dillionverma left a comment

Choose a reason for hiding this comment

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

small thing

style={{ color: buttonColor }}
const childrenArray = React.Children.toArray(children);
const initialChild = childrenArray[0];
const changeChild = childrenArray[1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

this approach is nice! we can also throw error if its not an array of 2 elements

@itsarghyadas
Copy link
Collaborator Author

image

It will throw this error for the animated-subscribe-button if it didn't get two <span> elements

Copy link
Collaborator

@dillionverma dillionverma left a comment

Choose a reason for hiding this comment

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

good stuff lets merge this

@itsarghyadas itsarghyadas merged commit e0fbe78 into main Jan 16, 2025
5 checks passed
@itsarghyadas itsarghyadas deleted the button-improvement branch January 16, 2025 07:40
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