forked from cerc-io/snowballtools-base
⚡️ feat: make the button component to forward ref
This commit is contained in:
parent
2ac657c32e
commit
0f7c6c73c9
@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback } from 'react';
|
import React, { forwardRef, useCallback } from 'react';
|
||||||
import type { ComponentPropsWithoutRef, ReactNode } from 'react';
|
import type { ComponentPropsWithoutRef, ReactNode } from 'react';
|
||||||
|
|
||||||
import { buttonTheme } from './Button.theme';
|
import { buttonTheme } from './Button.theme';
|
||||||
@ -64,7 +64,12 @@ export type ButtonOrLinkProps = (ButtonLinkProps | ButtonProps) &
|
|||||||
/**
|
/**
|
||||||
* A custom button component that can be used in React applications.
|
* A custom button component that can be used in React applications.
|
||||||
*/
|
*/
|
||||||
const Button = ({
|
const Button = forwardRef<
|
||||||
|
HTMLButtonElement | HTMLAnchorElement,
|
||||||
|
ButtonOrLinkProps
|
||||||
|
>(
|
||||||
|
(
|
||||||
|
{
|
||||||
children,
|
children,
|
||||||
className,
|
className,
|
||||||
leftIcon,
|
leftIcon,
|
||||||
@ -74,7 +79,9 @@ const Button = ({
|
|||||||
shape,
|
shape,
|
||||||
variant,
|
variant,
|
||||||
...props
|
...props
|
||||||
}: ButtonOrLinkProps) => {
|
},
|
||||||
|
ref,
|
||||||
|
) => {
|
||||||
// Conditionally render between <NextLink>, <a> or <button> depending on props
|
// Conditionally render between <NextLink>, <a> or <button> depending on props
|
||||||
// useCallback to prevent unnecessary re-rendering
|
// useCallback to prevent unnecessary re-rendering
|
||||||
const Component = useCallback(
|
const Component = useCallback(
|
||||||
@ -154,6 +161,8 @@ const Button = ({
|
|||||||
return (
|
return (
|
||||||
<Component
|
<Component
|
||||||
{...props}
|
{...props}
|
||||||
|
// @ts-expect-error - ref is not a valid prop for button elements
|
||||||
|
ref={ref}
|
||||||
className={buttonTheme({ ...styleProps, class: className })}
|
className={buttonTheme({ ...styleProps, class: className })}
|
||||||
>
|
>
|
||||||
{cloneIcon(leftIcon, { ...iconSize })}
|
{cloneIcon(leftIcon, { ...iconSize })}
|
||||||
@ -161,7 +170,8 @@ const Button = ({
|
|||||||
{cloneIcon(rightIcon, { ...iconSize })}
|
{cloneIcon(rightIcon, { ...iconSize })}
|
||||||
</Component>
|
</Component>
|
||||||
);
|
);
|
||||||
};
|
},
|
||||||
|
);
|
||||||
|
|
||||||
Button.displayName = 'Button';
|
Button.displayName = 'Button';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user