-
Notifications
You must be signed in to change notification settings - Fork 545
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this 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; |
There was a problem hiding this comment.
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
buttonColor: string; | ||
buttonTextColor?: string; | ||
subscribeStatus: boolean; | ||
initialText: React.ReactElement | string; | ||
changeText: React.ReactElement | string; | ||
ref?: React.Ref<HTMLButtonElement>; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>; |
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
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
There was a problem hiding this 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"> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can.
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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"> { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
a6e444c
to
22102de
Compare
There was a problem hiding this 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]; |
There was a problem hiding this comment.
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
There was a problem hiding this 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
337655f
to
a1e389d
Compare
default
HTMLButtonProps
andforwardRef
support.