2020-06-17 09:52:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-03 03:28:39 +00:00
|
|
|
import { Button as AriakitButton } from '@ariakit/react';
|
|
|
|
import { forwardRef } from '@wordpress/element';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2024-05-03 03:28:39 +00:00
|
|
|
import type { ForwardedRef } from 'react';
|
|
|
|
import type { ButtonProps as AriakitButtonProps } from '@ariakit/react';
|
|
|
|
import deprecated from '@wordpress/deprecated';
|
2021-06-16 12:44:40 +00:00
|
|
|
|
2020-06-17 09:52:03 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2023-10-20 11:26:09 +00:00
|
|
|
import Spinner from '../../../../../packages/components/spinner';
|
2020-06-17 09:52:03 +00:00
|
|
|
|
2024-05-03 03:28:39 +00:00
|
|
|
type WCButtonProps = AriakitButtonProps & { children?: React.ReactNode };
|
|
|
|
|
|
|
|
export interface ButtonProps extends WCButtonProps {
|
2022-02-03 12:29:06 +00:00
|
|
|
/**
|
2024-05-03 03:28:39 +00:00
|
|
|
* Deprecated: Show a spinner. Preferably,
|
|
|
|
* render a spinner in the button children
|
|
|
|
* instead.
|
2022-02-03 12:29:06 +00:00
|
|
|
*
|
|
|
|
* @default false
|
|
|
|
*/
|
2023-01-09 18:21:09 +00:00
|
|
|
showSpinner?: boolean | undefined;
|
2022-02-09 17:32:05 +00:00
|
|
|
/**
|
|
|
|
* Button variant
|
2024-05-03 03:28:39 +00:00
|
|
|
*
|
|
|
|
* @default 'contained'
|
2022-02-09 17:32:05 +00:00
|
|
|
*/
|
|
|
|
variant?: 'text' | 'contained' | 'outlined';
|
2023-03-13 07:34:12 +00:00
|
|
|
/**
|
2024-05-03 03:28:39 +00:00
|
|
|
* By default we render a wrapper around the button children,
|
|
|
|
* but you can opt in to removing it by setting removeTextWrap
|
|
|
|
* to true.
|
|
|
|
*
|
|
|
|
* @default false
|
2023-03-13 07:34:12 +00:00
|
|
|
*/
|
2024-05-03 03:28:39 +00:00
|
|
|
removeTextWrap?: boolean;
|
2023-01-09 18:21:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 03:28:39 +00:00
|
|
|
interface LinkProps extends ButtonProps {
|
2022-12-14 06:51:16 +00:00
|
|
|
/**
|
|
|
|
* Button href
|
|
|
|
*/
|
2024-05-03 03:28:39 +00:00
|
|
|
href: string;
|
2021-04-26 09:49:57 +00:00
|
|
|
}
|
2020-09-20 23:54:08 +00:00
|
|
|
|
2020-06-17 09:52:03 +00:00
|
|
|
/**
|
2020-09-20 23:54:08 +00:00
|
|
|
* Component that visually renders a button but semantically might be `<button>` or `<a>` depending
|
|
|
|
* on the props.
|
2020-06-17 09:52:03 +00:00
|
|
|
*/
|
2024-05-03 03:28:39 +00:00
|
|
|
const Button = forwardRef< HTMLButtonElement, ButtonProps | LinkProps >(
|
|
|
|
( props, ref ) => {
|
|
|
|
if ( 'showSpinner' in props ) {
|
|
|
|
deprecated( 'showSpinner prop', {
|
|
|
|
version: '8.9.0',
|
|
|
|
alternative: 'Render a spinner in the button children instead.',
|
|
|
|
plugin: 'WooCommerce',
|
|
|
|
} );
|
2020-06-17 09:52:03 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 03:28:39 +00:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
showSpinner = false,
|
|
|
|
children,
|
|
|
|
variant = 'contained',
|
|
|
|
// To maintain backward compat we render a wrapper for button text by default,
|
|
|
|
// but you can opt in to removing it by setting removeTextWrap to true.
|
|
|
|
removeTextWrap = false,
|
|
|
|
...rest
|
|
|
|
} = props;
|
|
|
|
|
2024-05-31 03:49:36 +00:00
|
|
|
const buttonClassName = clsx(
|
2024-05-03 03:28:39 +00:00
|
|
|
'wc-block-components-button',
|
|
|
|
'wp-element-button',
|
|
|
|
className,
|
|
|
|
variant,
|
|
|
|
{
|
|
|
|
'wc-block-components-button--loading': showSpinner,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( 'href' in props ) {
|
|
|
|
return (
|
|
|
|
<AriakitButton
|
|
|
|
render={
|
|
|
|
<a
|
|
|
|
ref={ ref as ForwardedRef< HTMLAnchorElement > }
|
|
|
|
href={ props.href }
|
|
|
|
>
|
|
|
|
{ showSpinner && <Spinner /> }
|
|
|
|
<span className="wc-block-components-button__text">
|
|
|
|
{ children }
|
|
|
|
</span>
|
|
|
|
</a>
|
|
|
|
}
|
|
|
|
className={ buttonClassName }
|
|
|
|
{ ...rest }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const buttonChildren = removeTextWrap ? (
|
|
|
|
props.children
|
|
|
|
) : (
|
2020-06-17 09:52:03 +00:00
|
|
|
<span className="wc-block-components-button__text">
|
2024-05-03 03:28:39 +00:00
|
|
|
{ props.children }
|
2020-06-17 09:52:03 +00:00
|
|
|
</span>
|
2024-05-03 03:28:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AriakitButton
|
|
|
|
ref={ ref }
|
|
|
|
className={ buttonClassName }
|
|
|
|
{ ...rest }
|
|
|
|
>
|
|
|
|
{ showSpinner && <Spinner /> }
|
|
|
|
{ buttonChildren }
|
|
|
|
</AriakitButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2020-06-17 09:52:03 +00:00
|
|
|
|
|
|
|
export default Button;
|